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
: ArrayIterator[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
233 redef fun unshift
(item
)
243 redef fun insert
(item
, pos
) do
245 copy_to
(pos
, length-pos
, self, pos
+ 1)
249 redef fun insert_all
(coll
, pos
)
252 if l
== 0 then return
255 copy_to
(pos
, length-pos-l
, self, pos
+ l
)
262 redef fun add
(item
) do self[length
] = item
264 redef fun clear
do _length
= 0
266 redef fun remove
(item
) do remove_at
(index_of
(item
))
268 redef fun remove_all
(item
)
270 var i
= index_of
(item
)
273 i
= index_of_from
(item
, i
)
277 redef fun remove_at
(i
)
280 if i
>= 0 and i
< l
then
290 # Invert two elements in the array
292 # var a = [10, 20, 30, 40]
294 # assert a == [10, 40, 30, 20]
295 fun swap_at
(a
: Int,b
: Int)
303 # Resizable one dimension array of objects.
305 # Arrays have a literal representation.
307 # var a = [12, 32, 8]
308 # # is equivalent with:
309 # var b = new Array[Int]
315 super AbstractArray[E
]
320 assert index
: index
>= 0 and index
< _length
324 redef fun []=(index
, item
)
326 assert index
: index
>= 0 and index
< _length
+ 1
327 if _capacity
<= index
then
330 if _length
<= index
then
339 if _capacity
<= l
then
346 # Slight optimization for arrays
347 redef fun add_all
(items
)
350 var nl
= l
+ items
.length
351 if _capacity
< nl
then
355 if items
isa Array[E
] then
358 _items
[l
] = items
._items
[k
]
372 redef fun enlarge
(cap
)
375 if cap
<= c
then return
376 while c
<= cap
do c
= c
* 2 + 2
377 var a
= new NativeArray[E
](c
)
378 if _capacity
> 0 then _items
.copy_to
(a
, _length
)
383 # Create an empty array.
390 # Create an array from a collection.
391 init from
(items
: Collection[E
]) do
392 with_capacity
(items
.length
)
396 # Create an array with some `objects`.
397 init with_items
(objects
: E
...)
399 _items
= objects
._items
400 _capacity
= objects
._capacity
401 _length
= objects
.length
404 # Create an empty array with a given capacity.
405 init with_capacity
(cap
: Int)
407 assert positive
: cap
>= 0
408 _items
= new NativeArray[E
](cap
)
413 # Create an array of `count` elements
414 init filled_with
(value
: E
, count
: Int)
416 assert positive
: count
>= 0
417 _items
= new NativeArray[E
](count
)
427 # Create a array filled with a given native array.
428 init with_native
(nat
: NativeArray[E
], size
: Int)
430 assert positive
: size
>= 0
436 # The internal storage.
437 private var items
: nullable NativeArray[E
] = null
439 # The size of `_items`.
440 private var capacity
: Int = 0
444 if not o
isa Array[nullable Object] then return super
445 # Efficient implementation
447 if l
!= o
.length
then return false
452 if it
[i
] != oit
[i
] then return false
458 # Shallow clone of `self`
470 # Note that the clone is shallow and elements are shared between `self` and the result.
479 redef fun clone
do return to_a
481 # Concatenation of arrays.
483 # Returns a new array built by concatenating `self` and `other` together.
488 # assert a3 == [1,2,3,4,5,6]
490 # Because a new array is always created, future modification on `self` and `other`
491 # does not impact the previously computed result.
495 # assert a3 == [1,2,3,4,5,6] # unchanged
496 # assert a1 + a2 == [1,2,3,30,4,5,6,60]
497 fun +(other
: Array[E
]): Array[E
]
499 var res
= new Array[E
].with_capacity
(length
+ other
.length
)
505 # Repetition of arrays.
507 # returns a new array built by concatenating `self` `repeat` times.
510 # assert (a * 0).is_empty
511 # assert a * 1 == [1,2,3]
512 # assert a * 2 == [1,2,3,1,2,3]
513 # assert (a * 10).length == 30
514 fun *(repeat
: Int): Array[E
]
517 var res
= new Array[E
].with_capacity
(length
* repeat
)
526 # An `Iterator` on `AbstractArray`
527 private class ArrayIterator[E
]
528 super IndexedIterator[E
]
530 redef fun item
do return _array
[_index
]
532 # redef fun item=(e) do _array[_index] = e
534 redef fun is_ok
do return _index
< _array
.length
536 redef fun next
do _index
+= 1
540 var array
: AbstractArrayRead[E
]
542 redef fun finish
do _array
._free_iterator
= self
545 private class ArrayReverseIterator[E
]
546 super ArrayIterator[E
]
548 redef fun is_ok
do return _index
>= 0
550 redef fun next
do _index
-= 1
554 _index
= _array
.length
- 1
557 # Do not cache `self`
558 redef fun finish
do end
561 # Others collections ##########################################################
563 # A set implemented with an Array.
568 # The stored elements.
569 private var array
: Array[E
] is noinit
571 redef fun has
(e
) do return _array
.has
(e
)
573 redef fun add
(e
) do if not _array
.has
(e
) then _array
.add
(e
)
575 redef fun is_empty
do return _array
.is_empty
577 redef fun length
do return _array
.length
581 assert _array
.length
> 0
585 redef fun remove
(item
)
587 var i
= _array
.index_of
(item
)
588 if i
>= 0 then remove_at
(i
)
591 redef fun remove_all
(item
) do remove
(item
)
593 redef fun clear
do _array
.clear
595 redef fun iterator
do return new ArraySetIterator[E
](_array
.iterator
)
597 # Assume the capacity is at least `cap`.
598 fun enlarge
(cap
: Int) do _array
.enlarge
(cap
)
600 private fun remove_at
(i
: Int)
602 _array
[i
] = _array
.last
606 # Create an empty set
607 init do _array
= new Array[E
]
609 # Create an empty set with a given capacity.
610 init with_capacity
(i
: Int) do _array
= new Array[E
].with_capacity
(i
)
612 redef fun new_set
do return new ArraySet[E
]
614 # Shallow clone of `self`
617 # var a = new ArraySet[Int]
628 # Note that the clone is shallow and keys and values are shared between `self` and the result.
631 # var aa = new ArraySet[Array[Int]]
640 var res
= new ArraySet[E
]
646 # Iterators on sets implemented with arrays.
647 private class ArraySetIterator[E
]
650 redef fun is_ok
do return _iter
.is_ok
652 redef fun next
do _iter
.next
654 redef fun item
: E
do return _iter
.item
656 var iter
: ArrayIterator[E
]
660 # Associative arrays implemented with an array of (key, value) pairs.
662 super CoupleMap[K
, E
]
670 return _items
[i
].second
672 return provide_default_value
(key
)
677 redef fun []=(key
, item
)
681 _items
[i
].second
= item
683 _items
.push
(new Couple[K
,E
](key
, item
))
687 redef var keys
: RemovableCollection[K
] = new ArrayMapKeys[K
, E
](self) is lazy
688 redef var values
: RemovableCollection[E
] = new ArrayMapValues[K
, E
](self) is lazy
691 redef fun length
do return _items
.length
693 redef fun couple_iterator
do return _items
.iterator
695 redef fun is_empty
do return _items
.is_empty
697 redef fun clear
do _items
.clear
699 # Assume the capacity to be at least `cap`.
700 fun enlarge
(cap
: Int) do _items
.enlarge
(cap
)
702 redef fun couple_at
(key
)
713 private var items
= new Array[Couple[K
,E
]]
715 # fast remove the ith element of the array
716 private fun remove_at_index
(i
: Int)
718 _items
[i
] = _items
.last
722 # The last positive result given by a index(1) call
723 private var last_index
: Int = 0
725 # Where is the `key` in `_item`?
726 # return -1 if not found
727 private fun index
(key
: K
): Int
730 if l
< _items
.length
and _items
[l
].first
== key
then return l
733 while i
< _items
.length
do
734 if _items
[i
].first
== key
then
743 # Shallow clone of `self`
746 # var a = new ArrayMap[String,Int]
755 # Note that the clone is shallow and keys and values are shared between `self` and the result.
758 # var aa = new ArrayMap[String, Array[Int]]
767 var res
= new ArrayMap[K
,E
]
768 res
.recover_with
self
773 private class ArrayMapKeys[K
, E
]
774 super RemovableCollection[K
]
776 var map
: ArrayMap[K
, E
]
777 redef fun count
(k
) do if self.has
(k
) then return 1 else return 0
778 redef fun first
do return self.map
._items
.first
.first
779 redef fun has
(k
) do return self.map
.index
(k
) >= 0
780 redef fun has_only
(k
) do return (self.has
(k
) and self.length
== 1) or self.is_empty
781 redef fun is_empty
do return self.map
.is_empty
782 redef fun length
do return self.map
.length
783 redef fun iterator
do return new MapKeysIterator[K
, E
](self.map
.iterator
)
784 redef fun clear
do self.map
.clear
785 redef fun remove
(key
)
787 var i
= self.map
.index
(key
)
788 if i
>= 0 then self.map
.remove_at_index
(i
)
790 redef fun remove_all
(key
) do self.remove
(key
)
793 private class ArrayMapValues[K
, E
]
794 super RemovableCollection[E
]
796 var map
: ArrayMap[K
, E
]
797 redef fun first
do return self.map
._items
.first
.second
798 redef fun is_empty
do return self.map
.is_empty
799 redef fun length
do return self.map
.length
800 redef fun iterator
do return new MapValuesIterator[K
, E
](self.map
.iterator
)
805 for i
in self.map
._items
do if i
.second
== item
then return true
810 redef fun has_only
(item
)
812 for i
in self.map
._items
do if i
.second
!= item
then return false
817 redef fun count
(item
)
820 for i
in self.map
._items
do if i
.second
== item
then nb
+= 1
824 redef fun clear
do self.map
.clear
826 redef fun remove
(item
)
829 var i
= map
._items
.length
- 1
831 if map
._items
[i
].second
== item
then
832 map
.remove_at_index
(i
)
839 redef fun remove_all
(item
)
842 var i
= map
._items
.length
- 1
844 if map
._items
[i
].second
== item
then
845 map
.remove_at_index
(i
)
852 # Comparable array for comparable elements.
854 # For two arrays, if one is a prefix, then it is lower.
857 # var a12 = new ArrayCmp[nullable Int].with_items(1,2)
858 # var a123 = new ArrayCmp[nullable Int].with_items(1,2,3)
862 # Otherwise, the first element just after the longest
863 # common prefix gives the order between the two arrays.
866 # var a124 = new ArrayCmp[nullable Int].with_items(1,2,4)
867 # var a13 = new ArrayCmp[nullable Int].with_items(1,3)
872 # Obviously, two equal arrays are equal.
875 # var b12 = new ArrayCmp[nullable Int].with_items(1,2)
876 # assert (a12 <=> b12) == 0
879 # `null` is considered lower than any other elements.
880 # But is still greater than no element.
883 # var a12n = new ArrayCmp[nullable Int].with_items(1,2,null)
887 class ArrayCmp[E
: nullable Comparable]
890 redef type OTHER: ArrayCmp[E
] is fixed
892 redef fun <(o
) do return (self <=> o
) < 0
902 if l
< ol
then len
= l
else len
= ol
907 if b
== null then return 1
909 if d
!= 0 then return d
911 if b
!= null then return -1
919 # Others tools ################################################################
921 redef class Iterator[E
]
922 # Interate on `self` and build an array
925 var res
= new Array[E
]
934 redef class Collection[E
]
935 # Build a new array from a collection
938 var res
= new Array[E
].with_capacity
(length
)
944 # Native classes ##############################################################
947 # Access are unchecked and it has a fixed size
948 # Not for public use: may become private.
949 universal NativeArray[E
]
950 # Creates a new NativeArray of capacity `length`
951 new(length
: Int) is intern
952 # The length of the array
953 fun length
: Int is intern
954 # Use `self` to initialize a standard Nit Array.
955 fun to_a
: Array[E
] do return new Array[E
].with_native
(self, length
)
957 # Get item at `index`.
958 fun [](index
: Int): E
is intern
960 # Set `item` at `index`.
961 fun []=(index
: Int, item
: E
) is intern
963 # Copy `length` items to `dest`.
964 fun copy_to
(dest
: NativeArray[E
], length
: Int) is intern
965 #fun =(o: NativeArray[E]): Bool is intern
966 #fun !=(o: NativeArray[E]): Bool is intern