metamodel: rename 'universal' to 'enum'
[nit.git] / lib / standard / collection / hash_collection.nit
index 6b1aefd..13d5a7a 100644 (file)
@@ -18,8 +18,8 @@ import hash
 
 # A HashCollection is an array of HashNode[K] indexed by the K hash value
 private class HashCollection[K: Object, N: HashNode[K], E]
-special Collection[E]
-special ArrayCapable[nullable N]
+       super Collection[E]
+       super ArrayCapable[nullable N]
        var _array: nullable NativeArray[nullable N] = null # Used to store items
        var _capacity: Int = 0 # Size of _array
        redef readable var _length: Int = 0 # Number of items in the map
@@ -27,42 +27,47 @@ special ArrayCapable[nullable N]
        readable 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)
 
-       # The last index accessed
-       var _last_accessed_index: Int = -1
-
-       # The last key accessed
+       # The last key accessed (used for cache)
        var _last_accessed_key: nullable K = null
 
-       # Return the index of the k element
+       # The last node accessed (used for cache)
+       var _last_accessed_node: nullable N = null
+
+       # Return the index of the key k
        fun index_at(k: K): Int
        do
-               var arr = _array
-
-               # Fisrt step: look in the last indexed elt
-               if k == _last_accessed_key then return _last_accessed_index
-
-               # Compute the base position of the key
-               var base = k.hash % _capacity
-               if base < 0 then base = - base
-
-               # Look for the key in the array
-               var cur = base
-               loop
-                       var c = arr[cur]
-                       if c == null or c._key == k then # REAL equals
-                               _last_accessed_index = cur
-                               _last_accessed_key = k
-                               return cur
+               var i = k.hash % _capacity
+               if i < 0 then i = - i
+               return i
+       end
+
+       # Return the node assosiated with the key
+       fun node_at(k: K): nullable N
+       do
+               # cache: `is' is used instead of `==' because it is a faster filter (even if not exact)
+               if k is _last_accessed_key then return _last_accessed_node
+
+               var res = node_at_idx(index_at(k), k)
+               _last_accessed_key = k
+               _last_accessed_node = res
+               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
+               var c = _array[i]
+               while c != null do
+                       var ck = c._key
+                       if ck is k or ck == k then # prefilter with `is' because the compiler is not smart enought yet
+                               break
                        end
-                       cur -= 1
-                       if cur < 0 then cur = _capacity - 1
-                       assert no_loop: cur != base
-                       if false then break # FIXME remove once unreach loop exits are in c_src
+                       c = c._next_in_bucklet
                end
-               abort #FIXME remove once unreach loop exits are in c_src
+               return c
        end
 
-       # Add a new node (should be free)
+       # Add a new node at a given index
        fun store(index: Int, node: N)
        do
                # Store the item in the list
@@ -74,22 +79,32 @@ special ArrayCapable[nullable N]
                node._prev_item = _last_item
                node._next_item = null
                _last_item = node
+
                # Then store it in the array
-               assert _array[index] == null
+               var next = _array[index]
                _array[index] = node
+               node._next_in_bucklet = next
+               if next != null then next._prev_in_bucklet = node
+
+               _last_accessed_key = node._key
+               _last_accessed_node = node
+
+               # Enlarge if needed
                var l = _length
                _length = l + 1
-               l = (l + 5) * 150 / 100
+               l = (l + 5) * 3 / 2
                if l >= _capacity then
                        enlarge(l * 2)
                end
        end
 
-       fun remove_index(i: Int)
+       # Remove the node assosiated with the key
+       fun remove_node(k: K)
        do
-               assert correct_index: i >= 0 and i < _capacity
-               var node = _array[i]
-               assert has_couple: node != null
+               var i = index_at(k)
+               var node = node_at_idx(i, k)
+               if node == null then return
+
                # Remove the item in the list
                var prev = node._prev_item
                var next = node._next_item
@@ -103,26 +118,21 @@ special ArrayCapable[nullable N]
                else
                        _last_item = prev
                end
+
                # Remove the item in the array
-               _array[i] = null
                _length -= 1
-               # Now replaces things before if needed
-               loop
-                       i -= 1
-                       if i < 0 then
-                               i = _capacity - 1
-                       end
-                       var n = _array[i]
-                       if n == null then
-                               return
-                       end
-                       var i2 = index_at(n._key)
-                       if i != i2 then
-                               _array[i] = null
-                               assert _array[i2] == null
-                               _array[i2] = n
-                       end
+               prev = node._prev_in_bucklet
+               next = node._next_in_bucklet
+               if prev != null then
+                       prev._next_in_bucklet = next
+               else
+                       _array[i] = next
+               end
+               if next != null then
+                       next._prev_in_bucklet = prev
                end
+
+               _last_accessed_key = null
        end
 
        fun raz
@@ -142,7 +152,6 @@ special ArrayCapable[nullable N]
        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
@@ -164,33 +173,37 @@ special ArrayCapable[nullable N]
                # Reput items in the array
                var node = _first_item
                while node != null do
-                       var ind = index_at(node._key)
-                       assert new_array[ind] == null
-                       new_array[ind] = node
+                       var index = index_at(node._key)
+                       # Then store it in the array
+                       var next = new_array[index]
+                       new_array[index] = node
+                       node._next_in_bucklet = next
+                       if next != null then next._prev_in_bucklet = node
                        node = node._next_item
                end
-               _last_accessed_key = null
        end
 end
 
-private class HashNode[K]
+private class HashNode[K: Object]
        var _key: K
        type N: HashNode[K]
        readable writable var _next_item: nullable N = null
        readable writable 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
 end
 
-class HashMap[K, V]
-special Map[K, V]
-special HashCollection[K, HashMapNode[K, V], V]
+class HashMap[K: Object, V]
+       super Map[K, V]
+       super HashCollection[K, HashMapNode[K, V], V]
 
        redef fun [](key)
        do
-               var c = couple_at(key)
+               var c = node_at(key)
                if c == null then
                        abort
                else
@@ -198,7 +211,7 @@ special HashCollection[K, HashMapNode[K, V], V]
                end
        end
 
-       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)
 
@@ -223,42 +236,38 @@ special HashCollection[K, HashMapNode[K, V], V]
        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
-               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
-               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
                end
                return true
        end
 
        redef fun []=(key, v)
        do
-               assert key != null
                var i = index_at(key)
-               var c = _array[i]
+               var c = node_at_idx(i, key)
                if c != null then
                        c._key = key
                        c._value = v
@@ -269,23 +278,20 @@ special HashCollection[K, HashMapNode[K, V], V]
 
        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)
                                return
                        end
-                       i += 1
+                       c = c._next_item
                end
        end
 
-       redef fun remove_at(key) do remove_index(index_at(key))
+       redef fun remove_at(key) do remove_node(key)
 
        redef fun clear do raz
 
-       fun couple_at(key: K): nullable HashMapNode[K, V] do return _array[index_at(key)]
-
        init
        do
                _capacity = 0
@@ -294,8 +300,8 @@ special HashCollection[K, HashMapNode[K, V], V]
        end
 end
 
-class HashMapNode[K, V]
-special HashNode[K]
+class HashMapNode[K: Object, V]
+       super HashNode[K]
        redef type N: HashMapNode[K, V]
        var _value: V
 
@@ -306,8 +312,8 @@ special HashNode[K]
        end
 end
 
-class HashMapIterator[K, V]
-special MapIterator[K, V]
+class HashMapIterator[K: Object, V]
+       super MapIterator[K, V]
        redef fun is_ok do return _node != null
 
        redef fun item
@@ -347,9 +353,9 @@ special MapIterator[K, V]
        end
 end
 
-class HashSet[E]
-special Set[E]
-special HashCollection[E, HashSetNode[E], E]
+class HashSet[E: Object]
+       super Set[E]
+       super HashCollection[E, HashSetNode[E], E]
 
        redef fun is_empty do return _length == 0
 
@@ -361,13 +367,13 @@ special HashCollection[E, HashSetNode[E], E]
 
        redef fun has(item)
        do
-               return _array[index_at(item)] != null
+               return node_at(item) != null
        end
 
        redef fun add(item)
        do
                var i = index_at(item)
-               var c = _array[i]
+               var c = node_at_idx(i, item)
                if c != null then
                        c._key = item
                else
@@ -375,7 +381,7 @@ special HashCollection[E, HashSetNode[E], E]
                end
        end
 
-       redef fun remove(item) do remove_index(index_at(item))
+       redef fun remove(item) do remove_node(item)
 
        redef fun clear do raz
 
@@ -389,8 +395,8 @@ special HashCollection[E, HashSetNode[E], E]
        end
 end
 
-class HashSetNode[E]
-special HashNode[E]
+class HashSetNode[E: Object]
+       super HashNode[E]
        redef type N: HashSetNode[E]
 
        init(e: E)
@@ -399,8 +405,8 @@ special HashNode[E]
        end
 end
 
-class HashSetIterator[E]
-special Iterator[E]
+class HashSetIterator[E: Object]
+       super Iterator[E]
        redef fun is_ok do return _node != null
 
        redef fun item