Add helper functions and refactorize some code
Signed-off-by: Jean Privat <jean@pryen.org>
abort #FIXME remove once unreach loop exits are in c_src
end
abort #FIXME remove once unreach loop exits are in c_src
end
+ # Return the node assosiated with the key
+ fun node_at(k: K): nullable N
+ do
+ var res = node_at_idx(index_at(k), k)
+ return res
+ end
+
+ # Return the node assosiated with the key (but with the index already known)
+ fun node_at_idx(i: Int, k: K): nullable N
+ do
+ return _array[i]
+ end
+
# Add a new node (should be free)
fun store(index: Int, node: N)
do
# Add a new node (should be free)
fun store(index: Int, node: N)
do
- fun remove_index(i: Int)
+ # Remove the node assosiated with the key
+ fun remove_node(k: K)
- assert correct_index: i >= 0 and i < _capacity
- assert has_couple: node != null
+ if node == null then return
# Remove the item in the list
var prev = node._prev_item
var next = node._next_item
# Remove the item in the list
var prev = node._prev_item
var next = node._next_item
do
var old_cap = _capacity
# get a new capacity
do
var old_cap = _capacity
# get a new capacity
- # cap = cap * 130 / 100 + 5 + 1000 # /
if cap < _length + 1 then cap = _length + 1
if cap <= _capacity then return
_capacity = cap
if cap < _length + 1 then cap = _length + 1
if cap <= _capacity then return
_capacity = cap
if c == null then
abort
else
if c == null then
abort
else
- redef fun has_key(key) do return couple_at(key) != null
+ redef fun has_key(key) do return node_at(key) != null
redef fun iterator: HashMapIterator[K, V] do return new HashMapIterator[K,V](self)
redef fun iterator: HashMapIterator[K, V] do return new HashMapIterator[K,V](self)
redef fun count(item)
do
var nb = 0
redef fun count(item)
do
var nb = 0
- var i = 0
- while i < _capacity do
- var c = _array[i]
- if c != null and c._value == item then nb += 1
- i += 1
+ var c = _first_item
+ while c != null do
+ if c._value == item then nb += 1
+ c = c._next_item
end
return nb
end
redef fun has(item)
do
end
return nb
end
redef fun has(item)
do
- var i = 0
- while i < _capacity do
- var c = _array[i]
- if c != null and c._value == item then return true
- i += 1
+ var c = _first_item
+ while c != null do
+ if c._value == item then return true
+ c = c._next_item
end
return false
end
redef fun has_only(item)
do
end
return false
end
redef fun has_only(item)
do
- var i = 0
- while i < _capacity do
- var c = _array[i]
- if c != null and c._value != item then return false
- i += 1
+ var c = _first_item
+ while c != null do
+ if c._value != item then return false
+ c = c._next_item
do
assert key != null
var i = index_at(key)
do
assert key != null
var i = index_at(key)
+ var c = node_at_idx(i, key)
if c != null then
c._key = key
c._value = v
if c != null then
c._key = key
c._value = v
redef fun remove(item)
do
redef fun remove(item)
do
- var i = 0
- while i < _capacity do
- var c = _array[i]
- if c != null and c._value == item then
- remove_index(i)
+ var c = _first_item
+ while c != null do
+ if c._value == item then
+ remove_node(c._key)
- redef fun remove_at(key) do remove_index(index_at(key))
+ redef fun remove_at(key) do remove_node(key)
- fun couple_at(key: K): nullable HashMapNode[K, V] do return _array[index_at(key)]
-
- return _array[index_at(item)] != null
+ return node_at(item) != null
end
redef fun add(item)
do
var i = index_at(item)
end
redef fun add(item)
do
var i = index_at(item)
+ var c = node_at_idx(i, item)
if c != null then
c._key = item
else
if c != null then
c._key = item
else
- redef fun remove(item) do remove_index(index_at(item))
+ redef fun remove(item) do remove_node(item)