1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 # Copyright 2008 Floréal Morandat <morandat@lirmm.fr>
6 # This file is free software, which comes along with NIT. This software is
7 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
8 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
9 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
10 # is kept unaltered, and a notification of the changes is added.
11 # You are allowed to redistribute it and sell it, alone or is a part of
14 # This module introduces the standard array structure.
15 # It also implements two other abstract collections : ArrayMap and ArraySet
17 no_warning
"useless-type-test" # to avoid warning with nitc while compiling with c_src
20 import abstract_collection
22 # One dimension array of objects.
23 abstract class AbstractArrayRead[E
]
28 redef fun is_empty
do return _length
== 0
35 if self[i
] == item
then return true
41 redef fun has_only
(item
)
46 if self[i
] != item
then return false
58 if self[i
] == item
then res
+= 1
64 redef fun index_of
(item
) do return index_of_from
(item
, 0)
66 redef fun last_index_of
(item
) do return last_index_of_from
(item
, length-1
)
68 redef fun index_of_from
(item
, pos
) do
72 if self[i
] == item
then
80 redef fun last_index_of_from
(item
, pos
) do
83 if self[i
] == item
then
92 # Return a new array that is the reverse of `self`
94 # assert [1,2,3].reversed == [3, 2, 1]
95 fun reversed
: Array[E
]
98 var result
= new Array[E
].with_capacity
(cmp
)
101 result
.add
(self[cmp
])
106 # Copy a portion of `self` to an other array.
108 # var a = [1, 2, 3, 4]
109 # var b = [10, 20, 30, 40, 50]
110 # a.copy_to(1, 2, b, 2)
111 # assert b == [10, 20, 2, 3, 50]
112 fun copy_to
(start
: Int, len
: Int, dest
: AbstractArray[E
], new_start
: Int)
118 dest
[new_start
+i
] = self[start
+i
]
128 if e
!= null then e
.output
133 redef fun iterator
: IndexedIterator[E
] do
134 var res
= _free_iterator
135 if res
== null then return new ArrayIterator[E
](self)
137 _free_iterator
= null
141 # An old iterator, free to reuse.
142 # Once an iterator is `finish`, it become reusable.
143 # Since some arrays are iterated a lot, this avoid most of the
144 # continuous allocation/garbage-collection of the needed iterators.
145 private var free_iterator
: nullable ArrayIterator[E
] = null
147 redef fun reverse_iterator
do return new ArrayReverseIterator[E
](self)
149 # Returns a sub-array containing `count` elements starting from `from`.
151 # For most cases (see other case bellow),
152 # the first element is `from` and
153 # the last element is `from+count-1`.
156 # var a = [10, 20, 30, 40, 50]
157 # assert a.sub(0, 3) == [10, 20, 30]
158 # assert a.sub(3, 2) == [40, 50]
159 # assert a.sub(3, 1) == [40]
162 # If `count` is 0 or negative then an empty array is returned
165 # assert a.sub(3,0).is_empty
166 # assert a.sub(3,-1).is_empty
169 # If `from < 0` or `from+count>length` then inexistent elements are ignored.
170 # In this case the length of the result is lower than count.
173 # assert a.sub(-2, 4) == [10, 20]
174 # assert a.sub(4, 99) == [50]
175 # assert a.sub(-9, 99) == [10,20,30,40,50]
176 # assert a.sub(-99, 9).is_empty
178 fun sub
(from
: Int, count
: Int): Array[E
] do
186 var to
= from
+ count
190 var res
= new Array[E
].with_capacity
(to
- from
)
199 # Resizable one dimension array of objects.
200 abstract class AbstractArray[E
]
201 super AbstractArrayRead[E
]
204 # Force the capacity to be at least `cap`.
205 # The capacity of the array is an internal information.
206 # However, this method can be used to prepare a large amount of add
207 fun enlarge
(cap
: Int) is abstract
209 redef fun push
(item
) do add
(item
)
213 assert not_empty
: not is_empty
221 assert not_empty
: not is_empty
224 copy_to
(1, l
, self, 0)
229 redef fun unshift
(item
)
234 copy_to
(0, l
, self, 1)
239 redef fun insert
(item
, pos
) do
241 copy_to
(pos
, length-pos
, self, pos
+ 1)
245 redef fun insert_all
(coll
, pos
)
248 if l
== 0 then return
251 copy_to
(pos
, length-pos-l
, self, pos
+ l
)
258 redef fun add
(item
) do self[length
] = item
260 redef fun clear
do _length
= 0
262 redef fun remove
(item
) do remove_at
(index_of
(item
))
264 redef fun remove_all
(item
)
266 var i
= index_of
(item
)
269 i
= index_of_from
(item
, i
)
273 redef fun remove_at
(i
)
276 if i
>= 0 and i
< l
then
286 # Invert two elements in the array
288 # var a = [10, 20, 30, 40]
290 # assert a == [10, 40, 30, 20]
291 fun swap_at
(a
: Int,b
: Int)
299 # Resizable one dimension array of objects.
301 # Arrays have a literal representation.
303 # var a = [12, 32, 8]
304 # # is equivalent with:
305 # var b = new Array[Int]
311 super AbstractArray[E
]
316 assert index
: index
>= 0 and index
< _length
320 redef fun []=(index
, item
)
322 assert index
: index
>= 0 and index
< _length
+ 1
323 if _capacity
<= index
then
326 if _length
<= index
then
335 if _capacity
<= l
then
342 # Slight optimization for arrays
343 redef fun add_all
(items
)
346 var nl
= l
+ items
.length
347 if _capacity
< nl
then
351 if items
isa Array[E
] then
354 _items
[l
] = items
._items
[k
]
368 redef fun enlarge
(cap
)
371 if cap
<= c
then return
372 while c
<= cap
do c
= c
* 2 + 2
373 var a
= new NativeArray[E
](c
)
374 if _capacity
> 0 then _items
.copy_to
(a
, _length
)
379 # Create an empty array.
386 # Create an array from a collection.
387 init from
(items
: Collection[E
]) do
388 with_capacity
(items
.length
)
392 # Create an array with some `objects`.
393 init with_items
(objects
: E
...)
395 _items
= objects
._items
396 _capacity
= objects
._capacity
397 _length
= objects
.length
400 # Create an empty array with a given capacity.
401 init with_capacity
(cap
: Int)
403 assert positive
: cap
>= 0
404 _items
= new NativeArray[E
](cap
)
409 # Create an array of `count` elements
410 init filled_with
(value
: E
, count
: Int)
412 assert positive
: count
>= 0
413 _items
= new NativeArray[E
](count
)
423 # Create a array filled with a given native array.
424 init with_native
(nat
: NativeArray[E
], size
: Int)
426 assert positive
: size
>= 0
432 # The internal storage.
433 private var items
: nullable NativeArray[E
] = null
435 # The size of `_items`.
436 private var capacity
: Int = 0
440 if not o
isa Array[nullable Object] then return super
441 # Efficient implementation
443 if l
!= o
.length
then return false
448 if it
[i
] != oit
[i
] then return false
454 # Shallow clone of `self`
466 # Note that the clone is shallow and elements are shared between `self` and the result.
475 redef fun clone
do return to_a
477 # Concatenation of arrays.
479 # Returns a new array built by concatenating `self` and `other` together.
484 # assert a3 == [1,2,3,4,5,6]
486 # Because a new array is always created, future modification on `self` and `other`
487 # does not impact the previously computed result.
491 # assert a3 == [1,2,3,4,5,6] # unchanged
492 # assert a1 + a2 == [1,2,3,30,4,5,6,60]
493 fun +(other
: Array[E
]): Array[E
]
495 var res
= new Array[E
].with_capacity
(length
+ other
.length
)
501 # Repetition of arrays.
503 # returns a new array built by concatenating `self` `repeat` times.
506 # assert (a * 0).is_empty
507 # assert a * 1 == [1,2,3]
508 # assert a * 2 == [1,2,3,1,2,3]
509 # assert (a * 10).length == 30
510 fun *(repeat
: Int): Array[E
]
513 var res
= new Array[E
].with_capacity
(length
* repeat
)
522 # An `Iterator` on `AbstractArray`
523 private class ArrayIterator[E
]
524 super IndexedIterator[E
]
526 redef fun item
do return _array
[_index
]
528 # redef fun item=(e) do _array[_index] = e
530 redef fun is_ok
do return _index
< _array
.length
532 redef fun next
do _index
+= 1
536 var array
: AbstractArrayRead[E
]
538 redef fun finish
do _array
._free_iterator
= self
541 private class ArrayReverseIterator[E
]
542 super ArrayIterator[E
]
544 redef fun is_ok
do return _index
>= 0
546 redef fun next
do _index
-= 1
550 _index
= _array
.length
- 1
553 # Do not cache `self`
554 redef fun finish
do end
557 # Others collections ##########################################################
559 # A set implemented with an Array.
564 # The stored elements.
565 private var array
: Array[E
] is noinit
567 redef fun has
(e
) do return _array
.has
(e
)
569 redef fun add
(e
) do if not _array
.has
(e
) then _array
.add
(e
)
571 redef fun is_empty
do return _array
.is_empty
573 redef fun length
do return _array
.length
577 assert _array
.length
> 0
581 redef fun remove
(item
)
583 var i
= _array
.index_of
(item
)
584 if i
>= 0 then remove_at
(i
)
587 redef fun remove_all
(item
) do remove
(item
)
589 redef fun clear
do _array
.clear
591 redef fun iterator
do return new ArraySetIterator[E
](_array
.iterator
)
593 # Assume the capacity is at least `cap`.
594 fun enlarge
(cap
: Int) do _array
.enlarge
(cap
)
596 private fun remove_at
(i
: Int)
598 _array
[i
] = _array
.last
602 # Create an empty set
603 init do _array
= new Array[E
]
605 # Create an empty set with a given capacity.
606 init with_capacity
(i
: Int) do _array
= new Array[E
].with_capacity
(i
)
608 redef fun new_set
do return new ArraySet[E
]
610 # Shallow clone of `self`
613 # var a = new ArraySet[Int]
624 # Note that the clone is shallow and keys and values are shared between `self` and the result.
627 # var aa = new ArraySet[Array[Int]]
636 var res
= new ArraySet[E
]
642 # Iterators on sets implemented with arrays.
643 private class ArraySetIterator[E
]
646 redef fun is_ok
do return _iter
.is_ok
648 redef fun next
do _iter
.next
650 redef fun item
: E
do return _iter
.item
652 var iter
: Iterator[E
]
656 # Associative arrays implemented with an array of (key, value) pairs.
658 super CoupleMap[K
, E
]
666 return _items
[i
].second
668 return provide_default_value
(key
)
673 redef fun []=(key
, item
)
677 _items
[i
].second
= item
679 _items
.push
(new Couple[K
,E
](key
, item
))
683 redef var keys
: RemovableCollection[K
] = new ArrayMapKeys[K
, E
](self) is lazy
684 redef var values
: RemovableCollection[E
] = new ArrayMapValues[K
, E
](self) is lazy
687 redef fun length
do return _items
.length
689 redef fun couple_iterator
do return _items
.iterator
691 redef fun is_empty
do return _items
.is_empty
693 redef fun clear
do _items
.clear
695 # Assume the capacity to be at least `cap`.
696 fun enlarge
(cap
: Int) do _items
.enlarge
(cap
)
698 redef fun couple_at
(key
)
709 private var items
= new Array[Couple[K
,E
]]
711 # fast remove the ith element of the array
712 private fun remove_at_index
(i
: Int)
714 _items
[i
] = _items
.last
718 # The last positive result given by a index(1) call
719 private var last_index
: Int = 0
721 # Where is the `key` in `_item`?
722 # return -1 if not found
723 private fun index
(key
: K
): Int
726 if l
< _items
.length
and _items
[l
].first
== key
then return l
729 while i
< _items
.length
do
730 if _items
[i
].first
== key
then
739 # Shallow clone of `self`
742 # var a = new ArrayMap[String,Int]
751 # Note that the clone is shallow and keys and values are shared between `self` and the result.
754 # var aa = new ArrayMap[String, Array[Int]]
763 var res
= new ArrayMap[K
,E
]
764 res
.recover_with
self
769 private class ArrayMapKeys[K
, E
]
770 super RemovableCollection[K
]
772 var map
: ArrayMap[K
, E
]
773 redef fun count
(k
) do if self.has
(k
) then return 1 else return 0
774 redef fun first
do return self.map
._items
.first
.first
775 redef fun has
(k
) do return self.map
.index
(k
) >= 0
776 redef fun has_only
(k
) do return (self.has
(k
) and self.length
== 1) or self.is_empty
777 redef fun is_empty
do return self.map
.is_empty
778 redef fun length
do return self.map
.length
779 redef fun iterator
do return new MapKeysIterator[K
, E
](self.map
.iterator
)
780 redef fun clear
do self.map
.clear
781 redef fun remove
(key
)
783 var i
= self.map
.index
(key
)
784 if i
>= 0 then self.map
.remove_at_index
(i
)
786 redef fun remove_all
(key
) do self.remove
(key
)
789 private class ArrayMapValues[K
, E
]
790 super RemovableCollection[E
]
792 var map
: ArrayMap[K
, E
]
793 redef fun first
do return self.map
._items
.first
.second
794 redef fun is_empty
do return self.map
.is_empty
795 redef fun length
do return self.map
.length
796 redef fun iterator
do return new MapValuesIterator[K
, E
](self.map
.iterator
)
801 for i
in self.map
._items
do if i
.second
== item
then return true
806 redef fun has_only
(item
)
808 for i
in self.map
._items
do if i
.second
!= item
then return false
813 redef fun count
(item
)
816 for i
in self.map
._items
do if i
.second
== item
then nb
+= 1
820 redef fun clear
do self.map
.clear
822 redef fun remove
(item
)
825 var i
= map
._items
.length
- 1
827 if map
._items
[i
].second
== item
then
828 map
.remove_at_index
(i
)
835 redef fun remove_all
(item
)
838 var i
= map
._items
.length
- 1
840 if map
._items
[i
].second
== item
then
841 map
.remove_at_index
(i
)
848 # Comparable array for comparable elements.
850 # For two arrays, if one is a prefix, then it is lower.
853 # var a12 = new ArrayCmp[nullable Int].with_items(1,2)
854 # var a123 = new ArrayCmp[nullable Int].with_items(1,2,3)
858 # Otherwise, the first element just after the longest
859 # common prefix gives the order between the two arrays.
862 # var a124 = new ArrayCmp[nullable Int].with_items(1,2,4)
863 # var a13 = new ArrayCmp[nullable Int].with_items(1,3)
868 # Obviously, two equal arrays are equal.
871 # var b12 = new ArrayCmp[nullable Int].with_items(1,2)
872 # assert (a12 <=> b12) == 0
875 # `null` is considered lower than any other elements.
876 # But is still greater than no element.
879 # var a12n = new ArrayCmp[nullable Int].with_items(1,2,null)
883 class ArrayCmp[E
: nullable Comparable]
886 redef type OTHER: ArrayCmp[E
] is fixed
888 redef fun <(o
) do return (self <=> o
) < 0
898 if l
< ol
then len
= l
else len
= ol
903 if b
== null then return 1
905 if d
!= 0 then return d
907 if b
!= null then return -1
915 # Others tools ################################################################
917 redef class Iterator[E
]
918 # Interate on `self` and build an array
921 var res
= new Array[E
]
930 redef class Collection[E
]
931 # Build a new array from a collection
934 var res
= new Array[E
].with_capacity
(length
)
940 # Native classes ##############################################################
943 # Access are unchecked and it has a fixed size
944 # Not for public use: may become private.
945 universal NativeArray[E
]
946 # Creates a new NativeArray of capacity `length`
947 new(length
: Int) is intern
948 # The length of the array
949 fun length
: Int is intern
950 # Use `self` to initialize a standard Nit Array.
951 fun to_a
: Array[E
] do return new Array[E
].with_native
(self, length
)
953 # Get item at `index`.
954 fun [](index
: Int): E
is intern
956 # Set `item` at `index`.
957 fun []=(index
: Int, item
: E
) is intern
959 # Copy `length` items to `dest`.
960 fun copy_to
(dest
: NativeArray[E
], length
: Int) is intern
961 #fun =(o: NativeArray[E]): Bool is intern
962 #fun !=(o: NativeArray[E]): Bool is intern