d389c5a10f9a4ce5aa9b3f57586c23fe5d1af3e0
[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 module hash_collection
15
16 import array
17
18 # A HashCollection is an array of HashNode[K] indexed by the K hash value
19 private abstract class HashCollection[K: Object]
20 type N: HashNode[K]
21
22 var array: nullable NativeArray[nullable N] = null # Used to store items
23 var capacity: Int = 0 # Size of _array
24 var the_length: Int = 0 # Number of items in the map
25
26 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_same_instance(_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_same_instance(k) or ck == k then # FIXME prefilter 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 = _the_length
93 _the_length = l + 1
94
95 # Magic values determined empirically
96 # We do not want to enlarge too much
97 # We also want a odd capacity so that the modulo is more distributive
98 l = (l + 5) * 2 + 1
99 if l >= _capacity then
100 enlarge(l * 3 / 2 + 1)
101 end
102 end
103
104 # Remove the node assosiated with the key
105 fun remove_node(k: K)
106 do
107 var i = index_at(k)
108 var node = node_at_idx(i, k)
109 if node == null then return
110
111 # Remove the item in the list
112 var prev = node._prev_item
113 var next = node._next_item
114 if prev != null then
115 prev._next_item = next
116 else
117 _first_item = next
118 end
119 if next != null then
120 next._prev_item = prev
121 else
122 _last_item = prev
123 end
124
125 # Remove the item in the array
126 _the_length -= 1
127 prev = node._prev_in_bucklet
128 next = node._next_in_bucklet
129 if prev != null then
130 prev._next_in_bucklet = next
131 else
132 _array[i] = next
133 end
134 if next != null then
135 next._prev_in_bucklet = prev
136 end
137
138 _last_accessed_key = null
139 end
140
141 # Clear the whole structure
142 fun raz
143 do
144 var i = _capacity - 1
145 while i >= 0 do
146 _array[i] = null
147 i -= 1
148 end
149 _the_length = 0
150 _first_item = null
151 _last_item = null
152 _last_accessed_key = null
153 end
154
155 # Force a capacity
156 fun enlarge(cap: Int)
157 do
158 var old_cap = _capacity
159 # get a new capacity
160 if cap < _the_length + 1 then cap = _the_length + 1
161 if cap <= _capacity then return
162 _capacity = cap
163 _last_accessed_key = null
164
165 # get a new array
166 var new_array = new NativeArray[nullable N](cap)
167 _array = new_array
168
169 # clean the new array
170 var i = cap - 1
171 while i >=0 do
172 new_array[i] = null
173 i -= 1
174 end
175
176 if _capacity <= old_cap then return
177
178 # Reput items in the array
179 var node = _first_item
180 while node != null do
181 var index = index_at(node._key)
182 # Then store it in the array
183 var next = new_array[index]
184 new_array[index] = node
185 node._prev_in_bucklet = null
186 node._next_in_bucklet = next
187 if next != null then next._prev_in_bucklet = node
188 node = node._next_item
189 end
190 end
191 end
192
193 private abstract class HashNode[K: Object]
194 var key: K
195 type N: HashNode[K]
196 var next_item: nullable N = null
197 var prev_item: nullable N = null
198 var prev_in_bucklet: nullable N = null
199 var next_in_bucklet: nullable N = null
200 end
201
202 # A map implemented with a hash table.
203 # Keys of such a map cannot be null and require a working `hash` method
204 class HashMap[K: Object, V]
205 super Map[K, V]
206 super HashCollection[K]
207
208 redef type N: HashMapNode[K, V] is fixed
209
210 redef fun [](key)
211 do
212 var c = node_at(key)
213 if c == null then
214 return provide_default_value(key)
215 else
216 return c._value
217 end
218 end
219
220 redef fun iterator: HashMapIterator[K, V] do return new HashMapIterator[K,V](self)
221
222 redef fun length do return _the_length
223
224 redef fun is_empty do return _the_length == 0
225
226 redef fun []=(key, v)
227 do
228 var i = index_at(key)
229 var c = node_at_idx(i, key)
230 if c != null then
231 c._key = key
232 c._value = v
233 else
234 store(i, new HashMapNode[K, V](key, v))
235 end
236 end
237
238 redef fun clear do raz
239
240 init
241 do
242 _capacity = 0
243 _the_length = 0
244 enlarge(0)
245 end
246
247 redef var keys: RemovableCollection[K] = new HashMapKeys[K, V](self)
248 redef var values: RemovableCollection[V] = new HashMapValues[K, V](self)
249 end
250
251 # View of the keys of a HashMap
252 private class HashMapKeys[K: Object, V]
253 super RemovableCollection[K]
254 # The original map
255 var map: HashMap[K, V]
256
257 redef fun count(k) do if self.has(k) then return 1 else return 0
258 redef fun first do return self.map._first_item._key
259 redef fun has(k) do return self.map.node_at(k) != null
260 redef fun has_only(k) do return (self.has(k) and self.length == 1) or self.is_empty
261 redef fun is_empty do return self.map.is_empty
262 redef fun length do return self.map.length
263
264 redef fun iterator do return new MapKeysIterator[K, V](self.map.iterator)
265
266 redef fun clear do self.map.clear
267
268 redef fun remove(key) do self.map.remove_node(key)
269 redef fun remove_all(key) do self.map.remove_node(key)
270 end
271
272 # View of the values of a Map
273 private class HashMapValues[K: Object, V]
274 super RemovableCollection[V]
275 # The original map
276 var map: HashMap[K, V]
277
278 redef fun count(item)
279 do
280 var nb = 0
281 var c = self.map._first_item
282 while c != null do
283 if c._value == item then nb += 1
284 c = c._next_item
285 end
286 return nb
287 end
288 redef fun first do return self.map._first_item._value
289
290 redef fun has(item)
291 do
292 var c = self.map._first_item
293 while c != null do
294 if c._value == item then return true
295 c = c._next_item
296 end
297 return false
298 end
299
300 redef fun has_only(item)
301 do
302 var c = self.map._first_item
303 while c != null do
304 if c._value != item then return false
305 c = c._next_item
306 end
307 return true
308 end
309
310 redef fun is_empty do return self.map.is_empty
311 redef fun length do return self.map.length
312
313 redef fun iterator do return new MapValuesIterator[K, V](self.map.iterator)
314
315 redef fun clear do self.map.clear
316
317 redef fun remove(item)
318 do
319 var map = self.map
320 var c = map._first_item
321 while c != null do
322 if c._value == item then
323 map.remove_node(c._key)
324 return
325 end
326 c = c._next_item
327 end
328 end
329
330 redef fun remove_all(item)
331 do
332 var map = self.map
333 var c = map._first_item
334 while c != null do
335 if c._value == item then
336 map.remove_node(c._key)
337 end
338 c = c._next_item
339 end
340 end
341 end
342
343 private class HashMapNode[K: Object, V]
344 super HashNode[K]
345 redef type N: HashMapNode[K, V]
346 var value: V
347 end
348
349 # A `MapIterator` over a `HashMap`.
350 class HashMapIterator[K: Object, V]
351 super MapIterator[K, V]
352 redef fun is_ok do return _node != null
353
354 redef fun item
355 do
356 assert is_ok
357 return _node._value
358 end
359
360 #redef fun item=(value)
361 #do
362 # assert is_ok
363 # _node.second = value
364 #end
365
366 redef fun key
367 do
368 assert is_ok
369 return _node._key
370 end
371
372 redef fun next
373 do
374 assert is_ok
375 _node = _node._next_item
376 end
377
378 # The map to iterate on
379 private var map: HashMap[K, V]
380
381 # The current node
382 private var node: nullable HashMapNode[K, V] = null
383
384 init
385 do
386 _map = map
387 _node = _map._first_item
388 end
389 end
390
391 # A `Set` implemented with a hash table.
392 # Keys of such a map cannot be null and require a working `hash` method
393 class HashSet[E: Object]
394 super Set[E]
395 super HashCollection[E]
396
397 redef type N: HashSetNode[E] is fixed
398
399 redef fun length do return _the_length
400
401 redef fun is_empty do return _the_length == 0
402
403 redef fun first
404 do
405 assert _the_length > 0
406 return _first_item._key
407 end
408
409 redef fun has(item)
410 do
411 return node_at(item) != null
412 end
413
414 redef fun add(item)
415 do
416 var i = index_at(item)
417 var c = node_at_idx(i, item)
418 if c != null then
419 c._key = item
420 else
421 store(i,new HashSetNode[E](item))
422 end
423 end
424
425 redef fun remove(item) do remove_node(item)
426
427 redef fun clear do raz
428
429 redef fun iterator do return new HashSetIterator[E](self)
430
431 init
432 do
433 _capacity = 0
434 _the_length = 0
435 enlarge(0)
436 end
437
438 # Build a list filled with the items of `coll`.
439 init from(coll: Collection[E]) do
440 init
441 add_all(coll)
442 end
443
444 redef fun new_set do return new HashSet[E]
445 end
446
447 private class HashSetNode[E: Object]
448 super HashNode[E]
449 redef type N: HashSetNode[E]
450 end
451
452 private class HashSetIterator[E: Object]
453 super Iterator[E]
454 redef fun is_ok do return _node != null
455
456 redef fun item
457 do
458 assert is_ok
459 return _node._key
460 end
461
462 redef fun next
463 do
464 assert is_ok
465 _node = _node._next_item
466 end
467
468 # The set to iterate on
469 var set: HashSet[E]
470
471 # The position in the internal map storage
472 var node: nullable HashSetNode[E] = null
473
474 init
475 do
476 _node = _set._first_item
477 end
478 end
479