1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2004-2009 Jean Privat <jean@pryen.org>
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
13 # Introduce `HashMap` and `HashSet`.
14 module hash_collection
19 # Get a `HashMap[K, V]` as default implementation
20 new do return new HashMap[K
, V
]
23 # A HashCollection is an array of HashNode[K] indexed by the K hash value
24 private abstract class HashCollection[K
]
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
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)
34 # The last key accessed (used for cache)
35 var last_accessed_key
: nullable K
= null
37 # The last node accessed (used for cache)
38 var last_accessed_node
: nullable N
= null
40 # Return the index of the key k
41 fun index_at
(k
: K
): Int
43 if k
== null then return 0
45 var i
= k
.hash
% _capacity
50 # Return the node associated with the key
51 fun node_at
(k
: K
): nullable N
53 # cache: `is` is used instead of `==` because it is a faster filter (even if not exact)
54 if k
.is_same_instance
(_last_accessed_key
) then return _last_accessed_node
56 var res
= node_at_idx
(index_at
(k
), k
)
57 _last_accessed_key
= k
58 _last_accessed_node
= res
62 # Return the node associated with the key (but with the index already known)
63 fun node_at_idx
(i
: Int, k
: K
): nullable N
68 if ck
.is_same_instance
(k
) or ck
== k
then # FIXME prefilter because the compiler is not smart enought yet
71 c
= c
._next_in_bucklet
76 # Add a new node at a given index
77 fun store
(index
: Int, node
: N
)
79 # Store the item in the list
80 if _first_item
== null then
83 _last_item
._next_item
= node
85 node
._prev_item
= _last_item
86 node
._next_item
= null
89 # Then store it in the array
90 var next
= _array
[index
]
92 node
._next_in_bucklet
= next
93 if next
!= null then next
._prev_in_bucklet
= node
95 _last_accessed_key
= node
._key
96 _last_accessed_node
= node
102 # Magic values determined empirically
103 # We do not want to enlarge too much
104 # We also want a odd capacity so that the modulo is more distributive
106 if l
>= _capacity
then
107 enlarge
(l
* 3 / 2 + 1)
111 # Remove the node assosiated with the key
112 fun remove_node
(k
: K
)
115 var node
= node_at_idx
(i
, k
)
116 if node
== null then return
118 # Remove the item in the list
119 var prev
= node
._prev_item
120 var next
= node
._next_item
122 prev
._next_item
= next
127 next
._prev_item
= prev
132 # Remove the item in the array
134 prev
= node
._prev_in_bucklet
135 next
= node
._next_in_bucklet
137 prev
._next_in_bucklet
= next
142 next
._prev_in_bucklet
= prev
145 _last_accessed_key
= null
148 # Clear the whole structure
151 var i
= _capacity
- 1
159 _last_accessed_key
= null
163 fun enlarge
(cap
: Int)
165 var old_cap
= _capacity
167 if cap
< _the_length
+ 1 then cap
= _the_length
+ 1
168 if cap
<= _capacity
then return
170 _last_accessed_key
= null
173 var new_array
= new NativeArray[nullable N
](cap
)
176 # clean the new array
183 if _capacity
<= old_cap
then return
185 # Reput items in the array
186 var node
= _first_item
187 while node
!= null do
188 var index
= index_at
(node
._key
)
189 # Then store it in the array
190 var next
= new_array
[index
]
191 new_array
[index
] = node
192 node
._prev_in_bucklet
= null
193 node
._next_in_bucklet
= next
194 if next
!= null then next
._prev_in_bucklet
= node
195 node
= node
._next_item
200 private abstract class HashNode[K
]
203 var next_item
: nullable N
= null
204 var prev_item
: nullable N
= null
205 var prev_in_bucklet
: nullable N
= null
206 var next_in_bucklet
: nullable N
= null
209 # A `Map` implemented with a hash table.
212 # var map = new HashMap[nullable String, Int]
217 # assert map[null] == 0
218 # assert map["one"] == 1
219 # assert map.keys.has("two")
220 # assert map.values.length == 3
224 super HashCollection[K
]
226 redef type N
: HashMapNode[K
, V
] is fixed
232 return provide_default_value
(key
)
238 redef fun get_or_null
(key
)
248 redef fun iterator
: HashMapIterator[K
, V
] do return new HashMapIterator[K
,V
](self)
250 redef fun length
do return _the_length
252 redef fun is_empty
do return _the_length
== 0
254 redef fun []=(key
, v
)
256 var i
= index_at
(key
)
257 var c
= node_at_idx
(i
, key
)
262 store
(i
, new HashMapNode[K
, V
](key
, v
))
266 redef fun clear
do raz
275 redef var keys
: RemovableCollection[K
] = new HashMapKeys[K
, V
](self) is lazy
276 redef var values
: RemovableCollection[V
] = new HashMapValues[K
, V
](self) is lazy
277 redef fun has_key
(k
) do return node_at
(k
) != null
280 # View of the keys of a HashMap
281 private class HashMapKeys[K
, V
]
282 super RemovableCollection[K
]
284 var map
: HashMap[K
, V
]
286 redef fun count
(k
) do if self.has
(k
) then return 1 else return 0
287 redef fun first
do return self.map
._first_item
._key
288 redef fun has
(k
) do return self.map
.node_at
(k
) != null
289 redef fun has_only
(k
) do return (self.has
(k
) and self.length
== 1) or self.is_empty
290 redef fun is_empty
do return self.map
.is_empty
291 redef fun length
do return self.map
.length
293 redef fun iterator
do return new MapKeysIterator[K
, V
](self.map
.iterator
)
295 redef fun clear
do self.map
.clear
297 redef fun remove
(key
) do self.map
.remove_node
(key
)
298 redef fun remove_all
(key
) do self.map
.remove_node
(key
)
301 # View of the values of a Map
302 private class HashMapValues[K
, V
]
303 super RemovableCollection[V
]
305 var map
: HashMap[K
, V
]
307 redef fun count
(item
)
310 var c
= self.map
._first_item
312 if c
._value
== item
then nb
+= 1
317 redef fun first
do return self.map
._first_item
._value
321 var c
= self.map
._first_item
323 if c
._value
== item
then return true
329 redef fun has_only
(item
)
331 var c
= self.map
._first_item
333 if c
._value
!= item
then return false
339 redef fun is_empty
do return self.map
.is_empty
340 redef fun length
do return self.map
.length
342 redef fun iterator
do return new MapValuesIterator[K
, V
](self.map
.iterator
)
344 redef fun clear
do self.map
.clear
346 redef fun remove
(item
)
349 var c
= map
._first_item
351 if c
._value
== item
then
352 map
.remove_node
(c
._key
)
359 redef fun remove_all
(item
)
362 var c
= map
._first_item
364 if c
._value
== item
then
365 map
.remove_node
(c
._key
)
372 private class HashMapNode[K
, V
]
374 redef type N
: HashMapNode[K
, V
]
378 # A `MapIterator` over a `HashMap`.
379 class HashMapIterator[K
, V
]
380 super MapIterator[K
, V
]
381 redef fun is_ok
do return _node
!= null
389 #redef fun item=(value)
392 # _node.second = value
404 _node
= _node
._next_item
407 # The map to iterate on
408 private var map
: HashMap[K
, V
]
411 private var node
: nullable HashMapNode[K
, V
] = null
416 _node
= _map
._first_item
420 # A `Set` implemented with a hash table.
421 # Keys of such a map cannot be null and require a working `hash` method
424 super HashCollection[E
]
426 redef type N
: HashSetNode[E
] is fixed
428 redef fun length
do return _the_length
430 redef fun is_empty
do return _the_length
== 0
434 assert _the_length
> 0
435 return _first_item
._key
440 return node_at
(item
) != null
445 var i
= index_at
(item
)
446 var c
= node_at_idx
(i
, item
)
450 store
(i
,new HashSetNode[E
](item
))
454 redef fun remove
(item
) do remove_node
(item
)
456 redef fun clear
do raz
458 redef fun iterator
do return new HashSetIterator[E
](self)
467 # Build a list filled with the items of `coll`.
468 init from
(coll
: Collection[E
]) do
473 redef fun new_set
do return new HashSet[E
]
476 private class HashSetNode[E
]
478 redef type N
: HashSetNode[E
]
481 private class HashSetIterator[E
]
483 redef fun is_ok
do return _node
!= null
494 _node
= _node
._next_item
497 # The set to iterate on
500 # The position in the internal map storage
501 var node
: nullable HashSetNode[E
] = null
505 _node
= _set
._first_item