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
]
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 fun last_index_of
(item
: E
): Int do return last_index_of_from
(item
, length-1
)
66 fun index_of_from
(item
: E
, pos
: Int): Int
71 if self[i
] == item
then
79 fun last_index_of_from
(item
: E
, pos
: Int): Int
83 if self[i
] == item
then
92 fun reversed
: Array[E
]
95 var result
= new Array[E
].with_capacity
(cmp
)
103 protected fun copy_to
(start
: Int, len
: Int, dest
: AbstractArray[E
], new_start
: Int)
109 dest
[new_start
+i
] = self[start
+i
]
119 if e
!= null then e
.output
124 redef fun iterator
: ArrayIterator[E
] do return new ArrayIterator[E
](self)
126 # Two arrays are equals if they have the same items in the same order.
129 if not o
isa AbstractArray[E
] or o
is null then return false
131 if o
.length
!= l
then return false
134 if self[i
] != o
[i
] then return false
141 # Resizeable one dimention array of objects.
142 class AbstractArray[E
]
143 super AbstractArrayRead[E
]
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 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
217 # Resizeable one dimention array of objects.
219 # Arrays have a literal representation.
221 # is equivalent with:
227 super AbstractArray[E
]
228 super ArrayCapable[E
]
243 assert index
: index
>= 0 and index
< _length
247 redef fun []=(index
, item
)
249 assert index
: index
>= 0 and index
< _length
+ 1
250 if _capacity
<= index
then
253 if _length
<= index
then
262 if _capacity
<= l
then
269 redef fun enlarge
(cap
)
272 if cap
<= c
then return
273 while c
<= cap
do c
= c
* 2 + 2
274 var a
= calloc_array
(c
)
275 if _capacity
> 0 then _items
.copy_to
(a
, _length
)
280 # Create an empty array.
287 # Create an array with some `items'.
288 init with_items
(objects
: E
...)
290 _items
= objects
._items
291 _capacity
= objects
._capacity
292 _length
= objects
.length
295 # Create an empty array with a given capacity.
296 init with_capacity
(cap
: Int)
298 assert positive
: cap
>= 0
299 _items
= calloc_array
(cap
)
304 # Create an array of `count' elements
305 init filled_with
(value
: E
, count
: Int)
307 assert positive
: count
>= 0
308 _items
= calloc_array
(count
)
318 # Create a array filled with a given native array.
319 init with_native
(nat
: NativeArray[E
], size
: Int)
321 assert positive
: size
>= 0
327 # The internal storage.
328 var _items
: nullable NativeArray[E
] = null
330 # Do not use this method
331 # FIXME: Remove it once modules can intrude non local modules
332 fun intern_items
: NativeArray[E
] do return _items
.as(not null)
334 # The size of `_items'.
335 var _capacity
: Int = 0
337 # Sort the array using the !cmp function.
341 sub_sort
(0, length-1
) !cmp
(x
,y
) = cmp
(x
, y
)
344 # Sort `array' between `from' and `to' indices
345 private fun sub_sort
(from
: Int, to
: Int)
350 else if from
+ 7 < to
then
351 var pivot
= self[from
]
355 while i
<= to
and cmp
(self[i
], pivot
) <= 0 do i
+= 1
356 while j
> i
and cmp
(self[j
], pivot
) >= 0 do j
-= 1
363 self[from
] = self[i-1
]
365 sub_sort
(from
, i-2
) !cmp
(x
,y
) = cmp
(x
, y
)
366 sub_sort
(i
, to
) !cmp
(x
,y
) = cmp
(x
, y
)
374 if cmp
(min_v
, self[j
]) > 0 then
390 # An `Iterator' on `AbstractArray'
391 class ArrayIterator[E
]
392 super IndexedIterator[E
]
393 redef fun item
do return _array
[_index
]
395 # redef fun item=(e) do _array[_index] = e
397 redef fun is_ok
do return _index
< _array
.length
399 redef fun next
do _index
+= 1
401 init(a
: AbstractArrayRead[E
])
407 redef readable var _index
: Int = 0
408 var _array
: AbstractArrayRead[E
]
411 # Others collections ##########################################################
413 # A set implemented with an Array.
414 class ArraySet[E
: Object]
416 # The stored elements.
419 redef fun has
(e
) do return _array
.has
(e
)
421 redef fun add
(e
) do if not _array
.has
(e
) then _array
.add
(e
)
423 redef fun is_empty
do return _array
.is_empty
425 redef fun length
do return _array
.length
429 assert _array
.length
> 0
433 redef fun remove
(item
)
435 var i
= _array
.index_of
(item
)
436 if i
>= 0 then remove_at
(i
)
439 redef fun remove_all
(item
) do remove
(item
)
441 redef fun clear
do _array
.clear
443 redef fun iterator
do return new ArraySetIterator[E
](_array
.iterator
)
445 # Assume the capacitydd is at least `cap'.
446 fun enlarge
(cap
: Int) do _array
.enlarge
(cap
)
448 private fun remove_at
(i
: Int)
450 _array
[i
] = _array
.last
454 # Create an empty set
455 init do _array
= new Array[E
]
457 # Create an empty set with a given capacity.
458 init with_capacity
(i
: Int) do _array
= new Array[E
].with_capacity
(i
)
461 # Iterators on sets implemented with arrays.
462 class ArraySetIterator[E
: Object]
465 redef fun is_ok
do return _iter
.is_ok
467 redef fun next
do _iter
.next
469 redef fun item
: E
do return _iter
.item
471 init(iter
: ArrayIterator[E
]) do _iter
= iter
473 var _iter
: ArrayIterator[E
]
477 # Associative arrays implemented with an array of (key, value) pairs.
478 class ArrayMap[K
: Object, E
]
479 super CoupleMap[K
, E
]
486 return _items
[i
].second
493 redef fun []=(key
, item
)
497 _items
[i
].second
= item
499 _items
.push
(new Couple[K
,E
](key
, item
))
503 redef var keys
: ArrayMapKeys[K
, E
] = new ArrayMapKeys[K
, E
](self)
504 redef var values
: ArrayMapValues[K
, E
] = new ArrayMapValues[K
, E
](self)
507 redef fun length
do return _items
.length
509 redef fun iterator
: CoupleMapIterator[K
, E
] do return new CoupleMapIterator[K
, E
](_items
.iterator
)
511 redef fun is_empty
do return _items
.is_empty
513 redef fun remove
(item
)
515 var i
= _items
.length
- 1
517 if _items
[i
].second
== item
then
525 redef fun remove_all
(item
: E
)
527 var i
= _items
.length
- 1
529 if _items
[i
].second
== item
then
536 redef fun remove_at
(key
)
539 if i
>= 0 then remove_at_index
(i
)
542 redef fun clear
do _items
.clear
544 # Assume the capacity to be at least `cap'.
545 fun enlarge
(cap
: Int) do _items
.enlarge
(cap
)
547 redef fun couple_at
(key
)
558 var _items
: Array[Couple[K
,E
]]
560 # fast remove the ith element of the array
561 private fun remove_at_index
(i
: Int)
563 _items
[i
] = _items
.last
567 # The last positive result given by a index(1) call
568 var _last_index
: Int = 0
570 # Where is the `key' in `_item'?
571 # return -1 if not found
572 private fun index
(key
: K
): Int
575 if l
< _items
.length
and _items
[l
].first
== key
then return l
578 while i
< _items
.length
do
579 if _items
[i
].first
== key
then
591 _items
= new Array[Couple[K
,E
]]
595 class ArrayMapKeys[K
: Object, E
]
598 var map
: ArrayMap[K
, E
]
599 redef fun count
(k
) do if self.has
(k
) then return 1 else return 0
600 redef fun first
do return self.map
._items
.first
.first
601 redef fun has
(k
) do return self.map
.index
(k
) >= 0
602 redef fun has_only
(k
) do return (self.has
(k
) and self.length
== 1) or self.is_empty
603 redef fun is_empty
do return self.map
.is_empty
604 redef fun length
do return self.map
.length
605 redef fun iterator
do return new MapKeysIterator[K
, E
](self.map
.iterator
)
608 class ArrayMapValues[K
: Object, E
]
611 var map
: ArrayMap[K
, E
]
612 redef fun first
do return self.map
._items
.first
.first
613 redef fun is_empty
do return self.map
.is_empty
614 redef fun length
do return self.map
.length
615 redef fun iterator
do return new MapValuesIterator[K
, E
](self.map
.iterator
)
620 for i
in self.map
._items
do if i
.second
== item
then return true
625 redef fun has_only
(item
)
627 for i
in self.map
._items
do if i
.second
!= item
then return false
632 redef fun count
(item
)
635 for i
in self.map
._items
do if i
.second
== item
then nb
+= 1
642 # Others tools ################################################################
644 redef class Iterator[E
]
645 # Interate on `self' and build an array
648 var res
= new Array[E
]
657 redef class Collection[E
]
658 # Build a new array from a collection
665 # Native classes ##############################################################
667 # Subclasses of this class can create native arrays
668 interface ArrayCapable[E
]
669 # Get a new array of `size' elements.
670 protected fun calloc_array
(size
: Int): NativeArray[E
] is intern
673 # Native C array (void ...).
674 universal NativeArray[E
]
675 fun [](index
: Int): E
is intern
676 fun []=(index
: Int, item
: E
) is intern
677 fun copy_to
(dest
: NativeArray[E
], length
: Int) is intern
678 #fun =(o: NativeArray[E]): Bool is intern
679 #fun !=(o: NativeArray[E]): Bool is intern