core :: Sequence :: defaultinit
# Sequence are indexed collection.
# The first item is 0. The last is `length-1`.
interface Sequence[E]
super SequenceRead[E]
super SimpleCollection[E]
# Set the first item.
# Is equivalent with `self[0] = item`.
#
# var a = [1,2,3]
# a.first = 10
# assert a == [10,2,3]
fun first=(item: E)
do self[0] = item end
# Set the last item.
# Is equivalent with `self[length-1] = item`.
#
# var a = [1,2,3]
# a.last = 10
# assert a == [1,2,10]
#
# If the sequence is empty, `last=` is equivalent with `self[0]=` (thus with `first=`)
#
# var b = new Array[Int]
# b.last = 10
# assert b == [10]
fun last=(item: E)
do
var l = length
if l > 0 then
self[l-1] = item
else
self[0] = item
end
end
# A synonym of `push`
redef fun add(e) do push(e)
# Add an item after the last one.
#
# var a = [1,2,3]
# a.push(10)
# a.push(20)
# assert a == [1,2,3,10,20]
fun push(e: E) is abstract
# Add each item of `coll` after the last.
#
# var a = [1,2,3]
# a.append([7..9])
# assert a == [1,2,3,7,8,9]
#
# Alias of `add_all`
fun append(coll: Collection[E]) do add_all(coll)
# Remove the last item.
#
# var a = [1,2,3]
# assert a.pop == 3
# assert a.pop == 2
# assert a == [1]
#
# REQUIRE `not is_empty`
fun pop: E is abstract
# Add an item before the first one.
#
# var a = [1,2,3]
# a.unshift(10)
# a.unshift(20)
# 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.
#
# var a = [1,2,3]
# assert a.shift == 1
# assert a.shift == 2
# assert a == [3]
#
# REQUIRE `not is_empty`
fun shift: E is abstract
# Set the `item` at `index`.
#
# var a = [10,20,30]
# a[1] = 200
# assert a == [10,200,30]
#
# like with `[]`, index should be between `0` and `length-1`
# However, if `index==length`, `[]=` works like `push`.
#
# a[3] = 400
# assert a == [10,200,30,400]
#
# REQUIRE `index >= 0 and index <= length`
fun []=(index: Int, item: E) is abstract
# Set the index-th element but wrap
#
# Whereas `self[]=` requires the index to exists, the `modulo` accessor automatically
# wraps overbound and underbouds indexes.
#
# ~~~
# var a = [10,20,30]
# a.modulo(1) = 200
# a.modulo(3) = 100
# a.modulo(-1) = 300
# a.modulo(-10) = 301
# assert a == [100, 200, 301]
# ~~~
#
# REQUIRE `not_empty`
# ENSURE `self[modulo_index(index)] == value`
fun modulo=(index: Int, value: E) do self[modulo_index(index)] = value
# Insert an element at a given position, following elements are shifted.
#
# var a = [10, 20, 30, 40]
# a.insert(100, 2)
# assert a == [10, 20, 100, 30, 40]
#
# 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]
# a.remove_at(1)
# assert a == [10,30]
#
# REQUIRE `index >= 0 and index < length`
fun remove_at(index: Int) is abstract
# Rotates the elements of self once to the left
#
# ~~~nit
# var a = [12, 23, 34, 45]
# a.rotate_left
# assert a == [23, 34, 45, 12]
# ~~~
fun rotate_left do
var fst = shift
push fst
end
# Rotates the elements of self once to the right
#
# ~~~nit
# var a = [12, 23, 34, 45]
# a.rotate_right
# assert a == [45, 12, 23, 34]
# ~~~
fun rotate_right do
var lst = pop
unshift lst
end
end
lib/core/collection/abstract_collection.nit:1072,1--1262,3