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
: NativeArray[nullable N
] is noautoinit
# 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 Object = 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
: nullable Object): 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
: nullable Object): nullable N
53 if _the_length
== 0 then return null
54 # cache: `is` is used instead of `==` because it is a faster filter (even if not exact)
55 if k
.is_same_instance
(_last_accessed_key
) then return _last_accessed_node
57 var res
= node_at_idx
(index_at
(k
), k
)
58 _last_accessed_key
= k
59 _last_accessed_node
= res
63 # Return the node associated with the key (but with the index already known)
64 fun node_at_idx
(i
: Int, k
: nullable Object): nullable N
66 if _the_length
== 0 then return null
70 if ck
.is_same_instance
(k
) or ck
== k
then # FIXME prefilter because the compiler is not smart enought yet
73 c
= c
._next_in_bucklet
78 # Add a new node at a given index
79 fun store
(index
: Int, node
: N
)
81 # Store the item in the list
82 if _first_item
== null then
85 _last_item
._next_item
= node
87 node
._prev_item
= _last_item
88 node
._next_item
= null
91 # Then store it in the array
92 var next
= _array
[index
]
94 node
._next_in_bucklet
= next
95 if next
!= null then next
._prev_in_bucklet
= node
97 _last_accessed_key
= node
._key
98 _last_accessed_node
= node
104 # Magic values determined empirically
105 # We do not want to enlarge too much
106 # We also want a odd capacity so that the modulo is more distributive
108 if l
>= _capacity
then
109 enlarge
(l
* 3 / 2 + 1)
113 # Remove the node assosiated with the key
114 fun remove_node
(k
: nullable Object)
116 if _the_length
== 0 then return
118 var node
= node_at_idx
(i
, k
)
119 if node
== null then return
121 # Remove the item in the list
122 var prev
= node
._prev_item
123 var next
= node
._next_item
125 prev
._next_item
= next
130 next
._prev_item
= prev
135 # Remove the item in the array
137 prev
= node
._prev_in_bucklet
138 next
= node
._next_in_bucklet
140 prev
._next_in_bucklet
= next
145 next
._prev_in_bucklet
= prev
148 _last_accessed_key
= null
151 # Clear the whole structure
154 var i
= _capacity
- 1
162 _last_accessed_key
= null
166 fun enlarge
(cap
: Int)
169 if cap
< _the_length
+ 1 then cap
= _the_length
+ 1
170 if cap
<= _capacity
then return
172 _last_accessed_key
= null
175 var new_array
= new NativeArray[nullable N
](cap
)
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
]
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
202 # A `Map` implemented with a hash table.
205 # var map = new HashMap[nullable String, Int]
210 # assert map[null] == 0
211 # assert map["one"] == 1
212 # assert map.keys.has("two")
213 # assert map.values.length == 3
217 super HashCollection[K
]
219 redef type N
: HashMapNode[K
, V
] is fixed
225 return provide_default_value
(key
)
231 redef fun get_or_null
(key
)
241 redef fun iterator
do return new HashMapIterator[K
,V
](self)
243 redef fun length
do return _the_length
245 redef fun is_empty
do return _the_length
== 0
247 redef fun []=(key
, v
)
249 if _capacity
== 0 then enlarge
(17) # 17 because magic in `store`
250 var i
= index_at
(key
)
251 var c
= node_at_idx
(i
, key
)
256 store
(i
, new HashMapNode[K
, V
](key
, v
))
260 redef fun clear
do raz
268 redef var keys
: RemovableCollection[K
] = new HashMapKeys[K
, V
](self) is lazy
269 redef var values
: RemovableCollection[V
] = new HashMapValues[K
, V
](self) is lazy
270 redef fun has_key
(k
) do return node_at
(k
) != null
273 # View of the keys of a HashMap
274 private class HashMapKeys[K
, V
]
275 super RemovableCollection[K
]
277 var map
: HashMap[K
, V
]
279 redef fun count
(k
) do if self.has
(k
) then return 1 else return 0
280 redef fun first
do return self.map
._first_item
._key
281 redef fun has
(k
) do return self.map
.node_at
(k
) != null
282 redef fun has_only
(k
) do return (self.has
(k
) and self.length
== 1) or self.is_empty
283 redef fun is_empty
do return self.map
.is_empty
284 redef fun length
do return self.map
.length
286 redef fun iterator
do return new MapKeysIterator[K
, V
](self.map
.iterator
)
288 redef fun clear
do self.map
.clear
290 redef fun remove
(key
) do self.map
.remove_node
(key
)
291 redef fun remove_all
(key
) do self.map
.remove_node
(key
)
294 # View of the values of a Map
295 private class HashMapValues[K
, V
]
296 super RemovableCollection[V
]
298 var map
: HashMap[K
, V
]
300 redef fun count
(item
)
303 var c
= self.map
._first_item
305 if c
._value
== item
then nb
+= 1
310 redef fun first
do return self.map
._first_item
._value
314 var c
= self.map
._first_item
316 if c
._value
== item
then return true
322 redef fun has_only
(item
)
324 var c
= self.map
._first_item
326 if c
._value
!= item
then return false
332 redef fun is_empty
do return self.map
.is_empty
333 redef fun length
do return self.map
.length
335 redef fun iterator
do return new MapValuesIterator[K
, V
](self.map
.iterator
)
337 redef fun clear
do self.map
.clear
339 redef fun remove
(item
)
342 var c
= map
._first_item
344 if c
._value
== item
then
345 map
.remove_node
(c
._key
)
352 redef fun remove_all
(item
)
355 var c
= map
._first_item
357 if c
._value
== item
then
358 map
.remove_node
(c
._key
)
365 private class HashMapNode[K
, V
]
367 redef type N
: HashMapNode[K
, V
]
371 # A `MapIterator` over a `HashMap`.
372 private class HashMapIterator[K
, V
]
373 super MapIterator[K
, V
]
374 redef fun is_ok
do return _node
!= null
382 #redef fun item=(value)
385 # _node.second = value
397 _node
= _node
._next_item
400 # The map to iterate on
401 var map
: HashMap[K
, V
]
404 var node
: nullable HashMapNode[K
, V
] = null
409 _node
= _map
._first_item
413 # A `Set` implemented with a hash table.
414 # Keys of such a map cannot be null and require a working `hash` method
417 super HashCollection[E
]
419 redef type N
: HashSetNode[E
] is fixed
421 redef fun length
do return _the_length
423 redef fun is_empty
do return _the_length
== 0
427 assert _the_length
> 0
428 return _first_item
._key
433 return node_at
(item
) != null
438 if _capacity
== 0 then enlarge
(17) # 17 because magic in `store`
439 var i
= index_at
(item
)
440 var c
= node_at_idx
(i
, item
)
444 store
(i
,new HashSetNode[E
](item
))
448 redef fun remove
(item
) do remove_node
(item
)
450 redef fun clear
do raz
452 redef fun iterator
do return new HashSetIterator[E
](self)
460 # Build a list filled with the items of `coll`.
461 init from
(coll
: Collection[E
]) do
466 redef fun new_set
do return new HashSet[E
]
469 private class HashSetNode[E
]
471 redef type N
: HashSetNode[E
]
474 private class HashSetIterator[E
]
476 redef fun is_ok
do return _node
!= null
487 _node
= _node
._next_item
490 # The set to iterate on
493 # The position in the internal map storage
494 var node
: nullable HashSetNode[E
] = null
498 _node
= _set
._first_item