Skip to content

Commit

Permalink
Pass equality comparator to is_hash_table_key_empty.
Browse files Browse the repository at this point in the history
  • Loading branch information
levlam committed Mar 21, 2024
1 parent 1d6e7af commit 0c3da40
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion tdutils/td/utils/FlatHashMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
namespace td {

template <class KeyT, class ValueT, class HashT = Hash<KeyT>, class EqT = std::equal_to<KeyT>>
using FlatHashMap = FlatHashTable<MapNode<KeyT, ValueT>, HashT, EqT>;
using FlatHashMap = FlatHashTable<MapNode<KeyT, ValueT, EqT>, HashT, EqT>;
//using FlatHashMap = FlatHashMapChunks<KeyT, ValueT, HashT, EqT>;
//using FlatHashMap = std::unordered_map<KeyT, ValueT, HashT, EqT>;

Expand Down
8 changes: 4 additions & 4 deletions tdutils/td/utils/FlatHashMapChunks.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ class FlatHashTableChunks {
}

Iterator find(const KeyT &key) {
if (empty() || is_hash_table_key_empty(key)) {
if (empty() || is_hash_table_key_empty<EqT>(key)) {
return end();
}
const auto hash = calc_hash(key);
Expand Down Expand Up @@ -326,7 +326,7 @@ class FlatHashTableChunks {

template <class... ArgsT>
std::pair<Iterator, bool> emplace(KeyT key, ArgsT &&...args) {
CHECK(!is_hash_table_key_empty(key));
CHECK(!is_hash_table_key_empty<EqT>(key));
auto it = find(key);
if (it != end()) {
return {it, false};
Expand Down Expand Up @@ -562,10 +562,10 @@ class FlatHashTableChunks {
};

template <class KeyT, class ValueT, class HashT = Hash<KeyT>, class EqT = std::equal_to<KeyT>>
using FlatHashMapChunks = FlatHashTableChunks<MapNode<KeyT, ValueT>, HashT, EqT>;
using FlatHashMapChunks = FlatHashTableChunks<MapNode<KeyT, ValueT, EqT>, HashT, EqT>;

template <class KeyT, class HashT = Hash<KeyT>, class EqT = std::equal_to<KeyT>>
using FlatHashSetChunks = FlatHashTableChunks<SetNode<KeyT>, HashT, EqT>;
using FlatHashSetChunks = FlatHashTableChunks<SetNode<KeyT, EqT>, HashT, EqT>;

template <class NodeT, class HashT, class EqT, class FuncT>
void table_remove_if(FlatHashTableChunks<NodeT, HashT, EqT> &table, FuncT &&func) {
Expand Down
2 changes: 1 addition & 1 deletion tdutils/td/utils/FlatHashSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
namespace td {

template <class KeyT, class HashT = Hash<KeyT>, class EqT = std::equal_to<KeyT>>
using FlatHashSet = FlatHashTable<SetNode<KeyT>, HashT, EqT>;
using FlatHashSet = FlatHashTable<SetNode<KeyT, EqT>, HashT, EqT>;
//using FlatHashSet = FlatHashSetChunks<KeyT, HashT, EqT>;
//using FlatHashSet = std::unordered_set<KeyT, HashT, EqT>;

Expand Down
4 changes: 2 additions & 2 deletions tdutils/td/utils/FlatHashTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ class FlatHashTable {

template <class... ArgsT>
std::pair<NodePointer, bool> emplace(KeyT key, ArgsT &&...args) {
CHECK(!is_hash_table_key_empty(key));
CHECK(!is_hash_table_key_empty<EqT>(key));
if (unlikely(bucket_count_mask_ == 0)) {
CHECK(used_node_count_ == 0);
resize(8);
Expand Down Expand Up @@ -447,7 +447,7 @@ class FlatHashTable {
}

NodeT *find_impl(const KeyT &key) {
if (unlikely(nodes_ == nullptr) || is_hash_table_key_empty(key)) {
if (unlikely(nodes_ == nullptr) || is_hash_table_key_empty<EqT>(key)) {
return nullptr;
}
auto bucket = calc_bucket(key);
Expand Down
4 changes: 2 additions & 2 deletions tdutils/td/utils/HashTableUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

namespace td {

template <class KeyT>
template <class EqT, class KeyT>
bool is_hash_table_key_empty(const KeyT &key) {
return key == KeyT();
return EqT()(key, KeyT());
}

inline uint32 randomize_hash(uint32 h) {
Expand Down
10 changes: 5 additions & 5 deletions tdutils/td/utils/MapNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace td {

template <class KeyT, class ValueT, class Enable = void>
template <class KeyT, class ValueT, class EqT, class Enable = void>
struct MapNode {
using first_type = KeyT;
using second_type = ValueT;
Expand Down Expand Up @@ -72,7 +72,7 @@ struct MapNode {
}

bool empty() const {
return is_hash_table_key_empty(first);
return is_hash_table_key_empty<EqT>(first);
}

void clear() {
Expand All @@ -91,8 +91,8 @@ struct MapNode {
}
};

template <class KeyT, class ValueT>
struct MapNode<KeyT, ValueT, typename std::enable_if_t<(sizeof(KeyT) + sizeof(ValueT) > 28 * sizeof(void *))>> {
template <class KeyT, class ValueT, class EqT>
struct MapNode<KeyT, ValueT, EqT, typename std::enable_if_t<(sizeof(KeyT) + sizeof(ValueT) > 28 * sizeof(void *))>> {
struct Impl {
using first_type = KeyT;
using second_type = ValueT;
Expand All @@ -105,7 +105,7 @@ struct MapNode<KeyT, ValueT, typename std::enable_if_t<(sizeof(KeyT) + sizeof(Va
template <class InputKeyT, class... ArgsT>
Impl(InputKeyT &&key, ArgsT &&...args) : first(std::forward<InputKeyT>(key)) {
new (&second) ValueT(std::forward<ArgsT>(args)...);
DCHECK(!is_hash_table_key_empty(first));
DCHECK(!is_hash_table_key_empty<EqT>(first));
}
Impl(const Impl &) = delete;
Impl &operator=(const Impl &) = delete;
Expand Down
10 changes: 5 additions & 5 deletions tdutils/td/utils/SetNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace td {

template <class KeyT, class Enable = void>
template <class KeyT, class EqT, class Enable = void>
struct SetNode {
using public_key_type = KeyT;
using public_type = const KeyT;
Expand Down Expand Up @@ -54,7 +54,7 @@ struct SetNode {
}

bool empty() const {
return is_hash_table_key_empty(first);
return is_hash_table_key_empty<EqT>(first);
}

void clear() {
Expand All @@ -67,16 +67,16 @@ struct SetNode {
}
};

template <class KeyT>
struct SetNode<KeyT, typename std::enable_if_t<(sizeof(KeyT) > 28 * sizeof(void *))>> {
template <class KeyT, class EqT>
struct SetNode<KeyT, EqT, typename std::enable_if_t<(sizeof(KeyT) > 28 * sizeof(void *))>> {
struct Impl {
using second_type = KeyT;

KeyT first;

template <class InputKeyT>
explicit Impl(InputKeyT &&key) : first(std::forward<InputKeyT>(key)) {
DCHECK(!is_hash_table_key_empty(first));
DCHECK(!is_hash_table_key_empty<EqT>(first));
}
Impl(const Impl &) = delete;
Impl &operator=(const Impl &) = delete;
Expand Down
2 changes: 1 addition & 1 deletion tdutils/test/hashset_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ BENCHMARK_TEMPLATE(BM_mask, td::MaskSse2);
#endif

template <class KeyT, class ValueT, class HashT = td::Hash<KeyT>, class EqT = std::equal_to<KeyT>>
using FlatHashMapImpl = td::FlatHashTable<td::MapNode<KeyT, ValueT>, HashT, EqT>;
using FlatHashMapImpl = td::FlatHashTable<td::MapNode<KeyT, ValueT, EqT>, HashT, EqT>;

#define FOR_EACH_TABLE(F) \
F(FlatHashMapImpl) \
Expand Down

0 comments on commit 0c3da40

Please sign in to comment.