diff options
author | 2018-03-15 12:06:24 -0700 | |
---|---|---|
committer | 2018-03-15 12:10:06 -0700 | |
commit | 9d0640e68e07a65eb315e75e6aa73eb84d60dcf4 (patch) | |
tree | 09105f9403db0a1bca9e199cda98d5bf1f9ac29f /tensorflow/core/lib | |
parent | 7e277054400e913af05672a2f6e3519f5b00873a (diff) |
Automated g4 rollback of changelist 189110935
PiperOrigin-RevId: 189224522
Diffstat (limited to 'tensorflow/core/lib')
-rw-r--r-- | tensorflow/core/lib/core/stringpiece.cc | 5 | ||||
-rw-r--r-- | tensorflow/core/lib/core/stringpiece.h | 6 | ||||
-rw-r--r-- | tensorflow/core/lib/core/stringpiece_test.cc | 70 | ||||
-rw-r--r-- | tensorflow/core/lib/hash/hash.h | 1 | ||||
-rw-r--r-- | tensorflow/core/lib/hash/hash_test.cc | 73 |
5 files changed, 74 insertions, 81 deletions
diff --git a/tensorflow/core/lib/core/stringpiece.cc b/tensorflow/core/lib/core/stringpiece.cc index 29b727fc44..5bd79778a6 100644 --- a/tensorflow/core/lib/core/stringpiece.cc +++ b/tensorflow/core/lib/core/stringpiece.cc @@ -17,14 +17,9 @@ limitations under the License. #include <algorithm> #include <iostream> -#include "tensorflow/core/lib/hash/hash.h" namespace tensorflow { -size_t StringPieceHasher::operator()(StringPiece s) const { - return Hash64(s.data(), s.size()); -} - std::ostream& operator<<(std::ostream& o, StringPiece piece) { o.write(piece.data(), piece.size()); return o; diff --git a/tensorflow/core/lib/core/stringpiece.h b/tensorflow/core/lib/core/stringpiece.h index caa9642774..910e4d9e2a 100644 --- a/tensorflow/core/lib/core/stringpiece.h +++ b/tensorflow/core/lib/core/stringpiece.h @@ -35,8 +35,6 @@ limitations under the License. namespace tensorflow { -struct StringPieceHasher; - class StringPiece { public: typedef size_t size_type; @@ -131,10 +129,6 @@ class StringPiece { // Intentionally copyable }; -struct StringPieceHasher { - size_t operator()(StringPiece s) const; -}; - inline bool operator==(StringPiece x, StringPiece y) { return ((x.size() == y.size()) && (memcmp(x.data(), y.data(), x.size()) == 0)); diff --git a/tensorflow/core/lib/core/stringpiece_test.cc b/tensorflow/core/lib/core/stringpiece_test.cc index 8f17b85b6d..d0dbeb6072 100644 --- a/tensorflow/core/lib/core/stringpiece_test.cc +++ b/tensorflow/core/lib/core/stringpiece_test.cc @@ -65,74 +65,4 @@ TEST(StringPiece, Contains) { EXPECT_TRUE(!a.contains(d)); } -TEST(StringPieceHasher, Equality) { - StringPieceHasher hasher; - - StringPiece s1("foo"); - StringPiece s2("bar"); - StringPiece s3("baz"); - StringPiece s4("zot"); - - EXPECT_TRUE(hasher(s1) != hasher(s2)); - EXPECT_TRUE(hasher(s1) != hasher(s3)); - EXPECT_TRUE(hasher(s1) != hasher(s4)); - EXPECT_TRUE(hasher(s2) != hasher(s3)); - EXPECT_TRUE(hasher(s2) != hasher(s4)); - EXPECT_TRUE(hasher(s3) != hasher(s4)); - - EXPECT_TRUE(hasher(s1) == hasher(s1)); - EXPECT_TRUE(hasher(s2) == hasher(s2)); - EXPECT_TRUE(hasher(s3) == hasher(s3)); - EXPECT_TRUE(hasher(s4) == hasher(s4)); -} - -TEST(StringPieceHasher, HashMap) { - string s1("foo"); - string s2("bar"); - string s3("baz"); - - StringPiece p1(s1); - StringPiece p2(s2); - StringPiece p3(s3); - - std::unordered_map<StringPiece, int, StringPieceHasher> map; - - map.insert(std::make_pair(p1, 0)); - map.insert(std::make_pair(p2, 1)); - map.insert(std::make_pair(p3, 2)); - EXPECT_EQ(map.size(), 3); - - bool found[3] = {false, false, false}; - for (auto const& val : map) { - int x = val.second; - EXPECT_TRUE(x >= 0 && x < 3); - EXPECT_TRUE(!found[x]); - found[x] = true; - } - EXPECT_EQ(found[0], true); - EXPECT_EQ(found[1], true); - EXPECT_EQ(found[2], true); - - auto new_iter = map.find("zot"); - EXPECT_TRUE(new_iter == map.end()); - - new_iter = map.find("bar"); - EXPECT_TRUE(new_iter != map.end()); - - map.erase(new_iter); - EXPECT_EQ(map.size(), 2); - - found[0] = false; - found[1] = false; - found[2] = false; - for (const auto& iter : map) { - int x = iter.second; - EXPECT_TRUE(x >= 0 && x < 3); - EXPECT_TRUE(!found[x]); - found[x] = true; - } - EXPECT_EQ(found[0], true); - EXPECT_EQ(found[1], false); - EXPECT_EQ(found[2], true); -} } // namespace tensorflow diff --git a/tensorflow/core/lib/hash/hash.h b/tensorflow/core/lib/hash/hash.h index 77b8031598..ca05e6346e 100644 --- a/tensorflow/core/lib/hash/hash.h +++ b/tensorflow/core/lib/hash/hash.h @@ -76,6 +76,7 @@ struct hash<StringPiece> { return static_cast<size_t>(Hash64(sp.data(), sp.size())); } }; +using StringPieceHasher = ::tensorflow::hash<StringPiece>; template <typename T, typename U> struct hash<std::pair<T, U>> { diff --git a/tensorflow/core/lib/hash/hash_test.cc b/tensorflow/core/lib/hash/hash_test.cc index 0e5f6c6803..7d58313132 100644 --- a/tensorflow/core/lib/hash/hash_test.cc +++ b/tensorflow/core/lib/hash/hash_test.cc @@ -13,6 +13,8 @@ See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ +#include <map> +#include <unordered_map> #include <vector> #include "tensorflow/core/lib/hash/hash.h" @@ -81,4 +83,75 @@ static void BM_Hash32(int iters, int len) { } BENCHMARK(BM_Hash32)->Range(1, 1024); +TEST(StringPieceHasher, Equality) { + StringPieceHasher hasher; + + StringPiece s1("foo"); + StringPiece s2("bar"); + StringPiece s3("baz"); + StringPiece s4("zot"); + + EXPECT_TRUE(hasher(s1) != hasher(s2)); + EXPECT_TRUE(hasher(s1) != hasher(s3)); + EXPECT_TRUE(hasher(s1) != hasher(s4)); + EXPECT_TRUE(hasher(s2) != hasher(s3)); + EXPECT_TRUE(hasher(s2) != hasher(s4)); + EXPECT_TRUE(hasher(s3) != hasher(s4)); + + EXPECT_TRUE(hasher(s1) == hasher(s1)); + EXPECT_TRUE(hasher(s2) == hasher(s2)); + EXPECT_TRUE(hasher(s3) == hasher(s3)); + EXPECT_TRUE(hasher(s4) == hasher(s4)); +} + +TEST(StringPieceHasher, HashMap) { + string s1("foo"); + string s2("bar"); + string s3("baz"); + + StringPiece p1(s1); + StringPiece p2(s2); + StringPiece p3(s3); + + std::unordered_map<StringPiece, int, StringPieceHasher> map; + + map.insert(std::make_pair(p1, 0)); + map.insert(std::make_pair(p2, 1)); + map.insert(std::make_pair(p3, 2)); + EXPECT_EQ(map.size(), 3); + + bool found[3] = {false, false, false}; + for (auto const& val : map) { + int x = val.second; + EXPECT_TRUE(x >= 0 && x < 3); + EXPECT_TRUE(!found[x]); + found[x] = true; + } + EXPECT_EQ(found[0], true); + EXPECT_EQ(found[1], true); + EXPECT_EQ(found[2], true); + + auto new_iter = map.find("zot"); + EXPECT_TRUE(new_iter == map.end()); + + new_iter = map.find("bar"); + EXPECT_TRUE(new_iter != map.end()); + + map.erase(new_iter); + EXPECT_EQ(map.size(), 2); + + found[0] = false; + found[1] = false; + found[2] = false; + for (const auto& iter : map) { + int x = iter.second; + EXPECT_TRUE(x >= 0 && x < 3); + EXPECT_TRUE(!found[x]); + found[x] = true; + } + EXPECT_EQ(found[0], true); + EXPECT_EQ(found[1], false); + EXPECT_EQ(found[2], true); +} + } // namespace tensorflow |