core :: AbstractArray :: defaultinit
# Resizable one dimension array of objects.
abstract class AbstractArray[E]
super AbstractArrayRead[E]
super Sequence[E]
# Force the capacity to be at least `cap`.
# The capacity of the array is an internal information.
# However, this method can be used to prepare a large amount of add
fun enlarge(cap: Int) is abstract
redef fun push(item) do add(item)
redef fun pop
do
assert not_empty: not is_empty
var r = last
_length -= 1
return r
end
redef fun shift
do
assert not_empty: not is_empty
var r = first
var l = length-1
copy_to(1, l, self, 0)
_length = l
return r
end
redef fun unshift(item)
do
var l = length
if l > 0 then
enlarge(l + 1)
copy_to(0, l, self, 1)
end
self[0] = item
end
redef fun insert(item, pos) do
enlarge(length + 1)
copy_to(pos, length-pos, self, pos + 1)
self[pos] = item
end
redef fun insert_all(coll, pos)
do
var l = coll.length
if l == 0 then return
enlarge(length + l)
_length += l
copy_to(pos, length-pos-l, self, pos + l)
for c in coll do
self[pos] = c
pos += 1
end
end
redef fun add(item) do self[length] = item
redef fun clear do _length = 0
redef fun remove(item) do remove_at(index_of(item))
redef fun remove_all(item)
do
var i = index_of(item)
while i >= 0 do
remove_at(i)
i = index_of_from(item, i)
end
end
redef fun remove_at(i)
do
var l = length
if i >= 0 and i < l then
var j = i + 1
while j < l do
self[j-1] = self[j]
j += 1
end
_length = l - 1
end
end
# Invert two elements in the array
#
# var a = [10, 20, 30, 40]
# a.swap_at(1, 3)
# assert a == [10, 40, 30, 20]
fun swap_at(a: Int,b: Int)
do
var e = self[a]
self[a] = self[b]
self[b] = e
end
end
lib/core/collection/array.nit:206,1--304,3