7f73c3b41f6def7af56ee1074ae4a5a0600578f9
[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, N: HashNode[Object]]
20 super ArrayCapable[nullable N]
21
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_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 = _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 # Clear the whole structure
138 fun raz
139 do
140 var i = _capacity - 1
141 while i >= 0 do
142 _array[i] = null
143 i -= 1
144 end
145 _length = 0
146 _first_item = null
147 _last_item = null
148 _last_accessed_key = null
149 end
150
151 # Force a capacity
152 fun enlarge(cap: Int)
153 do
154 var old_cap = _capacity
155 # get a new capacity
156 if cap < _length + 1 then cap = _length + 1
157 if cap <= _capacity then return
158 _capacity = cap
159 _last_accessed_key = null
160
161 # get a new array
162 var new_array = calloc_array(cap)
163 _array = new_array
164
165 # clean the new array
166 var i = cap - 1
167 while i >=0 do
168 new_array[i] = null
169 i -= 1
170 end
171
172 if _capacity <= old_cap then return
173
174 # Reput items in the array
175 var node = _first_item
176 while node != null do
177 var index = index_at(node._key)
178 # Then store it in the array
179 var next = new_array[index]
180 new_array[index] = node
181 node._next_in_bucklet = next
182 if next != null then next._prev_in_bucklet = node
183 node = node._next_item
184 end
185 end
186 end
187
188 private abstract class HashNode[K: Object]
189 var _key: K
190 type N: HashNode[K]
191 readable writable var _next_item: nullable N = null
192 readable writable var _prev_item: nullable N = null
193 var _prev_in_bucklet: nullable N = null
194 var _next_in_bucklet: nullable N = null
195 init(k: K)
196 do
197 _key = k
198 end
199 end
200
201 # A map implemented with a hash table.
202 # Keys of such a map cannot be null and require a working `hash` method
203 class HashMap[K: Object, V]
204 super Map[K, V]
205 super HashCollection[K, HashMapNode[K, V]]
206
207 redef fun [](key)
208 do
209 var c = node_at(key)
210 if c == null then
211 abort
212 else
213 return c._value
214 end
215 end
216
217 redef fun iterator: HashMapIterator[K, V] do return new HashMapIterator[K,V](self)
218
219 redef fun length do return _length
220
221 redef fun is_empty do return _length == 0
222
223 redef fun []=(key, v)
224 do
225 var i = index_at(key)
226 var c = node_at_idx(i, key)
227 if c != null then
228 c._key = key
229 c._value = v
230 else
231 store(i, new HashMapNode[K, V](key, v))
232 end
233 end
234
235 redef fun clear do raz
236
237 init
238 do
239 _capacity = 0
240 _length = 0
241 enlarge(0)
242 end
243
244 redef var keys: HashMapKeys[K, V] = new HashMapKeys[K, V](self)
245 redef var values: HashMapValues[K, V] = new HashMapValues[K, V](self)
246 end
247
248 # View of the keys of a HashMap
249 class HashMapKeys[K: Object, V]
250 super RemovableCollection[K]
251 # The original map
252 var map: HashMap[K, V]
253
254 redef fun count(k) do if self.has(k) then return 1 else return 0
255 redef fun first do return self.map._first_item._key
256 redef fun has(k) do return self.map.node_at(k) != null
257 redef fun has_only(k) do return (self.has(k) and self.length == 1) or self.is_empty
258 redef fun is_empty do return self.map.is_empty
259 redef fun length do return self.map.length
260
261 redef fun iterator do return new MapKeysIterator[K, V](self.map.iterator)
262
263 redef fun clear do self.map.clear
264
265 redef fun remove(key) do self.map.remove_node(key)
266 redef fun remove_all(key) do self.map.remove_node(key)
267 end
268
269 # View of the values of a Map
270 class HashMapValues[K: Object, V]
271 super RemovableCollection[V]
272 # The original map
273 var map: HashMap[K, V]
274
275 redef fun count(item)
276 do
277 var nb = 0
278 var c = self.map._first_item
279 while c != null do
280 if c._value == item then nb += 1
281 c = c._next_item
282 end
283 return nb
284 end
285 redef fun first do return self.map._first_item._value
286
287 redef fun has(item)
288 do
289 var c = self.map._first_item
290 while c != null do
291 if c._value == item then return true
292 c = c._next_item
293 end
294 return false
295 end
296
297 redef fun has_only(item)
298 do
299 var c = self.map._first_item
300 while c != null do
301 if c._value != item then return false
302 c = c._next_item
303 end
304 return true
305 end
306
307 redef fun is_empty do return self.map.is_empty
308 redef fun length do return self.map.length
309
310 redef fun iterator do return new MapValuesIterator[K, V](self.map.iterator)
311
312 redef fun clear do self.map.clear
313
314 redef fun remove(item)
315 do
316 var map = self.map
317 var c = map._first_item
318 while c != null do
319 if c._value == item then
320 map.remove_node(c._key)
321 return
322 end
323 c = c._next_item
324 end
325 end
326
327 redef fun remove_all(item)
328 do
329 var map = self.map
330 var c = map._first_item
331 while c != null do
332 if c._value == item then
333 map.remove_node(c._key)
334 end
335 c = c._next_item
336 end
337 end
338 end
339
340 private class HashMapNode[K: Object, V]
341 super HashNode[K]
342 redef type N: HashMapNode[K, V]
343 var _value: V
344
345 init(k: K, v: V)
346 do
347 super(k)
348 _value = v
349 end
350 end
351
352 class HashMapIterator[K: Object, V]
353 super MapIterator[K, V]
354 redef fun is_ok do return _node != null
355
356 redef fun item
357 do
358 assert is_ok
359 return _node._value
360 end
361
362 #redef fun item=(value)
363 #do
364 # assert is_ok
365 # _node.second = value
366 #end
367
368 redef fun key
369 do
370 assert is_ok
371 return _node._key
372 end
373
374 redef fun next
375 do
376 assert is_ok
377 _node = _node._next_item
378 end
379
380 # The map to iterate on
381 var _map: HashMap[K, V]
382
383 # The current node
384 var _node: nullable HashMapNode[K, V]
385
386 init(map: HashMap[K, V])
387 do
388 _map = map
389 _node = map.first_item
390 end
391 end
392
393 # A `Set` implemented with a hash table.
394 # Keys of such a map cannot be null and require a working `hash` method
395 class HashSet[E: Object]
396 super Set[E]
397 super HashCollection[E, HashSetNode[E]]
398
399 redef fun length do return _length
400
401 redef fun is_empty do return _length == 0
402
403 redef fun first
404 do
405 assert _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 _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 end
444
445 private class HashSetNode[E: Object]
446 super HashNode[E]
447 redef type N: HashSetNode[E]
448
449 init(e: E)
450 do
451 _key = e
452 end
453 end
454
455 class HashSetIterator[E: Object]
456 super Iterator[E]
457 redef fun is_ok do return _node != null
458
459 redef fun item
460 do
461 assert is_ok
462 return _node._key
463 end
464
465 redef fun next
466 do
467 assert is_ok
468 _node = _node._next_item
469 end
470
471 # The set to iterate on
472 var _set: HashSet[E]
473
474 # The position in the internal map storage
475 var _node: nullable HashSetNode[E]
476
477 init(set: HashSet[E])
478 do
479 _set = set
480 _node = set._first_item
481 end
482 end
483