11dfa61ef222f3b8ed91778c191f4c9f17e3a9a4
[nit.git] / lib / standard / collection / hash_collection.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2004-2009 Jean Privat <jean@pryen.org>
4 #
5 # This file is free software, which comes along with NIT. This software is
6 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
7 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
8 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
9 # is kept unaltered, and a notification of the changes is added.
10 # You are allowed to redistribute it and sell it, alone or is a part of
11 # another product.
12
13 # Introduce Hashmap and Hashset.
14 package hash_collection
15
16 import array
17 import hash
18
19 # A HashCollection is an array of HashNode[K] indexed by the K hash value
20 private class HashCollection[K: Object, N: HashNode[K]]
21 super ArrayCapable[nullable N]
22 var _array: nullable NativeArray[nullable N] = null # Used to store items
23 var _capacity: Int = 0 # Size of _array
24 var _length: Int = 0 # Number of items in the map
25
26 readable var _first_item: nullable N = null # First added item (used to visit items in nice order)
27 var _last_item: nullable N = null # Last added item (same)
28
29 # The last key accessed (used for cache)
30 var _last_accessed_key: nullable K = null
31
32 # The last node accessed (used for cache)
33 var _last_accessed_node: nullable N = null
34
35 # Return the index of the key k
36 fun index_at(k: K): Int
37 do
38 var i = k.hash % _capacity
39 if i < 0 then i = - i
40 return i
41 end
42
43 # Return the node assosiated with the key
44 fun node_at(k: K): nullable N
45 do
46 # cache: `is' is used instead of `==' because it is a faster filter (even if not exact)
47 if k is _last_accessed_key then return _last_accessed_node
48
49 var res = node_at_idx(index_at(k), k)
50 _last_accessed_key = k
51 _last_accessed_node = res
52 return res
53 end
54
55 # Return the node assosiated with the key (but with the index already known)
56 fun node_at_idx(i: Int, k: K): nullable N
57 do
58 var c = _array[i]
59 while c != null do
60 var ck = c._key
61 if ck is k or ck == k then # prefilter with `is' because the compiler is not smart enought yet
62 break
63 end
64 c = c._next_in_bucklet
65 end
66 return c
67 end
68
69 # Add a new node at a given index
70 fun store(index: Int, node: N)
71 do
72 # Store the item in the list
73 if _first_item == null then
74 _first_item = node
75 else
76 _last_item._next_item = node
77 end
78 node._prev_item = _last_item
79 node._next_item = null
80 _last_item = node
81
82 # Then store it in the array
83 var next = _array[index]
84 _array[index] = node
85 node._next_in_bucklet = next
86 if next != null then next._prev_in_bucklet = node
87
88 _last_accessed_key = node._key
89 _last_accessed_node = node
90
91 # Enlarge if needed
92 var l = _length
93 _length = l + 1
94 l = (l + 5) * 3 / 2
95 if l >= _capacity then
96 enlarge(l * 2)
97 end
98 end
99
100 # Remove the node assosiated with the key
101 fun remove_node(k: K)
102 do
103 var i = index_at(k)
104 var node = node_at_idx(i, k)
105 if node == null then return
106
107 # Remove the item in the list
108 var prev = node._prev_item
109 var next = node._next_item
110 if prev != null then
111 prev._next_item = next
112 else
113 _first_item = next
114 end
115 if next != null then
116 next._prev_item = prev
117 else
118 _last_item = prev
119 end
120
121 # Remove the item in the array
122 _length -= 1
123 prev = node._prev_in_bucklet
124 next = node._next_in_bucklet
125 if prev != null then
126 prev._next_in_bucklet = next
127 else
128 _array[i] = next
129 end
130 if next != null then
131 next._prev_in_bucklet = prev
132 end
133
134 _last_accessed_key = null
135 end
136
137 fun raz
138 do
139 var i = _capacity - 1
140 while i >= 0 do
141 _array[i] = null
142 i -= 1
143 end
144 _length = 0
145 _first_item = null
146 _last_item = null
147 _last_accessed_key = null
148 end
149
150 fun enlarge(cap: Int)
151 do
152 var old_cap = _capacity
153 # get a new capacity
154 if cap < _length + 1 then cap = _length + 1
155 if cap <= _capacity then return
156 _capacity = cap
157 _last_accessed_key = null
158
159 # get a new array
160 var new_array = calloc_array(cap)
161 _array = new_array
162
163 # clean the new array
164 var i = cap - 1
165 while i >=0 do
166 new_array[i] = null
167 i -= 1
168 end
169
170 if _capacity <= old_cap then return
171
172 # Reput items in the array
173 var node = _first_item
174 while node != null do
175 var index = index_at(node._key)
176 # Then store it in the array
177 var next = new_array[index]
178 new_array[index] = node
179 node._next_in_bucklet = next
180 if next != null then next._prev_in_bucklet = node
181 node = node._next_item
182 end
183 end
184 end
185
186 private class HashNode[K: Object]
187 var _key: K
188 type N: HashNode[K]
189 readable writable var _next_item: nullable N = null
190 readable writable var _prev_item: nullable N = null
191 var _prev_in_bucklet: nullable N = null
192 var _next_in_bucklet: nullable N = null
193 init(k: K)
194 do
195 _key = k
196 end
197 end
198
199 class HashMap[K: Object, V]
200 super Map[K, V]
201 super HashCollection[K, HashMapNode[K, V]]
202
203 redef fun [](key)
204 do
205 var c = node_at(key)
206 if c == null then
207 abort
208 else
209 return c._value
210 end
211 end
212
213 redef fun iterator: HashMapIterator[K, V] do return new HashMapIterator[K,V](self)
214
215 redef fun iterate
216 !each(k: K, v: V)
217 do
218 var c = _first_item
219 while c != null do
220 each(c._key, c._value)
221 c = c._next_item
222 end
223 end
224
225 redef fun length do return _length
226
227 redef fun is_empty do return _length == 0
228
229 redef fun []=(key, v)
230 do
231 var i = index_at(key)
232 var c = node_at_idx(i, key)
233 if c != null then
234 c._key = key
235 c._value = v
236 else
237 store(i, new HashMapNode[K, V](key, v))
238 end
239 end
240
241 redef fun remove(item)
242 do
243 var c = _first_item
244 while c != null do
245 if c._value == item then
246 remove_node(c._key)
247 return
248 end
249 c = c._next_item
250 end
251 end
252
253 redef fun remove_at(key) do remove_node(key)
254
255 redef fun clear do raz
256
257 init
258 do
259 _capacity = 0
260 _length = 0
261 enlarge(0)
262 end
263
264 redef var keys: HashMapKeys[K, V] = new HashMapKeys[K, V](self)
265 redef var values: HashMapValues[K, V] = new HashMapValues[K, V](self)
266 end
267
268 class HashMapKeys[K: Object, V]
269 super NaiveCollection[K]
270 # The original map
271 var map: HashMap[K, V]
272
273 redef fun count(k) do if self.has(k) then return 1 else return 0
274 redef fun first do return self.map._first_item._key
275 redef fun has(k) do return self.map.node_at(k) != null
276 redef fun has_only(k) do return (self.has(k) and self.length == 1) or self.is_empty
277 redef fun is_empty do return self.map.is_empty
278 redef fun length do return self.map.length
279
280 redef fun iterator do return new MapKeysIterator[K, V](self.map.iterator)
281 end
282
283 class HashMapValues[K: Object, V]
284 super NaiveCollection[V]
285 # The original map
286 var map: HashMap[K, V]
287
288 redef fun count(item)
289 do
290 var nb = 0
291 var c = self.map._first_item
292 while c != null do
293 if c._value == item then nb += 1
294 c = c._next_item
295 end
296 return nb
297 end
298 redef fun first do return self.map._first_item._value
299
300 redef fun has(item)
301 do
302 var c = self.map._first_item
303 while c != null do
304 if c._value == item then return true
305 c = c._next_item
306 end
307 return false
308 end
309
310 redef fun has_only(item)
311 do
312 var c = self.map._first_item
313 while c != null do
314 if c._value != item then return false
315 c = c._next_item
316 end
317 return true
318 end
319
320 redef fun is_empty do return self.map.is_empty
321 redef fun length do return self.map.length
322
323 redef fun iterator do return new MapValuesIterator[K, V](self.map.iterator)
324 end
325
326 class HashMapNode[K: Object, V]
327 super HashNode[K]
328 redef type N: HashMapNode[K, V]
329 var _value: V
330
331 init(k: K, v: V)
332 do
333 super(k)
334 _value = v
335 end
336 end
337
338 class HashMapIterator[K: Object, V]
339 super MapIterator[K, V]
340 redef fun is_ok do return _node != null
341
342 redef fun item
343 do
344 assert is_ok
345 return _node._value
346 end
347
348 #redef fun item=(value)
349 #do
350 # assert is_ok
351 # _node.second = value
352 #end
353
354 redef fun key
355 do
356 assert is_ok
357 return _node._key
358 end
359
360 redef fun next
361 do
362 assert is_ok
363 _node = _node._next_item
364 end
365
366 # The map to iterate on
367 var _map: HashMap[K, V]
368
369 # The current node
370 var _node: nullable HashMapNode[K, V]
371
372 init(map: HashMap[K, V])
373 do
374 _map = map
375 _node = map.first_item
376 end
377 end
378
379 class HashSet[E: Object]
380 super Set[E]
381 super HashCollection[E, HashSetNode[E]]
382
383 redef fun length do return _length
384
385 redef fun is_empty do return _length == 0
386
387 redef fun first
388 do
389 assert _length > 0
390 return _first_item._key
391 end
392
393 redef fun has(item)
394 do
395 return node_at(item) != null
396 end
397
398 redef fun add(item)
399 do
400 var i = index_at(item)
401 var c = node_at_idx(i, item)
402 if c != null then
403 c._key = item
404 else
405 store(i,new HashSetNode[E](item))
406 end
407 end
408
409 redef fun remove(item) do remove_node(item)
410
411 redef fun clear do raz
412
413 redef fun iterator do return new HashSetIterator[E](self)
414
415 init
416 do
417 _capacity = 0
418 _length = 0
419 enlarge(0)
420 end
421 end
422
423 class HashSetNode[E: Object]
424 super HashNode[E]
425 redef type N: HashSetNode[E]
426
427 init(e: E)
428 do
429 _key = e
430 end
431 end
432
433 class HashSetIterator[E: Object]
434 super Iterator[E]
435 redef fun is_ok do return _node != null
436
437 redef fun item
438 do
439 assert is_ok
440 return _node._key
441 end
442
443 redef fun next
444 do
445 assert is_ok
446 _node = _node._next_item
447 end
448
449 # The set to iterate on
450 var _set: HashSet[E]
451
452 # The position in the internal map storage
453 var _node: nullable HashSetNode[E]
454
455 init(set: HashSet[E])
456 do
457 _set = set
458 _node = set._first_item
459 end
460 end
461