init(e: E) do item = e
# The stored item
- var item: E writable
+ var item: E is writable
end
# This iterator is quite stupid since it is used for only one item.
redef var is_ok: Bool = true
- var _container: Container[E]
+ private var container: Container[E]
end
# Items can be removed from this collection
# Because of the law between `==` and `hash`, `hash` is redefined to be the sum of the hash of the elements
redef fun hash
do
- var res = 0
- for e in self do res += res.hash
+ # 23 is a magic number empirically determined to be not so bad.
+ var res = 23 + length
+ # Note: the order of the elements must not change the hash value.
+ # So, unlike usual hash functions, the accumulator is not combined with itself.
+ for e in self do res += e.hash
return res
end
end
# MapRead are abstract associative collections: `key` -> `item`.
-interface MapRead[K: Object, E]
+interface MapRead[K: Object, V]
# Get the item at `key`
#
# var x = new HashMap[String, Int]
#
# If the key is not in the map, `provide_default_value` is called (that aborts by default)
# See `get_or_null` and `get_or_default` for safe variations.
- fun [](key: K): E is abstract
+ fun [](key: K): V is abstract
# Get the item at `key` or null if `key` is not in the map.
#
# assert x.get_or_null("four") == 4
# assert x.get_or_null("five") == null
#
- # Note: use `has_key` and `[]` if you need the distinction bewteen a key associated with null, and no key.
- fun get_or_null(key: K): nullable E
+ # Note: use `has_key` and `[]` if you need the distinction between a key associated with null, and no key.
+ fun get_or_null(key: K): nullable V
do
if has_key(key) then return self[key]
return null
# assert x.get_or_default("four", 40) == 4
# assert x.get_or_default("five", 50) == 50
#
- fun get_or_default(key: K, default: E): E
+ fun get_or_default(key: K, default: V): V
do
if has_key(key) then return self[key]
return default
end
- # Depreciated alias for `keys.has`
+ # Alias for `keys.has`
fun has_key(key: K): Bool do return self.keys.has(key)
# Get a new iterator on the map.
- fun iterator: MapIterator[K, E] is abstract
+ fun iterator: MapIterator[K, V] is abstract
# Return the point of view of self on the values only.
# Note that `self` and `values` are views on the same data;
# x["four"] = 4
# assert x.values.has(4) == true
# assert x.values.has(5) == false
- fun values: Collection[E] is abstract
+ fun values: Collection[V] is abstract
# Return the point of view of self on the keys only.
# Note that `self` and `keys` are views on the same data;
#
# Note: the value is returned *as is*, implementations may want to store the value in the map before returning it
# @toimplement
- protected fun provide_default_value(key: K): E do abort
+ protected fun provide_default_value(key: K): V do abort
end
# Maps are associative collections: `key` -> `item`.
# assert map.values.has(1) == true
# assert map.values.has(3) == false
#
-interface Map[K: Object, E]
- super MapRead[K, E]
+interface Map[K: Object, V]
+ super MapRead[K, V]
# Set the `value` at `key`.
#
# x["four"] = 4
# assert x["four"] == 4
#
- # If the key was associated with a value, this old value is discarted
+ # If the key was associated with a value, this old value is discarded
# and replaced with the new one.
#
# x["four"] = 40
# assert x["four"] == 40
# assert x.values.has(4) == false
#
- fun []=(key: K, value: E) is abstract
+ fun []=(key: K, value: V) is abstract
# Add each (key,value) of `map` into `self`.
# If a same key exists in `map` and `self`, then the value in self is discarded.
# assert x["four"] == 40
# assert x["five"] == 5
# assert x["nine"] == 90
- fun recover_with(map: Map[K, E])
+ fun recover_with(map: MapRead[K, V])
do
var i = map.iterator
while i.is_ok do
# ENSURE `is_empty`
fun clear is abstract
- redef fun values: RemovableCollection[E] is abstract
+ redef fun values: RemovableCollection[V] is abstract
redef fun keys: RemovableCollection[K] is abstract
end
# Iterators for Map.
-interface MapIterator[K: Object, E]
+interface MapIterator[K: Object, V]
# The current item.
# Require `is_ok`.
- fun item: E is abstract
+ fun item: V is abstract
# The key of the current item.
# Require `is_ok`.
var p = 0
var i = iterator
while i.is_ok do
- if p>pos and i.item == item then return i.index
+ if p>=pos and i.item == item then return i.index
i.next
p += 1
end
# Because of the law between `==` and `hash`, `hash` is redefined to be the sum of the hash of the elements
redef fun hash
do
- var res = 0
- for e in self do res += res.hash
+ # The 17 and 2/3 magic numbers were determined empirically.
+ # Note: the standard hash functions djb2, sbdm and fnv1 were also
+ # tested but were comparable (or worse).
+ var res = 17 + length
+ for e in self do
+ res = res * 3 / 2
+ if e != null then res += e.hash
+ end
return res
end
# var a = [1,2,3]
# a.append([7..9])
# assert a == [1,2,3,7,8,9]
- fun append(coll: Collection[E]) do for i in coll do push(i)
+ #
+ # Alias of `add_all`
+ fun append(coll: Collection[E]) do add_all(coll)
# Remove the last item.
#
# assert a == [20,10,1,2,3]
fun unshift(e: E) is abstract
+ # Add all items of `coll` before the first one.
+ #
+ # var a = [1,2,3]
+ # a.prepend([7..9])
+ # assert a == [7,8,9,1,2,3]
+ #
+ # Alias of `insert_at(coll, 0)`
+ fun prepend(coll: Collection[E]) do insert_all(coll, 0)
+
# Remove the first item.
# The second item thus become the first.
#
# a.insert(100, 2)
# assert a == [10, 20, 100, 30, 40]
#
- # REQUIRE `index >= 0 and index < length`
+ # REQUIRE `index >= 0 and index <= length`
# ENSURE `self[index] == item`
fun insert(item: E, index: Int) is abstract
+ # Insert all elements at a given position, following elements are shifted.
+ #
+ # var a = [10, 20, 30, 40]
+ # a.insert_all([100..102], 2)
+ # assert a == [10, 20, 100, 101, 102, 30, 40]
+ #
+ # REQUIRE `index >= 0 and index <= length`
+ # ENSURE `self[index] == coll.first`
+ fun insert_all(coll: Collection[E], index: Int)
+ do
+ assert index >= 0 and index < length
+ if index == length then
+ add_all(coll)
+ end
+ for c in coll do
+ insert(c, index)
+ index += 1
+ end
+ end
+
# Remove the item at `index` and shift all following elements
#
# var a = [10,20,30]
# Associative arrays that internally uses couples to represent each (key, value) pairs.
# This is an helper class that some specific implementation of Map may implements.
-interface CoupleMap[K: Object, E]
- super Map[K, E]
+interface CoupleMap[K: Object, V]
+ super Map[K, V]
# Return the couple of the corresponding key
# Return null if the key is no associated element
- protected fun couple_at(key: K): nullable Couple[K, E] is abstract
+ protected fun couple_at(key: K): nullable Couple[K, V] is abstract
# Return a new iteralot on all couples
# Used to provide `iterator` and others
- protected fun couple_iterator: Iterator[Couple[K,E]] is abstract
+ protected fun couple_iterator: Iterator[Couple[K,V]] is abstract
- redef fun iterator do return new CoupleMapIterator[K,E](couple_iterator)
+ redef fun iterator do return new CoupleMapIterator[K,V](couple_iterator)
redef fun [](key)
do
# Iterator on CoupleMap
#
# Actually it is a wrapper around an iterator of the internal array of the map.
-private class CoupleMapIterator[K: Object, E]
- super MapIterator[K, E]
+private class CoupleMapIterator[K: Object, V]
+ super MapIterator[K, V]
redef fun item do return _iter.item.second
#redef fun item=(e) do _iter.item.second = e
_iter.next
end
- var _iter: Iterator[Couple[K,E]]
+ private var iter: Iterator[Couple[K,V]]
- init(i: Iterator[Couple[K,E]]) do _iter = i
+ init(i: Iterator[Couple[K,V]]) do _iter = i
end
# Some tools ###################################################################
class Couple[F, S]
# The first element of the couple.
- var first: F writable
+ var first: F is writable
# The second element of the couple.
- var second: S writable
+ var second: S is writable
# Create a new instance with a first and a second object.
init(f: F, s: S)