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
]
25 redef fun length
do return _length
27 redef fun is_empty
do return _length
== 0
34 if self[i
] == item
then return true
40 redef fun has_only
(item
)
45 if self[i
] != item
then return false
57 if self[i
] == item
then res
+= 1
63 redef fun index_of
(item
) do return index_of_from
(item
, 0)
65 redef fun last_index_of
(item
: E
): Int do return last_index_of_from
(item
, length-1
)
67 redef fun index_of_from
(item
: E
, pos
: Int): Int
72 if self[i
] == item
then
80 redef fun last_index_of_from
(item
: E
, pos
: Int): Int
84 if self[i
] == item
then
93 # Return a new array that is the reverse of `self`
95 # assert [1,2,3].reversed == [3, 2, 1]
96 fun reversed
: Array[E
]
99 var result
= new Array[E
].with_capacity
(cmp
)
102 result
.add
(self[cmp
])
107 # Copy a portion of `self` to an other array.
109 # var a = [1, 2, 3, 4]
110 # var b = [10, 20, 30, 40, 50]
111 # a.copy_to(1, 2, b, 2)
112 # assert b == [10, 20, 2, 3, 50]
113 protected fun copy_to
(start
: Int, len
: Int, dest
: AbstractArray[E
], new_start
: Int)
119 dest
[new_start
+i
] = self[start
+i
]
129 if e
!= null then e
.output
134 redef fun iterator
: ArrayIterator[E
] do return new ArrayIterator[E
](self)
135 redef fun reverse_iterator
do return new ArrayReverseIterator[E
](self)
138 # Resizable one dimension array of objects.
139 abstract class AbstractArray[E
]
140 super AbstractArrayRead[E
]
143 # Force the capacity to be at least `cap`.
144 # The capacity of the array is an internal information.
145 # However, this method can be used to prepare a large amount of add
146 fun enlarge
(cap
: Int) is abstract
148 redef fun push
(item
) do add
(item
)
152 assert not_empty
: not is_empty
160 assert not_empty
: not is_empty
172 redef fun unshift
(item
)
182 redef fun insert
(item
: E
, pos
: Int)
185 copy_to
(pos
, length-pos
, self, pos
+ 1)
189 redef fun insert_all
(coll
, pos
)
192 if l
== 0 then return
195 copy_to
(pos
, length-pos-l
, self, pos
+ l
)
202 redef fun add
(item
) do self[length
] = item
204 redef fun clear
do _length
= 0
206 redef fun remove
(item
) do remove_at
(index_of
(item
))
208 redef fun remove_all
(item
)
210 var i
= index_of
(item
)
213 i
= index_of_from
(item
, i
)
217 redef fun remove_at
(i
)
220 if i
>= 0 and i
< l
then
230 # Invert two elements in the array
232 # var a = [10, 20, 30, 40]
234 # assert a == [10, 40, 30, 20]
235 fun swap_at
(a
: Int,b
: Int)
243 # Resizable one dimension array of objects.
245 # 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
]
255 super ArrayCapable[E
]
259 assert index
: index
>= 0 and index
< _length
263 redef fun []=(index
, item
)
265 assert index
: index
>= 0 and index
< _length
+ 1
266 if _capacity
<= index
then
269 if _length
<= index
then
278 if _capacity
<= l
then
285 redef fun enlarge
(cap
)
288 if cap
<= c
then return
289 while c
<= cap
do c
= c
* 2 + 2
290 var a
= calloc_array
(c
)
291 if _capacity
> 0 then _items
.copy_to
(a
, _length
)
296 # Create an empty array.
303 # Create an array from a collection.
304 init from
(items
: Collection[E
]) do
305 with_capacity
(items
.length
)
309 # Create an array with some `objects`.
310 init with_items
(objects
: E
...)
312 _items
= objects
._items
313 _capacity
= objects
._capacity
314 _length
= objects
.length
317 # Create an empty array with a given capacity.
318 init with_capacity
(cap
: Int)
320 assert positive
: cap
>= 0
321 _items
= calloc_array
(cap
)
326 # Create an array of `count` elements
327 init filled_with
(value
: E
, count
: Int)
329 assert positive
: count
>= 0
330 _items
= calloc_array
(count
)
340 # Create a array filled with a given native array.
341 init with_native
(nat
: NativeArray[E
], size
: Int)
343 assert positive
: size
>= 0
349 # The internal storage.
350 var _items
: nullable NativeArray[E
] = null
352 # Do not use this method
353 # FIXME: Remove it once modules can intrude non local modules
354 fun intern_items
: NativeArray[E
] do return _items
.as(not null)
356 # The size of `_items`.
357 var _capacity
: Int = 0
361 if not o
isa Array[nullable Object] then return super
362 # Efficient implementation
364 if l
!= o
.length
then return false
369 if it
[i
] != oit
[i
] then return false
376 # An `Iterator` on `AbstractArray`
377 private class ArrayIterator[E
]
378 super IndexedIterator[E
]
380 redef fun item
do return _array
[_index
]
382 # redef fun item=(e) do _array[_index] = e
384 redef fun is_ok
do return _index
< _array
.length
386 redef fun next
do _index
+= 1
388 init(a
: AbstractArrayRead[E
])
395 redef fun index
do return _index
396 var _array
: AbstractArrayRead[E
]
399 private class ArrayReverseIterator[E
]
400 super ArrayIterator[E
]
402 redef fun is_ok
do return _index
>= 0
404 redef fun next
do _index
-= 1
406 init(a
: AbstractArrayRead[E
])
409 _index
= a
.length
- 1
413 # Others collections ##########################################################
415 # A set implemented with an Array.
416 class ArraySet[E
: Object]
419 # The stored elements.
422 redef fun has
(e
) do return _array
.has
(e
)
424 redef fun add
(e
) do if not _array
.has
(e
) then _array
.add
(e
)
426 redef fun is_empty
do return _array
.is_empty
428 redef fun length
do return _array
.length
432 assert _array
.length
> 0
436 redef fun remove
(item
)
438 var i
= _array
.index_of
(item
)
439 if i
>= 0 then remove_at
(i
)
442 redef fun remove_all
(item
) do remove
(item
)
444 redef fun clear
do _array
.clear
446 redef fun iterator
do return new ArraySetIterator[E
](_array
.iterator
)
448 # Assume the capacity is at least `cap`.
449 fun enlarge
(cap
: Int) do _array
.enlarge
(cap
)
451 private fun remove_at
(i
: Int)
453 _array
[i
] = _array
.last
457 # Create an empty set
458 init do _array
= new Array[E
]
460 # Create an empty set with a given capacity.
461 init with_capacity
(i
: Int) do _array
= new Array[E
].with_capacity
(i
)
463 redef fun new_set
do return new ArraySet[E
]
466 # Iterators on sets implemented with arrays.
467 private class ArraySetIterator[E
: Object]
470 redef fun is_ok
do return _iter
.is_ok
472 redef fun next
do _iter
.next
474 redef fun item
: E
do return _iter
.item
476 init(iter
: ArrayIterator[E
]) do _iter
= iter
478 var _iter
: ArrayIterator[E
]
482 # Associative arrays implemented with an array of (key, value) pairs.
483 class ArrayMap[K
: Object, E
]
484 super CoupleMap[K
, E
]
491 return _items
[i
].second
493 return provide_default_value
(key
)
498 redef fun []=(key
, item
)
502 _items
[i
].second
= item
504 _items
.push
(new Couple[K
,E
](key
, item
))
508 redef var keys
: RemovableCollection[K
] = new ArrayMapKeys[K
, E
](self)
509 redef var values
: RemovableCollection[E
] = new ArrayMapValues[K
, E
](self)
512 redef fun length
do return _items
.length
514 redef fun couple_iterator
do return _items
.iterator
516 redef fun is_empty
do return _items
.is_empty
518 redef fun clear
do _items
.clear
520 # Assume the capacity to be at least `cap`.
521 fun enlarge
(cap
: Int) do _items
.enlarge
(cap
)
523 redef fun couple_at
(key
)
534 var _items
= new Array[Couple[K
,E
]]
536 # fast remove the ith element of the array
537 private fun remove_at_index
(i
: Int)
539 _items
[i
] = _items
.last
543 # The last positive result given by a index(1) call
544 var _last_index
: Int = 0
546 # Where is the `key` in `_item`?
547 # return -1 if not found
548 private fun index
(key
: K
): Int
551 if l
< _items
.length
and _items
[l
].first
== key
then return l
554 while i
< _items
.length
do
555 if _items
[i
].first
== key
then
565 private class ArrayMapKeys[K
: Object, E
]
566 super RemovableCollection[K
]
568 var map
: ArrayMap[K
, E
]
569 redef fun count
(k
) do if self.has
(k
) then return 1 else return 0
570 redef fun first
do return self.map
._items
.first
.first
571 redef fun has
(k
) do return self.map
.index
(k
) >= 0
572 redef fun has_only
(k
) do return (self.has
(k
) and self.length
== 1) or self.is_empty
573 redef fun is_empty
do return self.map
.is_empty
574 redef fun length
do return self.map
.length
575 redef fun iterator
do return new MapKeysIterator[K
, E
](self.map
.iterator
)
576 redef fun clear
do self.map
.clear
577 redef fun remove
(key
)
579 var i
= self.map
.index
(key
)
580 if i
>= 0 then self.map
.remove_at_index
(i
)
582 redef fun remove_all
(key
) do self.remove
(key
)
585 private class ArrayMapValues[K
: Object, E
]
586 super RemovableCollection[E
]
588 var map
: ArrayMap[K
, E
]
589 redef fun first
do return self.map
._items
.first
.second
590 redef fun is_empty
do return self.map
.is_empty
591 redef fun length
do return self.map
.length
592 redef fun iterator
do return new MapValuesIterator[K
, E
](self.map
.iterator
)
597 for i
in self.map
._items
do if i
.second
== item
then return true
602 redef fun has_only
(item
)
604 for i
in self.map
._items
do if i
.second
!= item
then return false
609 redef fun count
(item
)
612 for i
in self.map
._items
do if i
.second
== item
then nb
+= 1
616 redef fun clear
do self.map
.clear
618 redef fun remove
(item
)
621 var i
= map
._items
.length
- 1
623 if map
._items
[i
].second
== item
then
624 map
.remove_at_index
(i
)
631 redef fun remove_all
(item
)
634 var i
= map
._items
.length
- 1
636 if map
._items
[i
].second
== item
then
637 map
.remove_at_index
(i
)
644 # Comparable array for comparable elements.
646 # For two arrays, if one is a prefix, then it is lower.
649 # var a12 = new ArrayCmp[nullable Int].with_items(1,2)
650 # var a123 = new ArrayCmp[nullable Int].with_items(1,2,3)
654 # Otherwise, the first element just after the longest
655 # common prefix gives the order between the two arrays.
658 # var a124 = new ArrayCmp[nullable Int].with_items(1,2,4)
659 # var a13 = new ArrayCmp[nullable Int].with_items(1,3)
664 # Obviously, two equal arrays are equal.
667 # var b12 = new ArrayCmp[nullable Int].with_items(1,2)
668 # assert (a12 <=> b12) == 0
671 # `null` is considered lower than any other elements.
672 # But is still greater than no element.
675 # var a12n = new ArrayCmp[nullable Int].with_items(1,2,null)
679 class ArrayCmp[E
: nullable Comparable]
682 redef type OTHER: ArrayCmp[E
] is fixed
684 redef fun <(o
) do return (self <=> o
) < 0
694 if l
< ol
then len
= l
else len
= ol
699 if b
== null then return 1
700 var d
= a
<=> b
.as(Comparable)
701 if d
!= 0 then return d
703 if b
!= null then return -1
711 # Others tools ################################################################
713 redef class Iterator[E
]
714 # Interate on `self` and build an array
717 var res
= new Array[E
]
726 redef class Collection[E
]
727 # Build a new array from a collection
730 var res
= new Array[E
].with_capacity
(length
)
736 # Native classes ##############################################################
738 # Subclasses of this class can create native arrays
739 interface ArrayCapable[E
]
740 # Get a new array of `size` elements.
741 protected fun calloc_array
(size
: Int): NativeArray[E
] is intern
745 # Access are unchecked and it has a fixed size
746 # Not for public use: may become private.
747 universal NativeArray[E
]
748 # The length of the array
749 fun length
: Int is intern
750 # Use `self` to initialize a standard Nit Array.
751 fun to_a
: Array[E
] do return new Array[E
].with_native
(self, length
)
752 fun [](index
: Int): E
is intern
753 fun []=(index
: Int, item
: E
) is intern
754 fun copy_to
(dest
: NativeArray[E
], length
: Int) is intern
755 #fun =(o: NativeArray[E]): Bool is intern
756 #fun !=(o: NativeArray[E]): Bool is intern