- var _items: nullable NativeArray[E] = null
-
- # Do not use this method
- # FIXME: Remove it once modules can intrude non local modules
- fun intern_items: NativeArray[E] do return _items.as(not null)
-
- # The size of `_items'.
- var _capacity: Int = 0
-
- # Sort the array using the !cmp function.
- fun sort
- !cmp(e1,e2: E): Int
- do
- sub_sort(0, length-1) !cmp(x,y) = cmp(x, y)
- end
-
- # Sort `array' between `from' and `to' indices
- private fun sub_sort(from: Int, to: Int)
- !cmp(e1,e2: E): Int
- do
- if from >= to then
- return
- else if from + 7 < to then
- var pivot = self[from]
- var i = from
- var j = to
- while j > i do
- while i <= to and cmp(self[i], pivot) <= 0 do i += 1
- while j > i and cmp(self[j], pivot) >= 0 do j -= 1
- if j > i then
- var t = self[i]
- self[i] = self[j]
- self[j] = t
- end
- end
- self[from] = self[i-1]
- self[i-1] = pivot
- sub_sort(from, i-2) !cmp(x,y) = cmp(x, y)
- sub_sort(i, to) !cmp(x,y) = cmp(x, y)
- else
- var i = from
- while i < to do
- var min = i
- var min_v = self[i]
- var j = i
- while j <= to do
- if cmp(min_v, self[j]) > 0 then
- min = j
- min_v = self[j]
- end
- j += 1
- end
- if min != i then
- self[min] = self[i]
- self[i] = min_v
- end
- i += 1
- end
+ private var items: nullable NativeArray[E] = null
+
+ # The size of `_items`.
+ private var capacity: Int = 0
+
+ redef fun ==(o)
+ do
+ if not o isa Array[nullable Object] then return super
+ # Efficient implementation
+ var l = length
+ if l != o.length then return false
+ var i = 0
+ var it = _items
+ var oit = o._items
+ while i < l do
+ if it[i] != oit[i] then return false
+ i += 1
+ end
+ return true
+ end
+
+ # Shallow clone of `self`
+ #
+ # ~~~
+ # var a = [1,2,3]
+ # var b = a.clone
+ # assert a == b
+ # a.add 4
+ # assert a != b
+ # b.add 4
+ # assert a == b
+ # ~~~
+ #
+ # Note that the clone is shallow and elements are shared between `self` and the result.
+ #
+ # ~~~
+ # var aa = [a]
+ # var bb = aa.clone
+ # assert aa == bb
+ # aa.first.add 5
+ # assert aa == bb
+ # ~~~
+ redef fun clone do return to_a
+
+ # Concatenation of arrays.
+ #
+ # Returns a new array built by concatenating `self` and `other` together.
+ #
+ # var a1 = [1,2,3]
+ # var a2 = [4,5,6]
+ # var a3 = a1 + a2
+ # assert a3 == [1,2,3,4,5,6]
+ #
+ # Because a new array is always created, future modification on `self` and `other`
+ # does not impact the previously computed result.
+ #
+ # a1.add(30)
+ # a2.add(60)
+ # assert a3 == [1,2,3,4,5,6] # unchanged
+ # assert a1 + a2 == [1,2,3,30,4,5,6,60]
+ fun +(other: Array[E]): Array[E]
+ do
+ var res = new Array[E].with_capacity(length + other.length)
+ res.append(self)
+ res.append(other)
+ return res
+ end
+
+ # Repetition of arrays.
+ #
+ # returns a new array built by concatenating `self` `repeat` times.
+ #
+ # var a = [1,2,3]
+ # assert (a * 0).is_empty
+ # assert a * 1 == [1,2,3]
+ # assert a * 2 == [1,2,3,1,2,3]
+ # assert (a * 10).length == 30
+ fun *(repeat: Int): Array[E]
+ do
+ assert repeat >= 0
+ var res = new Array[E].with_capacity(length * repeat)
+ while repeat > 0 do
+ res.add_all(self)
+ repeat -= 1