Skip to content

Instantly share code, notes, and snippets.

@deque-blog
Last active April 9, 2017 12:49
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save deque-blog/1157da99e5ee033e770befe84c539764 to your computer and use it in GitHub Desktop.
template <
typename LhsForwardIterator,
typename RhsForwardIterator,
typename Equal = std::equal_to<typename LhsForwardIterator::value_type>,
typename Hash = std::hash<typename LhsForwardIterator::value_type>
>
bool is_permutation_hash(
LhsForwardIterator lhs_first, LhsForwardIterator lhs_last,
RhsForwardIterator rhs_first, RhsForwardIterator rhs_last,
Equal equal = Equal(),
Hash hash = Hash())
{
using value_type = typename LhsForwardIterator::value_type;
std::unordered_map<value_type, int, Hash, Equal> frequencies;
for (; lhs_first != lhs_last; ++lhs_first) frequencies[*lhs_first] += 1;
for (; rhs_first != rhs_last; ++rhs_first) frequencies[*rhs_first] -= 1;
return std::all_of(begin(frequencies), end(frequencies),
[](auto const& p) { return p.second == 0; });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment