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
)
73 if self[i
] == item
then
81 redef fun last_index_of_from
(item
, pos
)
85 if self[i
] == item
then
94 # Return a new array that is the reverse of `self`
96 # assert [1,2,3].reversed == [3, 2, 1]
97 fun reversed
: Array[E
]
100 var result
= new Array[E
].with_capacity
(cmp
)
103 result
.add
(self[cmp
])
108 # Copy a portion of `self` to an other array.
110 # var a = [1, 2, 3, 4]
111 # var b = [10, 20, 30, 40, 50]
112 # a.copy_to(1, 2, b, 2)
113 # assert b == [10, 20, 2, 3, 50]
114 fun copy_to
(start
: Int, len
: Int, dest
: AbstractArray[E
], new_start
: Int)
120 dest
[new_start
+i
] = self[start
+i
]
130 if e
!= null then e
.output
135 redef fun iterator
: ArrayIterator[E
] do
136 var res
= _free_iterator
137 if res
== null then return new ArrayIterator[E
](self)
139 _free_iterator
= null
143 # An old iterator, free to reuse.
144 # Once an iterator is `finish`, it become reusable.
145 # Since some arrays are iterated a lot, this avoid most of the
146 # continuous allocation/garbage-collection of the needed iterators.
147 private var free_iterator
: nullable ArrayIterator[E
] = null
149 redef fun reverse_iterator
do return new ArrayReverseIterator[E
](self)
151 # Returns a sub-array containing `count` elements starting from `from`.
153 # For most cases (see other case bellow),
154 # the first element is `from` and
155 # the last element is `from+count-1`.
158 # var a = [10, 20, 30, 40, 50]
159 # assert a.sub(0, 3) == [10, 20, 30]
160 # assert a.sub(3, 2) == [40, 50]
161 # assert a.sub(3, 1) == [40]
164 # If `count` is 0 or negative then an empty array is returned
167 # assert a.sub(3,0).is_empty
168 # assert a.sub(3,-1).is_empty
171 # If `from < 0` or `from+count>length` then inexistent elements are ignored.
172 # In this case the length of the result is lower than count.
175 # assert a.sub(-2, 4) == [10, 20]
176 # assert a.sub(4, 99) == [50]
177 # assert a.sub(-9, 99) == [10,20,30,40,50]
178 # assert a.sub(-99, 9).is_empty
180 fun sub
(from
: Int, count
: Int): Array[E
] do
188 var to
= from
+ count
192 var res
= new Array[E
].with_capacity
(to
- from
)
201 # Resizable one dimension array of objects.
202 abstract class AbstractArray[E
]
203 super AbstractArrayRead[E
]
206 # Force the capacity to be at least `cap`.
207 # The capacity of the array is an internal information.
208 # However, this method can be used to prepare a large amount of add
209 fun enlarge
(cap
: Int) is abstract
211 redef fun push
(item
) do add
(item
)
215 assert not_empty
: not is_empty
223 assert not_empty
: not is_empty
235 redef fun unshift
(item
)
245 redef fun insert
(item
: E
, pos
: Int)
248 copy_to
(pos
, length-pos
, self, pos
+ 1)
252 redef fun insert_all
(coll
, pos
)
255 if l
== 0 then return
258 copy_to
(pos
, length-pos-l
, self, pos
+ l
)
265 redef fun add
(item
) do self[length
] = item
267 redef fun clear
do _length
= 0
269 redef fun remove
(item
) do remove_at
(index_of
(item
))
271 redef fun remove_all
(item
)
273 var i
= index_of
(item
)
276 i
= index_of_from
(item
, i
)
280 redef fun remove_at
(i
)
283 if i
>= 0 and i
< l
then
293 # Invert two elements in the array
295 # var a = [10, 20, 30, 40]
297 # assert a == [10, 40, 30, 20]
298 fun swap_at
(a
: Int,b
: Int)
306 # Resizable one dimension array of objects.
308 # Arrays have a literal representation.
310 # var a = [12, 32, 8]
311 # # is equivalent with:
312 # var b = new Array[Int]
318 super AbstractArray[E
]
323 assert index
: index
>= 0 and index
< _length
327 redef fun []=(index
, item
)
329 assert index
: index
>= 0 and index
< _length
+ 1
330 if _capacity
<= index
then
333 if _length
<= index
then
342 if _capacity
<= l
then
349 # Slight optimization for arrays
350 redef fun add_all
(items
)
353 var nl
= l
+ items
.length
354 if _capacity
< nl
then
358 if items
isa Array[E
] then
361 _items
[l
] = items
._items
[k
]
375 redef fun enlarge
(cap
)
378 if cap
<= c
then return
379 while c
<= cap
do c
= c
* 2 + 2
380 var a
= new NativeArray[E
](c
)
381 if _capacity
> 0 then _items
.copy_to
(a
, _length
)
386 # Create an empty array.
393 # Create an array from a collection.
394 init from
(items
: Collection[E
]) do
395 with_capacity
(items
.length
)
399 # Create an array with some `objects`.
400 init with_items
(objects
: E
...)
402 _items
= objects
._items
403 _capacity
= objects
._capacity
404 _length
= objects
.length
407 # Create an empty array with a given capacity.
408 init with_capacity
(cap
: Int)
410 assert positive
: cap
>= 0
411 _items
= new NativeArray[E
](cap
)
416 # Create an array of `count` elements
417 init filled_with
(value
: E
, count
: Int)
419 assert positive
: count
>= 0
420 _items
= new NativeArray[E
](count
)
430 # Create a array filled with a given native array.
431 init with_native
(nat
: NativeArray[E
], size
: Int)
433 assert positive
: size
>= 0
439 # The internal storage.
440 private var items
: nullable NativeArray[E
] = null
442 # The size of `_items`.
443 private var capacity
: Int = 0
447 if not o
isa Array[nullable Object] then return super
448 # Efficient implementation
450 if l
!= o
.length
then return false
455 if it
[i
] != oit
[i
] then return false
461 # Shallow clone of `self`
473 # Note that the clone is shallow and elements are shared between `self` and the result.
482 redef fun clone
do return to_a
484 # Concatenation of arrays.
486 # Returns a new array built by concatenating `self` and `other` together.
491 # assert a3 == [1,2,3,4,5,6]
493 # Because a new array is always created, future modification on `self` and `other`
494 # does not impact the previously computed result.
498 # assert a3 == [1,2,3,4,5,6] # unchanged
499 # assert a1 + a2 == [1,2,3,30,4,5,6,60]
500 fun +(other
: Array[E
]): Array[E
]
502 var res
= new Array[E
].with_capacity
(length
+ other
.length
)
508 # Repetition of arrays.
510 # returns a new array built by concatenating `self` `repeat` times.
513 # assert (a * 0).is_empty
514 # assert a * 1 == [1,2,3]
515 # assert a * 2 == [1,2,3,1,2,3]
516 # assert (a * 10).length == 30
517 fun *(repeat
: Int): Array[E
]
520 var res
= new Array[E
].with_capacity
(length
* repeat
)
529 # An `Iterator` on `AbstractArray`
530 private class ArrayIterator[E
]
531 super IndexedIterator[E
]
533 redef fun item
do return _array
[_index
]
535 # redef fun item=(e) do _array[_index] = e
537 redef fun is_ok
do return _index
< _array
.length
539 redef fun next
do _index
+= 1
543 var array
: AbstractArrayRead[E
]
545 redef fun finish
do _array
._free_iterator
= self
548 private class ArrayReverseIterator[E
]
549 super ArrayIterator[E
]
551 redef fun is_ok
do return _index
>= 0
553 redef fun next
do _index
-= 1
557 _index
= _array
.length
- 1
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
908 var d
= a
<=> b
.as(Comparable)
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