diff --git a/CMakeLists.txt b/CMakeLists.txt index 071c38c..bde0f9f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -124,6 +124,7 @@ if (PHMAP_BUILD_EXAMPLES) add_executable(ex_bench examples/bench.cc) add_executable(ex_emplace examples/emplace.cc) add_executable(ex_hash_std examples/hash_std.cc) + add_executable(ex_hash_value examples/hash_value.cc) add_executable(ex_two_files examples/f1.cc examples/f2.cc) add_executable(ex_insert_bench examples/insert_bench.cc) add_executable(ex_knucleotide examples/knucleotide.cc) diff --git a/examples/hash_value.cc b/examples/hash_value.cc new file mode 100644 index 0000000..04551ff --- /dev/null +++ b/examples/hash_value.cc @@ -0,0 +1,20 @@ +#include "hash_std.h" // defines Person with std::hash specialization + +#include +#include + +int main() +{ + // As we have defined a specialization of std::hash() for Person, + // we can now create sparse_hash_set or sparse_hash_map of Persons + // ---------------------------------------------------------------- + phmap::flat_hash_set persons = + { { "John", "Mitchell", 35 }, + { "Jane", "Smith", 32 }, + { "Jane", "Smith", 30 }, + }; + + for (auto& p: persons) + std::cout << p._first << ' ' << p._last << " (" << p._age << ")" << '\n'; + +} diff --git a/examples/hash_value.h b/examples/hash_value.h new file mode 100644 index 0000000..81d8574 --- /dev/null +++ b/examples/hash_value.h @@ -0,0 +1,25 @@ +#ifndef phmap_example_hash_value_ +#define phmap_example_hash_value_ + +#include // minimal header providing phmap::HashState() +#include +using std::string; + +struct Person +{ + bool operator==(const Person &o) const + { + return _first == o._first && _last == o._last && _age == o._age; + } + + friend size_t hash_value(const Person &p) + { + return phmap::HashState().combine(0, p._first, p._last, p._age); + } + + string _first; + string _last; + int _age; +}; + +#endif // phmap_example_hash_value_