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 dimention array of objects.
21 class AbstractArrayRead[E
]
22 special IndexedCollectionRead[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
50 redef fun has_key
(index
) do return index
>= 0 and index
< length
58 if self[i
] == item
then res
+= 1
64 redef fun index_of
(item
) do return index_of_from
(item
, 0)
66 fun last_index_of
(item
: E
): Int do return last_index_of_from
(item
, length-1
)
68 fun index_of_from
(item
: E
, pos
: Int): Int
73 if self[i
] == item
then
81 fun last_index_of_from
(item
: E
, pos
: Int): Int
85 if self[i
] == item
then
94 fun reversed
: Array[E
]
97 var result
= new Array[E
].with_capacity
(cmp
)
100 result
.add
(self[cmp
])
105 protected fun copy_to
(start
: Int, len
: Int, dest
: AbstractArray[E
], new_start
: Int)
111 dest
[new_start
+i
] = self[start
+i
]
121 if e
!= null then e
.output
126 redef fun iterator
: ArrayIterator[E
] do return new ArrayIterator[E
](self)
128 # Two arrays are equals if they have the same items in the same order.
131 if not o
isa AbstractArray[E
] or o
is null then return false
133 if o
.length
!= l
then return false
136 if self[i
] != o
[i
] then return false
143 # Resizeable one dimention array of objects.
144 class AbstractArray[E
]
145 special AbstractArrayRead[E
]
146 special IndexedCollection[E
]
147 fun enlarge
(cap
: Int) is abstract
149 redef fun push
(item
) do add
(item
)
153 assert not_empty
: not is_empty
161 assert not_empty
: not is_empty
173 redef fun unshift
(item
)
183 fun insert
(item
: E
, pos
: Int)
186 copy_to
(pos
, length-pos
, self, pos
+ 1)
190 redef fun add
(item
) do self[length
] = item
192 redef fun clear
do _length
= 0
194 redef fun remove
(item
) do remove_at
(index_of
(item
))
196 redef fun remove_all
(item
)
198 var i
= index_of
(item
)
201 i
= index_of_from
(item
, i
)
205 redef fun remove_at
(i
)
208 if i
>= 0 and i
< l
then
219 # Resizeable one dimention array of objects.
221 # Arrays have a literal representation.
223 # is equivalent with:
229 special AbstractArray[E
]
230 special ArrayCapable[E
]
233 assert index
: index
>= 0 and index
< _length
237 redef fun []=(index
, item
)
239 assert index
: index
>= 0 and index
< _length
+ 1
240 if _capacity
<= index
then
243 if _length
<= index
then
249 redef fun enlarge
(cap
)
252 if cap
<= c
then return
253 while c
<= cap
do c
= c
* 2 + 2
254 var a
= calloc_array
(c
)
255 if _capacity
> 0 then _items
.copy_to
(a
, _length
)
260 # Create an empty array.
267 # Create an array with some `items'.
268 init with_items
(objects
: E
...)
270 _items
= objects
._items
271 _capacity
= objects
._capacity
272 _length
= objects
.length
275 # Create an empty array with a given capacity.
276 init with_capacity
(cap
: Int)
278 assert positive
: cap
>= 0
279 _items
= calloc_array
(cap
)
284 # Create an array of `count' elements
285 init filled_with
(value
: E
, count
: Int)
287 assert positive
: count
>= 0
288 _items
= calloc_array
(count
)
298 # Create a array filled with a given native array.
299 init with_native
(nat
: NativeArray[E
], size
: Int)
301 assert positive
: size
>= 0
307 # The internal storage.
308 var _items
: nullable NativeArray[E
] = null
310 # The size of `_items'.
311 var _capacity
: Int = 0
314 # An `Iterator' on `AbstractArray'
315 class ArrayIterator[E
]
316 special IndexedIterator[E
]
317 redef fun item
do return _array
[_index
]
319 # redef fun item=(e) do _array[_index] = e
321 redef fun is_ok
do return _index
< _array
.length
323 redef fun next
do _index
+= 1
325 init(a
: AbstractArrayRead[E
])
331 redef readable var _index
: Int = 0
332 var _array
: AbstractArrayRead[E
]
335 # Others collections ##########################################################
337 # A set implemented with an Array.
340 # The stored elements.
343 redef fun has
(e
) do return _array
.has
(e
)
345 redef fun add
(e
) do if not _array
.has
(e
) then _array
.add
(e
)
347 redef fun is_empty
do return _array
.is_empty
349 redef fun length
do return _array
.length
353 assert _array
.length
> 0
357 redef fun remove
(item
)
359 var i
= _array
.index_of
(item
)
360 if i
>= 0 then remove_at
(i
)
363 redef fun remove_all
(item
) do remove
(item
)
365 redef fun clear
do _array
.clear
367 redef fun iterator
do return new ArraySetIterator[E
](_array
.iterator
)
369 # Assume the capacitydd is at least `cap'.
370 fun enlarge
(cap
: Int) do _array
.enlarge
(cap
)
372 private fun remove_at
(i
: Int)
374 _array
[i
] = _array
.last
378 # Create an empty set
379 init do _array
= new Array[E
]
381 # Create an empty set with a given capacity.
382 init with_capacity
(i
: Int) do _array
= new Array[E
].with_capacity
(i
)
385 # Iterators on sets implemented with arrays.
386 class ArraySetIterator[E
]
389 redef fun is_ok
do return _iter
.is_ok
391 redef fun next
do _iter
.next
393 redef fun item
: E
do return _iter
.item
395 init(iter
: ArrayIterator[E
]) do _iter
= iter
397 var _iter
: ArrayIterator[E
]
401 # Associative arrays implemented with an array of (key, value) pairs.
403 special CoupleMap[K
, E
]
410 return _items
[i
].second
417 redef fun []=(key
, item
)
421 _items
[i
].second
= item
423 _items
.push
(new Couple[K
,E
](key
, item
))
428 redef fun has_key
(key
) do return index
(key
) >= 0
433 for i
in _items
do if i
.second
== item
then return true
438 redef fun has_only
(item
)
440 for i
in _items
do if i
.second
!= item
then return false
445 redef fun length
do return _items
.length
447 redef fun first
do return _items
.first
.first
450 redef fun count
(item
)
453 for i
in _items
do if i
.second
== item
then nb
+= 1
457 redef fun iterator
: CoupleMapIterator[K
, E
] do return new CoupleMapIterator[K
, E
](_items
.iterator
)
459 redef fun is_empty
do return _items
.is_empty
461 redef fun remove
(item
)
463 var i
= _items
.length
- 1
465 if _items
[i
].second
== item
then
473 redef fun remove_all
(item
: E
)
475 var i
= _items
.length
- 1
477 if _items
[i
].second
== item
then
484 redef fun remove_at
(key
)
487 if i
>= 0 then remove_at_index
(i
)
490 redef fun clear
do _items
.clear
492 # Assume the capacity to be at least `cap'.
493 fun enlarge
(cap
: Int) do _items
.enlarge
(cap
)
495 redef fun couple_at
(key
)
506 var _items
: Array[Couple[K
,E
]]
508 # fast remove the ith element of the array
509 private fun remove_at_index
(i
: Int)
511 _items
[i
] = _items
.last
515 # The last positive result given by a index(1) call
516 var _last_index
: Int = 0
518 # Where is the `key' in `_item'?
519 # return -1 if not found
520 private fun index
(key
: K
): Int
523 if l
< _items
.length
and _items
[l
].first
== key
then return l
526 while i
< _items
.length
do
527 if _items
[i
].first
== key
then
539 _items
= new Array[Couple[K
,E
]]
543 # Others tools ################################################################
545 redef class Iterator[E
]
546 # Interate on `self' and build an array
549 var res
= new Array[E
]
558 redef class Collection[E
]
559 # Build a new array from a collection
566 # Native classes ##############################################################
568 # Subclasses of this class can create native arrays
569 interface ArrayCapable[E
]
570 # Get a new array of `size' elements.
571 protected fun calloc_array
(size
: Int): NativeArray[E
] is intern
574 # Native C array (void ...).
575 universal NativeArray[E
]
576 fun [](index
: Int): E
is intern
577 fun []=(index
: Int, item
: E
) is intern
578 fun copy_to
(dest
: NativeArray[E
], length
: Int) is intern
579 #fun =(o: NativeArray[E]): Bool is intern
580 #fun !=(o: NativeArray[E]): Bool is intern