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)
142 # Resizable one dimension array of objects.
143 abstract class AbstractArray[E
]
144 super AbstractArrayRead[E
]
147 # Force the capacity to be at least `cap`.
148 # The capacity of the array is an internal information.
149 # However, this method can be used to prepare a large amount of add
150 fun enlarge
(cap
: Int) is abstract
152 redef fun push
(item
) do add
(item
)
156 assert not_empty
: not is_empty
164 assert not_empty
: not is_empty
176 redef fun unshift
(item
)
186 # Insert an element at a given position, following elements are shifted.
188 # var a= [10, 20, 30, 40]
190 # assert a == [10, 20, 100, 30, 40]
191 fun insert
(item
: E
, pos
: Int)
194 copy_to
(pos
, length-pos
, self, pos
+ 1)
198 redef fun add
(item
) do self[length
] = item
200 redef fun clear
do _length
= 0
202 redef fun remove
(item
) do remove_at
(index_of
(item
))
204 redef fun remove_all
(item
)
206 var i
= index_of
(item
)
209 i
= index_of_from
(item
, i
)
213 redef fun remove_at
(i
)
216 if i
>= 0 and i
< l
then
226 # Invert two elements in the array
228 # var a = [10, 20, 30, 40]
230 # assert a == [10, 40, 30, 20]
231 fun swap_at
(a
: Int,b
: Int)
239 # Resizable one dimension array of objects.
241 # Arrays have a literal representation.
242 # var a = [12, 32, 8]
243 # # is equivalent with:
244 # var b = new Array[Int]
250 super AbstractArray[E
]
251 super ArrayCapable[E
]
255 assert index
: index
>= 0 and index
< _length
259 redef fun []=(index
, item
)
261 assert index
: index
>= 0 and index
< _length
+ 1
262 if _capacity
<= index
then
265 if _length
<= index
then
274 if _capacity
<= l
then
281 redef fun enlarge
(cap
)
284 if cap
<= c
then return
285 while c
<= cap
do c
= c
* 2 + 2
286 var a
= calloc_array
(c
)
287 if _capacity
> 0 then _items
.copy_to
(a
, _length
)
292 # Create an empty array.
299 # Create an array from a collection.
300 init from
(items
: Collection[E
]) do
301 with_capacity
(items
.length
)
305 # Create an array with some `objects`.
306 init with_items
(objects
: E
...)
308 _items
= objects
._items
309 _capacity
= objects
._capacity
310 _length
= objects
.length
313 # Create an empty array with a given capacity.
314 init with_capacity
(cap
: Int)
316 assert positive
: cap
>= 0
317 _items
= calloc_array
(cap
)
322 # Create an array of `count` elements
323 init filled_with
(value
: E
, count
: Int)
325 assert positive
: count
>= 0
326 _items
= calloc_array
(count
)
336 # Create a array filled with a given native array.
337 init with_native
(nat
: NativeArray[E
], size
: Int)
339 assert positive
: size
>= 0
345 # The internal storage.
346 var _items
: nullable NativeArray[E
] = null
348 # Do not use this method
349 # FIXME: Remove it once modules can intrude non local modules
350 fun intern_items
: NativeArray[E
] do return _items
.as(not null)
352 # The size of `_items`.
353 var _capacity
: Int = 0
356 # An `Iterator` on `AbstractArray`
357 class ArrayIterator[E
]
358 super IndexedIterator[E
]
360 redef fun item
do return _array
[_index
]
362 # redef fun item=(e) do _array[_index] = e
364 redef fun is_ok
do return _index
< _array
.length
366 redef fun next
do _index
+= 1
368 init(a
: AbstractArrayRead[E
])
374 redef readable var _index
: Int = 0
375 var _array
: AbstractArrayRead[E
]
378 # Others collections ##########################################################
380 # A set implemented with an Array.
381 class ArraySet[E
: Object]
384 # The stored elements.
387 redef fun has
(e
) do return _array
.has
(e
)
389 redef fun add
(e
) do if not _array
.has
(e
) then _array
.add
(e
)
391 redef fun is_empty
do return _array
.is_empty
393 redef fun length
do return _array
.length
397 assert _array
.length
> 0
401 redef fun remove
(item
)
403 var i
= _array
.index_of
(item
)
404 if i
>= 0 then remove_at
(i
)
407 redef fun remove_all
(item
) do remove
(item
)
409 redef fun clear
do _array
.clear
411 redef fun iterator
do return new ArraySetIterator[E
](_array
.iterator
)
413 # Assume the capacity is at least `cap`.
414 fun enlarge
(cap
: Int) do _array
.enlarge
(cap
)
416 private fun remove_at
(i
: Int)
418 _array
[i
] = _array
.last
422 # Create an empty set
423 init do _array
= new Array[E
]
425 # Create an empty set with a given capacity.
426 init with_capacity
(i
: Int) do _array
= new Array[E
].with_capacity
(i
)
428 redef fun new_set
do return new ArraySet[E
]
431 # Iterators on sets implemented with arrays.
432 class ArraySetIterator[E
: Object]
435 redef fun is_ok
do return _iter
.is_ok
437 redef fun next
do _iter
.next
439 redef fun item
: E
do return _iter
.item
441 init(iter
: ArrayIterator[E
]) do _iter
= iter
443 var _iter
: ArrayIterator[E
]
447 # Associative arrays implemented with an array of (key, value) pairs.
448 class ArrayMap[K
: Object, E
]
449 super CoupleMap[K
, E
]
456 return _items
[i
].second
463 redef fun []=(key
, item
)
467 _items
[i
].second
= item
469 _items
.push
(new Couple[K
,E
](key
, item
))
473 redef var keys
: ArrayMapKeys[K
, E
] = new ArrayMapKeys[K
, E
](self)
474 redef var values
: ArrayMapValues[K
, E
] = new ArrayMapValues[K
, E
](self)
477 redef fun length
do return _items
.length
479 redef fun iterator
: CoupleMapIterator[K
, E
] do return new CoupleMapIterator[K
, E
](_items
.iterator
)
481 redef fun is_empty
do return _items
.is_empty
483 redef fun clear
do _items
.clear
485 # Assume the capacity to be at least `cap`.
486 fun enlarge
(cap
: Int) do _items
.enlarge
(cap
)
488 redef fun couple_at
(key
)
499 var _items
: Array[Couple[K
,E
]]
501 # fast remove the ith element of the array
502 private fun remove_at_index
(i
: Int)
504 _items
[i
] = _items
.last
508 # The last positive result given by a index(1) call
509 var _last_index
: Int = 0
511 # Where is the `key` in `_item`?
512 # return -1 if not found
513 private fun index
(key
: K
): Int
516 if l
< _items
.length
and _items
[l
].first
== key
then return l
519 while i
< _items
.length
do
520 if _items
[i
].first
== key
then
532 _items
= new Array[Couple[K
,E
]]
536 class ArrayMapKeys[K
: Object, E
]
537 super RemovableCollection[K
]
539 var map
: ArrayMap[K
, E
]
540 redef fun count
(k
) do if self.has
(k
) then return 1 else return 0
541 redef fun first
do return self.map
._items
.first
.first
542 redef fun has
(k
) do return self.map
.index
(k
) >= 0
543 redef fun has_only
(k
) do return (self.has
(k
) and self.length
== 1) or self.is_empty
544 redef fun is_empty
do return self.map
.is_empty
545 redef fun length
do return self.map
.length
546 redef fun iterator
do return new MapKeysIterator[K
, E
](self.map
.iterator
)
547 redef fun clear
do self.map
.clear
548 redef fun remove
(key
)
550 var i
= self.map
.index
(key
)
551 if i
>= 0 then self.map
.remove_at_index
(i
)
553 redef fun remove_all
(key
) do self.remove
(key
)
556 class ArrayMapValues[K
: Object, E
]
557 super RemovableCollection[E
]
559 var map
: ArrayMap[K
, E
]
560 redef fun first
do return self.map
._items
.first
.second
561 redef fun is_empty
do return self.map
.is_empty
562 redef fun length
do return self.map
.length
563 redef fun iterator
do return new MapValuesIterator[K
, E
](self.map
.iterator
)
568 for i
in self.map
._items
do if i
.second
== item
then return true
573 redef fun has_only
(item
)
575 for i
in self.map
._items
do if i
.second
!= item
then return false
580 redef fun count
(item
)
583 for i
in self.map
._items
do if i
.second
== item
then nb
+= 1
587 redef fun clear
do self.map
.clear
589 redef fun remove
(item
)
592 var i
= map
._items
.length
- 1
594 if map
._items
[i
].second
== item
then
595 map
.remove_at_index
(i
)
602 redef fun remove_all
(item
)
605 var i
= map
._items
.length
- 1
607 if map
._items
[i
].second
== item
then
608 map
.remove_at_index
(i
)
616 # Others tools ################################################################
618 redef class Iterator[E
]
619 # Interate on `self` and build an array
622 var res
= new Array[E
]
631 redef class Collection[E
]
632 # Build a new array from a collection
639 # Native classes ##############################################################
641 # Subclasses of this class can create native arrays
642 interface ArrayCapable[E
]
643 # Get a new array of `size` elements.
644 protected fun calloc_array
(size
: Int): NativeArray[E
] is intern
647 # Native C array (void ...).
648 universal NativeArray[E
]
649 fun [](index
: Int): E
is intern
650 fun []=(index
: Int, item
: E
) is intern
651 fun copy_to
(dest
: NativeArray[E
], length
: Int) is intern
652 #fun =(o: NativeArray[E]): Bool is intern
653 #fun !=(o: NativeArray[E]): Bool is intern