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 return new ArrayIterator[E
](self)
134 redef fun reverse_iterator
do return new ArrayReverseIterator[E
](self)
137 # Resizable one dimension array of objects.
138 abstract class AbstractArray[E
]
139 super AbstractArrayRead[E
]
142 # Force the capacity to be at least `cap`.
143 # The capacity of the array is an internal information.
144 # However, this method can be used to prepare a large amount of add
145 fun enlarge
(cap
: Int) is abstract
147 redef fun push
(item
) do add
(item
)
151 assert not_empty
: not is_empty
159 assert not_empty
: not is_empty
171 redef fun unshift
(item
)
181 redef fun insert
(item
: E
, pos
: Int)
184 copy_to
(pos
, length-pos
, self, pos
+ 1)
188 redef fun insert_all
(coll
, pos
)
191 if l
== 0 then return
194 copy_to
(pos
, length-pos-l
, self, pos
+ l
)
201 redef fun add
(item
) do self[length
] = item
203 redef fun clear
do _length
= 0
205 redef fun remove
(item
) do remove_at
(index_of
(item
))
207 redef fun remove_all
(item
)
209 var i
= index_of
(item
)
212 i
= index_of_from
(item
, i
)
216 redef fun remove_at
(i
)
219 if i
>= 0 and i
< l
then
229 # Invert two elements in the array
231 # var a = [10, 20, 30, 40]
233 # assert a == [10, 40, 30, 20]
234 fun swap_at
(a
: Int,b
: Int)
242 # Resizable one dimension array of objects.
244 # Arrays have a literal representation.
246 # var a = [12, 32, 8]
247 # # is equivalent with:
248 # var b = new Array[Int]
254 super AbstractArray[E
]
258 assert index
: index
>= 0 and index
< _length
262 redef fun []=(index
, item
)
264 assert index
: index
>= 0 and index
< _length
+ 1
265 if _capacity
<= index
then
268 if _length
<= index
then
277 if _capacity
<= l
then
284 # Slight optimization for arrays
285 redef fun add_all
(items
)
288 var nl
= l
+ items
.length
289 if _capacity
< nl
then
293 if items
isa Array[E
] then
296 _items
[l
] = items
._items
[k
]
310 redef fun enlarge
(cap
)
313 if cap
<= c
then return
314 while c
<= cap
do c
= c
* 2 + 2
315 var a
= new NativeArray[E
](c
)
316 if _capacity
> 0 then _items
.copy_to
(a
, _length
)
321 # Create an empty array.
328 # Create an array from a collection.
329 init from
(items
: Collection[E
]) do
330 with_capacity
(items
.length
)
334 # Create an array with some `objects`.
335 init with_items
(objects
: E
...)
337 _items
= objects
._items
338 _capacity
= objects
._capacity
339 _length
= objects
.length
342 # Create an empty array with a given capacity.
343 init with_capacity
(cap
: Int)
345 assert positive
: cap
>= 0
346 _items
= new NativeArray[E
](cap
)
351 # Create an array of `count` elements
352 init filled_with
(value
: E
, count
: Int)
354 assert positive
: count
>= 0
355 _items
= new NativeArray[E
](count
)
365 # Create a array filled with a given native array.
366 init with_native
(nat
: NativeArray[E
], size
: Int)
368 assert positive
: size
>= 0
374 # The internal storage.
375 private var items
: nullable NativeArray[E
] = null
377 # Do not use this method
378 # FIXME: Remove it once modules can intrude non local modules
379 fun intern_items
: NativeArray[E
] do return _items
.as(not null)
381 # The size of `_items`.
382 private var capacity
: Int = 0
386 if not o
isa Array[nullable Object] then return super
387 # Efficient implementation
389 if l
!= o
.length
then return false
394 if it
[i
] != oit
[i
] then return false
400 # Concatenation of arrays.
402 # Returns a new array built by concatenating `self` and `other` together.
407 # assert a3 == [1,2,3,4,5,6]
409 # Because a new array is always created, future modification on `self` and `other`
410 # does not impact the previously computed result.
414 # assert a3 == [1,2,3,4,5,6] # unchanged
415 # assert a1 + a2 == [1,2,3,30,4,5,6,60]
416 fun +(other
: Array[E
]): Array[E
]
418 var res
= new Array[E
].with_capacity
(length
+ other
.length
)
424 # Repetition of arrays.
426 # returns a new array built by concatenating `self` `repeat` times.
429 # assert (a * 0).is_empty
430 # assert a * 1 == [1,2,3]
431 # assert a * 2 == [1,2,3,1,2,3]
432 # assert (a * 10).length == 30
433 fun *(repeat
: Int): Array[E
]
436 var res
= new Array[E
].with_capacity
(length
* repeat
)
445 # An `Iterator` on `AbstractArray`
446 private class ArrayIterator[E
]
447 super IndexedIterator[E
]
449 redef fun item
do return _array
[_index
]
451 # redef fun item=(e) do _array[_index] = e
453 redef fun is_ok
do return _index
< _array
.length
455 redef fun next
do _index
+= 1
459 var array
: AbstractArrayRead[E
]
462 private class ArrayReverseIterator[E
]
463 super ArrayIterator[E
]
465 redef fun is_ok
do return _index
>= 0
467 redef fun next
do _index
-= 1
471 _index
= _array
.length
- 1
475 # Others collections ##########################################################
477 # A set implemented with an Array.
478 class ArraySet[E
: Object]
481 # The stored elements.
482 private var array
: Array[E
] is noinit
484 redef fun has
(e
) do return _array
.has
(e
)
486 redef fun add
(e
) do if not _array
.has
(e
) then _array
.add
(e
)
488 redef fun is_empty
do return _array
.is_empty
490 redef fun length
do return _array
.length
494 assert _array
.length
> 0
498 redef fun remove
(item
)
500 var i
= _array
.index_of
(item
)
501 if i
>= 0 then remove_at
(i
)
504 redef fun remove_all
(item
) do remove
(item
)
506 redef fun clear
do _array
.clear
508 redef fun iterator
do return new ArraySetIterator[E
](_array
.iterator
)
510 # Assume the capacity is at least `cap`.
511 fun enlarge
(cap
: Int) do _array
.enlarge
(cap
)
513 private fun remove_at
(i
: Int)
515 _array
[i
] = _array
.last
519 # Create an empty set
520 init do _array
= new Array[E
]
522 # Create an empty set with a given capacity.
523 init with_capacity
(i
: Int) do _array
= new Array[E
].with_capacity
(i
)
525 redef fun new_set
do return new ArraySet[E
]
528 # Iterators on sets implemented with arrays.
529 private class ArraySetIterator[E
: Object]
532 redef fun is_ok
do return _iter
.is_ok
534 redef fun next
do _iter
.next
536 redef fun item
: E
do return _iter
.item
538 var iter
: ArrayIterator[E
]
542 # Associative arrays implemented with an array of (key, value) pairs.
543 class ArrayMap[K
: Object, E
]
544 super CoupleMap[K
, E
]
551 return _items
[i
].second
553 return provide_default_value
(key
)
558 redef fun []=(key
, item
)
562 _items
[i
].second
= item
564 _items
.push
(new Couple[K
,E
](key
, item
))
568 redef var keys
: RemovableCollection[K
] = new ArrayMapKeys[K
, E
](self)
569 redef var values
: RemovableCollection[E
] = new ArrayMapValues[K
, E
](self)
572 redef fun length
do return _items
.length
574 redef fun couple_iterator
do return _items
.iterator
576 redef fun is_empty
do return _items
.is_empty
578 redef fun clear
do _items
.clear
580 # Assume the capacity to be at least `cap`.
581 fun enlarge
(cap
: Int) do _items
.enlarge
(cap
)
583 redef fun couple_at
(key
)
594 private var items
= new Array[Couple[K
,E
]]
596 # fast remove the ith element of the array
597 private fun remove_at_index
(i
: Int)
599 _items
[i
] = _items
.last
603 # The last positive result given by a index(1) call
604 private var last_index
: Int = 0
606 # Where is the `key` in `_item`?
607 # return -1 if not found
608 private fun index
(key
: K
): Int
611 if l
< _items
.length
and _items
[l
].first
== key
then return l
614 while i
< _items
.length
do
615 if _items
[i
].first
== key
then
625 private class ArrayMapKeys[K
: Object, E
]
626 super RemovableCollection[K
]
628 var map
: ArrayMap[K
, E
]
629 redef fun count
(k
) do if self.has
(k
) then return 1 else return 0
630 redef fun first
do return self.map
._items
.first
.first
631 redef fun has
(k
) do return self.map
.index
(k
) >= 0
632 redef fun has_only
(k
) do return (self.has
(k
) and self.length
== 1) or self.is_empty
633 redef fun is_empty
do return self.map
.is_empty
634 redef fun length
do return self.map
.length
635 redef fun iterator
do return new MapKeysIterator[K
, E
](self.map
.iterator
)
636 redef fun clear
do self.map
.clear
637 redef fun remove
(key
)
639 var i
= self.map
.index
(key
)
640 if i
>= 0 then self.map
.remove_at_index
(i
)
642 redef fun remove_all
(key
) do self.remove
(key
)
645 private class ArrayMapValues[K
: Object, E
]
646 super RemovableCollection[E
]
648 var map
: ArrayMap[K
, E
]
649 redef fun first
do return self.map
._items
.first
.second
650 redef fun is_empty
do return self.map
.is_empty
651 redef fun length
do return self.map
.length
652 redef fun iterator
do return new MapValuesIterator[K
, E
](self.map
.iterator
)
657 for i
in self.map
._items
do if i
.second
== item
then return true
662 redef fun has_only
(item
)
664 for i
in self.map
._items
do if i
.second
!= item
then return false
669 redef fun count
(item
)
672 for i
in self.map
._items
do if i
.second
== item
then nb
+= 1
676 redef fun clear
do self.map
.clear
678 redef fun remove
(item
)
681 var i
= map
._items
.length
- 1
683 if map
._items
[i
].second
== item
then
684 map
.remove_at_index
(i
)
691 redef fun remove_all
(item
)
694 var i
= map
._items
.length
- 1
696 if map
._items
[i
].second
== item
then
697 map
.remove_at_index
(i
)
704 # Comparable array for comparable elements.
706 # For two arrays, if one is a prefix, then it is lower.
709 # var a12 = new ArrayCmp[nullable Int].with_items(1,2)
710 # var a123 = new ArrayCmp[nullable Int].with_items(1,2,3)
714 # Otherwise, the first element just after the longest
715 # common prefix gives the order between the two arrays.
718 # var a124 = new ArrayCmp[nullable Int].with_items(1,2,4)
719 # var a13 = new ArrayCmp[nullable Int].with_items(1,3)
724 # Obviously, two equal arrays are equal.
727 # var b12 = new ArrayCmp[nullable Int].with_items(1,2)
728 # assert (a12 <=> b12) == 0
731 # `null` is considered lower than any other elements.
732 # But is still greater than no element.
735 # var a12n = new ArrayCmp[nullable Int].with_items(1,2,null)
739 class ArrayCmp[E
: nullable Comparable]
742 redef type OTHER: ArrayCmp[E
] is fixed
744 redef fun <(o
) do return (self <=> o
) < 0
754 if l
< ol
then len
= l
else len
= ol
759 if b
== null then return 1
760 var d
= a
<=> b
.as(Comparable)
761 if d
!= 0 then return d
763 if b
!= null then return -1
771 # Others tools ################################################################
773 redef class Iterator[E
]
774 # Interate on `self` and build an array
777 var res
= new Array[E
]
786 redef class Collection[E
]
787 # Build a new array from a collection
790 var res
= new Array[E
].with_capacity
(length
)
796 # Native classes ##############################################################
799 # Access are unchecked and it has a fixed size
800 # Not for public use: may become private.
801 universal NativeArray[E
]
802 # Creates a new NativeArray of capacity `length`
803 new(length
: Int) is intern
804 # The length of the array
805 fun length
: Int is intern
806 # Use `self` to initialize a standard Nit Array.
807 fun to_a
: Array[E
] do return new Array[E
].with_native
(self, length
)
809 # Get item at `index`.
810 fun [](index
: Int): E
is intern
812 # Set `item` at `index`.
813 fun []=(index
: Int, item
: E
) is intern
815 # Copy `length` items to `dest`.
816 fun copy_to
(dest
: NativeArray[E
], length
: Int) is intern
817 #fun =(o: NativeArray[E]): Bool is intern
818 #fun !=(o: NativeArray[E]): Bool is intern