Skip to content

Instantly share code, notes, and snippets.

@termoose
Last active August 29, 2015 14:01
Show Gist options
  • Save termoose/bc0e09988720befc05d9 to your computer and use it in GitHub Desktop.
Save termoose/bc0e09988720befc05d9 to your computer and use it in GitHub Desktop.
template<typename T> using Index = typename T::iterator::difference_type;
template<typename T>
std::pair<typename T::iterator::difference_type, T> prefix_match(T a, T b)
{
auto mismatch = std::mismatch(a.cbegin(), a.cend(), b.cbegin());
auto index = std::distance(a.cbegin(), mismatch.first);
return std::make_pair(index, T(a.cbegin(), mismatch.first));
}
template<typename T>
std::pair<Index<T>, T> prefix_match(std::vector<T> list)
{
return std::accumulate(list.cbegin(), list.cend(),
std::make_pair(Index<T>(0), *list.cbegin()),
[](std::pair<Index<T>, T> a, T b)
{
return prefix_match(a.second, b);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment