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 index accessed
31 var _last_accessed_index
: Int = -1
33 # The last key accessed
34 var _last_accessed_key
: nullable K
= null
36 # Return the index of the k element
37 fun index_at
(k
: K
): Int
41 # Fisrt step: look in the last indexed elt
42 if k
== _last_accessed_key
then return _last_accessed_index
44 # Compute the base position of the key
45 var base
= k
.hash
% _capacity
46 if base
< 0 then base
= - base
48 # Look for the key in the array
52 if c
== null or c
.key
== k
then # REAL equals
53 _last_accessed_index
= cur
54 _last_accessed_key
= k
58 if cur
< 0 then cur
= _capacity
- 1
59 assert no_loop
: cur
!= base
64 # Add a new node (should be free)
65 fun store
(index
: Int, node
: N
)
67 # Store the item in the list
68 if _first_item
== null then
71 _last_item
.next_item
= node
73 node
.prev_item
= _last_item
76 # Then store it in the array
77 assert _array
[index
] == null
81 l
= (l
+ 5) * 150 / 100
82 if l
>= _capacity
then
87 fun remove_index
(i
: Int)
89 assert correct_index
: i
>= 0 and i
< _capacity
91 assert has_couple
: node
!= null
92 # Remove the item in the list
93 var prev
= node
.prev_item
94 var next
= node
.next_item
101 next
.prev_item
= prev
105 # Remove the item in the array
108 # Now replaces things before if needed
118 var i2
= index_at
(n
.key
)
121 assert _array
[i2
] == null
129 var i
= _capacity
- 1
137 _last_accessed_key
= null
140 fun enlarge
(cap
: Int)
142 var old_cap
= _capacity
144 # cap = cap * 130 / 100 + 5 + 1000 # /
145 if cap
< _length
+ 1 then cap
= _length
+ 1
146 if cap
<= _capacity
then return
148 _last_accessed_key
= null
151 var new_array
= calloc_array
(cap
)
154 # clean the new array
161 if _capacity
<= old_cap
then return
163 # Reput items in the array
164 var node
= _first_item
165 while node
!= null do
166 var ind
= index_at
(node
.key
)
167 assert new_array
[ind
] == null
168 new_array
[ind
] = node
169 node
= node
.next_item
171 _last_accessed_key
= null
175 private class HashNode[K
]
176 fun key
: K
is abstract
178 readable writable var _next_item
: nullable N
= null
179 readable writable var _prev_item
: nullable N
= null
183 special CoupleMap[K
, V
]
184 special HashCollection[K
, HashMapNode[K
, V
], V
]
186 redef fun iterator
: HashMapIterator[K
, V
] do return new HashMapIterator[K
,V
](self)
201 return _first_item
.second
204 redef fun is_empty
do return _length
== 0
206 redef fun count
(item
)
210 while i
< _capacity
do
212 if c
!= null and c
.second
== item
then nb
+= 1
221 while i
< _capacity
do
223 if c
!= null and c
.second
== item
then return true
229 redef fun has_only
(item
)
232 while i
< _capacity
do
234 if c
!= null and c
.second
!= item
then return false
240 redef fun []=(key
, v
)
243 var i
= index_at
(key
)
249 store
(i
, new HashMapNode[K
, V
](key
, v
))
253 redef fun remove
(item
)
256 while i
< _capacity
do
258 if c
!= null and c
.second
== item
then
266 redef fun remove_at
(key
) do remove_index
(index_at
(key
))
268 redef fun clear
do raz
270 redef fun couple_at
(key
) do return _array
[index_at
(key
)]
280 class HashMapNode[K
, V
]
283 redef fun key
do return first
284 redef type N
: HashMapNode[K
, V
]
293 class HashMapIterator[K
, V
]
294 special MapIterator[K
, V
]
295 redef fun is_ok
do return _node
!= null
303 #redef fun item=(value)
306 # _node.second = value
318 _node
= _node
.next_item
321 # The map to iterate on
322 var _map
: HashMap[K
, V
]
325 var _node
: nullable HashMapNode[K
, V
]
327 init(map
: HashMap[K
, V
])
330 _node
= map
.first_item
336 special HashCollection[E
, HashSetNode[E
], E
]
338 redef fun is_empty
do return _length
== 0
343 return _first_item
.key
348 return _array
[index_at
(item
)] != null
353 var i
= index_at
(item
)
358 store
(i
,new HashSetNode[E
](item
))
362 redef fun remove
(item
) do remove_index
(index_at
(item
))
364 redef fun clear
do raz
366 redef fun iterator
do return new HashSetIterator[E
](self)
378 redef type N
: HashSetNode[E
]
380 redef readable writable var _key
: E
388 class HashSetIterator[E
]
390 redef fun is_ok
do return _node
!= null
401 _node
= _node
.next_item
404 # The set to iterate on
407 # The position in the internal map storage
408 var _node
: nullable HashSetNode[E
]
410 init(set
: HashSet[E
])
413 _node
= set
.first_item