# A HashCollection is an array of HashNode[K] indexed by the K hash value
private abstract class HashCollection[K: Object, N: HashNode[Object]]
- super ArrayCapable[nullable N]
- var _array: nullable NativeArray[nullable N] = null # Used to store items
- var _capacity: Int = 0 # Size of _array
- var _length: Int = 0 # Number of items in the map
+ private var array: nullable NativeArray[nullable N] = null # Used to store items
+ private var capacity: Int = 0 # Size of _array
+ private var the_length: Int = 0 # Number of items in the map
- var _first_item: nullable N = null # First added item (used to visit items in nice order)
- var _last_item: nullable N = null # Last added item (same)
+ private var first_item: nullable N = null # First added item (used to visit items in nice order)
+ private var last_item: nullable N = null # Last added item (same)
# The last key accessed (used for cache)
- var _last_accessed_key: nullable K = null
+ private var last_accessed_key: nullable K = null
# The last node accessed (used for cache)
- var _last_accessed_node: nullable N = null
+ private var last_accessed_node: nullable N = null
# Return the index of the key k
fun index_at(k: K): Int
_last_accessed_node = node
# Enlarge if needed
- var l = _length
- _length = l + 1
+ var l = _the_length
+ _the_length = l + 1
# Magic values determined empirically
# We do not want to enlarge too much
end
# Remove the item in the array
- _length -= 1
+ _the_length -= 1
prev = node._prev_in_bucklet
next = node._next_in_bucklet
if prev != null then
_array[i] = null
i -= 1
end
- _length = 0
+ _the_length = 0
_first_item = null
_last_item = null
_last_accessed_key = null
do
var old_cap = _capacity
# get a new capacity
- if cap < _length + 1 then cap = _length + 1
+ if cap < _the_length + 1 then cap = _the_length + 1
if cap <= _capacity then return
_capacity = cap
_last_accessed_key = null
# get a new array
- var new_array = calloc_array(cap)
+ var new_array = new NativeArray[nullable N](cap)
_array = new_array
# clean the new array
end
private abstract class HashNode[K: Object]
- var _key: K
+ private var key: K
type N: HashNode[K]
- var _next_item: nullable N = null
- var _prev_item: nullable N = null
- var _prev_in_bucklet: nullable N = null
- var _next_in_bucklet: nullable N = null
- init(k: K)
- do
- _key = k
- end
+ private var next_item: nullable N = null
+ private var prev_item: nullable N = null
+ private var prev_in_bucklet: nullable N = null
+ private var next_in_bucklet: nullable N = null
end
# A map implemented with a hash table.
redef fun iterator: HashMapIterator[K, V] do return new HashMapIterator[K,V](self)
- redef fun length do return _length
+ redef fun length do return _the_length
- redef fun is_empty do return _length == 0
+ redef fun is_empty do return _the_length == 0
redef fun []=(key, v)
do
init
do
_capacity = 0
- _length = 0
+ _the_length = 0
enlarge(0)
end
private class HashMapNode[K: Object, V]
super HashNode[K]
redef type N: HashMapNode[K, V]
- var _value: V
-
- init(k: K, v: V)
- do
- super(k)
- _value = v
- end
+ private var value: V
end
class HashMapIterator[K: Object, V]
end
# The map to iterate on
- var _map: HashMap[K, V]
+ private var map: HashMap[K, V]
# The current node
- var _node: nullable HashMapNode[K, V]
+ private var node: nullable HashMapNode[K, V] = null
- init(map: HashMap[K, V])
+ init
do
_map = map
- _node = map._first_item
+ _node = _map._first_item
end
end
super Set[E]
super HashCollection[E, HashSetNode[E]]
- redef fun length do return _length
+ redef fun length do return _the_length
- redef fun is_empty do return _length == 0
+ redef fun is_empty do return _the_length == 0
redef fun first
do
- assert _length > 0
+ assert _the_length > 0
return _first_item._key
end
init
do
_capacity = 0
- _length = 0
+ _the_length = 0
enlarge(0)
end
private class HashSetNode[E: Object]
super HashNode[E]
redef type N: HashSetNode[E]
-
- init(e: E)
- do
- _key = e
- end
end
private class HashSetIterator[E: Object]
end
# The set to iterate on
- var _set: HashSet[E]
+ private var set: HashSet[E]
# The position in the internal map storage
- var _node: nullable HashSetNode[E]
+ private var node: nullable HashSetNode[E] = null
- init(set: HashSet[E])
+ init
do
- _set = set
- _node = set._first_item
+ _node = _set._first_item
end
end