Simplify FlatHashTable implementation.

This commit is contained in:
levlam 2022-02-23 16:34:42 +03:00
parent 73ade8cc43
commit b307396628

View File

@ -160,14 +160,12 @@ struct SetNode {
template <class NodeT, class HashT, class EqT> template <class NodeT, class HashT, class EqT>
class FlatHashTable { class FlatHashTable {
public: public:
using Self = FlatHashTable<NodeT, HashT, EqT>; using NodeIterator = NodeT *;
using Node = NodeT; using ConstNodeIterator = const NodeT *;
using NodeIterator = typename fixed_vector<Node>::iterator;
using ConstNodeIterator = typename fixed_vector<Node>::const_iterator;
using KeyT = typename Node::public_key_type; using KeyT = typename NodeT::public_key_type;
using key_type = typename Node::public_key_type; using key_type = typename NodeT::public_key_type;
using value_type = typename Node::public_type; using value_type = typename NodeT::public_type;
struct Iterator { struct Iterator {
using iterator_category = std::bidirectional_iterator_tag; using iterator_category = std::bidirectional_iterator_tag;
@ -205,12 +203,12 @@ class FlatHashTable {
} }
Iterator() = default; Iterator() = default;
Iterator(NodeIterator it, Self *map) : it_(std::move(it)), map_(map) { Iterator(NodeIterator it, FlatHashTable *map) : it_(std::move(it)), map_(map) {
} }
private: private:
NodeIterator it_; NodeIterator it_;
Self *map_; FlatHashTable *map_;
}; };
struct ConstIterator { struct ConstIterator {
@ -261,7 +259,7 @@ class FlatHashTable {
assign(other); assign(other);
} }
FlatHashTable(std::initializer_list<Node> nodes) { FlatHashTable(std::initializer_list<NodeT> nodes) {
if (nodes.size() == 0) { if (nodes.size() == 0) {
return; return;
} }
@ -320,7 +318,7 @@ class FlatHashTable {
} }
ConstIterator find(const KeyT &key) const { ConstIterator find(const KeyT &key) const {
return ConstIterator(const_cast<Self *>(this)->find(key)); return ConstIterator(const_cast<FlatHashTable *>(this)->find(key));
} }
size_t size() const { size_t size() const {
@ -346,10 +344,10 @@ class FlatHashTable {
} }
ConstIterator begin() const { ConstIterator begin() const {
return ConstIterator(const_cast<Self *>(this)->begin()); return ConstIterator(const_cast<FlatHashTable *>(this)->begin());
} }
ConstIterator end() const { ConstIterator end() const {
return ConstIterator(const_cast<Self *>(this)->end()); return ConstIterator(const_cast<FlatHashTable *>(this)->end());
} }
void reserve(size_t size) { void reserve(size_t size) {
@ -390,7 +388,7 @@ class FlatHashTable {
} }
} }
template <class T = typename Node::second_type> template <class T = typename NodeT::second_type>
T &operator[](const KeyT &key) { T &operator[](const KeyT &key) {
return emplace(key).first->second; return emplace(key).first->second;
} }
@ -445,7 +443,7 @@ class FlatHashTable {
} }
private: private:
fixed_vector<Node> nodes_; fixed_vector<NodeT> nodes_;
size_t used_nodes_{}; size_t used_nodes_{};
void assign(const FlatHashTable &other) { void assign(const FlatHashTable &other) {
@ -500,7 +498,7 @@ class FlatHashTable {
} }
void resize(size_t new_size) { void resize(size_t new_size) {
fixed_vector<Node> old_nodes(new_size); fixed_vector<NodeT> old_nodes(new_size);
old_nodes.swap(nodes_); old_nodes.swap(nodes_);
for (auto &old_node : old_nodes) { for (auto &old_node : old_nodes) {