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