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 protected 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.
245 # var a = [12, 32, 8]
246 # # is equivalent with:
247 # var b = new Array[Int]
253 super AbstractArray[E
]
254 super ArrayCapable[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 redef fun enlarge
(cap
)
287 if cap
<= c
then return
288 while c
<= cap
do c
= c
* 2 + 2
289 var a
= calloc_array
(c
)
290 if _capacity
> 0 then _items
.copy_to
(a
, _length
)
295 # Create an empty array.
302 # Create an array from a collection.
303 init from
(items
: Collection[E
]) do
304 with_capacity
(items
.length
)
308 # Create an array with some `objects`.
309 init with_items
(objects
: E
...)
311 _items
= objects
._items
312 _capacity
= objects
._capacity
313 _length
= objects
.length
316 # Create an empty array with a given capacity.
317 init with_capacity
(cap
: Int)
319 assert positive
: cap
>= 0
320 _items
= calloc_array
(cap
)
325 # Create an array of `count` elements
326 init filled_with
(value
: E
, count
: Int)
328 assert positive
: count
>= 0
329 _items
= calloc_array
(count
)
339 # Create a array filled with a given native array.
340 init with_native
(nat
: NativeArray[E
], size
: Int)
342 assert positive
: size
>= 0
348 # The internal storage.
349 private var items
: nullable NativeArray[E
] = null
351 # Do not use this method
352 # FIXME: Remove it once modules can intrude non local modules
353 fun intern_items
: NativeArray[E
] do return _items
.as(not null)
355 # The size of `_items`.
356 private var capacity
: Int = 0
360 if not o
isa Array[nullable Object] then return super
361 # Efficient implementation
363 if l
!= o
.length
then return false
368 if it
[i
] != oit
[i
] then return false
374 # Concatenation of arrays.
376 # Returns a new array built by concatenating `self` and `other` together.
381 # assert a3 == [1,2,3,4,5,6]
383 # Because a new array is always created, future modification on `self` and `other`
384 # does not impact the previously computed result.
388 # assert a3 == [1,2,3,4,5,6] # unchanged
389 # assert a1 + a2 == [1,2,3,30,4,5,6,60]
390 fun +(other
: Array[E
]): Array[E
]
392 var res
= new Array[E
].with_capacity
(length
+ other
.length
)
398 # Repetition of arrays.
400 # returns a new array built by concatenating `self` `repeat` times.
403 # assert (a * 0).is_empty
404 # assert a * 1 == [1,2,3]
405 # assert a * 2 == [1,2,3,1,2,3]
406 # assert (a * 10).length == 30
407 fun *(repeat
: Int): Array[E
]
410 var res
= new Array[E
].with_capacity
(length
* repeat
)
419 # An `Iterator` on `AbstractArray`
420 private class ArrayIterator[E
]
421 super IndexedIterator[E
]
423 redef fun item
do return _array
[_index
]
425 # redef fun item=(e) do _array[_index] = e
427 redef fun is_ok
do return _index
< _array
.length
429 redef fun next
do _index
+= 1
433 private var array
: AbstractArrayRead[E
]
436 private class ArrayReverseIterator[E
]
437 super ArrayIterator[E
]
439 redef fun is_ok
do return _index
>= 0
441 redef fun next
do _index
-= 1
445 _index
= _array
.length
- 1
449 # Others collections ##########################################################
451 # A set implemented with an Array.
452 class ArraySet[E
: Object]
455 # The stored elements.
456 private var array
: Array[E
] is noinit
458 redef fun has
(e
) do return _array
.has
(e
)
460 redef fun add
(e
) do if not _array
.has
(e
) then _array
.add
(e
)
462 redef fun is_empty
do return _array
.is_empty
464 redef fun length
do return _array
.length
468 assert _array
.length
> 0
472 redef fun remove
(item
)
474 var i
= _array
.index_of
(item
)
475 if i
>= 0 then remove_at
(i
)
478 redef fun remove_all
(item
) do remove
(item
)
480 redef fun clear
do _array
.clear
482 redef fun iterator
do return new ArraySetIterator[E
](_array
.iterator
)
484 # Assume the capacity is at least `cap`.
485 fun enlarge
(cap
: Int) do _array
.enlarge
(cap
)
487 private fun remove_at
(i
: Int)
489 _array
[i
] = _array
.last
493 # Create an empty set
494 init do _array
= new Array[E
]
496 # Create an empty set with a given capacity.
497 init with_capacity
(i
: Int) do _array
= new Array[E
].with_capacity
(i
)
499 redef fun new_set
do return new ArraySet[E
]
502 # Iterators on sets implemented with arrays.
503 private class ArraySetIterator[E
: Object]
506 redef fun is_ok
do return _iter
.is_ok
508 redef fun next
do _iter
.next
510 redef fun item
: E
do return _iter
.item
512 private var iter
: ArrayIterator[E
]
516 # Associative arrays implemented with an array of (key, value) pairs.
517 class ArrayMap[K
: Object, E
]
518 super CoupleMap[K
, E
]
525 return _items
[i
].second
527 return provide_default_value
(key
)
532 redef fun []=(key
, item
)
536 _items
[i
].second
= item
538 _items
.push
(new Couple[K
,E
](key
, item
))
542 redef var keys
: RemovableCollection[K
] = new ArrayMapKeys[K
, E
](self)
543 redef var values
: RemovableCollection[E
] = new ArrayMapValues[K
, E
](self)
546 redef fun length
do return _items
.length
548 redef fun couple_iterator
do return _items
.iterator
550 redef fun is_empty
do return _items
.is_empty
552 redef fun clear
do _items
.clear
554 # Assume the capacity to be at least `cap`.
555 fun enlarge
(cap
: Int) do _items
.enlarge
(cap
)
557 redef fun couple_at
(key
)
568 private var items
= new Array[Couple[K
,E
]]
570 # fast remove the ith element of the array
571 private fun remove_at_index
(i
: Int)
573 _items
[i
] = _items
.last
577 # The last positive result given by a index(1) call
578 private var last_index
: Int = 0
580 # Where is the `key` in `_item`?
581 # return -1 if not found
582 private fun index
(key
: K
): Int
585 if l
< _items
.length
and _items
[l
].first
== key
then return l
588 while i
< _items
.length
do
589 if _items
[i
].first
== key
then
599 private class ArrayMapKeys[K
: Object, E
]
600 super RemovableCollection[K
]
602 var map
: ArrayMap[K
, E
]
603 redef fun count
(k
) do if self.has
(k
) then return 1 else return 0
604 redef fun first
do return self.map
._items
.first
.first
605 redef fun has
(k
) do return self.map
.index
(k
) >= 0
606 redef fun has_only
(k
) do return (self.has
(k
) and self.length
== 1) or self.is_empty
607 redef fun is_empty
do return self.map
.is_empty
608 redef fun length
do return self.map
.length
609 redef fun iterator
do return new MapKeysIterator[K
, E
](self.map
.iterator
)
610 redef fun clear
do self.map
.clear
611 redef fun remove
(key
)
613 var i
= self.map
.index
(key
)
614 if i
>= 0 then self.map
.remove_at_index
(i
)
616 redef fun remove_all
(key
) do self.remove
(key
)
619 private class ArrayMapValues[K
: Object, E
]
620 super RemovableCollection[E
]
622 var map
: ArrayMap[K
, E
]
623 redef fun first
do return self.map
._items
.first
.second
624 redef fun is_empty
do return self.map
.is_empty
625 redef fun length
do return self.map
.length
626 redef fun iterator
do return new MapValuesIterator[K
, E
](self.map
.iterator
)
631 for i
in self.map
._items
do if i
.second
== item
then return true
636 redef fun has_only
(item
)
638 for i
in self.map
._items
do if i
.second
!= item
then return false
643 redef fun count
(item
)
646 for i
in self.map
._items
do if i
.second
== item
then nb
+= 1
650 redef fun clear
do self.map
.clear
652 redef fun remove
(item
)
655 var i
= map
._items
.length
- 1
657 if map
._items
[i
].second
== item
then
658 map
.remove_at_index
(i
)
665 redef fun remove_all
(item
)
668 var i
= map
._items
.length
- 1
670 if map
._items
[i
].second
== item
then
671 map
.remove_at_index
(i
)
678 # Comparable array for comparable elements.
680 # For two arrays, if one is a prefix, then it is lower.
683 # var a12 = new ArrayCmp[nullable Int].with_items(1,2)
684 # var a123 = new ArrayCmp[nullable Int].with_items(1,2,3)
688 # Otherwise, the first element just after the longest
689 # common prefix gives the order between the two arrays.
692 # var a124 = new ArrayCmp[nullable Int].with_items(1,2,4)
693 # var a13 = new ArrayCmp[nullable Int].with_items(1,3)
698 # Obviously, two equal arrays are equal.
701 # var b12 = new ArrayCmp[nullable Int].with_items(1,2)
702 # assert (a12 <=> b12) == 0
705 # `null` is considered lower than any other elements.
706 # But is still greater than no element.
709 # var a12n = new ArrayCmp[nullable Int].with_items(1,2,null)
713 class ArrayCmp[E
: nullable Comparable]
716 redef type OTHER: ArrayCmp[E
] is fixed
718 redef fun <(o
) do return (self <=> o
) < 0
728 if l
< ol
then len
= l
else len
= ol
733 if b
== null then return 1
734 var d
= a
<=> b
.as(Comparable)
735 if d
!= 0 then return d
737 if b
!= null then return -1
745 # Others tools ################################################################
747 redef class Iterator[E
]
748 # Interate on `self` and build an array
751 var res
= new Array[E
]
760 redef class Collection[E
]
761 # Build a new array from a collection
764 var res
= new Array[E
].with_capacity
(length
)
770 # Native classes ##############################################################
772 # Subclasses of this class can create native arrays
773 interface ArrayCapable[E
]
774 # Get a new array of `size` elements.
775 protected fun calloc_array
(size
: Int): NativeArray[E
] is intern
779 # Access are unchecked and it has a fixed size
780 # Not for public use: may become private.
781 universal NativeArray[E
]
782 # Creates a new NativeArray of capacity `length`
783 new(length
: Int) is intern
784 # The length of the array
785 fun length
: Int is intern
786 # Use `self` to initialize a standard Nit Array.
787 fun to_a
: Array[E
] do return new Array[E
].with_native
(self, length
)
788 fun [](index
: Int): E
is intern
789 fun []=(index
: Int, item
: E
) is intern
790 fun copy_to
(dest
: NativeArray[E
], length
: Int) is intern
791 #fun =(o: NativeArray[E]): Bool is intern
792 #fun !=(o: NativeArray[E]): Bool is intern