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 SequenceRead[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
]
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
252 if _capacity
<= l
then
259 redef fun enlarge
(cap
)
262 if cap
<= c
then return
263 while c
<= cap
do c
= c
* 2 + 2
264 var a
= calloc_array
(c
)
265 if _capacity
> 0 then _items
.copy_to
(a
, _length
)
270 # Create an empty array.
277 # Create an array with some `items'.
278 init with_items
(objects
: E
...)
280 _items
= objects
._items
281 _capacity
= objects
._capacity
282 _length
= objects
.length
285 # Create an empty array with a given capacity.
286 init with_capacity
(cap
: Int)
288 assert positive
: cap
>= 0
289 _items
= calloc_array
(cap
)
294 # Create an array of `count' elements
295 init filled_with
(value
: E
, count
: Int)
297 assert positive
: count
>= 0
298 _items
= calloc_array
(count
)
308 # Create a array filled with a given native array.
309 init with_native
(nat
: NativeArray[E
], size
: Int)
311 assert positive
: size
>= 0
317 # The internal storage.
318 var _items
: nullable NativeArray[E
] = null
320 # Do not use this method
321 # FIXME: Remove it once modules can intrude non local modules
322 fun intern_items
: NativeArray[E
] do return _items
.as(not null)
324 # The size of `_items'.
325 var _capacity
: Int = 0
328 # An `Iterator' on `AbstractArray'
329 class ArrayIterator[E
]
330 special IndexedIterator[E
]
331 redef fun item
do return _array
[_index
]
333 # redef fun item=(e) do _array[_index] = e
335 redef fun is_ok
do return _index
< _array
.length
337 redef fun next
do _index
+= 1
339 init(a
: AbstractArrayRead[E
])
345 redef readable var _index
: Int = 0
346 var _array
: AbstractArrayRead[E
]
349 # Others collections ##########################################################
351 # A set implemented with an Array.
354 # The stored elements.
357 redef fun has
(e
) do return _array
.has
(e
)
359 redef fun add
(e
) do if not _array
.has
(e
) then _array
.add
(e
)
361 redef fun is_empty
do return _array
.is_empty
363 redef fun length
do return _array
.length
367 assert _array
.length
> 0
371 redef fun remove
(item
)
373 var i
= _array
.index_of
(item
)
374 if i
>= 0 then remove_at
(i
)
377 redef fun remove_all
(item
) do remove
(item
)
379 redef fun clear
do _array
.clear
381 redef fun iterator
do return new ArraySetIterator[E
](_array
.iterator
)
383 # Assume the capacitydd is at least `cap'.
384 fun enlarge
(cap
: Int) do _array
.enlarge
(cap
)
386 private fun remove_at
(i
: Int)
388 _array
[i
] = _array
.last
392 # Create an empty set
393 init do _array
= new Array[E
]
395 # Create an empty set with a given capacity.
396 init with_capacity
(i
: Int) do _array
= new Array[E
].with_capacity
(i
)
399 # Iterators on sets implemented with arrays.
400 class ArraySetIterator[E
]
403 redef fun is_ok
do return _iter
.is_ok
405 redef fun next
do _iter
.next
407 redef fun item
: E
do return _iter
.item
409 init(iter
: ArrayIterator[E
]) do _iter
= iter
411 var _iter
: ArrayIterator[E
]
415 # Associative arrays implemented with an array of (key, value) pairs.
417 special CoupleMap[K
, E
]
424 return _items
[i
].second
431 redef fun []=(key
, item
)
435 _items
[i
].second
= item
437 _items
.push
(new Couple[K
,E
](key
, item
))
442 redef fun has_key
(key
) do return index
(key
) >= 0
447 for i
in _items
do if i
.second
== item
then return true
452 redef fun has_only
(item
)
454 for i
in _items
do if i
.second
!= item
then return false
459 redef fun length
do return _items
.length
461 redef fun first
do return _items
.first
.first
464 redef fun count
(item
)
467 for i
in _items
do if i
.second
== item
then nb
+= 1
471 redef fun iterator
: CoupleMapIterator[K
, E
] do return new CoupleMapIterator[K
, E
](_items
.iterator
)
473 redef fun is_empty
do return _items
.is_empty
475 redef fun remove
(item
)
477 var i
= _items
.length
- 1
479 if _items
[i
].second
== item
then
487 redef fun remove_all
(item
: E
)
489 var i
= _items
.length
- 1
491 if _items
[i
].second
== item
then
498 redef fun remove_at
(key
)
501 if i
>= 0 then remove_at_index
(i
)
504 redef fun clear
do _items
.clear
506 # Assume the capacity to be at least `cap'.
507 fun enlarge
(cap
: Int) do _items
.enlarge
(cap
)
509 redef fun couple_at
(key
)
520 var _items
: Array[Couple[K
,E
]]
522 # fast remove the ith element of the array
523 private fun remove_at_index
(i
: Int)
525 _items
[i
] = _items
.last
529 # The last positive result given by a index(1) call
530 var _last_index
: Int = 0
532 # Where is the `key' in `_item'?
533 # return -1 if not found
534 private fun index
(key
: K
): Int
537 if l
< _items
.length
and _items
[l
].first
== key
then return l
540 while i
< _items
.length
do
541 if _items
[i
].first
== key
then
553 _items
= new Array[Couple[K
,E
]]
557 # Others tools ################################################################
559 redef class Iterator[E
]
560 # Interate on `self' and build an array
563 var res
= new Array[E
]
572 redef class Collection[E
]
573 # Build a new array from a collection
580 # Native classes ##############################################################
582 # Subclasses of this class can create native arrays
583 interface ArrayCapable[E
]
584 # Get a new array of `size' elements.
585 protected fun calloc_array
(size
: Int): NativeArray[E
] is intern
588 # Native C array (void ...).
589 universal NativeArray[E
]
590 fun [](index
: Int): E
is intern
591 fun []=(index
: Int, item
: E
) is intern
592 fun copy_to
(dest
: NativeArray[E
], length
: Int) is intern
593 #fun =(o: NativeArray[E]): Bool is intern
594 #fun !=(o: NativeArray[E]): Bool is intern