From ca99ad3f0dfd67d8c3f0873e90afc27ff11c8545 Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Sun, 4 Mar 2012 10:55:27 +0000 Subject: [PATCH] Add generic support for hashing StringRef objects using the new hashing library. llvm-svn: 152003 --- llvm/include/llvm/ADT/StringRef.h | 4 ++++ llvm/lib/Support/StringRef.cpp | 7 +++++++ llvm/unittests/ADT/StringRefTest.cpp | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h index 53ebe6a2af39..a2bb7fe5fb9d 100644 --- a/llvm/include/llvm/ADT/StringRef.h +++ b/llvm/include/llvm/ADT/StringRef.h @@ -19,6 +19,7 @@ namespace llvm { template class SmallVectorImpl; class APInt; + class hash_code; /// StringRef - Represent a constant reference to a string, i.e. a character /// array and a length, which need not be null terminated. @@ -490,6 +491,9 @@ namespace llvm { /// @} + /// \brief Compute a hash_code for a StringRef. + hash_code hash_value(StringRef S); + // StringRefs can be treated like a POD type. template struct isPodLike; template <> struct isPodLike { static const bool value = true; }; diff --git a/llvm/lib/Support/StringRef.cpp b/llvm/lib/Support/StringRef.cpp index 44e732540351..1c28bf879e06 100644 --- a/llvm/lib/Support/StringRef.cpp +++ b/llvm/lib/Support/StringRef.cpp @@ -10,6 +10,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/ADT/APInt.h" #include "llvm/ADT/OwningPtr.h" +#include "llvm/ADT/Hashing.h" #include "llvm/ADT/edit_distance.h" #include @@ -447,3 +448,9 @@ bool StringRef::getAsInteger(unsigned Radix, APInt &Result) const { return false; } + + +// Implementation of StringRef hashing. +hash_code llvm::hash_value(StringRef S) { + return hash_combine_range(S.begin(), S.end()); +} diff --git a/llvm/unittests/ADT/StringRefTest.cpp b/llvm/unittests/ADT/StringRefTest.cpp index d91084381ec3..b2f6fdcce038 100644 --- a/llvm/unittests/ADT/StringRefTest.cpp +++ b/llvm/unittests/ADT/StringRefTest.cpp @@ -9,6 +9,7 @@ #include "gtest/gtest.h" #include "llvm/ADT/StringRef.h" +#include "llvm/ADT/Hashing.h" #include "llvm/ADT/SmallVector.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; @@ -291,4 +292,22 @@ TEST(StringRefTest, Misc) { EXPECT_EQ("hello", OS.str()); } +TEST(StringRefTest, Hashing) { + EXPECT_EQ(hash_value(std::string()), hash_value(StringRef())); + EXPECT_EQ(hash_value(std::string()), hash_value(StringRef(""))); + std::string S = "hello world"; + hash_code H = hash_value(S); + EXPECT_EQ(H, hash_value(StringRef("hello world"))); + EXPECT_EQ(H, hash_value(StringRef(S))); + EXPECT_NE(H, hash_value(StringRef("hello worl"))); + EXPECT_EQ(hash_value(std::string("hello worl")), + hash_value(StringRef("hello worl"))); + EXPECT_NE(H, hash_value(StringRef("hello world "))); + EXPECT_EQ(hash_value(std::string("hello world ")), + hash_value(StringRef("hello world "))); + EXPECT_EQ(H, hash_value(StringRef("hello world\0"))); + EXPECT_NE(hash_value(std::string("ello worl")), + hash_value(StringRef("hello world").slice(1, -1))); +} + } // end anonymous namespace