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
start = clock(); | |
for (int i = 0; i < j; i++) { | |
uTable.insert({ i, i * 5 }); | |
} | |
end = clock(); | |
cpu_time_used = end - start; |
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
#include <time.h> | |
void test() | |
{ | |
clock_t start, end; | |
double cpu_time_used; | |
start = clock(); | |
// insert your code here | |
end = clock(); |
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
template <class Key, class Value> | |
void HashTable<Key, Value>::Reserve(const std::size_t n) | |
{ | |
std::size_t size = (n <= 0) ? 1 : n; | |
Rehash(size); | |
} |
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
template <class Key, class Value> | |
void HashTable<Key, Value>::Remove(const Key& key) | |
{ | |
if (m_size == 0) | |
return; | |
std::size_t hash_code = m_hashFunc(key); | |
std::size_t index = HashCodeToIndex(hash_code, BucketCount()); | |
Node* node = m_table[index]; | |
Node* prev_node = nullptr; |
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
template <class Key, class Value> | |
Value HashTable<Key, Value>::At(const Key& key) | |
{ | |
if (m_size == 0) | |
throw std::out_of_range("table is empty"); | |
std::size_t hash_code = m_hashFunc(key); | |
std::size_t index = HashCodeToIndex(hash_code, BucketCount()); | |
Node* node = m_table[index]; |
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
template <class Key, class Value> | |
void HashTable<Key, Value>::Insert(const Key& key, const Value& value) | |
{ | |
Node* node = PrepareInsertion(key); | |
if (node != nullptr) { | |
node->value = value; | |
return; | |
} |
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
template <class Key, class Value> | |
void HashTable<Key, Value>::Rehash(const std::size_t n) | |
{ | |
std::vector<Node*> newTable(n); | |
for (int i = 0; i < n; i++) | |
newTable[i] = nullptr; | |
if (m_table.size() != 0) { | |
for (int i = 0; i < BucketCount(); i++) { | |
Node* current_node = m_table[i]; |
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
template <class Key, class Value> | |
typename HashTable<Key, Value>::Node* HashTable<Key, Value>::PrepareInsertion(const Key& key) | |
{ | |
if (m_size != 0) { | |
std::size_t hash_code = m_hashFunc(key); | |
std::size_t index = HashCodeToIndex(hash_code, BucketCount()); | |
Node* node = m_table[index]; | |
while (node != nullptr) { | |
if (node->key == key) |
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
std::vector<Node*> m_table; |
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
template <class Key, class Value> | |
struct Node { | |
Key key; | |
Value value; | |
std::size_t hash_code; | |
Node* next; | |
}; |
NewerOlder