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
]
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 with some `items'.
290 init with_items
(objects
: E
...)
292 _items
= objects
._items
293 _capacity
= objects
._capacity
294 _length
= objects
.length
297 # Create an empty array with a given capacity.
298 init with_capacity
(cap
: Int)
300 assert positive
: cap
>= 0
301 _items
= calloc_array
(cap
)
306 # Create an array of `count' elements
307 init filled_with
(value
: E
, count
: Int)
309 assert positive
: count
>= 0
310 _items
= calloc_array
(count
)
320 # Create a array filled with a given native array.
321 init with_native
(nat
: NativeArray[E
], size
: Int)
323 assert positive
: size
>= 0
329 # The internal storage.
330 var _items
: nullable NativeArray[E
] = null
332 # Do not use this method
333 # FIXME: Remove it once modules can intrude non local modules
334 fun intern_items
: NativeArray[E
] do return _items
.as(not null)
336 # The size of `_items'.
337 var _capacity
: Int = 0
340 # An `Iterator' on `AbstractArray'
341 class ArrayIterator[E
]
342 special IndexedIterator[E
]
343 redef fun item
do return _array
[_index
]
345 # redef fun item=(e) do _array[_index] = e
347 redef fun is_ok
do return _index
< _array
.length
349 redef fun next
do _index
+= 1
351 init(a
: AbstractArrayRead[E
])
357 redef readable var _index
: Int = 0
358 var _array
: AbstractArrayRead[E
]
361 # Others collections ##########################################################
363 # A set implemented with an Array.
366 # The stored elements.
369 redef fun has
(e
) do return _array
.has
(e
)
371 redef fun add
(e
) do if not _array
.has
(e
) then _array
.add
(e
)
373 redef fun is_empty
do return _array
.is_empty
375 redef fun length
do return _array
.length
379 assert _array
.length
> 0
383 redef fun remove
(item
)
385 var i
= _array
.index_of
(item
)
386 if i
>= 0 then remove_at
(i
)
389 redef fun remove_all
(item
) do remove
(item
)
391 redef fun clear
do _array
.clear
393 redef fun iterator
do return new ArraySetIterator[E
](_array
.iterator
)
395 # Assume the capacitydd is at least `cap'.
396 fun enlarge
(cap
: Int) do _array
.enlarge
(cap
)
398 private fun remove_at
(i
: Int)
400 _array
[i
] = _array
.last
404 # Create an empty set
405 init do _array
= new Array[E
]
407 # Create an empty set with a given capacity.
408 init with_capacity
(i
: Int) do _array
= new Array[E
].with_capacity
(i
)
411 # Iterators on sets implemented with arrays.
412 class ArraySetIterator[E
]
415 redef fun is_ok
do return _iter
.is_ok
417 redef fun next
do _iter
.next
419 redef fun item
: E
do return _iter
.item
421 init(iter
: ArrayIterator[E
]) do _iter
= iter
423 var _iter
: ArrayIterator[E
]
427 # Associative arrays implemented with an array of (key, value) pairs.
429 special CoupleMap[K
, E
]
436 return _items
[i
].second
443 redef fun []=(key
, item
)
447 _items
[i
].second
= item
449 _items
.push
(new Couple[K
,E
](key
, item
))
454 redef fun has_key
(key
) do return index
(key
) >= 0
459 for i
in _items
do if i
.second
== item
then return true
464 redef fun has_only
(item
)
466 for i
in _items
do if i
.second
!= item
then return false
471 redef fun length
do return _items
.length
473 redef fun first
do return _items
.first
.first
476 redef fun count
(item
)
479 for i
in _items
do if i
.second
== item
then nb
+= 1
483 redef fun iterator
: CoupleMapIterator[K
, E
] do return new CoupleMapIterator[K
, E
](_items
.iterator
)
485 redef fun is_empty
do return _items
.is_empty
487 redef fun remove
(item
)
489 var i
= _items
.length
- 1
491 if _items
[i
].second
== item
then
499 redef fun remove_all
(item
: E
)
501 var i
= _items
.length
- 1
503 if _items
[i
].second
== item
then
510 redef fun remove_at
(key
)
513 if i
>= 0 then remove_at_index
(i
)
516 redef fun clear
do _items
.clear
518 # Assume the capacity to be at least `cap'.
519 fun enlarge
(cap
: Int) do _items
.enlarge
(cap
)
521 redef fun couple_at
(key
)
532 var _items
: Array[Couple[K
,E
]]
534 # fast remove the ith element of the array
535 private fun remove_at_index
(i
: Int)
537 _items
[i
] = _items
.last
541 # The last positive result given by a index(1) call
542 var _last_index
: Int = 0
544 # Where is the `key' in `_item'?
545 # return -1 if not found
546 private fun index
(key
: K
): Int
549 if l
< _items
.length
and _items
[l
].first
== key
then return l
552 while i
< _items
.length
do
553 if _items
[i
].first
== key
then
565 _items
= new Array[Couple[K
,E
]]
569 # Others tools ################################################################
571 redef class Iterator[E
]
572 # Interate on `self' and build an array
575 var res
= new Array[E
]
584 redef class Collection[E
]
585 # Build a new array from a collection
592 # Native classes ##############################################################
594 # Subclasses of this class can create native arrays
595 interface ArrayCapable[E
]
596 # Get a new array of `size' elements.
597 protected fun calloc_array
(size
: Int): NativeArray[E
] is intern
600 # Native C array (void ...).
601 universal NativeArray[E
]
602 fun [](index
: Int): E
is intern
603 fun []=(index
: Int, item
: E
) is intern
604 fun copy_to
(dest
: NativeArray[E
], length
: Int) is intern
605 #fun =(o: NativeArray[E]): Bool is intern
606 #fun !=(o: NativeArray[E]): Bool is intern