Skip to content

Commit ad88cb8

Browse files
committed
Make parsing hashtables more easy:
Avoid unnecessary strings/cord.h include
1 parent f692a0b commit ad88cb8

File tree

5 files changed

+59
-13
lines changed

5 files changed

+59
-13
lines changed

absl/container/flat_hash_map_test.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#include "absl/container/flat_hash_map.h"
16+
#include "absl/strings/cord.h"
1617
#include "absl/algorithm/container.h"
1718

1819
#include <memory>
@@ -73,6 +74,15 @@ using UniquePtrMapTypes = ::testing::Types<Map<int, std::unique_ptr<int>>>;
7374
INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashMap, UniquePtrModifiersTest,
7475
UniquePtrMapTypes);
7576

77+
TEST(FlatHashMap, Cord) {
78+
absl::Cord cord;
79+
cord.Append("abcd");
80+
absl::flat_hash_map<absl::Cord, int> set;
81+
set[cord] = 10;
82+
EXPECT_EQ(set[cord], 10);
83+
}
84+
85+
7686
TEST(FlatHashMap, StandardLayout) {
7787
struct Int {
7888
explicit Int(size_t value) : value(value) {}

absl/container/flat_hash_set_test.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#include "absl/algorithm/container.h"
16+
#include "absl/strings/cord.h"
1617
#include "absl/container/flat_hash_set.h"
1718

1819
#include <vector>
@@ -64,6 +65,14 @@ INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, LookupTest, SetTypes);
6465
INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, MembersTest, SetTypes);
6566
INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, ModifiersTest, SetTypes);
6667

68+
TEST(FlatHashSet, Cord) {
69+
absl::Cord cord;
70+
cord.Append("abcd");
71+
absl::flat_hash_set<absl::Cord> set;
72+
set.insert(cord);
73+
EXPECT_TRUE(set.contains(cord));
74+
}
75+
6776
TEST(FlatHashSet, EmplaceString) {
6877
std::vector<std::string> v = {"a", "b"};
6978
absl::flat_hash_set<absl::string_view> hs(v.begin(), v.end());
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#if defined(ABSL_STRINGS_CORD_H_) && \
2+
defined(ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_DEFAULTS_H_)
3+
4+
#ifndef ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_CORD_H_
5+
#define ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_CORD_H_
6+
7+
namespace container_internal {
8+
9+
inline size_t StringHash::operator()(const absl::Cord& v) const {
10+
return absl::Hash<absl::Cord>{}(v);
11+
}
12+
inline bool StringEq::operator()(const absl::Cord& lhs, const Cord& rhs) const {
13+
return lhs == rhs;
14+
}
15+
inline bool StringEq::operator()(const absl::Cord& lhs,
16+
absl::string_view rhs) const {
17+
return lhs == rhs;
18+
}
19+
inline bool StringEq::operator()(absl::string_view lhs,
20+
const absl::Cord& rhs) const {
21+
return lhs == rhs;
22+
}
23+
24+
} // namespace container_internal
25+
26+
#endif // ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_CORD_H_
27+
#endif

absl/container/internal/hash_function_defaults.h

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@
5353

5454
#include "absl/base/config.h"
5555
#include "absl/hash/hash.h"
56-
#include "absl/strings/cord.h"
5756
#include "absl/strings/string_view.h"
5857

5958
namespace absl {
6059
ABSL_NAMESPACE_BEGIN
60+
61+
class Cord;
62+
6163
namespace container_internal {
6264

6365
// The hash of an object of type T is computed by using absl::Hash.
@@ -73,25 +75,17 @@ struct StringHash {
7375
size_t operator()(absl::string_view v) const {
7476
return absl::Hash<absl::string_view>{}(v);
7577
}
76-
size_t operator()(const absl::Cord& v) const {
77-
return absl::Hash<absl::Cord>{}(v);
78-
}
78+
size_t operator()(const absl::Cord& v) const;
7979
};
8080

8181
struct StringEq {
8282
using is_transparent = void;
8383
bool operator()(absl::string_view lhs, absl::string_view rhs) const {
8484
return lhs == rhs;
8585
}
86-
bool operator()(const absl::Cord& lhs, const absl::Cord& rhs) const {
87-
return lhs == rhs;
88-
}
89-
bool operator()(const absl::Cord& lhs, absl::string_view rhs) const {
90-
return lhs == rhs;
91-
}
92-
bool operator()(absl::string_view lhs, const absl::Cord& rhs) const {
93-
return lhs == rhs;
94-
}
86+
bool operator()(const absl::Cord& lhs, const absl::Cord& rhs) const;
87+
bool operator()(const absl::Cord& lhs, absl::string_view rhs) const;
88+
bool operator()(absl::string_view lhs, const absl::Cord& rhs) const;
9589
};
9690

9791
// Supports heterogeneous lookup for string-like elements.
@@ -157,6 +151,9 @@ template <class T>
157151
using hash_default_eq = typename container_internal::HashEq<T>::Eq;
158152

159153
} // namespace container_internal
154+
155+
#include "absl/container/internal/hash_function_cord.h"
156+
160157
ABSL_NAMESPACE_END
161158
} // namespace absl
162159

absl/strings/cord.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,6 +1669,9 @@ class CordTestAccess {
16691669
static uint8_t LengthToTag(size_t s);
16701670
};
16711671
} // namespace strings_internal
1672+
1673+
#include "absl/container/internal/hash_function_cord.h"
1674+
16721675
ABSL_NAMESPACE_END
16731676
} // namespace absl
16741677

0 commit comments

Comments
 (0)