lib/collection: fix doc of the popular `Container`
[nit.git] / lib / standard / collection / abstract_collection.nit
index 39fe9bb..e1cb6a0 100644 (file)
@@ -33,25 +33,29 @@ import kernel
 # Subclasses often provide a more efficient implementation.
 #
 # Because of the `iterator` method, Collections instances can use
-# the `for` control structure:
+# the `for` control structure.
 #
-#         var x: Collection[U]
-#         # ...
-#         for u in x do
-#             # u is a U
-#             # ...
-#         end
+# ~~~nitish
+# var x: Collection[U]
+# # ...
+# for u in x do
+#      # u is a U
+#      # ...
+# end
+# ~~~
 #
-# that is equivalent with
+# that is equivalent with the following:
 #
-#         var x: Collection[U]
-#         # ...
-#         var i = x.iterator
-#         while i.is_ok do
-#             var u = i.item # u is a U
-#             # ...
-#             i.next
-#         end
+# ~~~nitish
+# var x: Collection[U]
+# # ...
+# var i = x.iterator
+# while i.is_ok do
+#     var u = i.item # u is a U
+#     # ...
+#     i.next
+# end
+# ~~~
 interface Collection[E]
        # Get a new iterator on the collection.
        fun iterator: Iterator[E] is abstract
@@ -106,7 +110,7 @@ interface Collection[E]
        # How many occurrences of `item` are in the collection?
        # Comparisons are done with ==
        #
-       #    assert [10,20,10].count(10)         == 2
+       #     assert [10,20,10].count(10)         == 2
        fun count(item: E): Int
        do
                var nb = 0
@@ -116,24 +120,55 @@ interface Collection[E]
 
        # Return the first item of the collection
        #
-       #    assert [1,2,3].first                == 1
+       #     assert [1,2,3].first                == 1
        fun first: E
        do
                assert length > 0
                return iterator.item
        end
 
-       # Is the collection contains all the elements of `other`?
+       # Does the collection contain at least each element of `other`?
+       #
+       #     assert [1,3,4,2].has_all([1..2])    == true
+       #     assert [1,3,4,2].has_all([1..5])    == false
+       #
+       # Repeated elements in the collections are not considered.
        #
-       #    assert [1,1,1].has_all([1])         == true
-       #    assert [1,1,1].has_all([1,2])       == false
-       #    assert [1,3,4,2].has_all([1..2])    == true
-       #    assert [1,3,4,2].has_all([1..5])    == false
+       #     assert [1,1,1].has_all([1])         == true
+       #     assert [1..5].has_all([1,1,1])      == true
+       #
+       # Note that the default implementation is general and correct for any lawful Collections.
+       # It is memory-efficient but relies on `has` so may be CPU-inefficient for some kind of collections.
        fun has_all(other: Collection[E]): Bool
        do
                for x in other do if not has(x) then return false
                return true
        end
+
+       # Does the collection contain exactly all the elements of `other`?
+       #
+       # The same elements must be present in both `self` and `other`,
+       # but the order of the elements in the collections are not considered.
+       #
+       #     assert [1..3].has_exactly([3,1,2]) == true  # the same elements
+       #     assert [1..3].has_exactly([3,1])   == false # 2 is not in the array
+       #     assert [1..2].has_exactly([3,1,2]) == false # 3 is not in the range
+       #
+       # Repeated elements must be present in both collections in the same amount.
+       # So basically it is a multi-set comparison.
+       #
+       #     assert [1,2,3,2].has_exactly([1,2,2,3]) == true  # the same elements
+       #     assert [1,2,3,2].has_exactly([1,2,3])   == false # more 2 in the first array
+       #     assert [1,2,3].has_exactly([1,2,2,3])   == false # more 2 in the second array
+       #
+       # Note that the default implementation is general and correct for any lawful Collections.
+       # It is memory-efficient but relies on `count` so may be CPU-inefficient for some kind of collections.
+       fun has_exactly(other: Collection[E]): Bool
+       do
+               if length != other.length then return false
+               for e in self do if self.count(e) != other.count(e) then return false
+               return true
+       end
 end
 
 # Instances of the Iterator class generates a series of elements, one at a time.
@@ -149,30 +184,43 @@ interface Iterator[E]
 
        # Is there a current item ?
        fun is_ok: Bool is abstract
+
+       # Iterate over `self`
+       fun iterator: Iterator[E] do return self
+
+       # Post-iteration hook.
+       #
+       # Used to inform `self` that the iteration is over.
+       # Specific iterators can use this to free some resources.
+       #
+       # Is automatically invoked at the end of `for` structures.
+       #
+       # Do nothing by default.
+       fun finish do end
 end
 
 # A collection that contains only one item.
 #
 # Used to pass arguments by reference.
 #
-# Also used when one want to give asingle element when a full
+# Also used when one want to give a single element when a full
 # collection is expected
 class Container[E]
        super Collection[E]
 
-       redef fun first do return _item
+       redef fun first do return item
 
        redef fun is_empty do return false
 
        redef fun length do return 1
 
-       redef fun has(an_item) do return _item == an_item
+       redef fun has(an_item) do return item == an_item
 
-       redef fun has_only(an_item) do return _item == an_item
+       redef fun has_only(an_item) do return item == an_item
 
        redef fun count(an_item)
        do
-               if _item == an_item then
+               if item == an_item then
                        return 1
                else
                        return 0
@@ -181,25 +229,20 @@ class Container[E]
 
        redef fun iterator do return new ContainerIterator[E](self)
 
-       # Create a new instance with a given initial value.
-       init(e: E) do _item = e
-
        # The stored item
-       readable writable var _item: E
+       var item: E is writable
 end
 
 # This iterator is quite stupid since it is used for only one item.
-class ContainerIterator[E]
+private class ContainerIterator[E]
        super Iterator[E]
        redef fun item do return _container.item
 
-       redef fun next do _is_ok = false
-
-       init(c: Container[E]) do _container = c
+       redef fun next do is_ok = false
 
-       redef readable var _is_ok: Bool = true
+       redef var is_ok: Bool = true
 
-       var _container: Container[E]
+       var container: Container[E]
 end
 
 # Items can be removed from this collection
@@ -246,7 +289,7 @@ interface SimpleCollection[E]
 
        # Add each item of `coll`.
        #     var a = [1,2]
-       #     a.add_all [3..5]
+       #     a.add_all([3..5])
        #     assert a.has(4)  == true
        #     assert a.has(10) == false
        fun add_all(coll: Collection[E]) do for i in coll do add(i)
@@ -262,7 +305,7 @@ end
 #      # ...
 #      s.add(a)
 #      assert s.has(b)      ==  true
-interface Set[E: Object]
+interface Set[E]
        super SimpleCollection[E]
 
        redef fun has_only(item)
@@ -301,14 +344,40 @@ interface Set[E: Object]
        # 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
+
+       # Returns the union of this set with the `other` set
+       fun union(other: Set[E]): Set[E]
+       do
+               var nhs = new_set
+               nhs.add_all self
+               nhs.add_all other
+               return nhs
+       end
+
+       # Returns the intersection of this set with the `other` set
+       fun intersection(other: Set[E]): Set[E]
+       do
+               var nhs = new_set
+               for v in self do if other.has(v) then nhs.add(v)
+               return nhs
+       end
+
+       # Returns a new instance of `Set`.
+       #
+       # Depends on the subclass, mainly used for copy services
+       # like `union` or `intersection`.
+       protected fun new_set: Set[E] is abstract
 end
 
 # MapRead are abstract associative collections: `key` -> `item`.
-interface MapRead[K: Object, E]
+interface MapRead[K, V]
        # Get the item at `key`
        #
        #     var x = new HashMap[String, Int]
@@ -318,7 +387,7 @@ interface MapRead[K: Object, E]
        #
        # 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.
        #
@@ -327,8 +396,8 @@ interface MapRead[K: Object, E]
        #     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
@@ -341,17 +410,24 @@ interface MapRead[K: Object, E]
        #     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`
+       # Is there an item associated with `key`?
+       #
+       #     var x = new HashMap[String, Int]
+       #     x["four"] = 4
+       #     assert x.has_key("four") == true
+       #     assert x.has_key("five") == false
+       #
+       # By default it is a synonymous to `keys.has` but could be redefined with a direct implementation.
        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;
@@ -361,7 +437,7 @@ interface MapRead[K: Object, E]
        #     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;
@@ -396,7 +472,31 @@ interface MapRead[K: Object, E]
        #
        # 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
+
+       # Does `self` and `other` have the same keys associated with the same values?
+       #
+       # ~~~
+       # var a = new HashMap[String, Int]
+       # var b = new ArrayMap[Object, Numeric]
+       # assert a == b
+       # a["one"] = 1
+       # assert a != b
+       # b["one"] = 1
+       # assert a == b
+       # b["one"] = 2
+       # assert a != b
+       # ~~~
+       redef fun ==(other)
+       do
+               if not other isa MapRead[nullable Object, nullable Object] then return false
+               if other.length != self.length then return false
+               for k, v in self do
+                       if not other.has_key(k) then return false
+                       if other[k] != v then return false
+               end
+               return true
+       end
 end
 
 # Maps are associative collections: `key` -> `item`.
@@ -423,8 +523,8 @@ end
 #     assert map.values.has(1)      ==  true
 #     assert map.values.has(3)      ==  false
 #
-interface Map[K: Object, E]
-       super MapRead[K, E]
+interface Map[K, V]
+       super MapRead[K, V]
 
        # Set the `value` at `key`.
        #
@@ -434,14 +534,14 @@ interface Map[K: Object, E]
        #     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.
@@ -458,7 +558,7 @@ interface Map[K: Object, E]
        #     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
@@ -472,21 +572,21 @@ interface Map[K: Object, E]
        #     var x = new HashMap[String, Int]
        #     x["four"] = 4
        #     x.clear
-       #     x.keys.has("four") == false
+       #     assert x.keys.has("four") == false
        #
        # 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, 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`.
@@ -501,28 +601,38 @@ interface MapIterator[K: Object, E]
 
        # Set a new `item` at `key`.
        #fun item=(item: E) is abstract
+
+       # Post-iteration hook.
+       #
+       # Used to inform `self` that the iteration is over.
+       # Specific iterators can use this to free some resources.
+       #
+       # Is automatically invoked at the end of `for` structures.
+       #
+       # Do nothing by default.
+       fun finish do end
 end
 
 # Iterator on a 'keys' point of view of a map
-class MapKeysIterator[K: Object, V]
+class MapKeysIterator[K, V]
        super Iterator[K]
        # The original iterator
-       var iterator: MapIterator[K, V]
+       var original_iterator: MapIterator[K, V]
 
-       redef fun is_ok do return self.iterator.is_ok
-       redef fun next do self.iterator.next
-       redef fun item do return self.iterator.key
+       redef fun is_ok do return self.original_iterator.is_ok
+       redef fun next do self.original_iterator.next
+       redef fun item do return self.original_iterator.key
 end
 
 # Iterator on a 'values' point of view of a map
-class MapValuesIterator[K: Object, V]
+class MapValuesIterator[K, V]
        super Iterator[V]
        # The original iterator
-       var iterator: MapIterator[K, V]
+       var original_iterator: MapIterator[K, V]
 
-       redef fun is_ok do return self.iterator.is_ok
-       redef fun next do self.iterator.next
-       redef fun item do return self.iterator.item
+       redef fun is_ok do return self.original_iterator.is_ok
+       redef fun next do self.original_iterator.next
+       redef fun item do return self.original_iterator.item
 end
 
 # Sequences are indexed collections.
@@ -602,7 +712,7 @@ interface SequenceRead[E]
                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
@@ -631,8 +741,6 @@ interface SequenceRead[E]
                return res
        end
 
-       redef fun iterator: IndexedIterator[E] is abstract
-
        # Two sequences are equals if they have the same items in the same order.
        #
        #     var a = new List[Int]
@@ -657,8 +765,50 @@ interface SequenceRead[E]
        # 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
+
+       redef fun iterator: IndexedIterator[E] is abstract
+
+       # Gets a new Iterator starting at position `pos`
+       #
+       #     var iter = [10,20,30,40,50].iterator_from(2)
+       #     assert iter.to_a == [30, 40, 50]
+       fun iterator_from(pos: Int): IndexedIterator[E]
+       do
+               var res = iterator
+               while pos > 0 and res.is_ok do
+                       res.next
+                       pos -= 1
+               end
+               return res
+       end
+
+       # Gets an iterator starting at the end and going backwards
+       #
+       #     var reviter = [1,2,3].reverse_iterator
+       #     assert reviter.to_a == [3,2,1]
+       fun reverse_iterator: IndexedIterator[E] is abstract
+
+       # Gets an iterator on the chars of self starting from `pos`
+       #
+       #     var reviter = [10,20,30,40,50].reverse_iterator_from(2)
+       #     assert reviter.to_a == [30,20,10]
+       fun reverse_iterator_from(pos: Int): IndexedIterator[E]
+       do
+               var res = reverse_iterator
+               while pos > 0 and res.is_ok do
+                       res.next
+                       pos -= 1
+               end
                return res
        end
 end
@@ -716,7 +866,9 @@ interface Sequence[E]
        #     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.
        #
@@ -736,6 +888,15 @@ interface Sequence[E]
        #     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.
        #
@@ -768,10 +929,30 @@ interface Sequence[E]
        #     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]
@@ -790,11 +971,19 @@ interface IndexedIterator[E]
 end
 
 # Associative arrays that internally uses couples to represent each (key, value) pairs.
-interface CoupleMap[K: Object, E]
-       super Map[K, E]
+# This is an helper class that some specific implementation of Map may implements.
+interface CoupleMap[K, 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,V]] is abstract
+
+       redef fun iterator do return new CoupleMapIterator[K,V](couple_iterator)
 
        redef fun [](key)
        do
@@ -805,13 +994,15 @@ interface CoupleMap[K: Object, E]
                        return c.second
                end
        end
+
+       redef fun has_key(key) do return couple_at(key) != null
 end
 
 # Iterator on CoupleMap
 #
-# Actually is is a wrapper around an iterator of the internal array of the map.
-class CoupleMapIterator[K: Object, E]
-       super MapIterator[K, E]
+# Actually it is a wrapper around an iterator of the internal array of the map.
+private class CoupleMapIterator[K, V]
+       super MapIterator[K, V]
        redef fun item do return _iter.item.second
        
        #redef fun item=(e) do _iter.item.second = e
@@ -825,9 +1016,7 @@ class CoupleMapIterator[K: Object, E]
                _iter.next
        end
 
-       var _iter: Iterator[Couple[K,E]]
-
-       init(i: Iterator[Couple[K,E]]) do _iter = i
+       var iter: Iterator[Couple[K,V]]
 end
 
 # Some tools ###################################################################
@@ -836,15 +1025,8 @@ end
 class Couple[F, S]
 
        # The first element of the couple.
-       readable writable var _first: F
+       var first: F is writable
 
        # The second element of the couple.
-       readable writable var _second: S
-
-       # Create a new instance with a first and a second object.
-       init(f: F, s: S)
-       do
-               _first = f
-               _second = s
-       end
+       var second: S is writable
 end