-
Notifications
You must be signed in to change notification settings - Fork 0
✨ Implement a HashMap w/ LinkedList #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
YousefEZ
wants to merge
4
commits into
main
Choose a base branch
from
hash-table
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ add_library(redis_lib | |
| redis_server.cpp | ||
| redis_schema.cpp | ||
| redis_meta.cpp | ||
| redis_hashtable.cpp | ||
| ) | ||
|
|
||
|
|
||
|
|
||
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,302 @@ | ||
| #ifndef INCLUDE_REDIS_HASHTABLE_H | ||
| #define INCLUDE_REDIS_HASHTABLE_H | ||
|
|
||
| #include <cassert> | ||
| #include <concepts> | ||
| #include <cstdint> | ||
| #include <functional> | ||
| #include <memory> | ||
| #include <optional> | ||
| #include <utility> | ||
|
|
||
| namespace redis { | ||
|
|
||
| constexpr std::size_t MAX_SIZE = 1 << 8; // 1M | ||
|
|
||
| using HashCode = std::size_t; | ||
|
|
||
| template <typename K> | ||
| concept Key = requires(K key) | ||
| { | ||
| {std::hash<K>{}(key)}->std::convertible_to<HashCode>; | ||
| }; | ||
|
|
||
| template <Key K, typename V> | ||
| class Node { | ||
| K m_key; | ||
| V m_value; | ||
| std::unique_ptr<Node> m_next; | ||
| HashCode m_hashcode; | ||
|
|
||
| public: | ||
| Node(K key, V value) | ||
| : m_key{std::move(key)} | ||
| , m_value{std::move(value)} | ||
| , m_next{nullptr} | ||
| , m_hashcode{std::hash(m_key)} | ||
| { | ||
| } | ||
|
|
||
| Node(const Node& node) | ||
| : m_key{node.m_key} | ||
| , m_value{node.m_value} | ||
| , m_next{nullptr} | ||
| , m_hashcode{node.m_hashcode} | ||
| { | ||
| } | ||
|
|
||
| uint64_t chain_length() const | ||
| { | ||
| Node* curr = m_next.get(); | ||
| uint64_t size = 1; | ||
| while (curr) { | ||
| ++size; | ||
| curr = curr->m_next.get(); | ||
| } | ||
| return size; | ||
| } | ||
|
|
||
| const K& key() const { return m_key; } | ||
| V& value() const { return m_value; } | ||
| HashCode hashcode() const { return m_hashcode; } | ||
|
|
||
| operator V&() { return m_value; } | ||
|
|
||
| friend bool operator==(const Node<K, V>& a, const Node<K, V>& b); | ||
| }; | ||
|
|
||
| template <Key K, typename V> | ||
| bool operator==(const Node<K, V>& a, const Node<K, V>& b) | ||
| { | ||
| return a.key() == b.key(); | ||
| } | ||
|
|
||
| template <Key K, typename V> | ||
| class Rehasher; | ||
|
|
||
| template <Key K, typename V> | ||
| class HashMapImpl { | ||
| using HashNode = Node<K, V>; | ||
|
|
||
| friend Rehasher<K, V>; | ||
|
|
||
| // power of 2 array size 2^n - 1, used to cover hcode | ||
| std::size_t m_mask = MAX_SIZE - 1; | ||
|
|
||
| // number of keys in the table | ||
| std::size_t m_size = 0; | ||
|
|
||
| std::unique_ptr<std::unique_ptr<HashNode>[]> m_table; | ||
|
|
||
| std::unique_ptr<HashNode>& get_slot_linked_list(uint64_t hcode) | ||
| { | ||
| return m_table[hcode & m_mask]; | ||
| } | ||
|
|
||
| public: | ||
| HashMapImpl(std::size_t max_size) | ||
| : m_mask{max_size - 1} | ||
| , m_table{new std::unique_ptr<HashNode>[max_size]} | ||
| { | ||
| assert(max_size > 0 && ((max_size & (max_size - 1)) == 0) && | ||
| "size must be power of 2"); | ||
| } | ||
|
|
||
| V& insert_or_assign(std::unique_ptr<HashNode> node) | ||
| { | ||
| std::unique_ptr<HashNode>& head_node = get_slot_linked_list( | ||
| node.hashcode()); | ||
| if (head_node == nullptr) { | ||
| head_node = node; | ||
| return; | ||
| } | ||
| else if (*head_node == *node) { | ||
| node->m_next = std::move(head_node->m_next); | ||
| head_node = std::move(node); | ||
| return; | ||
| } | ||
|
|
||
| HashNode* previous = head_node.get(); | ||
| HashNode* current = head_node->m_next.get(); | ||
| while (current->m_next) { | ||
| if (*current == *node) { | ||
| // the key already exists, so we just need to assign the value | ||
| node->m_next = std::move(current->m_next); | ||
| previous->m_next = std::move(node); | ||
| return current->value(); | ||
| } | ||
| previous = current; | ||
| current = current->m_next.get(); | ||
| } | ||
|
|
||
| // current is now the tail of LL | ||
| current->m_next = std::move(node); | ||
|
|
||
| ++m_size; | ||
| return current->m_next.value(); | ||
| } | ||
|
|
||
| V& insert_or_assign(K key, V value) | ||
| { | ||
| return insert( | ||
| std::make_unique<HashNode>(std::move(key), std::move(value))); | ||
| } | ||
|
|
||
| std::optional<std::reference_wrapper<HashNode> > get(uint64_t hcode) | ||
| { | ||
| std::unique_ptr<HashNode>& head_node = get_slot_linked_list(hcode); | ||
| if (head_node == nullptr) { | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| HashNode* current = head_node.get(); | ||
| do { | ||
| if (current->m_hcode == hcode) { | ||
| return *current; | ||
| } | ||
| current = current->m_next.get(); | ||
| } while ((current->m_next)); | ||
|
|
||
| return std::nullopt; | ||
| } | ||
|
|
||
| std::unique_ptr<HashNode> detach(const K& key) | ||
| { | ||
| HashCode hcode = std::hash<K>{}(key); | ||
| std::size_t idx = hcode & m_mask; | ||
| std::unique_ptr<HashNode>& head_node = m_table[idx]; | ||
| if (head_node == nullptr) { | ||
| return nullptr; | ||
| } | ||
| else if (head_node->m_hcode == hcode && head_node->key() == key) { | ||
| // move the state of head_node (head of slot) out to local | ||
| std::unique_ptr<HashNode> detachedNode = std::move(head_node); | ||
| // set the value of head slot to what detached->m_next is pointing | ||
| head_node = std::move(detachedNode->m_next); | ||
| // move resets m_next to nullptr | ||
| return detachedNode; | ||
| } | ||
|
|
||
| HashNode* previous = head_node.get(); | ||
| HashNode* current = head_node->m_next.get(); | ||
|
|
||
| do { | ||
| if (current->m_hcode == hcode && head_node->key() == key) { | ||
| previous->m_next = std::move(current->m_next); | ||
| // move sets next m_next to nullptr. | ||
| --m_size; | ||
| return current; | ||
| } | ||
| previous = current; | ||
| current = current->m_next.get(); | ||
| } while ((current->m_next)); | ||
|
|
||
| return nullptr; | ||
| } | ||
| }; | ||
| template <Key K, typename V> | ||
| class Rehasher { | ||
| using HashNode = HashMapImpl<K, V>::HashNode; | ||
| HashMapImpl<K, V>& m_black_map; | ||
| HashMapImpl<K, V>& m_red_map; | ||
|
|
||
| std::size_t rehash_block(std::size_t idx) | ||
| { | ||
| std::unique_ptr<HashNode>& block = *(m_red_map.m_table)[idx]; | ||
| std::size_t count = 0; | ||
| for (std::size_t count = 0; block != nullptr; | ||
| ++count, --m_red_map.m_size) { | ||
| std::unique_ptr<HashNode> next = std::move(block->m_next); | ||
| m_black_map.insert_or_assign(std::move(block)); | ||
|
|
||
| block = std::move(next); | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| public: | ||
| template <std::size_t REHASH_SIZE> | ||
| void rehash() | ||
| { | ||
| std::size_t count = 0; | ||
| for (std::size_t idx = 0; | ||
| count < REHASH_SIZE && idx <= m_red_map.m_mask; | ||
| idx++) { | ||
| count += rehash_block(idx); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| template <Key K, typename V, std::size_t REHASH_SIZE = 128> | ||
| class HashMap { | ||
| using HashNode = Node<K, V>; | ||
|
|
||
| friend Rehasher<K, V>; | ||
|
|
||
| HashMapImpl<K, V> m_black_map; | ||
| std::optional<HashMapImpl<K, V> > m_red_map = std::nullopt; | ||
| uint64_t m_migration_position = 0; | ||
|
|
||
| void resize() {} | ||
|
|
||
| // TODO: think about whether the trigger should be implicit or explicit | ||
| void trigger_rehashing() | ||
| { | ||
| if (!m_red_map) { | ||
| return; | ||
| } | ||
|
|
||
| Rehasher{m_black_map, *m_red_map}.template rehash<REHASH_SIZE>(); | ||
| } | ||
|
|
||
| public: | ||
| HashMap(std::size_t size) | ||
| : m_black_map(size) | ||
| { | ||
| } | ||
|
|
||
| std::optional<std::reference_wrapper<V> > get(K key) | ||
| { | ||
| HashCode hcode = std::hash<K>{}(key); | ||
| if (auto node = m_black_map.get(hcode)) { | ||
| return node; | ||
| } | ||
|
|
||
| if (!m_red_map) { | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| if (auto node = m_red_map->detach(hcode)) { | ||
| // optional->get to get underlying of reference_wrapper | ||
| m_black_map.insert(std::move(node->get())); | ||
|
|
||
| if (m_red_map.size() == 0) [[unlikely]] { | ||
| m_red_map.reset(); | ||
| } | ||
| return *(m_black_map.get(hcode)).value(); | ||
| } | ||
| trigger_rehashing(); | ||
|
|
||
| return std::nullopt; | ||
| } | ||
|
|
||
| std::optional<std::reference_wrapper<V> > insert_or_assign(K key, V value) | ||
| { | ||
| trigger_rehashing(); | ||
| auto mapped_value = m_black_map.insert_or_assign(std::move(key), | ||
| std::move(value)); | ||
| m_red_map.detach(key); | ||
| return mapped_value; | ||
| } | ||
|
|
||
| void remove(const K& key) | ||
| { | ||
| if (!m_black_map.detach(key) && m_red_map) { | ||
| m_red_map->detach(key); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.