lib: remove remaining declaration of old-style attributes.
[nit.git] / lib / standard / collection / abstract_collection.nit
index f693ac1..9d37e15 100644 (file)
@@ -163,19 +163,19 @@ end
 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
@@ -185,10 +185,10 @@ 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
+       init(e: E) do item = e
 
        # The stored item
-       readable writable var _item: E
+       var item: E writable
 end
 
 # This iterator is quite stupid since it is used for only one item.
@@ -196,13 +196,13 @@ private class ContainerIterator[E]
        super Iterator[E]
        redef fun item do return _container.item
 
-       redef fun next do _is_ok = false
+       redef fun next do is_ok = false
 
        init(c: Container[E]) do _container = c
 
-       redef readable var _is_ok: Bool = true
+       redef var is_ok: Bool = true
 
-       var _container: Container[E]
+       private var container: Container[E]
 end
 
 # Items can be removed from this collection
@@ -249,7 +249,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)
@@ -304,8 +304,11 @@ 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
 
@@ -494,7 +497,7 @@ 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
@@ -624,7 +627,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
@@ -653,8 +656,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]
@@ -679,8 +680,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
@@ -738,7 +781,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.
        #
@@ -758,6 +803,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.
        #
@@ -790,10 +844,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]
@@ -855,7 +929,7 @@ private class CoupleMapIterator[K: Object, E]
                _iter.next
        end
 
-       var _iter: Iterator[Couple[K,E]]
+       private var iter: Iterator[Couple[K,E]]
 
        init(i: Iterator[Couple[K,E]]) do _iter = i
 end
@@ -866,15 +940,15 @@ end
 class Couple[F, S]
 
        # The first element of the couple.
-       readable writable var _first: F
+       var first: F writable
 
        # The second element of the couple.
-       readable writable var _second: S
+       var second: S writable
 
        # Create a new instance with a first and a second object.
        init(f: F, s: S)
        do
-               _first = f
-               _second = s
+               first = f
+               second = s
        end
 end