X-Git-Url: http://nitlanguage.org diff --git a/lib/standard/collection/abstract_collection.nit b/lib/standard/collection/abstract_collection.nit index 5501290..7320e83 100644 --- a/lib/standard/collection/abstract_collection.nit +++ b/lib/standard/collection/abstract_collection.nit @@ -188,7 +188,7 @@ class Container[E] 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. @@ -202,7 +202,7 @@ private class ContainerIterator[E] redef var is_ok: Bool = true - var _container: Container[E] + private var container: Container[E] end # Items can be removed from this collection @@ -781,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. # @@ -801,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. # @@ -833,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] @@ -898,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 @@ -909,10 +940,10 @@ end 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)