-
Notifications
You must be signed in to change notification settings - Fork 7
/
map.oc
279 lines (249 loc) · 6.63 KB
/
map.oc
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//! A simple hash-map
//!
//! This hash map uses closed hashing with linked lists for collision resolution.
//! The hash map will automatically resize itself when the number of items
//! exceeds the number of buckets.
//!
//! Sample usage:
//!
//! import std::map::Map
//!
//! let map = Map<u32, str>::new()
//! map.insert(1, "one")
//! map.insert(2, "two")
//! for it in map.iter() {
//! println(`{it.key} = {it.value}`)
//! }
//! map.remove(1)
import std::vector::Vector
import std::traits::{ hash, eq }
import std::mem
//* Linked list node for the hash map
struct Item<K, V> {
key: K
value: V
next: &Item<K, V>
}
//* Creates a new node
def Item::new(key: K, value: V, next: &Item<K, V> = null): &Item<K, V> {
let node = mem::alloc<Item<K, V>>()
node.key = key
node.value = value
node.next = next
return node
}
//* Free the linked list starting at this node
def Item::free_list(&this) {
let cur = this
while cur? {
let next = cur.next
std::mem::free(cur)
cur = next
}
}
//* A Hash-map
//*
//* Preconditions:
//* - `K` type must implement the `hash` method: `K::hash(this): u32`
//* - `K` type must implement the `eq` method: `K::eq(this, b: K): bool`
struct Map<K, V> {
buckets: &&Item<K, V>
size: u32
num_buckets: u32
num_collisions: u32
}
//* Creates a new hash map
def Map::new(capacity: u32 = 8): &Map<K, V> {
let map = mem::alloc<Map<K, V>>()
map.num_buckets = capacity
map.buckets = mem::alloc<&Item<K, V>>(map.num_buckets)
return map
}
//* Hashes a key and returns the bucket index
def Map::hash(&this, key: K): u32 {
let hash = key.hash()
hash = hash % .num_buckets
if hash < 0 {
hash += .num_buckets
}
return hash
}
//* Returns the node with the given key, or null
def Map::get_item(&this, key: K): &Item<K, V> {
let hash = .hash(key)
let node = .buckets[hash]
while node? {
if node.key.eq(key) {
return node
}
node = node.next
}
return null
}
[operator "[]"]
//* Returns the value for the given key, or fails
def Map::at(&this, key: K): V {
let node = .get_item(key)
assert node?, "Key not found"
return node.value
}
//* Returns the value for the given key, or the default value
def Map::get(&this, key: K, defolt: V): V {
let node = .get_item(key)
if not node? then return defolt
return node.value
}
[operator "in"]
//* Checks if the map contains the given key
def Map::contains(&this, key: K): bool {
return .get_item(key)?
}
[operator "[]="]
//* Inserts / updates the given key-value pair
def Map::insert(&this, key: K, value: V) {
let node = .get_item(key)
if node? {
node.value = value
} else {
let hash = .hash(key)
let new_node = Item<K, V>::new(key, value, .buckets[hash])
if .buckets[hash]? {
.num_collisions += 1
}
.buckets[hash] = new_node
.size += 1
if .size > .num_buckets {
.resize()
}
}
}
[operator "+="]
//* Inserts all key-value pairs from the other map into
def Map::extend(&this, other: &Map<K, V>) {
for iter in other.iter() {
.insert(iter.key, iter.value)
}
}
//* Removes the given key from the map
def Map::remove(&this, key: K) {
let node = .get_item(key)
if node? {
let hash = .hash(key)
if .buckets[hash] == node {
.buckets[hash] = node.next
} else {
let prev = .buckets[hash]
while prev.next != node {
prev = prev.next
}
prev.next = node.next
}
mem::free(node)
.size -= 1
}
}
//* Resizes the map to double the number of buckets
def Map::resize(&this) {
let old_buckets = .buckets
let old_num_buckets = .num_buckets
let old_size = .size
.num_collisions = 0
.num_buckets *= 2
.num_buckets = .num_buckets.max(16)
.buckets = mem::alloc<&Item<K, V>>(.num_buckets)
for let i = 0; i < old_num_buckets; i += 1 {
let node = old_buckets[i]
while node? {
let new_hash = .hash(node.key)
let new_node = Item<K, V>::new(node.key, node.value, .buckets[new_hash])
if .buckets[new_hash]? {
.num_collisions += 1
}
.buckets[new_hash] = new_node
node = node.next
}
}
for let i = 0; i < old_num_buckets; i += 1 {
old_buckets[i].free_list()
}
mem::free(old_buckets)
}
//* Checks if the map is empty
def Map::is_empty(&this): bool => .size == 0
//* Frees the map and all its nodes
def Map::free(&this) {
for let i = 0; i < .num_buckets; i += 1 {
.buckets[i].free_list()
}
mem::free(.buckets)
mem::free(this)
}
//* Clears the map
def Map::clear(&this) {
for let i = 0; i < .num_buckets; i += 1 {
.buckets[i].free_list()
.buckets[i] = null
}
.size = 0
.num_collisions = 0
}
//* Returns an iterator over the map
def Map::iter(&this): Iterator<K, V> {
return Iterator<K, V>::make(this)
}
//* Returns an iterator over the keys of the map
def Map::iter_keys(&this): KeyIterator<K, V> => KeyIterator<K, V>(.iter())
//* Returns an iterator over the values of the map
def Map::iter_values(&this): ValueIterator<K, V> => ValueIterator<K, V>(.iter())
//* Iterator over the items of a map
struct Iterator<K, V> {
idx: i32
node: &Item<K, V>
map: &Map<K, V>
}
def Iterator::key(&this): K {
return .node.key
}
def Iterator::value(&this): V {
return .node.value
}
def Iterator::make(map: &Map<K, V>): Iterator<K, V> {
let it = Iterator<K, V>(idx: -1, node: null, map)
it.next()
return it
}
def Iterator::has_value(&this): bool => .node?
def Iterator::cur(&this): &Item<K, V> => .node
def Iterator::next(&this) {
while .idx < .map.num_buckets as i32 {
while .node? {
.node = .node.next
if .node? return
}
.idx += 1
.node = if .idx < .map.num_buckets as i32 {
yield .map.buckets[.idx]
} else {
yield null
}
if .node? return
}
}
//* Iterator over the keys of a map
struct KeyIterator<K, V> {
map_iter: Iterator<K, V>
}
def KeyIterator::has_value(&this): bool => .map_iter.has_value()
def KeyIterator::cur(&this): K => .map_iter.cur().key
def KeyIterator::next(&this) {
.map_iter.next()
}
//* Iterator over the values of a map
struct ValueIterator<K, V> {
map_iter: Iterator<K, V>
}
def ValueIterator::has_value(&this): bool => .map_iter.has_value()
def ValueIterator::cur(&this): V => .map_iter.cur().value
def ValueIterator::next(&this) {
.map_iter.next()
}