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
]
267 assert index
: index
>= 0 and index
< _length
271 redef fun []=(index
, item
)
273 assert index
: index
>= 0 and index
< _length
+ 1
274 if _capacity
<= index
then
277 if _length
<= index
then
286 if _capacity
<= l
then
293 redef fun enlarge
(cap
)
296 if cap
<= c
then return
297 while c
<= cap
do c
= c
* 2 + 2
298 var a
= calloc_array
(c
)
299 if _capacity
> 0 then _items
.copy_to
(a
, _length
)
304 # Create an empty array.
311 # Create an array from a collection.
312 init from
(items
: Collection[E
]) do
313 with_capacity
(items
.length
)
317 # Create an array with some `objects`.
318 init with_items
(objects
: E
...)
320 _items
= objects
._items
321 _capacity
= objects
._capacity
322 _length
= objects
.length
325 # Create an empty array with a given capacity.
326 init with_capacity
(cap
: Int)
328 assert positive
: cap
>= 0
329 _items
= calloc_array
(cap
)
334 # Create an array of `count` elements
335 init filled_with
(value
: E
, count
: Int)
337 assert positive
: count
>= 0
338 _items
= calloc_array
(count
)
348 # Create a array filled with a given native array.
349 init with_native
(nat
: NativeArray[E
], size
: Int)
351 assert positive
: size
>= 0
357 # The internal storage.
358 var _items
: nullable NativeArray[E
] = null
360 # Do not use this method
361 # FIXME: Remove it once modules can intrude non local modules
362 fun intern_items
: NativeArray[E
] do return _items
.as(not null)
364 # The size of `_items`.
365 var _capacity
: Int = 0
367 # Sort the array using the !cmp function.
371 sub_sort
(0, length-1
) !cmp
(x
,y
) = cmp
(x
, y
)
374 # Sort `array` between `from` and `to` indices
375 private fun sub_sort
(from
: Int, to
: Int)
380 else if from
+ 7 < to
then
381 var pivot
= self[from
]
385 while i
<= to
and cmp
(self[i
], pivot
) <= 0 do i
+= 1
386 while j
> i
and cmp
(self[j
], pivot
) >= 0 do j
-= 1
393 self[from
] = self[i-1
]
395 sub_sort
(from
, i-2
) !cmp
(x
,y
) = cmp
(x
, y
)
396 sub_sort
(i
, to
) !cmp
(x
,y
) = cmp
(x
, y
)
404 if cmp
(min_v
, self[j
]) > 0 then
420 # An `Iterator` on `AbstractArray`
421 class ArrayIterator[E
]
422 super IndexedIterator[E
]
424 redef fun item
do return _array
[_index
]
426 # redef fun item=(e) do _array[_index] = e
428 redef fun is_ok
do return _index
< _array
.length
430 redef fun next
do _index
+= 1
432 init(a
: AbstractArrayRead[E
])
438 redef readable var _index
: Int = 0
439 var _array
: AbstractArrayRead[E
]
442 # Others collections ##########################################################
444 # A set implemented with an Array.
445 class ArraySet[E
: Object]
448 # The stored elements.
451 redef fun has
(e
) do return _array
.has
(e
)
453 redef fun add
(e
) do if not _array
.has
(e
) then _array
.add
(e
)
455 redef fun is_empty
do return _array
.is_empty
457 redef fun length
do return _array
.length
461 assert _array
.length
> 0
465 redef fun remove
(item
)
467 var i
= _array
.index_of
(item
)
468 if i
>= 0 then remove_at
(i
)
471 redef fun remove_all
(item
) do remove
(item
)
473 redef fun clear
do _array
.clear
475 redef fun iterator
do return new ArraySetIterator[E
](_array
.iterator
)
477 # Assume the capacity is at least `cap`.
478 fun enlarge
(cap
: Int) do _array
.enlarge
(cap
)
480 private fun remove_at
(i
: Int)
482 _array
[i
] = _array
.last
486 # Create an empty set
487 init do _array
= new Array[E
]
489 # Create an empty set with a given capacity.
490 init with_capacity
(i
: Int) do _array
= new Array[E
].with_capacity
(i
)
493 # Iterators on sets implemented with arrays.
494 class ArraySetIterator[E
: Object]
497 redef fun is_ok
do return _iter
.is_ok
499 redef fun next
do _iter
.next
501 redef fun item
: E
do return _iter
.item
503 init(iter
: ArrayIterator[E
]) do _iter
= iter
505 var _iter
: ArrayIterator[E
]
509 # Associative arrays implemented with an array of (key, value) pairs.
510 class ArrayMap[K
: Object, E
]
511 super CoupleMap[K
, E
]
518 return _items
[i
].second
525 redef fun []=(key
, item
)
529 _items
[i
].second
= item
531 _items
.push
(new Couple[K
,E
](key
, item
))
535 redef var keys
: ArrayMapKeys[K
, E
] = new ArrayMapKeys[K
, E
](self)
536 redef var values
: ArrayMapValues[K
, E
] = new ArrayMapValues[K
, E
](self)
539 redef fun length
do return _items
.length
541 redef fun iterator
: CoupleMapIterator[K
, E
] do return new CoupleMapIterator[K
, E
](_items
.iterator
)
543 redef fun is_empty
do return _items
.is_empty
545 redef fun clear
do _items
.clear
547 # Assume the capacity to be at least `cap`.
548 fun enlarge
(cap
: Int) do _items
.enlarge
(cap
)
550 redef fun couple_at
(key
)
561 var _items
: Array[Couple[K
,E
]]
563 # fast remove the ith element of the array
564 private fun remove_at_index
(i
: Int)
566 _items
[i
] = _items
.last
570 # The last positive result given by a index(1) call
571 var _last_index
: Int = 0
573 # Where is the `key` in `_item`?
574 # return -1 if not found
575 private fun index
(key
: K
): Int
578 if l
< _items
.length
and _items
[l
].first
== key
then return l
581 while i
< _items
.length
do
582 if _items
[i
].first
== key
then
594 _items
= new Array[Couple[K
,E
]]
598 class ArrayMapKeys[K
: Object, E
]
599 super RemovableCollection[K
]
601 var map
: ArrayMap[K
, E
]
602 redef fun count
(k
) do if self.has
(k
) then return 1 else return 0
603 redef fun first
do return self.map
._items
.first
.first
604 redef fun has
(k
) do return self.map
.index
(k
) >= 0
605 redef fun has_only
(k
) do return (self.has
(k
) and self.length
== 1) or self.is_empty
606 redef fun is_empty
do return self.map
.is_empty
607 redef fun length
do return self.map
.length
608 redef fun iterator
do return new MapKeysIterator[K
, E
](self.map
.iterator
)
609 redef fun clear
do self.map
.clear
610 redef fun remove
(key
)
612 var i
= self.map
.index
(key
)
613 if i
>= 0 then self.map
.remove_at_index
(i
)
615 redef fun remove_all
(key
) do self.remove
(key
)
618 class ArrayMapValues[K
: Object, E
]
619 super RemovableCollection[E
]
621 var map
: ArrayMap[K
, E
]
622 redef fun first
do return self.map
._items
.first
.second
623 redef fun is_empty
do return self.map
.is_empty
624 redef fun length
do return self.map
.length
625 redef fun iterator
do return new MapValuesIterator[K
, E
](self.map
.iterator
)
630 for i
in self.map
._items
do if i
.second
== item
then return true
635 redef fun has_only
(item
)
637 for i
in self.map
._items
do if i
.second
!= item
then return false
642 redef fun count
(item
)
645 for i
in self.map
._items
do if i
.second
== item
then nb
+= 1
649 redef fun clear
do self.map
.clear
651 redef fun remove
(item
)
654 var i
= map
._items
.length
- 1
656 if map
._items
[i
].second
== item
then
657 map
.remove_at_index
(i
)
664 redef fun remove_all
(item
)
667 var i
= map
._items
.length
- 1
669 if map
._items
[i
].second
== item
then
670 map
.remove_at_index
(i
)
678 # Others tools ################################################################
680 redef class Iterator[E
]
681 # Interate on `self` and build an array
684 var res
= new Array[E
]
693 redef class Collection[E
]
694 # Build a new array from a collection
701 # Native classes ##############################################################
703 # Subclasses of this class can create native arrays
704 interface ArrayCapable[E
]
705 # Get a new array of `size` elements.
706 protected fun calloc_array
(size
: Int): NativeArray[E
] is intern
709 # Native C array (void ...).
710 universal NativeArray[E
]
711 fun [](index
: Int): E
is intern
712 fun []=(index
: Int, item
: E
) is intern
713 fun copy_to
(dest
: NativeArray[E
], length
: Int) is intern
714 #fun =(o: NativeArray[E]): Bool is intern
715 #fun !=(o: NativeArray[E]): Bool is intern