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 package hash_collection
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
]
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
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)
30 # The last key accessed (used for cache)
31 var _last_accessed_key
: nullable K
= null
33 # The last node accessed (used for cache)
34 var _last_accessed_node
: nullable N
= null
36 # Return the index of the key k
37 fun index_at
(k
: K
): Int
39 var i
= k
.hash
% _capacity
44 # Return the node assosiated with the key
45 fun node_at
(k
: K
): nullable N
47 # cache: `is' is used instead of `==' because it is a faster filter (even if not exact)
48 if k
is _last_accessed_key
then return _last_accessed_node
50 var res
= node_at_idx
(index_at
(k
), k
)
51 _last_accessed_key
= k
52 _last_accessed_node
= res
56 # Return the node assosiated with the key (but with the index already known)
57 fun node_at_idx
(i
: Int, k
: K
): nullable N
62 if ck
is k
or ck
== k
then # prefilter with `is' because the compiler is not smart enought yet
65 c
= c
._next_in_bucklet
70 # Add a new node at a given index
71 fun store
(index
: Int, node
: N
)
73 # Store the item in the list
74 if _first_item
== null then
77 _last_item
._next_item
= node
79 node
._prev_item
= _last_item
80 node
._next_item
= null
83 # Then store it in the array
84 var next
= _array
[index
]
86 node
._next_in_bucklet
= next
87 if next
!= null then next
._prev_in_bucklet
= node
89 _last_accessed_key
= node
._key
90 _last_accessed_node
= node
96 if l
>= _capacity
then
101 # Remove the node assosiated with the key
102 fun remove_node
(k
: K
)
105 var node
= node_at_idx
(i
, k
)
106 if node
== null then return
108 # Remove the item in the list
109 var prev
= node
._prev_item
110 var next
= node
._next_item
112 prev
._next_item
= next
117 next
._prev_item
= prev
122 # Remove the item in the array
124 prev
= node
._prev_in_bucklet
125 next
= node
._next_in_bucklet
127 prev
._next_in_bucklet
= next
132 next
._prev_in_bucklet
= prev
135 _last_accessed_key
= null
140 var i
= _capacity
- 1
148 _last_accessed_key
= null
151 fun enlarge
(cap
: Int)
153 var old_cap
= _capacity
155 if cap
< _length
+ 1 then cap
= _length
+ 1
156 if cap
<= _capacity
then return
158 _last_accessed_key
= null
161 var new_array
= calloc_array
(cap
)
164 # clean the new array
171 if _capacity
<= old_cap
then return
173 # Reput items in the array
174 var node
= _first_item
175 while node
!= null do
176 var index
= index_at
(node
._key
)
177 # Then store it in the array
178 var next
= new_array
[index
]
179 new_array
[index
] = node
180 node
._next_in_bucklet
= next
181 if next
!= null then next
._prev_in_bucklet
= node
182 node
= node
._next_item
187 private class HashNode[K
: Object]
190 readable writable var _next_item
: nullable N
= null
191 readable writable var _prev_item
: nullable N
= null
192 var _prev_in_bucklet
: nullable N
= null
193 var _next_in_bucklet
: nullable N
= null
200 class HashMap[K
: Object, V
]
202 special HashCollection[K
, HashMapNode[K
, V
], V
]
214 redef fun has_key
(key
) do return node_at
(key
) != null
216 redef fun iterator
: HashMapIterator[K
, V
] do return new HashMapIterator[K
,V
](self)
231 return _first_item
._value
234 redef fun is_empty
do return _length
== 0
236 redef fun count
(item
)
241 if c
._value
== item
then nb
+= 1
251 if c
._value
== item
then return true
257 redef fun has_only
(item
)
261 if c
._value
!= item
then return false
267 redef fun []=(key
, v
)
269 var i
= index_at
(key
)
270 var c
= node_at_idx
(i
, key
)
275 store
(i
, new HashMapNode[K
, V
](key
, v
))
279 redef fun remove
(item
)
283 if c
._value
== item
then
291 redef fun remove_at
(key
) do remove_node
(key
)
293 redef fun clear
do raz
303 class HashMapNode[K
: Object, V
]
305 redef type N
: HashMapNode[K
, V
]
315 class HashMapIterator[K
: Object, V
]
316 special MapIterator[K
, V
]
317 redef fun is_ok
do return _node
!= null
325 #redef fun item=(value)
328 # _node.second = value
340 _node
= _node
._next_item
343 # The map to iterate on
344 var _map
: HashMap[K
, V
]
347 var _node
: nullable HashMapNode[K
, V
]
349 init(map
: HashMap[K
, V
])
352 _node
= map
.first_item
356 class HashSet[E
: Object]
358 special HashCollection[E
, HashSetNode[E
], E
]
360 redef fun is_empty
do return _length
== 0
365 return _first_item
._key
370 return node_at
(item
) != null
375 var i
= index_at
(item
)
376 var c
= node_at_idx
(i
, item
)
380 store
(i
,new HashSetNode[E
](item
))
384 redef fun remove
(item
) do remove_node
(item
)
386 redef fun clear
do raz
388 redef fun iterator
do return new HashSetIterator[E
](self)
398 class HashSetNode[E
: Object]
400 redef type N
: HashSetNode[E
]
408 class HashSetIterator[E
: Object]
410 redef fun is_ok
do return _node
!= null
421 _node
= _node
._next_item
424 # The set to iterate on
427 # The position in the internal map storage
428 var _node
: nullable HashSetNode[E
]
430 init(set
: HashSet[E
])
433 _node
= set
._first_item