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
]
24 # Get an instance of `HashSet[E]`, the default implementation
25 new do return new HashSet[E
]
28 # A HashCollection is an array of HashNode[K] indexed by the K hash value
29 private abstract class HashCollection[K
]
32 var array
: NativeArray[nullable N
] is noautoinit
# Used to store items
33 var capacity
: Int = 0 # Size of _array
34 var the_length
: Int = 0 # Number of items in the map
36 var first_item
: nullable N
= null # First added item (used to visit items in nice order)
37 var last_item
: nullable N
= null # Last added item (same)
39 # The last key accessed (used for cache)
40 var last_accessed_key
: nullable Object = null
42 # The last node accessed (used for cache)
43 var last_accessed_node
: nullable N
= null
45 # Return the index of the key k
46 fun index_at
(k
: nullable Object): Int
48 if k
== null then return 0
50 var i
= k
.hash
% _capacity
55 # Return the node associated with the key
56 fun node_at
(k
: nullable Object): nullable N
58 if _the_length
== 0 then return null
59 # cache: `is` is used instead of `==` because it is a faster filter (even if not exact)
60 if k
.is_same_instance
(_last_accessed_key
) then return _last_accessed_node
62 var res
= node_at_idx
(index_at
(k
), k
)
63 _last_accessed_key
= k
64 _last_accessed_node
= res
68 # Return the node associated with the key (but with the index already known)
69 fun node_at_idx
(i
: Int, k
: nullable Object): nullable N
71 if _the_length
== 0 then return null
75 if ck
.is_same_instance
(k
) or ck
== k
then # FIXME prefilter because the compiler is not smart enought yet
78 c
= c
._next_in_bucklet
83 # Add a new node at a given index
84 fun store
(index
: Int, node
: N
)
86 # Store the item in the list
87 if _first_item
== null then
90 _last_item
._next_item
= node
92 node
._prev_item
= _last_item
93 node
._next_item
= null
96 # Then store it in the array
97 var next
= _array
[index
]
99 node
._next_in_bucklet
= next
100 if next
!= null then next
._prev_in_bucklet
= node
102 _last_accessed_key
= node
._key
103 _last_accessed_node
= node
109 # Magic values determined empirically
110 # We do not want to enlarge too much
111 # We also want a odd capacity so that the modulo is more distributive
113 if l
>= _capacity
then
114 enlarge
(l
* 3 / 2 + 1)
118 # Remove the node assosiated with the key
119 fun remove_node
(k
: nullable Object)
121 if _the_length
== 0 then return
123 var node
= node_at_idx
(i
, k
)
124 if node
== null then return
126 # Remove the item in the list
127 var prev
= node
._prev_item
128 var next
= node
._next_item
130 prev
._next_item
= next
135 next
._prev_item
= prev
140 # Remove the item in the array
142 prev
= node
._prev_in_bucklet
143 next
= node
._next_in_bucklet
145 prev
._next_in_bucklet
= next
150 next
._prev_in_bucklet
= prev
153 _last_accessed_key
= null
156 # Clear the whole structure
159 var i
= _capacity
- 1
167 _last_accessed_key
= null
171 fun enlarge
(cap
: Int)
174 if cap
< _the_length
+ 1 then cap
= _the_length
+ 1
175 if cap
<= _capacity
then return
177 _last_accessed_key
= null
180 var new_array
= new NativeArray[nullable N
](cap
)
183 # Reput items in the array
184 var node
= _first_item
185 while node
!= null do
186 var index
= index_at
(node
._key
)
187 # Then store it in the array
188 var next
= new_array
[index
]
189 new_array
[index
] = node
190 node
._prev_in_bucklet
= null
191 node
._next_in_bucklet
= next
192 if next
!= null then next
._prev_in_bucklet
= node
193 node
= node
._next_item
198 private abstract class HashNode[K
]
201 var next_item
: nullable N
= null
202 var prev_item
: nullable N
= null
203 var prev_in_bucklet
: nullable N
= null
204 var next_in_bucklet
: nullable N
= null
207 # A `Map` implemented with a hash table.
210 # var map = new HashMap[nullable String, Int]
215 # assert map[null] == 0
216 # assert map["one"] == 1
217 # assert map.keys.has("two")
218 # assert map.values.length == 3
222 super HashCollection[K
]
224 redef type N
: HashMapNode[K
, V
] is fixed
230 return provide_default_value
(key
)
236 redef fun get_or_null
(key
)
246 redef fun iterator
do return new HashMapIterator[K
,V
](self)
248 redef fun length
do return _the_length
250 redef fun is_empty
do return _the_length
== 0
252 redef fun []=(key
, v
)
254 if _capacity
== 0 then enlarge
(17) # 17 because magic in `store`
255 var i
= index_at
(key
)
256 var c
= node_at_idx
(i
, key
)
261 store
(i
, new HashMapNode[K
, V
](key
, v
))
265 redef fun clear
do raz
273 # Build a list filled with the items of `coll`.
274 init from
(coll
: Map[K
, V
]) do
279 redef var keys
: RemovableCollection[K
] = new HashMapKeys[K
, V
](self) is lazy
280 redef var values
: RemovableCollection[V
] = new HashMapValues[K
, V
](self) is lazy
281 redef fun has_key
(k
) do return node_at
(k
) != null
284 # View of the keys of a HashMap
285 private class HashMapKeys[K
, V
]
286 super RemovableCollection[K
]
288 var map
: HashMap[K
, V
]
290 redef fun count
(k
) do if self.has
(k
) then return 1 else return 0
291 redef fun first
do return self.map
._first_item
._key
292 redef fun has
(k
) do return self.map
.node_at
(k
) != null
293 redef fun has_only
(k
) do return (self.has
(k
) and self.length
== 1) or self.is_empty
294 redef fun is_empty
do return self.map
.is_empty
295 redef fun length
do return self.map
.length
297 redef fun iterator
do return new MapKeysIterator[K
, V
](self.map
.iterator
)
299 redef fun clear
do self.map
.clear
301 redef fun remove
(key
) do self.map
.remove_node
(key
)
302 redef fun remove_all
(key
) do self.map
.remove_node
(key
)
305 # View of the values of a Map
306 private class HashMapValues[K
, V
]
307 super RemovableCollection[V
]
309 var map
: HashMap[K
, V
]
311 redef fun count
(item
)
314 var c
= self.map
._first_item
316 if c
._value
== item
then nb
+= 1
321 redef fun first
do return self.map
._first_item
._value
325 var c
= self.map
._first_item
327 if c
._value
== item
then return true
333 redef fun has_only
(item
)
335 var c
= self.map
._first_item
337 if c
._value
!= item
then return false
343 redef fun is_empty
do return self.map
.is_empty
344 redef fun length
do return self.map
.length
346 redef fun iterator
do return new MapValuesIterator[K
, V
](self.map
.iterator
)
348 redef fun clear
do self.map
.clear
350 redef fun remove
(item
)
353 var c
= map
._first_item
355 if c
._value
== item
then
356 map
.remove_node
(c
._key
)
363 redef fun remove_all
(item
)
366 var c
= map
._first_item
368 if c
._value
== item
then
369 map
.remove_node
(c
._key
)
376 private class HashMapNode[K
, V
]
378 redef type N
: HashMapNode[K
, V
]
382 # A `MapIterator` over a `HashMap`.
383 private class HashMapIterator[K
, V
]
384 super MapIterator[K
, V
]
385 redef fun is_ok
do return _node
!= null
393 #redef fun item=(value)
396 # _node.second = value
408 _node
= _node
._next_item
411 # The map to iterate on
412 var map
: HashMap[K
, V
]
415 var node
: nullable HashMapNode[K
, V
] = null
420 _node
= _map
._first_item
424 # A `Set` implemented with a hash table.
425 # Keys of such a map cannot be null and require a working `hash` method
428 super HashCollection[E
]
430 redef type N
: HashSetNode[E
] is fixed
432 redef fun length
do return _the_length
434 redef fun is_empty
do return _the_length
== 0
438 assert _the_length
> 0
439 return _first_item
._key
444 return node_at
(item
) != null
449 if _capacity
== 0 then enlarge
(17) # 17 because magic in `store`
450 var i
= index_at
(item
)
451 var c
= node_at_idx
(i
, item
)
455 store
(i
,new HashSetNode[E
](item
))
459 redef fun remove
(item
) do remove_node
(item
)
461 redef fun clear
do raz
463 redef fun iterator
do return new HashSetIterator[E
](self)
471 # Build a list filled with the items of `coll`.
472 init from
(coll
: Collection[E
]) do
477 redef fun new_set
do return new HashSet[E
]
480 private class HashSetNode[E
]
482 redef type N
: HashSetNode[E
]
485 private class HashSetIterator[E
]
487 redef fun is_ok
do return _node
!= null
498 _node
= _node
._next_item
501 # The set to iterate on
504 # The position in the internal map storage
505 var node
: nullable HashSetNode[E
] = null
509 _node
= _set
._first_item