diff --git a/hash/examples/Jamfile.v2 b/hash/examples/Jamfile.v2 new file mode 100644 index 0000000..98802b8 --- /dev/null +++ b/hash/examples/Jamfile.v2 @@ -0,0 +1,8 @@ + +# Copyright Daniel James 2005. Use, modification, and distribution are +# subject to the Boost Software License, Version 1.0. (See accompanying +# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +exe books : books.cpp ; +exe point : point.cpp ; +exe portable : portable.cpp ; diff --git a/hash/examples/books.cpp b/hash/examples/books.cpp new file mode 100644 index 0000000..00d41fc --- /dev/null +++ b/hash/examples/books.cpp @@ -0,0 +1,55 @@ + +// Copyright Daniel James 2005. Use, modification, and distribution are +// subject to the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include +// If we had std::unordered_set. +//#include +#include + +// This example illustrates how to use boost::hash with a custom hash function. +// For full details, see the tutorial. + +namespace library +{ + struct book + { + int id; + std::string author; + std::string title; + + book(int i, std::string const& a, std::string const& t) + : id(i), author(a), title(t) {} + }; + + bool operator==(book const& a, book const& b) + { + return a.id == b.id; + } + + std::size_t hash_value(book const& b) + { + return boost::hash_value(b.id); + } +} + +int main() +{ + library::book knife(3458, "Zane Grey", "The Hash Knife Outfit"); + library::book dandelion(1354, "Paul J. Shanley", "Hash & Dandelion Greens"); + + boost::hash book_hasher; + std::size_t knife_hash_value = book_hasher(knife); + + // If we had std::unordered_set: + // + //std::unordered_set > books; + //books.insert(knife); + //books.insert(library::book(2443, "Lindgren, Torgny", "Hash")); + //books.insert(library::book(1953, "Snyder, Bernadette M.", + // "Heavenly Hash: A Tasty Mix of a Mother's Meditations")); + + //assert(books.find(knife) != books.end()); + //assert(books.find(dandelion) == books.end()); +} diff --git a/hash/examples/point.cpp b/hash/examples/point.cpp new file mode 100644 index 0000000..30e6080 --- /dev/null +++ b/hash/examples/point.cpp @@ -0,0 +1,51 @@ + +// Copyright Daniel James 2005. Use, modification, and distribution are +// subject to the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include +#include + +// This example illustrates how to use boost::hash_combine to generate a hash +// value from the different members of a class. For full details see the hash +// tutorial. + +class point +{ + int x; + int y; +public: + point() : x(0), y(0) {} + point(int x, int y) : x(x), y(y) {} + + bool operator==(point const& other) const + { + return x = other.x && y == other.y; + } + + friend std::size_t hash_value(point const& p) + { + std::size_t seed = 0; + boost::hash_combine(seed, p.x); + boost::hash_combine(seed, p.y); + + return seed; + } +}; + +int main() +{ + boost::hash point_hasher; + + point p1(0, 0); + point p2(1, 2); + point p3(4, 1); + point p4 = p1; + + assert(point_hasher(p1) == point_hasher(p4)); + + // These tests could legally fail, but if they did it'd be a pretty bad + // hash function. + assert(point_hasher(p1) != point_hasher(p2)); + assert(point_hasher(p1) != point_hasher(p3)); +} diff --git a/hash/examples/portable.cpp b/hash/examples/portable.cpp new file mode 100644 index 0000000..dce6f16 --- /dev/null +++ b/hash/examples/portable.cpp @@ -0,0 +1,56 @@ + +// Copyright Daniel James 2005. Use, modification, and distribution are +// subject to the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include +#include + +// This example illustrates how to customise boost::hash portably, so that +// it'll work on both compilers that don't implement argument dependent lookup +// and compilers that implement strict two-phase template instantiation. + +namespace foo +{ + struct custom_type + { + int value; + + custom_type(int x) : value(x) {} + +#ifndef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP + friend inline std::size_t hash_value(custom_type x) + { + boost::hash hasher; + return hasher(x.value); + } +#else + std::size_t hash() const + { + boost::hash hasher; + return hasher(value); + } +#endif + }; +} + +#ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP +namespace boost +{ + std::size_t hash_value(foo::custom_type x) + { + return x.hash(); + } +} +#endif + +int main() +{ + foo::custom_type x(1), y(2), z(1); + + boost::hash hasher; + + assert(hasher(x) == hasher(x)); + assert(hasher(x) != hasher(y)); + assert(hasher(x) == hasher(z)); +}