1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2004-2008 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 # This module is about hashable things.
14 # It introduces an hash funtion in objects.
15 # It also introduces two classes, an Hashmap and an Hashset.
21 # The hash code of the object.
22 # Assuming that a = b -> a.hash = b.hash
24 # Without redefinition, it is the `id' of the instance.
25 meth hash
: Int do return object_id
/ 8
36 h
= (h
* 32) + h
+ it
[i
].ascii
45 redef meth hash
do return self
49 redef meth hash
do return ascii
63 # A HashCollection is an array of HashNode[K] indexed by the K hash value
64 private class HashCollection[K
: Object, N
: HashNode[K
], E
: Object]
66 special ArrayCapable[N
]
67 attr _array
: NativeArray[N
] # Used to store items
68 attr _capacity
: Int # Size of _array
69 redef readable attr _length
: Int # Number of items in the map
71 readable attr _first_item
: N
# First added item (used to visit items in nice order)
72 attr _last_item
: N
# Last added item (same)
74 # The last index accessed
75 attr _last_accessed_index
: Int
77 # The last key accessed
78 attr _last_accessed_key
: K
80 # Return the index of the k element
81 meth index_at
(k
: K
): Int
86 # Fisrt step: look in the last indexed elt
87 if k
== _last_accessed_key
then return _last_accessed_index
89 # Compute the base position of the key
90 var base
= k
.hash
% _capacity
91 if base
< 0 then base
= - base
93 # Look for the key in the array
97 if c
== null or c
.key
== k
then # REAL equals
98 _last_accessed_index
= cur
99 _last_accessed_key
= k
103 if cur
< 0 then cur
= _capacity
- 1
104 assert no_loop
: cur
!= base
109 # Add a new node (should be free)
110 meth store
(index
: Int, node
: N
)
112 # Store the item in the list
113 if _first_item
== null then
116 _last_item
.next_item
= node
118 node
.prev_item
= _last_item
119 node
.next_item
= null
121 # Then store it in the array
122 assert _array
[index
] == null
126 l
= (l
+ 5) * 150 / 100
127 if l
>= _capacity
then
132 meth remove_index
(i
: Int)
134 assert correct_index
: i
>= 0 and i
< _capacity
136 assert has_couple
: node
!= null
137 # Remove the item in the list
138 var prev
= node
.prev_item
139 var next
= node
.next_item
141 prev
.next_item
= next
146 next
.prev_item
= prev
150 # Remove the item in the array
153 # Now replaces things before if needed
163 var i2
= index_at
(n
.key
)
166 assert _array
[i2
] == null
174 var i
= _capacity
- 1
182 _last_accessed_key
= null
185 meth enlarge
(cap
: Int)
187 var old_cap
= _capacity
189 # cap = cap * 130 / 100 + 5 + 1000 # /
190 if cap
< _length
+ 1 then cap
= _length
+ 1
191 if cap
<= _capacity
then return
193 _last_accessed_key
= null
196 var new_array
= calloc_array
(cap
)
199 # clean the new array
206 if _capacity
<= old_cap
then return
208 var new_array
= _array
209 # Reput items in the array
210 var node
= _first_item
211 while node
!= null do
212 var ind
= index_at
(node
.key
)
213 assert new_array
[ind
] == null
214 new_array
[ind
] = node
215 node
= node
.next_item
217 _last_accessed_key
= null
221 private class HashNode[K
]
222 meth key
: K
is abstract
224 readable writable attr _next_item
: N
225 readable writable attr _prev_item
: N
229 special CoupleMap[K
, V
]
230 special HashCollection[K
, HashMapNode[K
, V
], V
]
232 redef meth iterator
: HashMapIterator[K
, V
] do return new HashMapIterator[K
,V
](self)
237 return _first_item
.second
240 redef meth is_empty
do return _length
== 0
242 redef meth count
(item
)
246 while i
< _capacity
do
248 if c
!= null and c
.second
== item
then nb
+= 1
257 while i
< _capacity
do
259 if c
!= null and c
.second
== item
then return true
265 redef meth has_only
(item
)
268 while i
< _capacity
do
270 if c
!= null and c
.second
!= item
then return false
276 redef meth
[]=(key
, v
)
279 var i
= index_at
(key
)
285 store
(i
, new HashMapNode[K
, V
](key
, v
))
289 redef meth remove
(item
)
292 while i
< _capacity
do
294 if c
!= null and c
.second
== item
then
302 redef meth remove_at
(key
) do remove_index
(index_at
(key
))
304 redef meth clear
do raz
306 redef meth couple_at
(key
) do return _array
[index_at
(key
)]
316 class HashMapNode[K
, V
]
319 redef meth key
do return first
320 redef type N
: HashMapNode[K
, V
]
322 redef init(k
: K
, v
: V
)
329 class HashMapIterator[K
, V
]
330 special MapIterator[K
, V
]
331 redef meth is_ok
do return _node
!= null
339 redef meth item
=(value
)
354 _node
= _node
.next_item
357 # The map to iterate on
358 attr _map
: HashMap[K
, V
]
361 attr _node
: HashMapNode[K
, V
]
363 init(map
: HashMap[K
, V
])
366 _node
= map
.first_item
372 special HashCollection[E
, HashSetNode[E
], E
]
374 redef meth is_empty
do return _length
== 0
379 return _first_item
.key
384 return _array
[index_at
(item
)] != null
389 var i
= index_at
(item
)
394 store
(i
,new HashSetNode[E
](item
))
398 redef meth remove
(item
) do remove_index
(index_at
(item
))
400 redef meth clear
do raz
402 redef meth iterator
do return new HashSetIterator[E
](self)
414 redef type N
: HashSetNode[E
]
416 redef readable writable attr _key
: E
424 class HashSetIterator[E
]
426 redef meth is_ok
do return _node
!= null
437 _node
= _node
.next_item
440 # The set to iterate on
441 attr _set
: HashSet[E
]
443 # The position in the internal map storage
444 attr _node
: HashSetNode[E
]
446 init(set
: HashSet[E
])
449 _node
= set
.first_item