5bb6ff743d44df652539a4347e4b47c1c78f0973
[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], E]
21 special Collection[E]
22 special ArrayCapable[nullable N]
23 var _array: nullable NativeArray[nullable N] = null # Used to store items
24 var _capacity: Int = 0 # Size of _array
25 redef readable 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 index accessed
31 var _last_accessed_index: Int = -1
32
33 # The last key accessed
34 var _last_accessed_key: nullable K = null
35
36 # Return the index of the k element
37 fun index_at(k: K): Int
38 do
39 var arr = _array
40
41 # Fisrt step: look in the last indexed elt
42 if k == _last_accessed_key then return _last_accessed_index
43
44 # Compute the base position of the key
45 var base = k.hash % _capacity
46 if base < 0 then base = - base
47
48 # Look for the key in the array
49 var cur = base
50 while true do
51 var c = arr[cur]
52 if c == null or c.key == k then # REAL equals
53 _last_accessed_index = cur
54 _last_accessed_key = k
55 return cur
56 end
57 cur -= 1
58 if cur < 0 then cur = _capacity - 1
59 assert no_loop: cur != base
60 end
61 abort
62 end
63
64 # Add a new node (should be free)
65 fun store(index: Int, node: N)
66 do
67 # Store the item in the list
68 if _first_item == null then
69 _first_item = node
70 else
71 _last_item.next_item = node
72 end
73 node.prev_item = _last_item
74 node.next_item = null
75 _last_item = node
76 # Then store it in the array
77 assert _array[index] == null
78 _array[index] = node
79 var l = _length
80 _length = l + 1
81 l = (l + 5) * 150 / 100
82 if l >= _capacity then
83 enlarge(l * 2)
84 end
85 end
86
87 fun remove_index(i: Int)
88 do
89 assert correct_index: i >= 0 and i < _capacity
90 var node = _array[i]
91 assert has_couple: node != null
92 # Remove the item in the list
93 var prev = node.prev_item
94 var next = node.next_item
95 if prev != null then
96 prev.next_item = next
97 else
98 _first_item = next
99 end
100 if next != null then
101 next.prev_item = prev
102 else
103 _last_item = prev
104 end
105 # Remove the item in the array
106 _array[i] = null
107 _length -= 1
108 # Now replaces things before if needed
109 while true do
110 i -= 1
111 if i < 0 then
112 i = _capacity - 1
113 end
114 var n = _array[i]
115 if n == null then
116 return
117 end
118 var i2 = index_at(n.key)
119 if i != i2 then
120 _array[i] = null
121 assert _array[i2] == null
122 _array[i2] = n
123 end
124 end
125 end
126
127 fun raz
128 do
129 var i = _capacity - 1
130 while i >= 0 do
131 _array[i] = null
132 i -= 1
133 end
134 _length = 0
135 _first_item = null
136 _last_item = null
137 _last_accessed_key = null
138 end
139
140 fun enlarge(cap: Int)
141 do
142 var old_cap = _capacity
143 # get a new capacity
144 # cap = cap * 130 / 100 + 5 + 1000 # /
145 if cap < _length + 1 then cap = _length + 1
146 if cap <= _capacity then return
147 _capacity = cap
148 _last_accessed_key = null
149
150 # get a new array
151 var new_array = calloc_array(cap)
152 _array = new_array
153
154 # clean the new array
155 var i = cap - 1
156 while i >=0 do
157 new_array[i] = null
158 i -= 1
159 end
160
161 if _capacity <= old_cap then return
162
163 var new_array = _array
164 # Reput items in the array
165 var node = _first_item
166 while node != null do
167 var ind = index_at(node.key)
168 assert new_array[ind] == null
169 new_array[ind] = node
170 node = node.next_item
171 end
172 _last_accessed_key = null
173 end
174 end
175
176 private class HashNode[K]
177 fun key: K is abstract
178 type N: HashNode[K]
179 readable writable var _next_item: nullable N = null
180 readable writable var _prev_item: nullable N = null
181 end
182
183 class HashMap[K, V]
184 special CoupleMap[K, V]
185 special HashCollection[K, HashMapNode[K, V], V]
186
187 redef fun iterator: HashMapIterator[K, V] do return new HashMapIterator[K,V](self)
188
189 redef fun first
190 do
191 assert _length > 0
192 return _first_item.second
193 end
194
195 redef fun is_empty do return _length == 0
196
197 redef fun count(item)
198 do
199 var nb = 0
200 var i = 0
201 while i < _capacity do
202 var c = _array[i]
203 if c != null and c.second == item then nb += 1
204 i += 1
205 end
206 return nb
207 end
208
209 redef fun has(item)
210 do
211 var i = 0
212 while i < _capacity do
213 var c = _array[i]
214 if c != null and c.second == item then return true
215 i += 1
216 end
217 return false
218 end
219
220 redef fun has_only(item)
221 do
222 var i = 0
223 while i < _capacity do
224 var c = _array[i]
225 if c != null and c.second != item then return false
226 i += 1
227 end
228 return true
229 end
230
231 redef fun []=(key, v)
232 do
233 assert key != null
234 var i = index_at(key)
235 var c = _array[i]
236 if c != null then
237 c.first = key
238 c.second = v
239 else
240 store(i, new HashMapNode[K, V](key, v))
241 end
242 end
243
244 redef fun remove(item)
245 do
246 var i = 0
247 while i < _capacity do
248 var c = _array[i]
249 if c != null and c.second == item then
250 remove_index(i)
251 return
252 end
253 i += 1
254 end
255 end
256
257 redef fun remove_at(key) do remove_index(index_at(key))
258
259 redef fun clear do raz
260
261 redef fun couple_at(key) do return _array[index_at(key)]
262
263 init
264 do
265 _capacity = 0
266 _length = 0
267 enlarge(0)
268 end
269 end
270
271 class HashMapNode[K, V]
272 special Couple[K, V]
273 special HashNode[K]
274 redef fun key do return first
275 redef type N: HashMapNode[K, V]
276
277 init(k: K, v: V)
278 do
279 first = k
280 second = v
281 end
282 end
283
284 class HashMapIterator[K, V]
285 special MapIterator[K, V]
286 redef fun is_ok do return _node != null
287
288 redef fun item
289 do
290 assert is_ok
291 return _node.second
292 end
293
294 #redef fun item=(value)
295 #do
296 # assert is_ok
297 # _node.second = value
298 #end
299
300 redef fun key
301 do
302 assert is_ok
303 return _node.first
304 end
305
306 redef fun next
307 do
308 assert is_ok
309 _node = _node.next_item
310 end
311
312 # The map to iterate on
313 var _map: HashMap[K, V]
314
315 # The current node
316 var _node: nullable HashMapNode[K, V]
317
318 init(map: HashMap[K, V])
319 do
320 _map = map
321 _node = map.first_item
322 end
323 end
324
325 class HashSet[E]
326 special Set[E]
327 special HashCollection[E, HashSetNode[E], E]
328
329 redef fun is_empty do return _length == 0
330
331 redef fun first
332 do
333 assert _length > 0
334 return _first_item.key
335 end
336
337 redef fun has(item)
338 do
339 return _array[index_at(item)] != null
340 end
341
342 redef fun add(item)
343 do
344 var i = index_at(item)
345 var c = _array[i]
346 if c != null then
347 c.key = item
348 else
349 store(i,new HashSetNode[E](item))
350 end
351 end
352
353 redef fun remove(item) do remove_index(index_at(item))
354
355 redef fun clear do raz
356
357 redef fun iterator do return new HashSetIterator[E](self)
358
359 init
360 do
361 _capacity = 0
362 _length = 0
363 enlarge(0)
364 end
365 end
366
367 class HashSetNode[E]
368 special HashNode[E]
369 redef type N: HashSetNode[E]
370
371 redef readable writable var _key: E
372
373 init(e: E)
374 do
375 _key = e
376 end
377 end
378
379 class HashSetIterator[E]
380 special Iterator[E]
381 redef fun is_ok do return _node != null
382
383 redef fun item
384 do
385 assert is_ok
386 return _node.key
387 end
388
389 redef fun next
390 do
391 assert is_ok
392 _node = _node.next_item
393 end
394
395 # The set to iterate on
396 var _set: HashSet[E]
397
398 # The position in the internal map storage
399 var _node: nullable HashSetNode[E]
400
401 init(set: HashSet[E])
402 do
403 _set = set
404 _node = set.first_item
405 end
406 end
407