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 redef fun last_index_of
(item
: E
): Int do return last_index_of_from
(item
, length-1
)
66 redef fun index_of_from
(item
: E
, pos
: Int): Int
71 if self[i
] == item
then
79 redef fun last_index_of_from
(item
: E
, pos
: Int): Int
83 if self[i
] == item
then
92 # Return a new array that is the reverse of `self`
94 # assert [1,2,3].reversed == [3, 2, 1]
95 fun reversed
: Array[E
]
98 var result
= new Array[E
].with_capacity
(cmp
)
101 result
.add
(self[cmp
])
106 # Copy a portion of `self` to an other array.
108 # var a = [1, 2, 3, 4]
109 # var b = [10, 20, 30, 40, 50]
110 # a.copy_to(1, 2, b, 2)
111 # assert b == [10, 20, 2, 3, 50]
112 protected fun copy_to
(start
: Int, len
: Int, dest
: AbstractArray[E
], new_start
: Int)
118 dest
[new_start
+i
] = self[start
+i
]
128 if e
!= null then e
.output
133 redef fun iterator
: ArrayIterator[E
] do return new ArrayIterator[E
](self)
134 redef fun reverse_iterator
do return new ArrayReverseIterator[E
](self)
137 # Resizable one dimension array of objects.
138 abstract class AbstractArray[E
]
139 super AbstractArrayRead[E
]
142 # Force the capacity to be at least `cap`.
143 # The capacity of the array is an internal information.
144 # However, this method can be used to prepare a large amount of add
145 fun enlarge
(cap
: Int) is abstract
147 redef fun push
(item
) do add
(item
)
151 assert not_empty
: not is_empty
159 assert not_empty
: not is_empty
171 redef fun unshift
(item
)
181 redef fun insert
(item
: E
, pos
: Int)
184 copy_to
(pos
, length-pos
, self, pos
+ 1)
188 redef fun add
(item
) do self[length
] = item
190 redef fun clear
do _length
= 0
192 redef fun remove
(item
) do remove_at
(index_of
(item
))
194 redef fun remove_all
(item
)
196 var i
= index_of
(item
)
199 i
= index_of_from
(item
, i
)
203 redef fun remove_at
(i
)
206 if i
>= 0 and i
< l
then
216 # Invert two elements in the array
218 # var a = [10, 20, 30, 40]
220 # assert a == [10, 40, 30, 20]
221 fun swap_at
(a
: Int,b
: Int)
229 # Resizable one dimension array of objects.
231 # Arrays have a literal representation.
232 # var a = [12, 32, 8]
233 # # is equivalent with:
234 # var b = new Array[Int]
240 super AbstractArray[E
]
241 super ArrayCapable[E
]
245 assert index
: index
>= 0 and index
< _length
249 redef fun []=(index
, item
)
251 assert index
: index
>= 0 and index
< _length
+ 1
252 if _capacity
<= index
then
255 if _length
<= index
then
264 if _capacity
<= l
then
271 redef fun enlarge
(cap
)
274 if cap
<= c
then return
275 while c
<= cap
do c
= c
* 2 + 2
276 var a
= calloc_array
(c
)
277 if _capacity
> 0 then _items
.copy_to
(a
, _length
)
282 # Create an empty array.
289 # Create an array from a collection.
290 init from
(items
: Collection[E
]) do
291 with_capacity
(items
.length
)
295 # Create an array with some `objects`.
296 init with_items
(objects
: E
...)
298 _items
= objects
._items
299 _capacity
= objects
._capacity
300 _length
= objects
.length
303 # Create an empty array with a given capacity.
304 init with_capacity
(cap
: Int)
306 assert positive
: cap
>= 0
307 _items
= calloc_array
(cap
)
312 # Create an array of `count` elements
313 init filled_with
(value
: E
, count
: Int)
315 assert positive
: count
>= 0
316 _items
= calloc_array
(count
)
326 # Create a array filled with a given native array.
327 init with_native
(nat
: NativeArray[E
], size
: Int)
329 assert positive
: size
>= 0
335 # The internal storage.
336 var _items
: nullable NativeArray[E
] = null
338 # Do not use this method
339 # FIXME: Remove it once modules can intrude non local modules
340 fun intern_items
: NativeArray[E
] do return _items
.as(not null)
342 # The size of `_items`.
343 var _capacity
: Int = 0
346 # An `Iterator` on `AbstractArray`
347 private class ArrayIterator[E
]
348 super IndexedIterator[E
]
350 redef fun item
do return _array
[_index
]
352 # redef fun item=(e) do _array[_index] = e
354 redef fun is_ok
do return _index
< _array
.length
356 redef fun next
do _index
+= 1
358 init(a
: AbstractArrayRead[E
])
364 redef readable var _index
: Int = 0
365 var _array
: AbstractArrayRead[E
]
368 private class ArrayReverseIterator[E
]
369 super ArrayIterator[E
]
371 redef fun is_ok
do return _index
>= 0
373 redef fun next
do _index
-= 1
375 init(a
: AbstractArrayRead[E
])
378 _index
= a
.length
- 1
382 # Others collections ##########################################################
384 # A set implemented with an Array.
385 class ArraySet[E
: Object]
388 # The stored elements.
391 redef fun has
(e
) do return _array
.has
(e
)
393 redef fun add
(e
) do if not _array
.has
(e
) then _array
.add
(e
)
395 redef fun is_empty
do return _array
.is_empty
397 redef fun length
do return _array
.length
401 assert _array
.length
> 0
405 redef fun remove
(item
)
407 var i
= _array
.index_of
(item
)
408 if i
>= 0 then remove_at
(i
)
411 redef fun remove_all
(item
) do remove
(item
)
413 redef fun clear
do _array
.clear
415 redef fun iterator
do return new ArraySetIterator[E
](_array
.iterator
)
417 # Assume the capacity is at least `cap`.
418 fun enlarge
(cap
: Int) do _array
.enlarge
(cap
)
420 private fun remove_at
(i
: Int)
422 _array
[i
] = _array
.last
426 # Create an empty set
427 init do _array
= new Array[E
]
429 # Create an empty set with a given capacity.
430 init with_capacity
(i
: Int) do _array
= new Array[E
].with_capacity
(i
)
433 # Iterators on sets implemented with arrays.
434 private class ArraySetIterator[E
: Object]
437 redef fun is_ok
do return _iter
.is_ok
439 redef fun next
do _iter
.next
441 redef fun item
: E
do return _iter
.item
443 init(iter
: ArrayIterator[E
]) do _iter
= iter
445 var _iter
: ArrayIterator[E
]
449 # Associative arrays implemented with an array of (key, value) pairs.
450 class ArrayMap[K
: Object, E
]
451 super CoupleMap[K
, E
]
458 return _items
[i
].second
460 return provide_default_value
(key
)
465 redef fun []=(key
, item
)
469 _items
[i
].second
= item
471 _items
.push
(new Couple[K
,E
](key
, item
))
475 redef var keys
: RemovableCollection[K
] = new ArrayMapKeys[K
, E
](self)
476 redef var values
: RemovableCollection[E
] = new ArrayMapValues[K
, E
](self)
479 redef fun length
do return _items
.length
481 redef fun couple_iterator
do return _items
.iterator
483 redef fun is_empty
do return _items
.is_empty
485 redef fun clear
do _items
.clear
487 # Assume the capacity to be at least `cap`.
488 fun enlarge
(cap
: Int) do _items
.enlarge
(cap
)
490 redef fun couple_at
(key
)
501 var _items
: Array[Couple[K
,E
]]
503 # fast remove the ith element of the array
504 private fun remove_at_index
(i
: Int)
506 _items
[i
] = _items
.last
510 # The last positive result given by a index(1) call
511 var _last_index
: Int = 0
513 # Where is the `key` in `_item`?
514 # return -1 if not found
515 private fun index
(key
: K
): Int
518 if l
< _items
.length
and _items
[l
].first
== key
then return l
521 while i
< _items
.length
do
522 if _items
[i
].first
== key
then
534 _items
= new Array[Couple[K
,E
]]
538 private class ArrayMapKeys[K
: Object, E
]
539 super RemovableCollection[K
]
541 var map
: ArrayMap[K
, E
]
542 redef fun count
(k
) do if self.has
(k
) then return 1 else return 0
543 redef fun first
do return self.map
._items
.first
.first
544 redef fun has
(k
) do return self.map
.index
(k
) >= 0
545 redef fun has_only
(k
) do return (self.has
(k
) and self.length
== 1) or self.is_empty
546 redef fun is_empty
do return self.map
.is_empty
547 redef fun length
do return self.map
.length
548 redef fun iterator
do return new MapKeysIterator[K
, E
](self.map
.iterator
)
549 redef fun clear
do self.map
.clear
550 redef fun remove
(key
)
552 var i
= self.map
.index
(key
)
553 if i
>= 0 then self.map
.remove_at_index
(i
)
555 redef fun remove_all
(key
) do self.remove
(key
)
558 private class ArrayMapValues[K
: Object, E
]
559 super RemovableCollection[E
]
561 var map
: ArrayMap[K
, E
]
562 redef fun first
do return self.map
._items
.first
.second
563 redef fun is_empty
do return self.map
.is_empty
564 redef fun length
do return self.map
.length
565 redef fun iterator
do return new MapValuesIterator[K
, E
](self.map
.iterator
)
570 for i
in self.map
._items
do if i
.second
== item
then return true
575 redef fun has_only
(item
)
577 for i
in self.map
._items
do if i
.second
!= item
then return false
582 redef fun count
(item
)
585 for i
in self.map
._items
do if i
.second
== item
then nb
+= 1
589 redef fun clear
do self.map
.clear
591 redef fun remove
(item
)
594 var i
= map
._items
.length
- 1
596 if map
._items
[i
].second
== item
then
597 map
.remove_at_index
(i
)
604 redef fun remove_all
(item
)
607 var i
= map
._items
.length
- 1
609 if map
._items
[i
].second
== item
then
610 map
.remove_at_index
(i
)
618 # Others tools ################################################################
620 redef class Iterator[E
]
621 # Interate on `self` and build an array
624 var res
= new Array[E
]
633 redef class Collection[E
]
634 # Build a new array from a collection
641 # Native classes ##############################################################
643 # Subclasses of this class can create native arrays
644 interface ArrayCapable[E
]
645 # Get a new array of `size` elements.
646 protected fun calloc_array
(size
: Int): NativeArray[E
] is intern
649 # Native C array (void ...).
650 universal NativeArray[E
]
651 fun [](index
: Int): E
is intern
652 fun []=(index
: Int, item
: E
) is intern
653 fun copy_to
(dest
: NativeArray[E
], length
: Int) is intern
654 #fun =(o: NativeArray[E]): Bool is intern
655 #fun !=(o: NativeArray[E]): Bool is intern