233621edd7c663e20e4c4dea831cca6728a59eeb
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
18 import abstract_collection
20 # One dimension array of objects.
21 abstract class AbstractArrayRead[E
]
26 redef fun is_empty
do return _length
== 0
33 if self[i
] == item
then return true
39 redef fun has_only
(item
)
44 if self[i
] != item
then return false
56 if self[i
] == item
then res
+= 1
62 redef fun index_of
(item
) do return index_of_from
(item
, 0)
64 redef fun last_index_of
(item
: E
): Int do return last_index_of_from
(item
, length-1
)
66 redef fun index_of_from
(item
: E
, pos
: Int): Int
71 if self[i
] == item
then
79 redef fun last_index_of_from
(item
: E
, pos
: Int): Int
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)
150 # Resizable one dimension array of objects.
151 abstract class AbstractArray[E
]
152 super AbstractArrayRead[E
]
155 # Force the capacity to be at least `cap`.
156 # The capacity of the array is an internal information.
157 # However, this method can be used to prepare a large amount of add
158 fun enlarge
(cap
: Int) is abstract
160 redef fun push
(item
) do add
(item
)
164 assert not_empty
: not is_empty
172 assert not_empty
: not is_empty
184 redef fun unshift
(item
)
194 redef fun insert
(item
: E
, pos
: Int)
197 copy_to
(pos
, length-pos
, self, pos
+ 1)
201 redef fun insert_all
(coll
, pos
)
204 if l
== 0 then return
207 copy_to
(pos
, length-pos-l
, self, pos
+ l
)
214 redef fun add
(item
) do self[length
] = item
216 redef fun clear
do _length
= 0
218 redef fun remove
(item
) do remove_at
(index_of
(item
))
220 redef fun remove_all
(item
)
222 var i
= index_of
(item
)
225 i
= index_of_from
(item
, i
)
229 redef fun remove_at
(i
)
232 if i
>= 0 and i
< l
then
242 # Invert two elements in the array
244 # var a = [10, 20, 30, 40]
246 # assert a == [10, 40, 30, 20]
247 fun swap_at
(a
: Int,b
: Int)
255 # Resizable one dimension array of objects.
257 # Arrays have a literal representation.
259 # var a = [12, 32, 8]
260 # # is equivalent with:
261 # var b = new Array[Int]
267 super AbstractArray[E
]
272 assert index
: index
>= 0 and index
< _length
276 redef fun []=(index
, item
)
278 assert index
: index
>= 0 and index
< _length
+ 1
279 if _capacity
<= index
then
282 if _length
<= index
then
291 if _capacity
<= l
then
298 # Slight optimization for arrays
299 redef fun add_all
(items
)
302 var nl
= l
+ items
.length
303 if _capacity
< nl
then
307 if items
isa Array[E
] then
310 _items
[l
] = items
._items
[k
]
324 redef fun enlarge
(cap
)
327 if cap
<= c
then return
328 while c
<= cap
do c
= c
* 2 + 2
329 var a
= new NativeArray[E
](c
)
330 if _capacity
> 0 then _items
.copy_to
(a
, _length
)
335 # Create an empty array.
342 # Create an array from a collection.
343 init from
(items
: Collection[E
]) do
344 with_capacity
(items
.length
)
348 # Create an array with some `objects`.
349 init with_items
(objects
: E
...)
351 _items
= objects
._items
352 _capacity
= objects
._capacity
353 _length
= objects
.length
356 # Create an empty array with a given capacity.
357 init with_capacity
(cap
: Int)
359 assert positive
: cap
>= 0
360 _items
= new NativeArray[E
](cap
)
365 # Create an array of `count` elements
366 init filled_with
(value
: E
, count
: Int)
368 assert positive
: count
>= 0
369 _items
= new NativeArray[E
](count
)
379 # Create a array filled with a given native array.
380 init with_native
(nat
: NativeArray[E
], size
: Int)
382 assert positive
: size
>= 0
388 # The internal storage.
389 private var items
: nullable NativeArray[E
] = null
391 # The size of `_items`.
392 private var capacity
: Int = 0
396 if not o
isa Array[nullable Object] then return super
397 # Efficient implementation
399 if l
!= o
.length
then return false
404 if it
[i
] != oit
[i
] then return false
410 # Shallow clone of `self`
422 # Note that the clone is shallow and elements are shared between `self` and the result.
431 redef fun clone
do return to_a
433 # Concatenation of arrays.
435 # Returns a new array built by concatenating `self` and `other` together.
440 # assert a3 == [1,2,3,4,5,6]
442 # Because a new array is always created, future modification on `self` and `other`
443 # does not impact the previously computed result.
447 # assert a3 == [1,2,3,4,5,6] # unchanged
448 # assert a1 + a2 == [1,2,3,30,4,5,6,60]
449 fun +(other
: Array[E
]): Array[E
]
451 var res
= new Array[E
].with_capacity
(length
+ other
.length
)
457 # Repetition of arrays.
459 # returns a new array built by concatenating `self` `repeat` times.
462 # assert (a * 0).is_empty
463 # assert a * 1 == [1,2,3]
464 # assert a * 2 == [1,2,3,1,2,3]
465 # assert (a * 10).length == 30
466 fun *(repeat
: Int): Array[E
]
469 var res
= new Array[E
].with_capacity
(length
* repeat
)
478 # An `Iterator` on `AbstractArray`
479 private class ArrayIterator[E
]
480 super IndexedIterator[E
]
482 redef fun item
do return _array
[_index
]
484 # redef fun item=(e) do _array[_index] = e
486 redef fun is_ok
do return _index
< _array
.length
488 redef fun next
do _index
+= 1
492 var array
: AbstractArrayRead[E
]
494 redef fun finish
do _array
._free_iterator
= self
497 private class ArrayReverseIterator[E
]
498 super ArrayIterator[E
]
500 redef fun is_ok
do return _index
>= 0
502 redef fun next
do _index
-= 1
506 _index
= _array
.length
- 1
510 # Others collections ##########################################################
512 # A set implemented with an Array.
517 # The stored elements.
518 private var array
: Array[E
] is noinit
520 redef fun has
(e
) do return _array
.has
(e
)
522 redef fun add
(e
) do if not _array
.has
(e
) then _array
.add
(e
)
524 redef fun is_empty
do return _array
.is_empty
526 redef fun length
do return _array
.length
530 assert _array
.length
> 0
534 redef fun remove
(item
)
536 var i
= _array
.index_of
(item
)
537 if i
>= 0 then remove_at
(i
)
540 redef fun remove_all
(item
) do remove
(item
)
542 redef fun clear
do _array
.clear
544 redef fun iterator
do return new ArraySetIterator[E
](_array
.iterator
)
546 # Assume the capacity is at least `cap`.
547 fun enlarge
(cap
: Int) do _array
.enlarge
(cap
)
549 private fun remove_at
(i
: Int)
551 _array
[i
] = _array
.last
555 # Create an empty set
556 init do _array
= new Array[E
]
558 # Create an empty set with a given capacity.
559 init with_capacity
(i
: Int) do _array
= new Array[E
].with_capacity
(i
)
561 redef fun new_set
do return new ArraySet[E
]
563 # Shallow clone of `self`
566 # var a = new ArraySet[Int]
577 # Note that the clone is shallow and keys and values are shared between `self` and the result.
580 # var aa = new ArraySet[Array[Int]]
589 var res
= new ArraySet[E
]
595 # Iterators on sets implemented with arrays.
596 private class ArraySetIterator[E
]
599 redef fun is_ok
do return _iter
.is_ok
601 redef fun next
do _iter
.next
603 redef fun item
: E
do return _iter
.item
605 var iter
: ArrayIterator[E
]
609 # Associative arrays implemented with an array of (key, value) pairs.
611 super CoupleMap[K
, E
]
619 return _items
[i
].second
621 return provide_default_value
(key
)
626 redef fun []=(key
, item
)
630 _items
[i
].second
= item
632 _items
.push
(new Couple[K
,E
](key
, item
))
636 redef var keys
: RemovableCollection[K
] = new ArrayMapKeys[K
, E
](self) is lazy
637 redef var values
: RemovableCollection[E
] = new ArrayMapValues[K
, E
](self) is lazy
640 redef fun length
do return _items
.length
642 redef fun couple_iterator
do return _items
.iterator
644 redef fun is_empty
do return _items
.is_empty
646 redef fun clear
do _items
.clear
648 # Assume the capacity to be at least `cap`.
649 fun enlarge
(cap
: Int) do _items
.enlarge
(cap
)
651 redef fun couple_at
(key
)
662 private var items
= new Array[Couple[K
,E
]]
664 # fast remove the ith element of the array
665 private fun remove_at_index
(i
: Int)
667 _items
[i
] = _items
.last
671 # The last positive result given by a index(1) call
672 private var last_index
: Int = 0
674 # Where is the `key` in `_item`?
675 # return -1 if not found
676 private fun index
(key
: K
): Int
679 if l
< _items
.length
and _items
[l
].first
== key
then return l
682 while i
< _items
.length
do
683 if _items
[i
].first
== key
then
692 # Shallow clone of `self`
695 # var a = new ArrayMap[String,Int]
704 # Note that the clone is shallow and keys and values are shared between `self` and the result.
707 # var aa = new ArrayMap[String, Array[Int]]
716 var res
= new ArrayMap[K
,E
]
717 res
.recover_with
self
722 private class ArrayMapKeys[K
, E
]
723 super RemovableCollection[K
]
725 var map
: ArrayMap[K
, E
]
726 redef fun count
(k
) do if self.has
(k
) then return 1 else return 0
727 redef fun first
do return self.map
._items
.first
.first
728 redef fun has
(k
) do return self.map
.index
(k
) >= 0
729 redef fun has_only
(k
) do return (self.has
(k
) and self.length
== 1) or self.is_empty
730 redef fun is_empty
do return self.map
.is_empty
731 redef fun length
do return self.map
.length
732 redef fun iterator
do return new MapKeysIterator[K
, E
](self.map
.iterator
)
733 redef fun clear
do self.map
.clear
734 redef fun remove
(key
)
736 var i
= self.map
.index
(key
)
737 if i
>= 0 then self.map
.remove_at_index
(i
)
739 redef fun remove_all
(key
) do self.remove
(key
)
742 private class ArrayMapValues[K
, E
]
743 super RemovableCollection[E
]
745 var map
: ArrayMap[K
, E
]
746 redef fun first
do return self.map
._items
.first
.second
747 redef fun is_empty
do return self.map
.is_empty
748 redef fun length
do return self.map
.length
749 redef fun iterator
do return new MapValuesIterator[K
, E
](self.map
.iterator
)
754 for i
in self.map
._items
do if i
.second
== item
then return true
759 redef fun has_only
(item
)
761 for i
in self.map
._items
do if i
.second
!= item
then return false
766 redef fun count
(item
)
769 for i
in self.map
._items
do if i
.second
== item
then nb
+= 1
773 redef fun clear
do self.map
.clear
775 redef fun remove
(item
)
778 var i
= map
._items
.length
- 1
780 if map
._items
[i
].second
== item
then
781 map
.remove_at_index
(i
)
788 redef fun remove_all
(item
)
791 var i
= map
._items
.length
- 1
793 if map
._items
[i
].second
== item
then
794 map
.remove_at_index
(i
)
801 # Comparable array for comparable elements.
803 # For two arrays, if one is a prefix, then it is lower.
806 # var a12 = new ArrayCmp[nullable Int].with_items(1,2)
807 # var a123 = new ArrayCmp[nullable Int].with_items(1,2,3)
811 # Otherwise, the first element just after the longest
812 # common prefix gives the order between the two arrays.
815 # var a124 = new ArrayCmp[nullable Int].with_items(1,2,4)
816 # var a13 = new ArrayCmp[nullable Int].with_items(1,3)
821 # Obviously, two equal arrays are equal.
824 # var b12 = new ArrayCmp[nullable Int].with_items(1,2)
825 # assert (a12 <=> b12) == 0
828 # `null` is considered lower than any other elements.
829 # But is still greater than no element.
832 # var a12n = new ArrayCmp[nullable Int].with_items(1,2,null)
836 class ArrayCmp[E
: nullable Comparable]
839 redef type OTHER: ArrayCmp[E
] is fixed
841 redef fun <(o
) do return (self <=> o
) < 0
851 if l
< ol
then len
= l
else len
= ol
856 if b
== null then return 1
857 var d
= a
<=> b
.as(Comparable)
858 if d
!= 0 then return d
860 if b
!= null then return -1
868 # Others tools ################################################################
870 redef class Iterator[E
]
871 # Interate on `self` and build an array
874 var res
= new Array[E
]
883 redef class Collection[E
]
884 # Build a new array from a collection
887 var res
= new Array[E
].with_capacity
(length
)
893 # Native classes ##############################################################
896 # Access are unchecked and it has a fixed size
897 # Not for public use: may become private.
898 universal NativeArray[E
]
899 # Creates a new NativeArray of capacity `length`
900 new(length
: Int) is intern
901 # The length of the array
902 fun length
: Int is intern
903 # Use `self` to initialize a standard Nit Array.
904 fun to_a
: Array[E
] do return new Array[E
].with_native
(self, length
)
906 # Get item at `index`.
907 fun [](index
: Int): E
is intern
909 # Set `item` at `index`.
910 fun []=(index
: Int, item
: E
) is intern
912 # Copy `length` items to `dest`.
913 fun copy_to
(dest
: NativeArray[E
], length
: Int) is intern
914 #fun =(o: NativeArray[E]): Bool is intern
915 #fun !=(o: NativeArray[E]): Bool is intern