# var b = [10, 20, 30, 40, 50]
# a.copy_to(1, 2, b, 2)
# assert b == [10, 20, 2, 3, 50]
- protected fun copy_to(start: Int, len: Int, dest: AbstractArray[E], new_start: Int)
+ fun copy_to(start: Int, len: Int, dest: AbstractArray[E], new_start: Int)
do
# TODO native one
var i = len
# Resizable one dimension array of objects.
#
# Arrays have a literal representation.
+#
# var a = [12, 32, 8]
# # is equivalent with:
# var b = new Array[Int]
# assert a == b
class Array[E]
super AbstractArray[E]
- super ArrayCapable[E]
redef fun [](index)
do
_items[l] = item
end
+ # Slight optimization for arrays
+ redef fun add_all(items)
+ do
+ var l = _length
+ var nl = l + items.length
+ if _capacity < nl then
+ enlarge nl
+ end
+
+ if items isa Array[E] then
+ var k = 0
+ while l < nl do
+ _items[l] = items._items[k]
+ l += 1
+ k += 1
+ end
+ else
+ for item in items do
+ _items[l] = item
+ l += 1
+ end
+ end
+
+ _length = nl
+ end
+
redef fun enlarge(cap)
do
var c = _capacity
if cap <= c then return
while c <= cap do c = c * 2 + 2
- var a = calloc_array(c)
+ var a = new NativeArray[E](c)
if _capacity > 0 then _items.copy_to(a, _length)
_items = a
_capacity = c
init with_capacity(cap: Int)
do
assert positive: cap >= 0
- _items = calloc_array(cap)
+ _items = new NativeArray[E](cap)
_capacity = cap
_length = 0
end
init filled_with(value: E, count: Int)
do
assert positive: count >= 0
- _items = calloc_array(count)
+ _items = new NativeArray[E](count)
_capacity = count
_length = count
var i = 0
# The internal storage.
private 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`.
private var capacity: Int = 0
#
# 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
+ # 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
# Associative arrays implemented with an array of (key, value) pairs.
-class ArrayMap[K: Object, E]
+class ArrayMap[K, E]
super CoupleMap[K, E]
# O(n)
end
end
-private class ArrayMapKeys[K: Object, E]
+private class ArrayMapKeys[K, E]
super RemovableCollection[K]
# The original map
var map: ArrayMap[K, E]
redef fun remove_all(key) do self.remove(key)
end
-private class ArrayMapValues[K: Object, E]
+private class ArrayMapValues[K, E]
super RemovableCollection[E]
# The original map
var map: ArrayMap[K, E]
# Native classes ##############################################################
-# Subclasses of this class can create native arrays
-interface ArrayCapable[E]
- # Get a new array of `size` elements.
- protected fun calloc_array(size: Int): NativeArray[E] is intern
-end
-
# Native Nit array
# Access are unchecked and it has a fixed size
# Not for public use: may become private.