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
)
399 # An `Iterator` on `AbstractArray`
400 private class ArrayIterator[E
]
401 super IndexedIterator[E
]
403 redef fun item
do return _array
[_index
]
405 # redef fun item=(e) do _array[_index] = e
407 redef fun is_ok
do return _index
< _array
.length
409 redef fun next
do _index
+= 1
413 private var array
: AbstractArrayRead[E
]
416 private class ArrayReverseIterator[E
]
417 super ArrayIterator[E
]
419 redef fun is_ok
do return _index
>= 0
421 redef fun next
do _index
-= 1
425 _index
= _array
.length
- 1
429 # Others collections ##########################################################
431 # A set implemented with an Array.
432 class ArraySet[E
: Object]
435 # The stored elements.
436 private var array
: Array[E
] is noinit
438 redef fun has
(e
) do return _array
.has
(e
)
440 redef fun add
(e
) do if not _array
.has
(e
) then _array
.add
(e
)
442 redef fun is_empty
do return _array
.is_empty
444 redef fun length
do return _array
.length
448 assert _array
.length
> 0
452 redef fun remove
(item
)
454 var i
= _array
.index_of
(item
)
455 if i
>= 0 then remove_at
(i
)
458 redef fun remove_all
(item
) do remove
(item
)
460 redef fun clear
do _array
.clear
462 redef fun iterator
do return new ArraySetIterator[E
](_array
.iterator
)
464 # Assume the capacity is at least `cap`.
465 fun enlarge
(cap
: Int) do _array
.enlarge
(cap
)
467 private fun remove_at
(i
: Int)
469 _array
[i
] = _array
.last
473 # Create an empty set
474 init do _array
= new Array[E
]
476 # Create an empty set with a given capacity.
477 init with_capacity
(i
: Int) do _array
= new Array[E
].with_capacity
(i
)
479 redef fun new_set
do return new ArraySet[E
]
482 # Iterators on sets implemented with arrays.
483 private class ArraySetIterator[E
: Object]
486 redef fun is_ok
do return _iter
.is_ok
488 redef fun next
do _iter
.next
490 redef fun item
: E
do return _iter
.item
492 private var iter
: ArrayIterator[E
]
496 # Associative arrays implemented with an array of (key, value) pairs.
497 class ArrayMap[K
: Object, E
]
498 super CoupleMap[K
, E
]
505 return _items
[i
].second
507 return provide_default_value
(key
)
512 redef fun []=(key
, item
)
516 _items
[i
].second
= item
518 _items
.push
(new Couple[K
,E
](key
, item
))
522 redef var keys
: RemovableCollection[K
] = new ArrayMapKeys[K
, E
](self)
523 redef var values
: RemovableCollection[E
] = new ArrayMapValues[K
, E
](self)
526 redef fun length
do return _items
.length
528 redef fun couple_iterator
do return _items
.iterator
530 redef fun is_empty
do return _items
.is_empty
532 redef fun clear
do _items
.clear
534 # Assume the capacity to be at least `cap`.
535 fun enlarge
(cap
: Int) do _items
.enlarge
(cap
)
537 redef fun couple_at
(key
)
548 private var items
= new Array[Couple[K
,E
]]
550 # fast remove the ith element of the array
551 private fun remove_at_index
(i
: Int)
553 _items
[i
] = _items
.last
557 # The last positive result given by a index(1) call
558 private var last_index
: Int = 0
560 # Where is the `key` in `_item`?
561 # return -1 if not found
562 private fun index
(key
: K
): Int
565 if l
< _items
.length
and _items
[l
].first
== key
then return l
568 while i
< _items
.length
do
569 if _items
[i
].first
== key
then
579 private class ArrayMapKeys[K
: Object, E
]
580 super RemovableCollection[K
]
582 var map
: ArrayMap[K
, E
]
583 redef fun count
(k
) do if self.has
(k
) then return 1 else return 0
584 redef fun first
do return self.map
._items
.first
.first
585 redef fun has
(k
) do return self.map
.index
(k
) >= 0
586 redef fun has_only
(k
) do return (self.has
(k
) and self.length
== 1) or self.is_empty
587 redef fun is_empty
do return self.map
.is_empty
588 redef fun length
do return self.map
.length
589 redef fun iterator
do return new MapKeysIterator[K
, E
](self.map
.iterator
)
590 redef fun clear
do self.map
.clear
591 redef fun remove
(key
)
593 var i
= self.map
.index
(key
)
594 if i
>= 0 then self.map
.remove_at_index
(i
)
596 redef fun remove_all
(key
) do self.remove
(key
)
599 private class ArrayMapValues[K
: Object, E
]
600 super RemovableCollection[E
]
602 var map
: ArrayMap[K
, E
]
603 redef fun first
do return self.map
._items
.first
.second
604 redef fun is_empty
do return self.map
.is_empty
605 redef fun length
do return self.map
.length
606 redef fun iterator
do return new MapValuesIterator[K
, E
](self.map
.iterator
)
611 for i
in self.map
._items
do if i
.second
== item
then return true
616 redef fun has_only
(item
)
618 for i
in self.map
._items
do if i
.second
!= item
then return false
623 redef fun count
(item
)
626 for i
in self.map
._items
do if i
.second
== item
then nb
+= 1
630 redef fun clear
do self.map
.clear
632 redef fun remove
(item
)
635 var i
= map
._items
.length
- 1
637 if map
._items
[i
].second
== item
then
638 map
.remove_at_index
(i
)
645 redef fun remove_all
(item
)
648 var i
= map
._items
.length
- 1
650 if map
._items
[i
].second
== item
then
651 map
.remove_at_index
(i
)
658 # Comparable array for comparable elements.
660 # For two arrays, if one is a prefix, then it is lower.
663 # var a12 = new ArrayCmp[nullable Int].with_items(1,2)
664 # var a123 = new ArrayCmp[nullable Int].with_items(1,2,3)
668 # Otherwise, the first element just after the longest
669 # common prefix gives the order between the two arrays.
672 # var a124 = new ArrayCmp[nullable Int].with_items(1,2,4)
673 # var a13 = new ArrayCmp[nullable Int].with_items(1,3)
678 # Obviously, two equal arrays are equal.
681 # var b12 = new ArrayCmp[nullable Int].with_items(1,2)
682 # assert (a12 <=> b12) == 0
685 # `null` is considered lower than any other elements.
686 # But is still greater than no element.
689 # var a12n = new ArrayCmp[nullable Int].with_items(1,2,null)
693 class ArrayCmp[E
: nullable Comparable]
696 redef type OTHER: ArrayCmp[E
] is fixed
698 redef fun <(o
) do return (self <=> o
) < 0
708 if l
< ol
then len
= l
else len
= ol
713 if b
== null then return 1
714 var d
= a
<=> b
.as(Comparable)
715 if d
!= 0 then return d
717 if b
!= null then return -1
725 # Others tools ################################################################
727 redef class Iterator[E
]
728 # Interate on `self` and build an array
731 var res
= new Array[E
]
740 redef class Collection[E
]
741 # Build a new array from a collection
744 var res
= new Array[E
].with_capacity
(length
)
750 # Native classes ##############################################################
752 # Subclasses of this class can create native arrays
753 interface ArrayCapable[E
]
754 # Get a new array of `size` elements.
755 protected fun calloc_array
(size
: Int): NativeArray[E
] is intern
759 # Access are unchecked and it has a fixed size
760 # Not for public use: may become private.
761 universal NativeArray[E
]
762 # Creates a new NativeArray of capacity `length`
763 new(length
: Int) is intern
764 # The length of the array
765 fun length
: Int is intern
766 # Use `self` to initialize a standard Nit Array.
767 fun to_a
: Array[E
] do return new Array[E
].with_native
(self, length
)
768 fun [](index
: Int): E
is intern
769 fun []=(index
: Int, item
: E
) is intern
770 fun copy_to
(dest
: NativeArray[E
], length
: Int) is intern
771 #fun =(o: NativeArray[E]): Bool is intern
772 #fun !=(o: NativeArray[E]): Bool is intern