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
]
24 redef readable var _length
: Int = 0
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 # The index of the last occurrence of an element.
65 # Return -1 if not found.
66 fun last_index_of
(item
: E
): Int do return last_index_of_from
(item
, length-1
)
68 # The index of the first occurrence of an element starting from pos.
69 # Return -1 if not found.
70 fun index_of_from
(item
: E
, pos
: Int): Int
75 if self[i
] == item
then
83 # The index of the last occurrence of an element starting from pos.
84 # Return -1 if not found.
85 fun last_index_of_from
(item
: E
, pos
: Int): Int
89 if self[i
] == item
then
98 # Return a new array that is the reverse of `self`
100 # assert [1,2,3].reversed == [3, 2, 1]
101 fun reversed
: Array[E
]
104 var result
= new Array[E
].with_capacity
(cmp
)
107 result
.add
(self[cmp
])
112 # Copy a portion of `self` to an other array.
114 # var a = [1, 2, 3, 4]
115 # var b = [10, 20, 30, 40, 50]
116 # a.copy_to(1, 2, b, 2)
117 # assert b == [10, 20, 2, 3, 50]
118 protected fun copy_to
(start
: Int, len
: Int, dest
: AbstractArray[E
], new_start
: Int)
124 dest
[new_start
+i
] = self[start
+i
]
134 if e
!= null then e
.output
139 redef fun iterator
: ArrayIterator[E
] do return new ArrayIterator[E
](self)
141 # Two arrays are equals if they have the same items in the same order.
144 if not o
isa AbstractArray[nullable Object] or o
is null then return false
146 if o
.length
!= l
then return false
149 if self[i
] != o
[i
] then return false
156 # Resizable one dimension array of objects.
157 abstract class AbstractArray[E
]
158 super AbstractArrayRead[E
]
161 # Force the capacity to be at least `cap`.
162 # The capacity of the array is an internal information.
163 # However, this method can be used to prepare a large amount of add
164 fun enlarge
(cap
: Int) is abstract
166 redef fun push
(item
) do add
(item
)
170 assert not_empty
: not is_empty
178 assert not_empty
: not is_empty
190 redef fun unshift
(item
)
200 # Insert an element at a given position, following elements are shifted.
202 # var a= [10, 20, 30, 40]
204 # assert a == [10, 20, 100, 30, 40]
205 fun insert
(item
: E
, pos
: Int)
208 copy_to
(pos
, length-pos
, self, pos
+ 1)
212 redef fun add
(item
) do self[length
] = item
214 redef fun clear
do _length
= 0
216 redef fun remove
(item
) do remove_at
(index_of
(item
))
218 redef fun remove_all
(item
)
220 var i
= index_of
(item
)
223 i
= index_of_from
(item
, i
)
227 redef fun remove_at
(i
)
230 if i
>= 0 and i
< l
then
240 # Invert two elements in the array
242 # var a = [10, 20, 30, 40]
244 # assert a == [10, 40, 30, 20]
245 fun swap_at
(a
: Int,b
: Int)
253 # Resizable one dimension array of objects.
255 # Arrays have a literal representation.
256 # var a = [12, 32, 8]
257 # # is equivalent with:
258 # var b = new Array[Int]
264 super AbstractArray[E
]
265 super ArrayCapable[E
]
281 assert index
: index
>= 0 and index
< _length
285 redef fun []=(index
, item
)
287 assert index
: index
>= 0 and index
< _length
+ 1
288 if _capacity
<= index
then
291 if _length
<= index
then
300 if _capacity
<= l
then
307 redef fun enlarge
(cap
)
310 if cap
<= c
then return
311 while c
<= cap
do c
= c
* 2 + 2
312 var a
= calloc_array
(c
)
313 if _capacity
> 0 then _items
.copy_to
(a
, _length
)
318 # Create an empty array.
325 # Create an array from a collection.
326 init from
(items
: Collection[E
]) do
327 with_capacity
(items
.length
)
331 # Create an array with some `objects`.
332 init with_items
(objects
: E
...)
334 _items
= objects
._items
335 _capacity
= objects
._capacity
336 _length
= objects
.length
339 # Create an empty array with a given capacity.
340 init with_capacity
(cap
: Int)
342 assert positive
: cap
>= 0
343 _items
= calloc_array
(cap
)
348 # Create an array of `count` elements
349 init filled_with
(value
: E
, count
: Int)
351 assert positive
: count
>= 0
352 _items
= calloc_array
(count
)
362 # Create a array filled with a given native array.
363 init with_native
(nat
: NativeArray[E
], size
: Int)
365 assert positive
: size
>= 0
371 # The internal storage.
372 var _items
: nullable NativeArray[E
] = null
374 # Do not use this method
375 # FIXME: Remove it once modules can intrude non local modules
376 fun intern_items
: NativeArray[E
] do return _items
.as(not null)
378 # The size of `_items`.
379 var _capacity
: Int = 0
381 # Sort the array using the !cmp function.
385 sub_sort
(0, length-1
) !cmp
(x
,y
) = cmp
(x
, y
)
388 # Sort `array` between `from` and `to` indices
389 private fun sub_sort
(from
: Int, to
: Int)
394 else if from
+ 7 < to
then
395 var pivot
= self[from
]
399 while i
<= to
and cmp
(self[i
], pivot
) <= 0 do i
+= 1
400 while j
> i
and cmp
(self[j
], pivot
) >= 0 do j
-= 1
407 self[from
] = self[i-1
]
409 sub_sort
(from
, i-2
) !cmp
(x
,y
) = cmp
(x
, y
)
410 sub_sort
(i
, to
) !cmp
(x
,y
) = cmp
(x
, y
)
418 if cmp
(min_v
, self[j
]) > 0 then
434 # An `Iterator` on `AbstractArray`
435 class ArrayIterator[E
]
436 super IndexedIterator[E
]
438 redef fun item
do return _array
[_index
]
440 # redef fun item=(e) do _array[_index] = e
442 redef fun is_ok
do return _index
< _array
.length
444 redef fun next
do _index
+= 1
446 init(a
: AbstractArrayRead[E
])
452 redef readable var _index
: Int = 0
453 var _array
: AbstractArrayRead[E
]
456 # Others collections ##########################################################
458 # A set implemented with an Array.
459 class ArraySet[E
: Object]
462 # The stored elements.
465 redef fun has
(e
) do return _array
.has
(e
)
467 redef fun add
(e
) do if not _array
.has
(e
) then _array
.add
(e
)
469 redef fun is_empty
do return _array
.is_empty
471 redef fun length
do return _array
.length
475 assert _array
.length
> 0
479 redef fun remove
(item
)
481 var i
= _array
.index_of
(item
)
482 if i
>= 0 then remove_at
(i
)
485 redef fun remove_all
(item
) do remove
(item
)
487 redef fun clear
do _array
.clear
489 redef fun iterator
do return new ArraySetIterator[E
](_array
.iterator
)
491 # Assume the capacity is at least `cap`.
492 fun enlarge
(cap
: Int) do _array
.enlarge
(cap
)
494 private fun remove_at
(i
: Int)
496 _array
[i
] = _array
.last
500 # Create an empty set
501 init do _array
= new Array[E
]
503 # Create an empty set with a given capacity.
504 init with_capacity
(i
: Int) do _array
= new Array[E
].with_capacity
(i
)
507 # Iterators on sets implemented with arrays.
508 class ArraySetIterator[E
: Object]
511 redef fun is_ok
do return _iter
.is_ok
513 redef fun next
do _iter
.next
515 redef fun item
: E
do return _iter
.item
517 init(iter
: ArrayIterator[E
]) do _iter
= iter
519 var _iter
: ArrayIterator[E
]
523 # Associative arrays implemented with an array of (key, value) pairs.
524 class ArrayMap[K
: Object, E
]
525 super CoupleMap[K
, E
]
532 return _items
[i
].second
539 redef fun []=(key
, item
)
543 _items
[i
].second
= item
545 _items
.push
(new Couple[K
,E
](key
, item
))
549 redef var keys
: ArrayMapKeys[K
, E
] = new ArrayMapKeys[K
, E
](self)
550 redef var values
: ArrayMapValues[K
, E
] = new ArrayMapValues[K
, E
](self)
553 redef fun length
do return _items
.length
555 redef fun iterator
: CoupleMapIterator[K
, E
] do return new CoupleMapIterator[K
, E
](_items
.iterator
)
557 redef fun is_empty
do return _items
.is_empty
559 redef fun clear
do _items
.clear
561 # Assume the capacity to be at least `cap`.
562 fun enlarge
(cap
: Int) do _items
.enlarge
(cap
)
564 redef fun couple_at
(key
)
575 var _items
: Array[Couple[K
,E
]]
577 # fast remove the ith element of the array
578 private fun remove_at_index
(i
: Int)
580 _items
[i
] = _items
.last
584 # The last positive result given by a index(1) call
585 var _last_index
: Int = 0
587 # Where is the `key` in `_item`?
588 # return -1 if not found
589 private fun index
(key
: K
): Int
592 if l
< _items
.length
and _items
[l
].first
== key
then return l
595 while i
< _items
.length
do
596 if _items
[i
].first
== key
then
608 _items
= new Array[Couple[K
,E
]]
612 class ArrayMapKeys[K
: Object, E
]
613 super RemovableCollection[K
]
615 var map
: ArrayMap[K
, E
]
616 redef fun count
(k
) do if self.has
(k
) then return 1 else return 0
617 redef fun first
do return self.map
._items
.first
.first
618 redef fun has
(k
) do return self.map
.index
(k
) >= 0
619 redef fun has_only
(k
) do return (self.has
(k
) and self.length
== 1) or self.is_empty
620 redef fun is_empty
do return self.map
.is_empty
621 redef fun length
do return self.map
.length
622 redef fun iterator
do return new MapKeysIterator[K
, E
](self.map
.iterator
)
623 redef fun clear
do self.map
.clear
624 redef fun remove
(key
)
626 var i
= self.map
.index
(key
)
627 if i
>= 0 then self.map
.remove_at_index
(i
)
629 redef fun remove_all
(key
) do self.remove
(key
)
632 class ArrayMapValues[K
: Object, E
]
633 super RemovableCollection[E
]
635 var map
: ArrayMap[K
, E
]
636 redef fun first
do return self.map
._items
.first
.second
637 redef fun is_empty
do return self.map
.is_empty
638 redef fun length
do return self.map
.length
639 redef fun iterator
do return new MapValuesIterator[K
, E
](self.map
.iterator
)
644 for i
in self.map
._items
do if i
.second
== item
then return true
649 redef fun has_only
(item
)
651 for i
in self.map
._items
do if i
.second
!= item
then return false
656 redef fun count
(item
)
659 for i
in self.map
._items
do if i
.second
== item
then nb
+= 1
663 redef fun clear
do self.map
.clear
665 redef fun remove
(item
)
668 var i
= map
._items
.length
- 1
670 if map
._items
[i
].second
== item
then
671 map
.remove_at_index
(i
)
678 redef fun remove_all
(item
)
681 var i
= map
._items
.length
- 1
683 if map
._items
[i
].second
== item
then
684 map
.remove_at_index
(i
)
692 # Others tools ################################################################
694 redef class Iterator[E
]
695 # Interate on `self` and build an array
698 var res
= new Array[E
]
707 redef class Collection[E
]
708 # Build a new array from a collection
715 # Native classes ##############################################################
717 # Subclasses of this class can create native arrays
718 interface ArrayCapable[E
]
719 # Get a new array of `size` elements.
720 protected fun calloc_array
(size
: Int): NativeArray[E
] is intern
723 # Native C array (void ...).
724 universal NativeArray[E
]
725 fun [](index
: Int): E
is intern
726 fun []=(index
: Int, item
: E
) is intern
727 fun copy_to
(dest
: NativeArray[E
], length
: Int) is intern
728 #fun =(o: NativeArray[E]): Bool is intern
729 #fun !=(o: NativeArray[E]): Bool is intern