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
: E
): Int do return last_index_of_from
(item
, length-1
)
68 redef fun index_of_from
(item
: E
, pos
: Int): Int
73 if self[i
] == item
then
81 redef fun last_index_of_from
(item
: E
, pos
: Int): Int
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)
152 # Resizable one dimension array of objects.
153 abstract class AbstractArray[E
]
154 super AbstractArrayRead[E
]
157 # Force the capacity to be at least `cap`.
158 # The capacity of the array is an internal information.
159 # However, this method can be used to prepare a large amount of add
160 fun enlarge
(cap
: Int) is abstract
162 redef fun push
(item
) do add
(item
)
166 assert not_empty
: not is_empty
174 assert not_empty
: not is_empty
186 redef fun unshift
(item
)
196 redef fun insert
(item
: E
, pos
: Int)
199 copy_to
(pos
, length-pos
, self, pos
+ 1)
203 redef fun insert_all
(coll
, pos
)
206 if l
== 0 then return
209 copy_to
(pos
, length-pos-l
, self, pos
+ l
)
216 redef fun add
(item
) do self[length
] = item
218 redef fun clear
do _length
= 0
220 redef fun remove
(item
) do remove_at
(index_of
(item
))
222 redef fun remove_all
(item
)
224 var i
= index_of
(item
)
227 i
= index_of_from
(item
, i
)
231 redef fun remove_at
(i
)
234 if i
>= 0 and i
< l
then
244 # Invert two elements in the array
246 # var a = [10, 20, 30, 40]
248 # assert a == [10, 40, 30, 20]
249 fun swap_at
(a
: Int,b
: Int)
257 # Resizable one dimension array of objects.
259 # Arrays have a literal representation.
261 # var a = [12, 32, 8]
262 # # is equivalent with:
263 # var b = new Array[Int]
269 super AbstractArray[E
]
274 assert index
: index
>= 0 and index
< _length
278 redef fun []=(index
, item
)
280 assert index
: index
>= 0 and index
< _length
+ 1
281 if _capacity
<= index
then
284 if _length
<= index
then
293 if _capacity
<= l
then
300 # Slight optimization for arrays
301 redef fun add_all
(items
)
304 var nl
= l
+ items
.length
305 if _capacity
< nl
then
309 if items
isa Array[E
] then
312 _items
[l
] = items
._items
[k
]
326 redef fun enlarge
(cap
)
329 if cap
<= c
then return
330 while c
<= cap
do c
= c
* 2 + 2
331 var a
= new NativeArray[E
](c
)
332 if _capacity
> 0 then _items
.copy_to
(a
, _length
)
337 # Create an empty array.
344 # Create an array from a collection.
345 init from
(items
: Collection[E
]) do
346 with_capacity
(items
.length
)
350 # Create an array with some `objects`.
351 init with_items
(objects
: E
...)
353 _items
= objects
._items
354 _capacity
= objects
._capacity
355 _length
= objects
.length
358 # Create an empty array with a given capacity.
359 init with_capacity
(cap
: Int)
361 assert positive
: cap
>= 0
362 _items
= new NativeArray[E
](cap
)
367 # Create an array of `count` elements
368 init filled_with
(value
: E
, count
: Int)
370 assert positive
: count
>= 0
371 _items
= new NativeArray[E
](count
)
381 # Create a array filled with a given native array.
382 init with_native
(nat
: NativeArray[E
], size
: Int)
384 assert positive
: size
>= 0
390 # The internal storage.
391 private var items
: nullable NativeArray[E
] = null
393 # The size of `_items`.
394 private var capacity
: Int = 0
398 if not o
isa Array[nullable Object] then return super
399 # Efficient implementation
401 if l
!= o
.length
then return false
406 if it
[i
] != oit
[i
] then return false
412 # Shallow clone of `self`
424 # Note that the clone is shallow and elements are shared between `self` and the result.
433 redef fun clone
do return to_a
435 # Concatenation of arrays.
437 # Returns a new array built by concatenating `self` and `other` together.
442 # assert a3 == [1,2,3,4,5,6]
444 # Because a new array is always created, future modification on `self` and `other`
445 # does not impact the previously computed result.
449 # assert a3 == [1,2,3,4,5,6] # unchanged
450 # assert a1 + a2 == [1,2,3,30,4,5,6,60]
451 fun +(other
: Array[E
]): Array[E
]
453 var res
= new Array[E
].with_capacity
(length
+ other
.length
)
459 # Repetition of arrays.
461 # returns a new array built by concatenating `self` `repeat` times.
464 # assert (a * 0).is_empty
465 # assert a * 1 == [1,2,3]
466 # assert a * 2 == [1,2,3,1,2,3]
467 # assert (a * 10).length == 30
468 fun *(repeat
: Int): Array[E
]
471 var res
= new Array[E
].with_capacity
(length
* repeat
)
480 # An `Iterator` on `AbstractArray`
481 private class ArrayIterator[E
]
482 super IndexedIterator[E
]
484 redef fun item
do return _array
[_index
]
486 # redef fun item=(e) do _array[_index] = e
488 redef fun is_ok
do return _index
< _array
.length
490 redef fun next
do _index
+= 1
494 var array
: AbstractArrayRead[E
]
496 redef fun finish
do _array
._free_iterator
= self
499 private class ArrayReverseIterator[E
]
500 super ArrayIterator[E
]
502 redef fun is_ok
do return _index
>= 0
504 redef fun next
do _index
-= 1
508 _index
= _array
.length
- 1
512 # Others collections ##########################################################
514 # A set implemented with an Array.
519 # The stored elements.
520 private var array
: Array[E
] is noinit
522 redef fun has
(e
) do return _array
.has
(e
)
524 redef fun add
(e
) do if not _array
.has
(e
) then _array
.add
(e
)
526 redef fun is_empty
do return _array
.is_empty
528 redef fun length
do return _array
.length
532 assert _array
.length
> 0
536 redef fun remove
(item
)
538 var i
= _array
.index_of
(item
)
539 if i
>= 0 then remove_at
(i
)
542 redef fun remove_all
(item
) do remove
(item
)
544 redef fun clear
do _array
.clear
546 redef fun iterator
do return new ArraySetIterator[E
](_array
.iterator
)
548 # Assume the capacity is at least `cap`.
549 fun enlarge
(cap
: Int) do _array
.enlarge
(cap
)
551 private fun remove_at
(i
: Int)
553 _array
[i
] = _array
.last
557 # Create an empty set
558 init do _array
= new Array[E
]
560 # Create an empty set with a given capacity.
561 init with_capacity
(i
: Int) do _array
= new Array[E
].with_capacity
(i
)
563 redef fun new_set
do return new ArraySet[E
]
565 # Shallow clone of `self`
568 # var a = new ArraySet[Int]
579 # Note that the clone is shallow and keys and values are shared between `self` and the result.
582 # var aa = new ArraySet[Array[Int]]
591 var res
= new ArraySet[E
]
597 # Iterators on sets implemented with arrays.
598 private class ArraySetIterator[E
]
601 redef fun is_ok
do return _iter
.is_ok
603 redef fun next
do _iter
.next
605 redef fun item
: E
do return _iter
.item
607 var iter
: ArrayIterator[E
]
611 # Associative arrays implemented with an array of (key, value) pairs.
613 super CoupleMap[K
, E
]
621 return _items
[i
].second
623 return provide_default_value
(key
)
628 redef fun []=(key
, item
)
632 _items
[i
].second
= item
634 _items
.push
(new Couple[K
,E
](key
, item
))
638 redef var keys
: RemovableCollection[K
] = new ArrayMapKeys[K
, E
](self) is lazy
639 redef var values
: RemovableCollection[E
] = new ArrayMapValues[K
, E
](self) is lazy
642 redef fun length
do return _items
.length
644 redef fun couple_iterator
do return _items
.iterator
646 redef fun is_empty
do return _items
.is_empty
648 redef fun clear
do _items
.clear
650 # Assume the capacity to be at least `cap`.
651 fun enlarge
(cap
: Int) do _items
.enlarge
(cap
)
653 redef fun couple_at
(key
)
664 private var items
= new Array[Couple[K
,E
]]
666 # fast remove the ith element of the array
667 private fun remove_at_index
(i
: Int)
669 _items
[i
] = _items
.last
673 # The last positive result given by a index(1) call
674 private var last_index
: Int = 0
676 # Where is the `key` in `_item`?
677 # return -1 if not found
678 private fun index
(key
: K
): Int
681 if l
< _items
.length
and _items
[l
].first
== key
then return l
684 while i
< _items
.length
do
685 if _items
[i
].first
== key
then
694 # Shallow clone of `self`
697 # var a = new ArrayMap[String,Int]
706 # Note that the clone is shallow and keys and values are shared between `self` and the result.
709 # var aa = new ArrayMap[String, Array[Int]]
718 var res
= new ArrayMap[K
,E
]
719 res
.recover_with
self
724 private class ArrayMapKeys[K
, E
]
725 super RemovableCollection[K
]
727 var map
: ArrayMap[K
, E
]
728 redef fun count
(k
) do if self.has
(k
) then return 1 else return 0
729 redef fun first
do return self.map
._items
.first
.first
730 redef fun has
(k
) do return self.map
.index
(k
) >= 0
731 redef fun has_only
(k
) do return (self.has
(k
) and self.length
== 1) or self.is_empty
732 redef fun is_empty
do return self.map
.is_empty
733 redef fun length
do return self.map
.length
734 redef fun iterator
do return new MapKeysIterator[K
, E
](self.map
.iterator
)
735 redef fun clear
do self.map
.clear
736 redef fun remove
(key
)
738 var i
= self.map
.index
(key
)
739 if i
>= 0 then self.map
.remove_at_index
(i
)
741 redef fun remove_all
(key
) do self.remove
(key
)
744 private class ArrayMapValues[K
, E
]
745 super RemovableCollection[E
]
747 var map
: ArrayMap[K
, E
]
748 redef fun first
do return self.map
._items
.first
.second
749 redef fun is_empty
do return self.map
.is_empty
750 redef fun length
do return self.map
.length
751 redef fun iterator
do return new MapValuesIterator[K
, E
](self.map
.iterator
)
756 for i
in self.map
._items
do if i
.second
== item
then return true
761 redef fun has_only
(item
)
763 for i
in self.map
._items
do if i
.second
!= item
then return false
768 redef fun count
(item
)
771 for i
in self.map
._items
do if i
.second
== item
then nb
+= 1
775 redef fun clear
do self.map
.clear
777 redef fun remove
(item
)
780 var i
= map
._items
.length
- 1
782 if map
._items
[i
].second
== item
then
783 map
.remove_at_index
(i
)
790 redef fun remove_all
(item
)
793 var i
= map
._items
.length
- 1
795 if map
._items
[i
].second
== item
then
796 map
.remove_at_index
(i
)
803 # Comparable array for comparable elements.
805 # For two arrays, if one is a prefix, then it is lower.
808 # var a12 = new ArrayCmp[nullable Int].with_items(1,2)
809 # var a123 = new ArrayCmp[nullable Int].with_items(1,2,3)
813 # Otherwise, the first element just after the longest
814 # common prefix gives the order between the two arrays.
817 # var a124 = new ArrayCmp[nullable Int].with_items(1,2,4)
818 # var a13 = new ArrayCmp[nullable Int].with_items(1,3)
823 # Obviously, two equal arrays are equal.
826 # var b12 = new ArrayCmp[nullable Int].with_items(1,2)
827 # assert (a12 <=> b12) == 0
830 # `null` is considered lower than any other elements.
831 # But is still greater than no element.
834 # var a12n = new ArrayCmp[nullable Int].with_items(1,2,null)
838 class ArrayCmp[E
: nullable Comparable]
841 redef type OTHER: ArrayCmp[E
] is fixed
843 redef fun <(o
) do return (self <=> o
) < 0
853 if l
< ol
then len
= l
else len
= ol
858 if b
== null then return 1
859 var d
= a
<=> b
.as(Comparable)
860 if d
!= 0 then return d
862 if b
!= null then return -1
870 # Others tools ################################################################
872 redef class Iterator[E
]
873 # Interate on `self` and build an array
876 var res
= new Array[E
]
885 redef class Collection[E
]
886 # Build a new array from a collection
889 var res
= new Array[E
].with_capacity
(length
)
895 # Native classes ##############################################################
898 # Access are unchecked and it has a fixed size
899 # Not for public use: may become private.
900 universal NativeArray[E
]
901 # Creates a new NativeArray of capacity `length`
902 new(length
: Int) is intern
903 # The length of the array
904 fun length
: Int is intern
905 # Use `self` to initialize a standard Nit Array.
906 fun to_a
: Array[E
] do return new Array[E
].with_native
(self, length
)
908 # Get item at `index`.
909 fun [](index
: Int): E
is intern
911 # Set `item` at `index`.
912 fun []=(index
: Int, item
: E
) is intern
914 # Copy `length` items to `dest`.
915 fun copy_to
(dest
: NativeArray[E
], length
: Int) is intern
916 #fun =(o: NativeArray[E]): Bool is intern
917 #fun !=(o: NativeArray[E]): Bool is intern