core::hash_collection: do not allocate the storage, wait for the first `store`.
[nit.git] / lib / core / 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 module hash_collection
15
16 import array
17
18 redef class Map[K, V]
19 # Get a `HashMap[K, V]` as default implementation
20 new do return new HashMap[K, V]
21 end
22
23 # A HashCollection is an array of HashNode[K] indexed by the K hash value
24 private abstract class HashCollection[K]
25 type N: HashNode[K]
26
27 var array: nullable NativeArray[nullable N] = null # Used to store items
28 var capacity: Int = 0 # Size of _array
29 var the_length: Int = 0 # Number of items in the map
30
31 var first_item: nullable N = null # First added item (used to visit items in nice order)
32 var last_item: nullable N = null # Last added item (same)
33
34 # The last key accessed (used for cache)
35 var last_accessed_key: nullable Object = null
36
37 # The last node accessed (used for cache)
38 var last_accessed_node: nullable N = null
39
40 # Return the index of the key k
41 fun index_at(k: nullable Object): Int
42 do
43 if k == null then return 0
44
45 var i = k.hash % _capacity
46 if i < 0 then i = - i
47 return i
48 end
49
50 # Return the node associated with the key
51 fun node_at(k: nullable Object): nullable N
52 do
53 if _the_length == 0 then return null
54 # cache: `is` is used instead of `==` because it is a faster filter (even if not exact)
55 if k.is_same_instance(_last_accessed_key) then return _last_accessed_node
56
57 var res = node_at_idx(index_at(k), k)
58 _last_accessed_key = k
59 _last_accessed_node = res
60 return res
61 end
62
63 # Return the node associated with the key (but with the index already known)
64 fun node_at_idx(i: Int, k: nullable Object): nullable N
65 do
66 if _the_length == 0 then return null
67 var c = _array[i]
68 while c != null do
69 var ck = c._key
70 if ck.is_same_instance(k) or ck == k then # FIXME prefilter because the compiler is not smart enought yet
71 break
72 end
73 c = c._next_in_bucklet
74 end
75 return c
76 end
77
78 # Add a new node at a given index
79 fun store(index: Int, node: N)
80 do
81 # Store the item in the list
82 if _first_item == null then
83 _first_item = node
84 else
85 _last_item._next_item = node
86 end
87 node._prev_item = _last_item
88 node._next_item = null
89 _last_item = node
90
91 # Then store it in the array
92 var next = _array[index]
93 _array[index] = node
94 node._next_in_bucklet = next
95 if next != null then next._prev_in_bucklet = node
96
97 _last_accessed_key = node._key
98 _last_accessed_node = node
99
100 # Enlarge if needed
101 var l = _the_length
102 _the_length = l + 1
103
104 # Magic values determined empirically
105 # We do not want to enlarge too much
106 # We also want a odd capacity so that the modulo is more distributive
107 l = (l + 5) * 2 + 1
108 if l >= _capacity then
109 enlarge(l * 3 / 2 + 1)
110 end
111 end
112
113 # Remove the node assosiated with the key
114 fun remove_node(k: nullable Object)
115 do
116 if _the_length == 0 then return
117 var i = index_at(k)
118 var node = node_at_idx(i, k)
119 if node == null then return
120
121 # Remove the item in the list
122 var prev = node._prev_item
123 var next = node._next_item
124 if prev != null then
125 prev._next_item = next
126 else
127 _first_item = next
128 end
129 if next != null then
130 next._prev_item = prev
131 else
132 _last_item = prev
133 end
134
135 # Remove the item in the array
136 _the_length -= 1
137 prev = node._prev_in_bucklet
138 next = node._next_in_bucklet
139 if prev != null then
140 prev._next_in_bucklet = next
141 else
142 _array[i] = next
143 end
144 if next != null then
145 next._prev_in_bucklet = prev
146 end
147
148 _last_accessed_key = null
149 end
150
151 # Clear the whole structure
152 fun raz
153 do
154 var i = _capacity - 1
155 while i >= 0 do
156 _array[i] = null
157 i -= 1
158 end
159 _the_length = 0
160 _first_item = null
161 _last_item = null
162 _last_accessed_key = null
163 end
164
165 # Force a capacity
166 fun enlarge(cap: Int)
167 do
168 var old_cap = _capacity
169 # get a new capacity
170 if cap < _the_length + 1 then cap = _the_length + 1
171 if cap <= _capacity then return
172 _capacity = cap
173 _last_accessed_key = null
174
175 # get a new array
176 var new_array = new NativeArray[nullable N](cap)
177 _array = new_array
178
179 # clean the new array
180 var i = cap - 1
181 while i >=0 do
182 new_array[i] = null
183 i -= 1
184 end
185
186 if _the_length == 0 then return
187 if _capacity <= old_cap then return
188
189 # Reput items in the array
190 var node = _first_item
191 while node != null do
192 var index = index_at(node._key)
193 # Then store it in the array
194 var next = new_array[index]
195 new_array[index] = node
196 node._prev_in_bucklet = null
197 node._next_in_bucklet = next
198 if next != null then next._prev_in_bucklet = node
199 node = node._next_item
200 end
201 end
202 end
203
204 private abstract class HashNode[K]
205 var key: K
206 type N: HashNode[K]
207 var next_item: nullable N = null
208 var prev_item: nullable N = null
209 var prev_in_bucklet: nullable N = null
210 var next_in_bucklet: nullable N = null
211 end
212
213 # A `Map` implemented with a hash table.
214 #
215 # ~~~
216 # var map = new HashMap[nullable String, Int]
217 # map[null] = 0
218 # map["one"] = 1
219 # map["two"] = 2
220 #
221 # assert map[null] == 0
222 # assert map["one"] == 1
223 # assert map.keys.has("two")
224 # assert map.values.length == 3
225 # ~~~
226 class HashMap[K, V]
227 super Map[K, V]
228 super HashCollection[K]
229
230 redef type N: HashMapNode[K, V] is fixed
231
232 redef fun [](key)
233 do
234 var c = node_at(key)
235 if c == null then
236 return provide_default_value(key)
237 else
238 return c._value
239 end
240 end
241
242 redef fun get_or_null(key)
243 do
244 var c = node_at(key)
245 if c == null then
246 return null
247 else
248 return c._value
249 end
250 end
251
252 redef fun iterator: HashMapIterator[K, V] do return new HashMapIterator[K,V](self)
253
254 redef fun length do return _the_length
255
256 redef fun is_empty do return _the_length == 0
257
258 redef fun []=(key, v)
259 do
260 if _capacity == 0 then enlarge(17) # 17 because magic in `store`
261 var i = index_at(key)
262 var c = node_at_idx(i, key)
263 if c != null then
264 c._key = key
265 c._value = v
266 else
267 store(i, new HashMapNode[K, V](key, v))
268 end
269 end
270
271 redef fun clear do raz
272
273 init
274 do
275 _capacity = 0
276 _the_length = 0
277 end
278
279 redef var keys: RemovableCollection[K] = new HashMapKeys[K, V](self) is lazy
280 redef var values: RemovableCollection[V] = new HashMapValues[K, V](self) is lazy
281 redef fun has_key(k) do return node_at(k) != null
282 end
283
284 # View of the keys of a HashMap
285 private class HashMapKeys[K, V]
286 super RemovableCollection[K]
287 # The original map
288 var map: HashMap[K, V]
289
290 redef fun count(k) do if self.has(k) then return 1 else return 0
291 redef fun first do return self.map._first_item._key
292 redef fun has(k) do return self.map.node_at(k) != null
293 redef fun has_only(k) do return (self.has(k) and self.length == 1) or self.is_empty
294 redef fun is_empty do return self.map.is_empty
295 redef fun length do return self.map.length
296
297 redef fun iterator do return new MapKeysIterator[K, V](self.map.iterator)
298
299 redef fun clear do self.map.clear
300
301 redef fun remove(key) do self.map.remove_node(key)
302 redef fun remove_all(key) do self.map.remove_node(key)
303 end
304
305 # View of the values of a Map
306 private class HashMapValues[K, V]
307 super RemovableCollection[V]
308 # The original map
309 var map: HashMap[K, V]
310
311 redef fun count(item)
312 do
313 var nb = 0
314 var c = self.map._first_item
315 while c != null do
316 if c._value == item then nb += 1
317 c = c._next_item
318 end
319 return nb
320 end
321 redef fun first do return self.map._first_item._value
322
323 redef fun has(item)
324 do
325 var c = self.map._first_item
326 while c != null do
327 if c._value == item then return true
328 c = c._next_item
329 end
330 return false
331 end
332
333 redef fun has_only(item)
334 do
335 var c = self.map._first_item
336 while c != null do
337 if c._value != item then return false
338 c = c._next_item
339 end
340 return true
341 end
342
343 redef fun is_empty do return self.map.is_empty
344 redef fun length do return self.map.length
345
346 redef fun iterator do return new MapValuesIterator[K, V](self.map.iterator)
347
348 redef fun clear do self.map.clear
349
350 redef fun remove(item)
351 do
352 var map = self.map
353 var c = map._first_item
354 while c != null do
355 if c._value == item then
356 map.remove_node(c._key)
357 return
358 end
359 c = c._next_item
360 end
361 end
362
363 redef fun remove_all(item)
364 do
365 var map = self.map
366 var c = map._first_item
367 while c != null do
368 if c._value == item then
369 map.remove_node(c._key)
370 end
371 c = c._next_item
372 end
373 end
374 end
375
376 private class HashMapNode[K, V]
377 super HashNode[K]
378 redef type N: HashMapNode[K, V]
379 var value: V
380 end
381
382 # A `MapIterator` over a `HashMap`.
383 private class HashMapIterator[K, V]
384 super MapIterator[K, V]
385 redef fun is_ok do return _node != null
386
387 redef fun item
388 do
389 assert is_ok
390 return _node._value
391 end
392
393 #redef fun item=(value)
394 #do
395 # assert is_ok
396 # _node.second = value
397 #end
398
399 redef fun key
400 do
401 assert is_ok
402 return _node._key
403 end
404
405 redef fun next
406 do
407 assert is_ok
408 _node = _node._next_item
409 end
410
411 # The map to iterate on
412 var map: HashMap[K, V]
413
414 # The current node
415 var node: nullable HashMapNode[K, V] = null
416
417 init
418 do
419 _map = map
420 _node = _map._first_item
421 end
422 end
423
424 # A `Set` implemented with a hash table.
425 # Keys of such a map cannot be null and require a working `hash` method
426 class HashSet[E]
427 super Set[E]
428 super HashCollection[E]
429
430 redef type N: HashSetNode[E] is fixed
431
432 redef fun length do return _the_length
433
434 redef fun is_empty do return _the_length == 0
435
436 redef fun first
437 do
438 assert _the_length > 0
439 return _first_item._key
440 end
441
442 redef fun has(item)
443 do
444 return node_at(item) != null
445 end
446
447 redef fun add(item)
448 do
449 if _capacity == 0 then enlarge(17) # 17 because magic in `store`
450 var i = index_at(item)
451 var c = node_at_idx(i, item)
452 if c != null then
453 c._key = item
454 else
455 store(i,new HashSetNode[E](item))
456 end
457 end
458
459 redef fun remove(item) do remove_node(item)
460
461 redef fun clear do raz
462
463 redef fun iterator do return new HashSetIterator[E](self)
464
465 init
466 do
467 _capacity = 0
468 _the_length = 0
469 end
470
471 # Build a list filled with the items of `coll`.
472 init from(coll: Collection[E]) do
473 init
474 add_all(coll)
475 end
476
477 redef fun new_set do return new HashSet[E]
478 end
479
480 private class HashSetNode[E]
481 super HashNode[E]
482 redef type N: HashSetNode[E]
483 end
484
485 private class HashSetIterator[E]
486 super Iterator[E]
487 redef fun is_ok do return _node != null
488
489 redef fun item
490 do
491 assert is_ok
492 return _node._key
493 end
494
495 redef fun next
496 do
497 assert is_ok
498 _node = _node._next_item
499 end
500
501 # The set to iterate on
502 var set: HashSet[E]
503
504 # The position in the internal map storage
505 var node: nullable HashSetNode[E] = null
506
507 init
508 do
509 _node = _set._first_item
510 end
511 end