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)
115 if start
< new_start
then
119 dest
[new_start
+i
] = self[start
+i
]
124 dest
[new_start
+i
] = self[start
+i
]
136 if e
!= null then e
.output
141 redef fun iterator
: IndexedIterator[E
] do
142 var res
= _free_iterator
143 if res
== null then return new ArrayIterator[E
](self)
145 _free_iterator
= null
149 # An old iterator, free to reuse.
150 # Once an iterator is `finish`, it become reusable.
151 # Since some arrays are iterated a lot, this avoid most of the
152 # continuous allocation/garbage-collection of the needed iterators.
153 private var free_iterator
: nullable ArrayIterator[E
] = null
155 redef fun reverse_iterator
do return new ArrayReverseIterator[E
](self)
157 # Returns a sub-array containing `count` elements starting from `from`.
159 # For most cases (see other case bellow),
160 # the first element is `from` and
161 # the last element is `from+count-1`.
164 # var a = [10, 20, 30, 40, 50]
165 # assert a.sub(0, 3) == [10, 20, 30]
166 # assert a.sub(3, 2) == [40, 50]
167 # assert a.sub(3, 1) == [40]
170 # If `count` is 0 or negative then an empty array is returned
173 # assert a.sub(3,0).is_empty
174 # assert a.sub(3,-1).is_empty
177 # If `from < 0` or `from+count>length` then inexistent elements are ignored.
178 # In this case the length of the result is lower than count.
181 # assert a.sub(-2, 4) == [10, 20]
182 # assert a.sub(4, 99) == [50]
183 # assert a.sub(-9, 99) == [10,20,30,40,50]
184 # assert a.sub(-99, 9).is_empty
186 fun sub
(from
: Int, count
: Int): Array[E
] do
194 var to
= from
+ count
198 var res
= new Array[E
].with_capacity
(to
- from
)
207 # Resizable one dimension array of objects.
208 abstract class AbstractArray[E
]
209 super AbstractArrayRead[E
]
212 # Force the capacity to be at least `cap`.
213 # The capacity of the array is an internal information.
214 # However, this method can be used to prepare a large amount of add
215 fun enlarge
(cap
: Int) is abstract
217 redef fun push
(item
) do add
(item
)
221 assert not_empty
: not is_empty
229 assert not_empty
: not is_empty
232 copy_to
(1, l
, self, 0)
237 redef fun unshift
(item
)
242 copy_to
(0, l
, self, 1)
247 redef fun insert
(item
, pos
) do
249 copy_to
(pos
, length-pos
, self, pos
+ 1)
253 redef fun insert_all
(coll
, pos
)
256 if l
== 0 then return
259 copy_to
(pos
, length-pos-l
, self, pos
+ l
)
266 redef fun add
(item
) do self[length
] = item
268 redef fun clear
do _length
= 0
270 redef fun remove
(item
) do remove_at
(index_of
(item
))
272 redef fun remove_all
(item
)
274 var i
= index_of
(item
)
277 i
= index_of_from
(item
, i
)
281 redef fun remove_at
(i
)
284 if i
>= 0 and i
< l
then
294 # Invert two elements in the array
296 # var a = [10, 20, 30, 40]
298 # assert a == [10, 40, 30, 20]
299 fun swap_at
(a
: Int,b
: Int)
307 # Resizable one dimension array of objects.
309 # Arrays have a literal representation.
311 # var a = [12, 32, 8]
312 # # is equivalent with:
313 # var b = new Array[Int]
319 super AbstractArray[E
]
324 assert index
: index
>= 0 and index
< _length
328 redef fun []=(index
, item
)
330 assert index
: index
>= 0 and index
< _length
+ 1
331 if _capacity
<= index
then
334 if _length
<= index
then
343 if _capacity
<= l
then
350 # Slight optimization for arrays
351 redef fun add_all
(items
)
354 var nl
= l
+ items
.length
355 if _capacity
< nl
then
359 if items
isa Array[E
] then
362 _items
[l
] = items
._items
[k
]
376 redef fun enlarge
(cap
)
379 if cap
<= c
then return
380 while c
<= cap
do c
= c
* 2 + 2
381 var a
= new NativeArray[E
](c
)
382 if _capacity
> 0 then _items
.copy_to
(a
, _length
)
387 # Create an empty array.
394 # Create an array from a collection.
395 init from
(items
: Collection[E
]) do
396 with_capacity
(items
.length
)
400 # Create an array with some `objects`.
401 init with_items
(objects
: E
...)
403 _items
= objects
._items
404 _capacity
= objects
._capacity
405 _length
= objects
.length
408 # Create an empty array with a given capacity.
409 init with_capacity
(cap
: Int)
411 assert positive
: cap
>= 0
412 _items
= new NativeArray[E
](cap
)
417 # Create an array of `count` elements
418 init filled_with
(value
: E
, count
: Int)
420 assert positive
: count
>= 0
421 _items
= new NativeArray[E
](count
)
431 # Create a array filled with a given native array.
432 init with_native
(nat
: NativeArray[E
], size
: Int)
434 assert positive
: size
>= 0
440 # The internal storage.
441 private var items
: nullable NativeArray[E
] = null
443 # The size of `_items`.
444 private var capacity
: Int = 0
448 if not o
isa Array[nullable Object] then return super
449 # Efficient implementation
451 if l
!= o
.length
then return false
456 if it
[i
] != oit
[i
] then return false
462 # Shallow clone of `self`
474 # Note that the clone is shallow and elements are shared between `self` and the result.
483 redef fun clone
do return to_a
485 # Concatenation of arrays.
487 # Returns a new array built by concatenating `self` and `other` together.
492 # assert a3 == [1,2,3,4,5,6]
494 # Because a new array is always created, future modification on `self` and `other`
495 # does not impact the previously computed result.
499 # assert a3 == [1,2,3,4,5,6] # unchanged
500 # assert a1 + a2 == [1,2,3,30,4,5,6,60]
501 fun +(other
: Array[E
]): Array[E
]
503 var res
= new Array[E
].with_capacity
(length
+ other
.length
)
509 # Repetition of arrays.
511 # returns a new array built by concatenating `self` `repeat` times.
514 # assert (a * 0).is_empty
515 # assert a * 1 == [1,2,3]
516 # assert a * 2 == [1,2,3,1,2,3]
517 # assert (a * 10).length == 30
518 fun *(repeat
: Int): Array[E
]
521 var res
= new Array[E
].with_capacity
(length
* repeat
)
530 # An `Iterator` on `AbstractArray`
531 private class ArrayIterator[E
]
532 super IndexedIterator[E
]
534 redef fun item
do return _array
[_index
]
536 # redef fun item=(e) do _array[_index] = e
538 redef fun is_ok
do return _index
< _array
.length
540 redef fun next
do _index
+= 1
544 var array
: AbstractArrayRead[E
]
546 redef fun finish
do _array
._free_iterator
= self
549 private class ArrayReverseIterator[E
]
550 super ArrayIterator[E
]
552 redef fun is_ok
do return _index
>= 0
554 redef fun next
do _index
-= 1
558 _index
= _array
.length
- 1
561 # Do not cache `self`
562 redef fun finish
do end
565 # Others collections ##########################################################
567 # A set implemented with an Array.
572 # The stored elements.
573 private var array
: Array[E
] is noinit
575 redef fun has
(e
) do return _array
.has
(e
)
577 redef fun add
(e
) do if not _array
.has
(e
) then _array
.add
(e
)
579 redef fun is_empty
do return _array
.is_empty
581 redef fun length
do return _array
.length
585 assert _array
.length
> 0
589 redef fun remove
(item
)
591 var i
= _array
.index_of
(item
)
592 if i
>= 0 then remove_at
(i
)
595 redef fun remove_all
(item
) do remove
(item
)
597 redef fun clear
do _array
.clear
599 redef fun iterator
do return new ArraySetIterator[E
](_array
.iterator
)
601 # Assume the capacity is at least `cap`.
602 fun enlarge
(cap
: Int) do _array
.enlarge
(cap
)
604 private fun remove_at
(i
: Int)
606 _array
[i
] = _array
.last
610 # Create an empty set
611 init do _array
= new Array[E
]
613 # Create an empty set with a given capacity.
614 init with_capacity
(i
: Int) do _array
= new Array[E
].with_capacity
(i
)
616 redef fun new_set
do return new ArraySet[E
]
618 # Shallow clone of `self`
621 # var a = new ArraySet[Int]
632 # Note that the clone is shallow and keys and values are shared between `self` and the result.
635 # var aa = new ArraySet[Array[Int]]
644 var res
= new ArraySet[E
]
650 # Iterators on sets implemented with arrays.
651 private class ArraySetIterator[E
]
654 redef fun is_ok
do return _iter
.is_ok
656 redef fun next
do _iter
.next
658 redef fun item
: E
do return _iter
.item
660 var iter
: Iterator[E
]
664 # Associative arrays implemented with an array of (key, value) pairs.
666 super CoupleMap[K
, E
]
674 return _items
[i
].second
676 return provide_default_value
(key
)
681 redef fun []=(key
, item
)
685 _items
[i
].second
= item
687 _items
.push
(new Couple[K
,E
](key
, item
))
691 redef var keys
: RemovableCollection[K
] = new ArrayMapKeys[K
, E
](self) is lazy
692 redef var values
: RemovableCollection[E
] = new ArrayMapValues[K
, E
](self) is lazy
695 redef fun length
do return _items
.length
697 redef fun couple_iterator
do return _items
.iterator
699 redef fun is_empty
do return _items
.is_empty
701 redef fun clear
do _items
.clear
703 # Assume the capacity to be at least `cap`.
704 fun enlarge
(cap
: Int) do _items
.enlarge
(cap
)
706 redef fun couple_at
(key
)
717 private var items
= new Array[Couple[K
,E
]]
719 # fast remove the ith element of the array
720 private fun remove_at_index
(i
: Int)
722 _items
[i
] = _items
.last
726 # The last positive result given by a index(1) call
727 private var last_index
: Int = 0
729 # Where is the `key` in `_item`?
730 # return -1 if not found
731 private fun index
(key
: K
): Int
734 if l
< _items
.length
and _items
[l
].first
== key
then return l
737 while i
< _items
.length
do
738 if _items
[i
].first
== key
then
747 # Shallow clone of `self`
750 # var a = new ArrayMap[String,Int]
759 # Note that the clone is shallow and keys and values are shared between `self` and the result.
762 # var aa = new ArrayMap[String, Array[Int]]
771 var res
= new ArrayMap[K
,E
]
772 res
.recover_with
self
777 private class ArrayMapKeys[K
, E
]
778 super RemovableCollection[K
]
780 var map
: ArrayMap[K
, E
]
781 redef fun count
(k
) do if self.has
(k
) then return 1 else return 0
782 redef fun first
do return self.map
._items
.first
.first
783 redef fun has
(k
) do return self.map
.index
(k
) >= 0
784 redef fun has_only
(k
) do return (self.has
(k
) and self.length
== 1) or self.is_empty
785 redef fun is_empty
do return self.map
.is_empty
786 redef fun length
do return self.map
.length
787 redef fun iterator
do return new MapKeysIterator[K
, E
](self.map
.iterator
)
788 redef fun clear
do self.map
.clear
789 redef fun remove
(key
)
791 var i
= self.map
.index
(key
)
792 if i
>= 0 then self.map
.remove_at_index
(i
)
794 redef fun remove_all
(key
) do self.remove
(key
)
797 private class ArrayMapValues[K
, E
]
798 super RemovableCollection[E
]
800 var map
: ArrayMap[K
, E
]
801 redef fun first
do return self.map
._items
.first
.second
802 redef fun is_empty
do return self.map
.is_empty
803 redef fun length
do return self.map
.length
804 redef fun iterator
do return new MapValuesIterator[K
, E
](self.map
.iterator
)
809 for i
in self.map
._items
do if i
.second
== item
then return true
814 redef fun has_only
(item
)
816 for i
in self.map
._items
do if i
.second
!= item
then return false
821 redef fun count
(item
)
824 for i
in self.map
._items
do if i
.second
== item
then nb
+= 1
828 redef fun clear
do self.map
.clear
830 redef fun remove
(item
)
833 var i
= map
._items
.length
- 1
835 if map
._items
[i
].second
== item
then
836 map
.remove_at_index
(i
)
843 redef fun remove_all
(item
)
846 var i
= map
._items
.length
- 1
848 if map
._items
[i
].second
== item
then
849 map
.remove_at_index
(i
)
856 # Comparable array for comparable elements.
858 # For two arrays, if one is a prefix, then it is lower.
861 # var a12 = new ArrayCmp[nullable Int].with_items(1,2)
862 # var a123 = new ArrayCmp[nullable Int].with_items(1,2,3)
866 # Otherwise, the first element just after the longest
867 # common prefix gives the order between the two arrays.
870 # var a124 = new ArrayCmp[nullable Int].with_items(1,2,4)
871 # var a13 = new ArrayCmp[nullable Int].with_items(1,3)
876 # Obviously, two equal arrays are equal.
879 # var b12 = new ArrayCmp[nullable Int].with_items(1,2)
880 # assert (a12 <=> b12) == 0
883 # `null` is considered lower than any other elements.
884 # But is still greater than no element.
887 # var a12n = new ArrayCmp[nullable Int].with_items(1,2,null)
891 class ArrayCmp[E
: nullable Comparable]
894 redef type OTHER: ArrayCmp[E
] is fixed
896 redef fun <(o
) do return (self <=> o
) < 0
906 if l
< ol
then len
= l
else len
= ol
911 if b
== null then return 1
913 if d
!= 0 then return d
915 if b
!= null then return -1
923 # Others tools ################################################################
925 redef class Iterator[E
]
926 # Interate on `self` and build an array
929 var res
= new Array[E
]
938 redef class Collection[E
]
939 # Build a new array from a collection
942 var res
= new Array[E
].with_capacity
(length
)
948 # Native classes ##############################################################
951 # Access are unchecked and it has a fixed size
952 # Not for public use: may become private.
953 universal NativeArray[E
]
954 # Creates a new NativeArray of capacity `length`
955 new(length
: Int) is intern
956 # The length of the array
957 fun length
: Int is intern
958 # Use `self` to initialize a standard Nit Array.
959 fun to_a
: Array[E
] do return new Array[E
].with_native
(self, length
)
961 # Get item at `index`.
962 fun [](index
: Int): E
is intern
964 # Set `item` at `index`.
965 fun []=(index
: Int, item
: E
) is intern
967 # Copy `length` items to `dest`.
968 fun copy_to
(dest
: NativeArray[E
], length
: Int) is intern
969 #fun =(o: NativeArray[E]): Bool is intern
970 #fun !=(o: NativeArray[E]): Bool is intern