diff options
author | Chandler Carruth <chandlerc@gmail.com> | 2012-03-04 10:23:15 +0000 |
---|---|---|
committer | Chandler Carruth <chandlerc@gmail.com> | 2012-03-04 10:23:15 +0000 |
commit | dc2cada889b3bcf652fd8f356e2b96cea8e6e228 (patch) | |
tree | 28ff58a06f1ea4821edb10a7edc1dfce022801ae | |
parent | 23df81aabeb111d33a6e3c0c9a811198a2702823 (diff) | |
download | bcm5719-llvm-dc2cada889b3bcf652fd8f356e2b96cea8e6e228.tar.gz bcm5719-llvm-dc2cada889b3bcf652fd8f356e2b96cea8e6e228.zip |
Teach the hashing facilities how to hash std::string objects.
llvm-svn: 152000
-rw-r--r-- | llvm/include/llvm/ADT/Hashing.h | 11 | ||||
-rw-r--r-- | llvm/unittests/ADT/HashingTest.cpp | 17 |
2 files changed, 28 insertions, 0 deletions
diff --git a/llvm/include/llvm/ADT/Hashing.h b/llvm/include/llvm/ADT/Hashing.h index 7bb540e8331..e4dc613652d 100644 --- a/llvm/include/llvm/ADT/Hashing.h +++ b/llvm/include/llvm/ADT/Hashing.h @@ -124,6 +124,10 @@ template <typename T> hash_code hash_value(const T *ptr); template <typename T, typename U> hash_code hash_value(const std::pair<T, U> &arg); +/// \brief Compute a hash_code for a standard string. +template <typename T> +hash_code hash_value(const std::basic_string<T> &arg); + /// \brief Override the execution seed with a fixed value. /// @@ -748,6 +752,13 @@ hash_code hash_value(const std::pair<T, U> &arg) { return hash_combine(arg.first, arg.second); } +// Declared and documented above, but defined here so that any of the hashing +// infrastructure is available. +template <typename T> +hash_code hash_value(const std::basic_string<T> &arg) { + return hash_combine_range(arg.begin(), arg.end()); +} + } // namespace llvm #endif diff --git a/llvm/unittests/ADT/HashingTest.cpp b/llvm/unittests/ADT/HashingTest.cpp index f00cac253f5..f5d6aed5b99 100644 --- a/llvm/unittests/ADT/HashingTest.cpp +++ b/llvm/unittests/ADT/HashingTest.cpp @@ -97,6 +97,23 @@ TEST(HashingTest, HashValueStdPair) { hash_value(std::make_pair(obj1, std::make_pair(obj2, obj3)))); } +TEST(HashingTest, HashValueStdString) { + std::string s = "Hello World!"; + EXPECT_EQ(hash_combine_range(s.c_str(), s.c_str() + s.size()), hash_value(s)); + EXPECT_EQ(hash_combine_range(s.c_str(), s.c_str() + s.size() - 1), + hash_value(s.substr(0, s.size() - 1))); + EXPECT_EQ(hash_combine_range(s.c_str() + 1, s.c_str() + s.size() - 1), + hash_value(s.substr(1, s.size() - 2))); + + std::wstring ws = L"Hello Wide World!"; + EXPECT_EQ(hash_combine_range(ws.c_str(), ws.c_str() + ws.size()), + hash_value(ws)); + EXPECT_EQ(hash_combine_range(ws.c_str(), ws.c_str() + ws.size() - 1), + hash_value(ws.substr(0, ws.size() - 1))); + EXPECT_EQ(hash_combine_range(ws.c_str() + 1, ws.c_str() + ws.size() - 1), + hash_value(ws.substr(1, ws.size() - 2))); +} + template <typename T, size_t N> T *begin(T (&arr)[N]) { return arr; } template <typename T, size_t N> T *end(T (&arr)[N]) { return arr + N; } |