Skip to content
This repository has been archived by the owner on Dec 26, 2023. It is now read-only.

Commit

Permalink
operator== and operator!= work with unordered_set
Browse files Browse the repository at this point in the history
  • Loading branch information
martinus committed Feb 29, 2020
1 parent f577ca9 commit 1c0413a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/include/robin_hood.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// _/_____/
//
// Fast & memory efficient hashtable based on robin hood hashing for C++11/14/17/20
// version 3.5.1
// version 3.5.2
// https://github.com/martinus/robin-hood-hashing
//
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
Expand Down Expand Up @@ -37,7 +37,7 @@
// see https://semver.org/
#define ROBIN_HOOD_VERSION_MAJOR 3 // for incompatible API changes
#define ROBIN_HOOD_VERSION_MINOR 5 // for adding functionality in a backwards-compatible manner
#define ROBIN_HOOD_VERSION_PATCH 1 // for backwards-compatible bug fixes
#define ROBIN_HOOD_VERSION_PATCH 2 // for backwards-compatible bug fixes

#include <algorithm>
#include <cstdlib>
Expand Down Expand Up @@ -1584,15 +1584,14 @@ class Table
destroy();
}

// Checks if both maps contain the same entries. Order is irrelevant.
// Checks if both tables contain the same entries. Order is irrelevant.
bool operator==(const Table& other) const {
ROBIN_HOOD_TRACE(this);
if (other.size() != size()) {
return false;
}
for (auto const& otherEntry : other) {
auto const myIt = find(otherEntry.first);
if (myIt == end() || !(myIt->second == otherEntry.second)) {
if (!has(otherEntry)) {
return false;
}
}
Expand Down Expand Up @@ -1887,6 +1886,19 @@ class Table
}

private:
template <typename Q = mapped_type>
typename std::enable_if<!std::is_void<Q>::value, bool>::type has(const value_type& e) const {
ROBIN_HOOD_TRACE(this);
auto it = find(e.first);
return it != end() && it->second == e.second;
}

template <typename Q = mapped_type>
typename std::enable_if<std::is_void<Q>::value, bool>::type has(const value_type& e) const {
ROBIN_HOOD_TRACE(this);
return find(e) != end();
}

// reserves space for at least the specified number of elements.
// only works if numBuckets if power of two
void rehashPowerOfTwo(size_t numBuckets) {
Expand Down
57 changes: 57 additions & 0 deletions src/test/unit/unit_unordered_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,60 @@ TEST_CASE_TEMPLATE("unordered_set_string", Set, robin_hood::unordered_flat_set<s
auto it = set.begin();
REQUIRE(++it == set.end());
}

TEST_CASE_TEMPLATE("unordered_set_eq", Set, robin_hood::unordered_flat_set<std::string>,
robin_hood::unordered_node_set<std::string>) {
Set set1;
Set set2;
REQUIRE(set1.size() == set2.size());
REQUIRE(set1 == set2);
REQUIRE(set2 == set1);

set1.emplace("asdf");
// (asdf) == ()
REQUIRE(set1.size() != set2.size());
REQUIRE(set1 != set2);
REQUIRE(set2 != set1);

set2.emplace("huh");
// (asdf) == (huh)
REQUIRE(set1.size() == set2.size());
REQUIRE(set1 != set2);
REQUIRE(set2 != set1);

set1.emplace("huh");
// (asdf, huh) == (huh)
REQUIRE(set1.size() != set2.size());
REQUIRE(set1 != set2);
REQUIRE(set2 != set1);

set2.emplace("asdf");
// (asdf, huh) == (asdf, huh)
REQUIRE(set1.size() == set2.size());
REQUIRE(set1 == set2);
REQUIRE(set2 == set1);

set1.erase("asdf");
// (huh) == (asdf, huh)
REQUIRE(set1.size() != set2.size());
REQUIRE(set1 != set2);
REQUIRE(set2 != set1);

set2.erase("asdf");
// (huh) == (huh)
REQUIRE(set1.size() == set2.size());
REQUIRE(set1 == set2);
REQUIRE(set2 == set1);

set1.clear();
// () == (huh)
REQUIRE(set1.size() != set2.size());
REQUIRE(set1 != set2);
REQUIRE(set2 != set1);

set2.erase("huh");
// () == ()
REQUIRE(set1.size() == set2.size());
REQUIRE(set1 == set2);
REQUIRE(set2 == set1);
}

0 comments on commit 1c0413a

Please sign in to comment.