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 # [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 # 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 # 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 # 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.
257 # is equivalent with:
263 super AbstractArray[E
]
264 super ArrayCapable[E
]
280 assert index
: index
>= 0 and index
< _length
284 redef fun []=(index
, item
)
286 assert index
: index
>= 0 and index
< _length
+ 1
287 if _capacity
<= index
then
290 if _length
<= index
then
299 if _capacity
<= l
then
306 redef fun enlarge
(cap
)
309 if cap
<= c
then return
310 while c
<= cap
do c
= c
* 2 + 2
311 var a
= calloc_array
(c
)
312 if _capacity
> 0 then _items
.copy_to
(a
, _length
)
317 # Create an empty array.
324 # Create an array from a collection.
325 init from
(items
: Collection[E
]) do
326 with_capacity
(items
.length
)
330 # Create an array with some `objects`.
331 init with_items
(objects
: E
...)
333 _items
= objects
._items
334 _capacity
= objects
._capacity
335 _length
= objects
.length
338 # Create an empty array with a given capacity.
339 init with_capacity
(cap
: Int)
341 assert positive
: cap
>= 0
342 _items
= calloc_array
(cap
)
347 # Create an array of `count` elements
348 init filled_with
(value
: E
, count
: Int)
350 assert positive
: count
>= 0
351 _items
= calloc_array
(count
)
361 # Create a array filled with a given native array.
362 init with_native
(nat
: NativeArray[E
], size
: Int)
364 assert positive
: size
>= 0
370 # The internal storage.
371 var _items
: nullable NativeArray[E
] = null
373 # Do not use this method
374 # FIXME: Remove it once modules can intrude non local modules
375 fun intern_items
: NativeArray[E
] do return _items
.as(not null)
377 # The size of `_items`.
378 var _capacity
: Int = 0
380 # Sort the array using the !cmp function.
384 sub_sort
(0, length-1
) !cmp
(x
,y
) = cmp
(x
, y
)
387 # Sort `array` between `from` and `to` indices
388 private fun sub_sort
(from
: Int, to
: Int)
393 else if from
+ 7 < to
then
394 var pivot
= self[from
]
398 while i
<= to
and cmp
(self[i
], pivot
) <= 0 do i
+= 1
399 while j
> i
and cmp
(self[j
], pivot
) >= 0 do j
-= 1
406 self[from
] = self[i-1
]
408 sub_sort
(from
, i-2
) !cmp
(x
,y
) = cmp
(x
, y
)
409 sub_sort
(i
, to
) !cmp
(x
,y
) = cmp
(x
, y
)
417 if cmp
(min_v
, self[j
]) > 0 then
433 # An `Iterator` on `AbstractArray`
434 class ArrayIterator[E
]
435 super IndexedIterator[E
]
437 redef fun item
do return _array
[_index
]
439 # redef fun item=(e) do _array[_index] = e
441 redef fun is_ok
do return _index
< _array
.length
443 redef fun next
do _index
+= 1
445 init(a
: AbstractArrayRead[E
])
451 redef readable var _index
: Int = 0
452 var _array
: AbstractArrayRead[E
]
455 # Others collections ##########################################################
457 # A set implemented with an Array.
458 class ArraySet[E
: Object]
461 # The stored elements.
464 redef fun has
(e
) do return _array
.has
(e
)
466 redef fun add
(e
) do if not _array
.has
(e
) then _array
.add
(e
)
468 redef fun is_empty
do return _array
.is_empty
470 redef fun length
do return _array
.length
474 assert _array
.length
> 0
478 redef fun remove
(item
)
480 var i
= _array
.index_of
(item
)
481 if i
>= 0 then remove_at
(i
)
484 redef fun remove_all
(item
) do remove
(item
)
486 redef fun clear
do _array
.clear
488 redef fun iterator
do return new ArraySetIterator[E
](_array
.iterator
)
490 # Assume the capacity is at least `cap`.
491 fun enlarge
(cap
: Int) do _array
.enlarge
(cap
)
493 private fun remove_at
(i
: Int)
495 _array
[i
] = _array
.last
499 # Create an empty set
500 init do _array
= new Array[E
]
502 # Create an empty set with a given capacity.
503 init with_capacity
(i
: Int) do _array
= new Array[E
].with_capacity
(i
)
506 # Iterators on sets implemented with arrays.
507 class ArraySetIterator[E
: Object]
510 redef fun is_ok
do return _iter
.is_ok
512 redef fun next
do _iter
.next
514 redef fun item
: E
do return _iter
.item
516 init(iter
: ArrayIterator[E
]) do _iter
= iter
518 var _iter
: ArrayIterator[E
]
522 # Associative arrays implemented with an array of (key, value) pairs.
523 class ArrayMap[K
: Object, E
]
524 super CoupleMap[K
, E
]
531 return _items
[i
].second
538 redef fun []=(key
, item
)
542 _items
[i
].second
= item
544 _items
.push
(new Couple[K
,E
](key
, item
))
548 redef var keys
: ArrayMapKeys[K
, E
] = new ArrayMapKeys[K
, E
](self)
549 redef var values
: ArrayMapValues[K
, E
] = new ArrayMapValues[K
, E
](self)
552 redef fun length
do return _items
.length
554 redef fun iterator
: CoupleMapIterator[K
, E
] do return new CoupleMapIterator[K
, E
](_items
.iterator
)
556 redef fun is_empty
do return _items
.is_empty
558 redef fun clear
do _items
.clear
560 # Assume the capacity to be at least `cap`.
561 fun enlarge
(cap
: Int) do _items
.enlarge
(cap
)
563 redef fun couple_at
(key
)
574 var _items
: Array[Couple[K
,E
]]
576 # fast remove the ith element of the array
577 private fun remove_at_index
(i
: Int)
579 _items
[i
] = _items
.last
583 # The last positive result given by a index(1) call
584 var _last_index
: Int = 0
586 # Where is the `key` in `_item`?
587 # return -1 if not found
588 private fun index
(key
: K
): Int
591 if l
< _items
.length
and _items
[l
].first
== key
then return l
594 while i
< _items
.length
do
595 if _items
[i
].first
== key
then
607 _items
= new Array[Couple[K
,E
]]
611 class ArrayMapKeys[K
: Object, E
]
612 super RemovableCollection[K
]
614 var map
: ArrayMap[K
, E
]
615 redef fun count
(k
) do if self.has
(k
) then return 1 else return 0
616 redef fun first
do return self.map
._items
.first
.first
617 redef fun has
(k
) do return self.map
.index
(k
) >= 0
618 redef fun has_only
(k
) do return (self.has
(k
) and self.length
== 1) or self.is_empty
619 redef fun is_empty
do return self.map
.is_empty
620 redef fun length
do return self.map
.length
621 redef fun iterator
do return new MapKeysIterator[K
, E
](self.map
.iterator
)
622 redef fun clear
do self.map
.clear
623 redef fun remove
(key
)
625 var i
= self.map
.index
(key
)
626 if i
>= 0 then self.map
.remove_at_index
(i
)
628 redef fun remove_all
(key
) do self.remove
(key
)
631 class ArrayMapValues[K
: Object, E
]
632 super RemovableCollection[E
]
634 var map
: ArrayMap[K
, E
]
635 redef fun first
do return self.map
._items
.first
.second
636 redef fun is_empty
do return self.map
.is_empty
637 redef fun length
do return self.map
.length
638 redef fun iterator
do return new MapValuesIterator[K
, E
](self.map
.iterator
)
643 for i
in self.map
._items
do if i
.second
== item
then return true
648 redef fun has_only
(item
)
650 for i
in self.map
._items
do if i
.second
!= item
then return false
655 redef fun count
(item
)
658 for i
in self.map
._items
do if i
.second
== item
then nb
+= 1
662 redef fun clear
do self.map
.clear
664 redef fun remove
(item
)
667 var i
= map
._items
.length
- 1
669 if map
._items
[i
].second
== item
then
670 map
.remove_at_index
(i
)
677 redef fun remove_all
(item
)
680 var i
= map
._items
.length
- 1
682 if map
._items
[i
].second
== item
then
683 map
.remove_at_index
(i
)
691 # Others tools ################################################################
693 redef class Iterator[E
]
694 # Interate on `self` and build an array
697 var res
= new Array[E
]
706 redef class Collection[E
]
707 # Build a new array from a collection
714 # Native classes ##############################################################
716 # Subclasses of this class can create native arrays
717 interface ArrayCapable[E
]
718 # Get a new array of `size` elements.
719 protected fun calloc_array
(size
: Int): NativeArray[E
] is intern
722 # Native C array (void ...).
723 universal NativeArray[E
]
724 fun [](index
: Int): E
is intern
725 fun []=(index
: Int, item
: E
) is intern
726 fun copy_to
(dest
: NativeArray[E
], length
: Int) is intern
727 #fun =(o: NativeArray[E]): Bool is intern
728 #fun !=(o: NativeArray[E]): Bool is intern