-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmap.sn
53 lines (42 loc) · 1.16 KB
/
map.sn
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
import "./linked_list"
namespace std {
class KeyValuePair<K, T> {
key: K;
value: T*;
static fn new(key: K, value: const T&) : KeyValuePair<K, T> {
let ptr = std::memory::allocate<T>(1);
ptr[0] = value;
return KeyValuePair<K, T> {
key,
value = ptr,
};
}
}
class Map<K, T> {
list: LinkedList<KeyValuePair<K, T>>;
static fn new() : Map<K, T> {
return Map<K, T> {
list = LinkedList<KeyValuePair<K, T>>::new(),
};
}
fn insert(key: K, value: T) {
let pair = KeyValuePair<K, T>::new(key, value);
this->insert(pair);
}
fn insert(pair: KeyValuePair<K, T>) {
this->list.insert(pair);
}
fn get(key: K) : T& {
let node = this->list.first;
while node {
if node->value.key == key {
return node->value.value[0];
}
node = node->next;
}
}
fn [](key: K) : T& {
return this->get(key);
}
}
}