[llvm-commits] [llvm] r152000 - in /llvm/trunk: include/llvm/ADT/Hashing.h unittests/ADT/HashingTest.cpp

Chandler Carruth chandlerc at gmail.com
Sun Mar 4 02:23:16 PST 2012


Author: chandlerc
Date: Sun Mar  4 04:23:15 2012
New Revision: 152000

URL: http://llvm.org/viewvc/llvm-project?rev=152000&view=rev
Log:
Teach the hashing facilities how to hash std::string objects.

Modified:
    llvm/trunk/include/llvm/ADT/Hashing.h
    llvm/trunk/unittests/ADT/HashingTest.cpp

Modified: llvm/trunk/include/llvm/ADT/Hashing.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/Hashing.h?rev=152000&r1=151999&r2=152000&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/Hashing.h (original)
+++ llvm/trunk/include/llvm/ADT/Hashing.h Sun Mar  4 04:23:15 2012
@@ -124,6 +124,10 @@
 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 @@
   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

Modified: llvm/trunk/unittests/ADT/HashingTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/HashingTest.cpp?rev=152000&r1=151999&r2=152000&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/HashingTest.cpp (original)
+++ llvm/trunk/unittests/ADT/HashingTest.cpp Sun Mar  4 04:23:15 2012
@@ -97,6 +97,23 @@
             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; }
 





More information about the llvm-commits mailing list