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
]
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 insert_all
(coll
, pos
)
191 if l
== 0 then return
194 copy_to
(pos
, length-pos-l
, self, pos
+ l
)
201 redef fun add
(item
) do self[length
] = item
203 redef fun clear
do _length
= 0
205 redef fun remove
(item
) do remove_at
(index_of
(item
))
207 redef fun remove_all
(item
)
209 var i
= index_of
(item
)
212 i
= index_of_from
(item
, i
)
216 redef fun remove_at
(i
)
219 if i
>= 0 and i
< l
then
229 # Invert two elements in the array
231 # var a = [10, 20, 30, 40]
233 # assert a == [10, 40, 30, 20]
234 fun swap_at
(a
: Int,b
: Int)
242 # Resizable one dimension array of objects.
244 # Arrays have a literal representation.
245 # var a = [12, 32, 8]
246 # # is equivalent with:
247 # var b = new Array[Int]
253 super AbstractArray[E
]
254 super ArrayCapable[E
]
258 assert index
: index
>= 0 and index
< _length
262 redef fun []=(index
, item
)
264 assert index
: index
>= 0 and index
< _length
+ 1
265 if _capacity
<= index
then
268 if _length
<= index
then
277 if _capacity
<= l
then
284 redef fun enlarge
(cap
)
287 if cap
<= c
then return
288 while c
<= cap
do c
= c
* 2 + 2
289 var a
= calloc_array
(c
)
290 if _capacity
> 0 then _items
.copy_to
(a
, _length
)
295 # Create an empty array.
302 # Create an array from a collection.
303 init from
(items
: Collection[E
]) do
304 with_capacity
(items
.length
)
308 # Create an array with some `objects`.
309 init with_items
(objects
: E
...)
311 _items
= objects
._items
312 _capacity
= objects
._capacity
313 _length
= objects
.length
316 # Create an empty array with a given capacity.
317 init with_capacity
(cap
: Int)
319 assert positive
: cap
>= 0
320 _items
= calloc_array
(cap
)
325 # Create an array of `count` elements
326 init filled_with
(value
: E
, count
: Int)
328 assert positive
: count
>= 0
329 _items
= calloc_array
(count
)
339 # Create a array filled with a given native array.
340 init with_native
(nat
: NativeArray[E
], size
: Int)
342 assert positive
: size
>= 0
348 # The internal storage.
349 private var items
: nullable NativeArray[E
] = null
351 # Do not use this method
352 # FIXME: Remove it once modules can intrude non local modules
353 fun intern_items
: NativeArray[E
] do return _items
.as(not null)
355 # The size of `_items`.
356 private var capacity
: Int = 0
360 if not o
isa Array[nullable Object] then return super
361 # Efficient implementation
363 if l
!= o
.length
then return false
368 if it
[i
] != oit
[i
] then return false
375 # An `Iterator` on `AbstractArray`
376 private class ArrayIterator[E
]
377 super IndexedIterator[E
]
379 redef fun item
do return _array
[_index
]
381 # redef fun item=(e) do _array[_index] = e
383 redef fun is_ok
do return _index
< _array
.length
385 redef fun next
do _index
+= 1
387 init(a
: AbstractArrayRead[E
])
395 private var array
: AbstractArrayRead[E
]
398 private class ArrayReverseIterator[E
]
399 super ArrayIterator[E
]
401 redef fun is_ok
do return _index
>= 0
403 redef fun next
do _index
-= 1
405 init(a
: AbstractArrayRead[E
])
408 _index
= a
.length
- 1
412 # Others collections ##########################################################
414 # A set implemented with an Array.
415 class ArraySet[E
: Object]
418 # The stored elements.
419 private var array
: Array[E
]
421 redef fun has
(e
) do return _array
.has
(e
)
423 redef fun add
(e
) do if not _array
.has
(e
) then _array
.add
(e
)
425 redef fun is_empty
do return _array
.is_empty
427 redef fun length
do return _array
.length
431 assert _array
.length
> 0
435 redef fun remove
(item
)
437 var i
= _array
.index_of
(item
)
438 if i
>= 0 then remove_at
(i
)
441 redef fun remove_all
(item
) do remove
(item
)
443 redef fun clear
do _array
.clear
445 redef fun iterator
do return new ArraySetIterator[E
](_array
.iterator
)
447 # Assume the capacity is at least `cap`.
448 fun enlarge
(cap
: Int) do _array
.enlarge
(cap
)
450 private fun remove_at
(i
: Int)
452 _array
[i
] = _array
.last
456 # Create an empty set
457 init do _array
= new Array[E
]
459 # Create an empty set with a given capacity.
460 init with_capacity
(i
: Int) do _array
= new Array[E
].with_capacity
(i
)
462 redef fun new_set
do return new ArraySet[E
]
465 # Iterators on sets implemented with arrays.
466 private class ArraySetIterator[E
: Object]
469 redef fun is_ok
do return _iter
.is_ok
471 redef fun next
do _iter
.next
473 redef fun item
: E
do return _iter
.item
475 init(iter
: ArrayIterator[E
]) do _iter
= iter
477 private var iter
: ArrayIterator[E
]
481 # Associative arrays implemented with an array of (key, value) pairs.
482 class ArrayMap[K
: Object, E
]
483 super CoupleMap[K
, E
]
490 return _items
[i
].second
492 return provide_default_value
(key
)
497 redef fun []=(key
, item
)
501 _items
[i
].second
= item
503 _items
.push
(new Couple[K
,E
](key
, item
))
507 redef var keys
: RemovableCollection[K
] = new ArrayMapKeys[K
, E
](self)
508 redef var values
: RemovableCollection[E
] = new ArrayMapValues[K
, E
](self)
511 redef fun length
do return _items
.length
513 redef fun couple_iterator
do return _items
.iterator
515 redef fun is_empty
do return _items
.is_empty
517 redef fun clear
do _items
.clear
519 # Assume the capacity to be at least `cap`.
520 fun enlarge
(cap
: Int) do _items
.enlarge
(cap
)
522 redef fun couple_at
(key
)
533 private var items
= new Array[Couple[K
,E
]]
535 # fast remove the ith element of the array
536 private fun remove_at_index
(i
: Int)
538 _items
[i
] = _items
.last
542 # The last positive result given by a index(1) call
543 private var last_index
: Int = 0
545 # Where is the `key` in `_item`?
546 # return -1 if not found
547 private fun index
(key
: K
): Int
550 if l
< _items
.length
and _items
[l
].first
== key
then return l
553 while i
< _items
.length
do
554 if _items
[i
].first
== key
then
564 private class ArrayMapKeys[K
: Object, E
]
565 super RemovableCollection[K
]
567 var map
: ArrayMap[K
, E
]
568 redef fun count
(k
) do if self.has
(k
) then return 1 else return 0
569 redef fun first
do return self.map
._items
.first
.first
570 redef fun has
(k
) do return self.map
.index
(k
) >= 0
571 redef fun has_only
(k
) do return (self.has
(k
) and self.length
== 1) or self.is_empty
572 redef fun is_empty
do return self.map
.is_empty
573 redef fun length
do return self.map
.length
574 redef fun iterator
do return new MapKeysIterator[K
, E
](self.map
.iterator
)
575 redef fun clear
do self.map
.clear
576 redef fun remove
(key
)
578 var i
= self.map
.index
(key
)
579 if i
>= 0 then self.map
.remove_at_index
(i
)
581 redef fun remove_all
(key
) do self.remove
(key
)
584 private class ArrayMapValues[K
: Object, E
]
585 super RemovableCollection[E
]
587 var map
: ArrayMap[K
, E
]
588 redef fun first
do return self.map
._items
.first
.second
589 redef fun is_empty
do return self.map
.is_empty
590 redef fun length
do return self.map
.length
591 redef fun iterator
do return new MapValuesIterator[K
, E
](self.map
.iterator
)
596 for i
in self.map
._items
do if i
.second
== item
then return true
601 redef fun has_only
(item
)
603 for i
in self.map
._items
do if i
.second
!= item
then return false
608 redef fun count
(item
)
611 for i
in self.map
._items
do if i
.second
== item
then nb
+= 1
615 redef fun clear
do self.map
.clear
617 redef fun remove
(item
)
620 var i
= map
._items
.length
- 1
622 if map
._items
[i
].second
== item
then
623 map
.remove_at_index
(i
)
630 redef fun remove_all
(item
)
633 var i
= map
._items
.length
- 1
635 if map
._items
[i
].second
== item
then
636 map
.remove_at_index
(i
)
643 # Comparable array for comparable elements.
645 # For two arrays, if one is a prefix, then it is lower.
648 # var a12 = new ArrayCmp[nullable Int].with_items(1,2)
649 # var a123 = new ArrayCmp[nullable Int].with_items(1,2,3)
653 # Otherwise, the first element just after the longest
654 # common prefix gives the order between the two arrays.
657 # var a124 = new ArrayCmp[nullable Int].with_items(1,2,4)
658 # var a13 = new ArrayCmp[nullable Int].with_items(1,3)
663 # Obviously, two equal arrays are equal.
666 # var b12 = new ArrayCmp[nullable Int].with_items(1,2)
667 # assert (a12 <=> b12) == 0
670 # `null` is considered lower than any other elements.
671 # But is still greater than no element.
674 # var a12n = new ArrayCmp[nullable Int].with_items(1,2,null)
678 class ArrayCmp[E
: nullable Comparable]
681 redef type OTHER: ArrayCmp[E
] is fixed
683 redef fun <(o
) do return (self <=> o
) < 0
693 if l
< ol
then len
= l
else len
= ol
698 if b
== null then return 1
699 var d
= a
<=> b
.as(Comparable)
700 if d
!= 0 then return d
702 if b
!= null then return -1
710 # Others tools ################################################################
712 redef class Iterator[E
]
713 # Interate on `self` and build an array
716 var res
= new Array[E
]
725 redef class Collection[E
]
726 # Build a new array from a collection
729 var res
= new Array[E
].with_capacity
(length
)
735 # Native classes ##############################################################
737 # Subclasses of this class can create native arrays
738 interface ArrayCapable[E
]
739 # Get a new array of `size` elements.
740 protected fun calloc_array
(size
: Int): NativeArray[E
] is intern
744 # Access are unchecked and it has a fixed size
745 # Not for public use: may become private.
746 universal NativeArray[E
]
747 # Creates a new NativeArray of capacity `length`
748 new(length
: Int) is intern
749 # The length of the array
750 fun length
: Int is intern
751 # Use `self` to initialize a standard Nit Array.
752 fun to_a
: Array[E
] do return new Array[E
].with_native
(self, length
)
753 fun [](index
: Int): E
is intern
754 fun []=(index
: Int, item
: E
) is intern
755 fun copy_to
(dest
: NativeArray[E
], length
: Int) is intern
756 #fun =(o: NativeArray[E]): Bool is intern
757 #fun !=(o: NativeArray[E]): Bool is intern