llvm.org GIT mirror llvm / 9406da6
Teach the hashing facilities how to hash std::string objects. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@152000 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 8 years ago
2 changed file(s) with 28 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
122122 /// \brief Compute a hash_code for a pair of objects.
123123 template
124124 hash_code hash_value(const std::pair &arg);
125
126 /// \brief Compute a hash_code for a standard string.
127 template
128 hash_code hash_value(const std::basic_string &arg);
125129
126130
127131 /// \brief Override the execution seed with a fixed value.
747751 return hash_combine(arg.first, arg.second);
748752 }
749753
754 // Declared and documented above, but defined here so that any of the hashing
755 // infrastructure is available.
756 template
757 hash_code hash_value(const std::basic_string &arg) {
758 return hash_combine_range(arg.begin(), arg.end());
759 }
760
750761 } // namespace llvm
751762
752763 #endif
9494 NonPOD obj1(1, 2), obj2(3, 4), obj3(5, 6);
9595 EXPECT_EQ(hash_combine(obj1, hash_combine(obj2, obj3)),
9696 hash_value(std::make_pair(obj1, std::make_pair(obj2, obj3))));
97 }
98
99 TEST(HashingTest, HashValueStdString) {
100 std::string s = "Hello World!";
101 EXPECT_EQ(hash_combine_range(s.c_str(), s.c_str() + s.size()), hash_value(s));
102 EXPECT_EQ(hash_combine_range(s.c_str(), s.c_str() + s.size() - 1),
103 hash_value(s.substr(0, s.size() - 1)));
104 EXPECT_EQ(hash_combine_range(s.c_str() + 1, s.c_str() + s.size() - 1),
105 hash_value(s.substr(1, s.size() - 2)));
106
107 std::wstring ws = L"Hello Wide World!";
108 EXPECT_EQ(hash_combine_range(ws.c_str(), ws.c_str() + ws.size()),
109 hash_value(ws));
110 EXPECT_EQ(hash_combine_range(ws.c_str(), ws.c_str() + ws.size() - 1),
111 hash_value(ws.substr(0, ws.size() - 1)));
112 EXPECT_EQ(hash_combine_range(ws.c_str() + 1, ws.c_str() + ws.size() - 1),
113 hash_value(ws.substr(1, ws.size() - 2)));
97114 }
98115
99116 template T *begin(T (&arr)[N]) { return arr; }