include cstdio include cstring include cassert include HashFunctions c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <cstdio>
#include <cstring>
#include <cassert>
#include "HashFunctions.cpp"
template <typename T>
class Node {
public:
T val;
Node* next_;
Node(T val): val(val), next_(nullptr) {};
};
template <typename T>
class List {
public:
Node<T>* head_;
unsigned long size_;
List(): head_(nullptr), size_(0){}
void push_front(Node<T>* new_node){
assert(new_node != nullptr);
new_node->next_ = this->head_;
this->head_ = new_node;
++this->size_;
}
};
template <typename T1, typename T2>
struct Pair_t {
T1 first;
T2 second;
};
template <typename key_t, typename val_t, unsigned long (*hash)(key_t), int (*cmp)(key_t, key_t), unsigned long max_size>
class HashTable {
public:
List<Pair_t<key_t, val_t>>* table;
HashTable(){
table = new List<Pair_t<key_t, val_t>>[max_size]();
}
void insert(key_t key, val_t val){
unsigned long h = hash(key);
Node<Pair_t<key_t, val_t>>* new_node = new Node<Pair_t<key_t, val_t>>(Pair_t<key_t, val_t>{key, val});
table[h].push_front(new_node);
}
Node<Pair_t<key_t, val_t>>* find(key_t key){
unsigned long h = hash(key);
Node<Pair_t<key_t, val_t>>* cur = table[h].head_;
while (cur != nullptr){
if (cmp(key, cur->val.first) == 0){
return cur;
}
cur = cur->next_;
}
}
};
int main(void){
HashTable<const char*, int, one_at_a_time, strcmp, 100> t{};
FILE* input = fopen("./words_alpha.txt", "r");
char word[100] = "";
fscanf(input, "%s", word);
t.insert(word, 228);
fclose(input);
}