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
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
]
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
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)
29 # The last key accessed (used for cache)
30 var _last_accessed_key
: nullable K
= null
32 # The last node accessed (used for cache)
33 var _last_accessed_node
: nullable N
= null
35 # Return the index of the key k
36 fun index_at
(k
: K
): Int
38 var i
= k
.hash
% _capacity
43 # Return the node assosiated with the key
44 fun node_at
(k
: K
): nullable N
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
49 var res
= node_at_idx
(index_at
(k
), k
)
50 _last_accessed_key
= k
51 _last_accessed_node
= res
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
61 if ck
.is_same_instance
(k
) or ck
== k
then # FIXME prefilter because the compiler is not smart enought yet
64 c
= c
._next_in_bucklet
69 # Add a new node at a given index
70 fun store
(index
: Int, node
: N
)
72 # Store the item in the list
73 if _first_item
== null then
76 _last_item
._next_item
= node
78 node
._prev_item
= _last_item
79 node
._next_item
= null
82 # Then store it in the array
83 var next
= _array
[index
]
85 node
._next_in_bucklet
= next
86 if next
!= null then next
._prev_in_bucklet
= node
88 _last_accessed_key
= node
._key
89 _last_accessed_node
= node
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
99 if l
>= _capacity
then
100 enlarge
(l
* 3 / 2 + 1)
104 # Remove the node assosiated with the key
105 fun remove_node
(k
: K
)
108 var node
= node_at_idx
(i
, k
)
109 if node
== null then return
111 # Remove the item in the list
112 var prev
= node
._prev_item
113 var next
= node
._next_item
115 prev
._next_item
= next
120 next
._prev_item
= prev
125 # Remove the item in the array
127 prev
= node
._prev_in_bucklet
128 next
= node
._next_in_bucklet
130 prev
._next_in_bucklet
= next
135 next
._prev_in_bucklet
= prev
138 _last_accessed_key
= null
141 # Clear the whole structure
144 var i
= _capacity
- 1
152 _last_accessed_key
= null
156 fun enlarge
(cap
: Int)
158 var old_cap
= _capacity
160 if cap
< _length
+ 1 then cap
= _length
+ 1
161 if cap
<= _capacity
then return
163 _last_accessed_key
= null
166 var new_array
= calloc_array
(cap
)
169 # clean the new array
176 if _capacity
<= old_cap
then return
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
193 private abstract class HashNode[K
: Object]
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
206 # A map implemented with a hash table.
207 # Keys of such a map cannot be null and require a working `hash` method
208 class HashMap[K
: Object, V
]
210 super HashCollection[K
, HashMapNode[K
, V
]]
216 return provide_default_value
(key
)
222 redef fun iterator
: HashMapIterator[K
, V
] do return new HashMapIterator[K
,V
](self)
224 redef fun length
do return _length
226 redef fun is_empty
do return _length
== 0
228 redef fun []=(key
, v
)
230 var i
= index_at
(key
)
231 var c
= node_at_idx
(i
, key
)
236 store
(i
, new HashMapNode[K
, V
](key
, v
))
240 redef fun clear
do raz
249 redef var keys
: RemovableCollection[K
] = new HashMapKeys[K
, V
](self)
250 redef var values
: RemovableCollection[V
] = new HashMapValues[K
, V
](self)
253 # View of the keys of a HashMap
254 private class HashMapKeys[K
: Object, V
]
255 super RemovableCollection[K
]
257 var map
: HashMap[K
, V
]
259 redef fun count
(k
) do if self.has
(k
) then return 1 else return 0
260 redef fun first
do return self.map
._first_item
._key
261 redef fun has
(k
) do return self.map
.node_at
(k
) != null
262 redef fun has_only
(k
) do return (self.has
(k
) and self.length
== 1) or self.is_empty
263 redef fun is_empty
do return self.map
.is_empty
264 redef fun length
do return self.map
.length
266 redef fun iterator
do return new MapKeysIterator[K
, V
](self.map
.iterator
)
268 redef fun clear
do self.map
.clear
270 redef fun remove
(key
) do self.map
.remove_node
(key
)
271 redef fun remove_all
(key
) do self.map
.remove_node
(key
)
274 # View of the values of a Map
275 private class HashMapValues[K
: Object, V
]
276 super RemovableCollection[V
]
278 var map
: HashMap[K
, V
]
280 redef fun count
(item
)
283 var c
= self.map
._first_item
285 if c
._value
== item
then nb
+= 1
290 redef fun first
do return self.map
._first_item
._value
294 var c
= self.map
._first_item
296 if c
._value
== item
then return true
302 redef fun has_only
(item
)
304 var c
= self.map
._first_item
306 if c
._value
!= item
then return false
312 redef fun is_empty
do return self.map
.is_empty
313 redef fun length
do return self.map
.length
315 redef fun iterator
do return new MapValuesIterator[K
, V
](self.map
.iterator
)
317 redef fun clear
do self.map
.clear
319 redef fun remove
(item
)
322 var c
= map
._first_item
324 if c
._value
== item
then
325 map
.remove_node
(c
._key
)
332 redef fun remove_all
(item
)
335 var c
= map
._first_item
337 if c
._value
== item
then
338 map
.remove_node
(c
._key
)
345 private class HashMapNode[K
: Object, V
]
347 redef type N
: HashMapNode[K
, V
]
357 class HashMapIterator[K
: Object, V
]
358 super MapIterator[K
, V
]
359 redef fun is_ok
do return _node
!= null
367 #redef fun item=(value)
370 # _node.second = value
382 _node
= _node
._next_item
385 # The map to iterate on
386 var _map
: HashMap[K
, V
]
389 var _node
: nullable HashMapNode[K
, V
]
391 init(map
: HashMap[K
, V
])
394 _node
= map
._first_item
398 # A `Set` implemented with a hash table.
399 # Keys of such a map cannot be null and require a working `hash` method
400 class HashSet[E
: Object]
402 super HashCollection[E
, HashSetNode[E
]]
404 redef fun length
do return _length
406 redef fun is_empty
do return _length
== 0
411 return _first_item
._key
416 return node_at
(item
) != null
421 var i
= index_at
(item
)
422 var c
= node_at_idx
(i
, item
)
426 store
(i
,new HashSetNode[E
](item
))
430 redef fun remove
(item
) do remove_node
(item
)
432 redef fun clear
do raz
434 redef fun iterator
do return new HashSetIterator[E
](self)
443 # Build a list filled with the items of `coll`.
444 init from
(coll
: Collection[E
]) do
449 redef fun new_set
do return new HashSet[E
]
452 private class HashSetNode[E
: Object]
454 redef type N
: HashSetNode[E
]
462 private class HashSetIterator[E
: Object]
464 redef fun is_ok
do return _node
!= null
475 _node
= _node
._next_item
478 # The set to iterate on
481 # The position in the internal map storage
482 var _node
: nullable HashSetNode[E
]
484 init(set
: HashSet[E
])
487 _node
= set
._first_item