llvm.org GIT mirror llvm / 528f0bb
Add generic support for hashing StringRef objects using the new hashing library. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@152003 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 8 years ago
3 changed file(s) with 30 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
1818 template
1919 class SmallVectorImpl;
2020 class APInt;
21 class hash_code;
2122
2223 /// StringRef - Represent a constant reference to a string, i.e. a character
2324 /// array and a length, which need not be null terminated.
489490
490491 /// @}
491492
493 /// \brief Compute a hash_code for a StringRef.
494 hash_code hash_value(StringRef S);
495
492496 // StringRefs can be treated like a POD type.
493497 template struct isPodLike;
494498 template <> struct isPodLike { static const bool value = true; };
99 #include "llvm/ADT/StringRef.h"
1010 #include "llvm/ADT/APInt.h"
1111 #include "llvm/ADT/OwningPtr.h"
12 #include "llvm/ADT/Hashing.h"
1213 #include "llvm/ADT/edit_distance.h"
1314 #include
1415
446447
447448 return false;
448449 }
450
451
452 // Implementation of StringRef hashing.
453 hash_code llvm::hash_value(StringRef S) {
454 return hash_combine_range(S.begin(), S.end());
455 }
88
99 #include "gtest/gtest.h"
1010 #include "llvm/ADT/StringRef.h"
11 #include "llvm/ADT/Hashing.h"
1112 #include "llvm/ADT/SmallVector.h"
1213 #include "llvm/Support/raw_ostream.h"
1314 using namespace llvm;
290291 EXPECT_EQ("hello", OS.str());
291292 }
292293
294 TEST(StringRefTest, Hashing) {
295 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef()));
296 EXPECT_EQ(hash_value(std::string()), hash_value(StringRef("")));
297 std::string S = "hello world";
298 hash_code H = hash_value(S);
299 EXPECT_EQ(H, hash_value(StringRef("hello world")));
300 EXPECT_EQ(H, hash_value(StringRef(S)));
301 EXPECT_NE(H, hash_value(StringRef("hello worl")));
302 EXPECT_EQ(hash_value(std::string("hello worl")),
303 hash_value(StringRef("hello worl")));
304 EXPECT_NE(H, hash_value(StringRef("hello world ")));
305 EXPECT_EQ(hash_value(std::string("hello world ")),
306 hash_value(StringRef("hello world ")));
307 EXPECT_EQ(H, hash_value(StringRef("hello world\0")));
308 EXPECT_NE(hash_value(std::string("ello worl")),
309 hash_value(StringRef("hello world").slice(1, -1)));
310 }
311
293312 } // end anonymous namespace