Skip to content

Instantly share code, notes, and snippets.

View debbynirwan's full-sized avatar
🏠
Working from home

Debby Nirwan debbynirwan

🏠
Working from home
View GitHub Profile
start = clock();
for (int i = 0; i < j; i++) {
uTable.insert({ i, i * 5 });
}
end = clock();
cpu_time_used = end - start;
#include <time.h>
void test()
{
clock_t start, end;
double cpu_time_used;
start = clock();
// insert your code here
end = clock();
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);
}
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;
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];
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;
}
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];
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)
std::vector<Node*> m_table;
template <class Key, class Value>
struct Node {
Key key;
Value value;
std::size_t hash_code;
Node* next;
};