Merge: standard: Clean warnings
[nit.git] / lib / standard / collection / array.nit
index 4848257..331a9d9 100644 (file)
@@ -21,8 +21,7 @@ import abstract_collection
 abstract class AbstractArrayRead[E]
        super SequenceRead[E]
 
-       var _length: Int = 0
-       redef fun length do return _length
+       redef var length = 0
 
        redef fun is_empty do return _length == 0
 
@@ -186,6 +185,19 @@ abstract class AbstractArray[E]
                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
@@ -239,7 +251,6 @@ end
 #     assert a == b
 class Array[E]
        super AbstractArray[E]
-       super ArrayCapable[E]
 
        redef fun [](index)
        do
@@ -274,7 +285,7 @@ class Array[E]
                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
@@ -305,7 +316,7 @@ class Array[E]
        init with_capacity(cap: Int)
        do
                assert positive: cap >= 0
-               _items = calloc_array(cap)
+               _items = new NativeArray[E](cap)
                _capacity = cap
                _length = 0
        end
@@ -314,7 +325,7 @@ class Array[E]
        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
@@ -334,14 +345,14 @@ class Array[E]
        end
 
        # The internal storage.
-       var _items: nullable NativeArray[E] = null
+       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`.
-       var _capacity: Int = 0
+       private var capacity: Int = 0
 
        redef fun ==(o)
        do
@@ -358,6 +369,50 @@ class Array[E]
                end
                return true
        end
+
+       # 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
+               end
+               return res
+       end
 end
 
 # An `Iterator` on `AbstractArray`
@@ -372,15 +427,9 @@ private class ArrayIterator[E]
 
        redef fun next do _index += 1
 
-       init(a: AbstractArrayRead[E])
-       do
-               _array = a
-               _index = 0
-       end
+       redef var index = 0
 
-       var _index: Int = 0
-       redef fun index do return _index
-       var _array: AbstractArrayRead[E]
+       var array: AbstractArrayRead[E]
 end
 
 private class ArrayReverseIterator[E]
@@ -390,10 +439,9 @@ private class ArrayReverseIterator[E]
 
        redef fun next do _index -= 1
 
-       init(a: AbstractArrayRead[E])
+       init
        do
-               _array = a
-               _index = a.length - 1
+               _index = _array.length - 1
        end
 end
 
@@ -404,7 +452,7 @@ class ArraySet[E: Object]
        super Set[E]
 
        # The stored elements.
-       var _array: Array[E]
+       private var array: Array[E] is noinit
 
        redef fun has(e) do return _array.has(e)
 
@@ -460,9 +508,7 @@ private class ArraySetIterator[E: Object]
 
        redef fun item: E do return _iter.item
 
-       init(iter: ArrayIterator[E]) do _iter = iter
-
-       var _iter: ArrayIterator[E]
+       var iter: ArrayIterator[E]
 end
 
 
@@ -518,7 +564,7 @@ class ArrayMap[K: Object, E]
        end
 
        # Internal storage.
-       var _items = new Array[Couple[K,E]]
+       private var items = new Array[Couple[K,E]]
 
        # fast remove the ith element of the array
        private fun remove_at_index(i: Int)
@@ -528,7 +574,7 @@ class ArrayMap[K: Object, E]
        end
 
        # The last positive result given by a index(1) call
-       var _last_index: Int = 0
+       private var last_index: Int = 0
 
        # Where is the `key` in `_item`?
        # return -1 if not found
@@ -722,22 +768,24 @@ end
 
 # 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.
 universal NativeArray[E]
+       # Creates a new NativeArray of capacity `length`
+       new(length: Int) is intern
        # The length of the array
        fun length: Int is intern
        # Use `self` to initialize a standard Nit Array.
        fun to_a: Array[E] do return new Array[E].with_native(self, length)
+
+       # Get item at `index`.
        fun [](index: Int): E is intern
+
+       # Set `item` at `index`.
        fun []=(index: Int, item: E) is intern
+
+       # Copy `length` items to `dest`.
        fun copy_to(dest: NativeArray[E], length: Int) is intern
        #fun =(o: NativeArray[E]): Bool is intern
        #fun !=(o: NativeArray[E]): Bool is intern