Skip to content

Commit

Permalink
Make parsing hashtables more easy:
Browse files Browse the repository at this point in the history
Avoid unnecessary strings/cord.h include
  • Loading branch information
MBkkt committed Dec 19, 2022
1 parent f692a0b commit ad88cb8
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 13 deletions.
10 changes: 10 additions & 0 deletions absl/container/flat_hash_map_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include "absl/container/flat_hash_map.h"
#include "absl/strings/cord.h"
#include "absl/algorithm/container.h"

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

TEST(FlatHashMap, Cord) {
absl::Cord cord;
cord.Append("abcd");
absl::flat_hash_map<absl::Cord, int> set;
set[cord] = 10;
EXPECT_EQ(set[cord], 10);
}


TEST(FlatHashMap, StandardLayout) {
struct Int {
explicit Int(size_t value) : value(value) {}
Expand Down
9 changes: 9 additions & 0 deletions absl/container/flat_hash_set_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include "absl/algorithm/container.h"
#include "absl/strings/cord.h"
#include "absl/container/flat_hash_set.h"

#include <vector>
Expand Down Expand Up @@ -64,6 +65,14 @@ INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, LookupTest, SetTypes);
INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, MembersTest, SetTypes);
INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, ModifiersTest, SetTypes);

TEST(FlatHashSet, Cord) {
absl::Cord cord;
cord.Append("abcd");
absl::flat_hash_set<absl::Cord> set;
set.insert(cord);
EXPECT_TRUE(set.contains(cord));
}

TEST(FlatHashSet, EmplaceString) {
std::vector<std::string> v = {"a", "b"};
absl::flat_hash_set<absl::string_view> hs(v.begin(), v.end());
Expand Down
27 changes: 27 additions & 0 deletions absl/container/internal/hash_function_cord.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#if defined(ABSL_STRINGS_CORD_H_) && \
defined(ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_DEFAULTS_H_)

#ifndef ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_CORD_H_
#define ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_CORD_H_

namespace container_internal {

inline size_t StringHash::operator()(const absl::Cord& v) const {
return absl::Hash<absl::Cord>{}(v);
}
inline bool StringEq::operator()(const absl::Cord& lhs, const Cord& rhs) const {
return lhs == rhs;
}
inline bool StringEq::operator()(const absl::Cord& lhs,
absl::string_view rhs) const {
return lhs == rhs;
}
inline bool StringEq::operator()(absl::string_view lhs,
const absl::Cord& rhs) const {
return lhs == rhs;
}

} // namespace container_internal

#endif // ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_CORD_H_
#endif
23 changes: 10 additions & 13 deletions absl/container/internal/hash_function_defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@

#include "absl/base/config.h"
#include "absl/hash/hash.h"
#include "absl/strings/cord.h"
#include "absl/strings/string_view.h"

namespace absl {
ABSL_NAMESPACE_BEGIN

class Cord;

namespace container_internal {

// The hash of an object of type T is computed by using absl::Hash.
Expand All @@ -73,25 +75,17 @@ struct StringHash {
size_t operator()(absl::string_view v) const {
return absl::Hash<absl::string_view>{}(v);
}
size_t operator()(const absl::Cord& v) const {
return absl::Hash<absl::Cord>{}(v);
}
size_t operator()(const absl::Cord& v) const;
};

struct StringEq {
using is_transparent = void;
bool operator()(absl::string_view lhs, absl::string_view rhs) const {
return lhs == rhs;
}
bool operator()(const absl::Cord& lhs, const absl::Cord& rhs) const {
return lhs == rhs;
}
bool operator()(const absl::Cord& lhs, absl::string_view rhs) const {
return lhs == rhs;
}
bool operator()(absl::string_view lhs, const absl::Cord& rhs) const {
return lhs == rhs;
}
bool operator()(const absl::Cord& lhs, const absl::Cord& rhs) const;
bool operator()(const absl::Cord& lhs, absl::string_view rhs) const;
bool operator()(absl::string_view lhs, const absl::Cord& rhs) const;
};

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

} // namespace container_internal

#include "absl/container/internal/hash_function_cord.h"

ABSL_NAMESPACE_END
} // namespace absl

Expand Down
3 changes: 3 additions & 0 deletions absl/strings/cord.h
Original file line number Diff line number Diff line change
Expand Up @@ -1669,6 +1669,9 @@ class CordTestAccess {
static uint8_t LengthToTag(size_t s);
};
} // namespace strings_internal

#include "absl/container/internal/hash_function_cord.h"

ABSL_NAMESPACE_END
} // namespace absl

Expand Down

0 comments on commit ad88cb8

Please sign in to comment.