1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
5 # This file is free software, which comes along with NIT. This software is
6 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
7 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
8 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
9 # is kept unaltered, and a notification of the changes is added.
10 # You are allowed to redistribute it and sell it, alone or is a part of
13 # Abstract collection classes and services.
15 # TODO specify the behavior on iterators when collections are modified.
16 module abstract_collection
20 # The root of the collection hierarchy.
22 # Collections modelize finite groups of objects, called elements.
24 # The specific behavior and representation of collections is determined
25 # by the subclasses of the hierarchy.
27 # The main service of Collection is to provide a stable `iterator`
28 # method usable to retrieve all the elements of the collection.
30 # Additional services are provided.
31 # For an implementation point of view, Collection provide a basic
32 # implementation of these services using the `iterator` method.
33 # Subclasses often provide a more efficient implementation.
35 # Because of the `iterator` method, Collections instances can use
36 # the `for` control structure.
39 # var x: Collection[U]
47 # that is equivalent with the following:
50 # var x: Collection[U]
54 # var u = i.item # u is a U
59 interface Collection[E
]
60 # Get a new iterator on the collection.
61 fun iterator
: Iterator[E
] is abstract
63 # Is there no item in the collection?
65 # assert [1,2,3].is_empty == false
66 # assert [1..1[.is_empty == true
67 fun is_empty
: Bool do return length
== 0
69 # Alias for `not is_empty`.
71 # Some people prefer to have conditions grammatically easier to read.
73 # assert [1,2,3].not_empty == true
74 # assert [1..1[.not_empty == false
75 fun not_empty
: Bool do return not self.is_empty
77 # Number of items in the collection.
79 # assert [10,20,30].length == 3
80 # assert [20..30[.length == 10
84 for i
in self do nb
+= 1
88 # Is `item` in the collection ?
89 # Comparisons are done with ==
91 # assert [1,2,3].has(2) == true
92 # assert [1,2,3].has(9) == false
93 # assert [1..5[.has(2) == true
94 # assert [1..5[.has(9) == false
95 fun has
(item
: nullable Object): Bool
97 for i
in self do if i
== item
then return true
101 # Is the collection contain only `item`?
102 # Comparisons are done with ==
103 # Return true if the collection is empty.
105 # assert [1,1,1].has_only(1) == true
106 # assert [1,2,3].has_only(1) == false
107 # assert [1..1].has_only(1) == true
108 # assert [1..3].has_only(1) == false
109 # assert [3..3[.has_only(1) == true # empty collection
111 # ENSURE `is_empty implies result == true`
112 fun has_only
(item
: nullable Object): Bool
114 for i
in self do if i
!= item
then return false
118 # How many occurrences of `item` are in the collection?
119 # Comparisons are done with ==
121 # assert [10,20,10].count(10) == 2
122 fun count
(item
: nullable Object): Int
125 for i
in self do if i
== item
then nb
+= 1
129 # Return the first item of the collection
131 # assert [1,2,3].first == 1
138 # Does the collection contain at least each element of `other`?
140 # assert [1,3,4,2].has_all([1..2]) == true
141 # assert [1,3,4,2].has_all([1..5]) == false
143 # Repeated elements in the collections are not considered.
145 # assert [1,1,1].has_all([1]) == true
146 # assert [1..5].has_all([1,1,1]) == true
148 # Note that the default implementation is general and correct for any lawful Collections.
149 # It is memory-efficient but relies on `has` so may be CPU-inefficient for some kind of collections.
150 fun has_all
(other
: Collection[nullable Object]): Bool
152 if is_same_instance
(other
) then return true
153 var ol
= other
.length
155 if ol
== 0 then return true
156 if l
== 0 then return false
157 if ol
== 1 then return has
(other
.first
)
158 for x
in other
do if not has
(x
) then return false
162 # Does the collection contain exactly all the elements of `other`?
164 # The same elements must be present in both `self` and `other`,
165 # but the order of the elements in the collections are not considered.
167 # assert [1..3].has_exactly([3,1,2]) == true # the same elements
168 # assert [1..3].has_exactly([3,1]) == false # 2 is not in the array
169 # assert [1..2].has_exactly([3,1,2]) == false # 3 is not in the range
171 # Repeated elements must be present in both collections in the same amount.
172 # So basically it is a multi-set comparison.
174 # assert [1,2,3,2].has_exactly([1,2,2,3]) == true # the same elements
175 # assert [1,2,3,2].has_exactly([1,2,3]) == false # more 2 in the first array
176 # assert [1,2,3].has_exactly([1,2,2,3]) == false # more 2 in the second array
178 # Note that the default implementation is general and correct for any lawful Collections.
179 # It is memory-efficient but relies on `count` so may be CPU-inefficient for some kind of collections.
180 fun has_exactly
(other
: Collection[nullable Object]): Bool
182 if length
!= other
.length
then return false
183 for e
in self do if self.count
(e
) != other
.count
(e
) then return false
187 # Does the collection contain at least one element of `other`?
189 # assert [1,3,4,2].has_any([1..10]) == true
190 # assert [1,3,4,2].has_any([5..10]) == false
192 # Note that the default implementation is general and correct for any lawful Collections.
193 # It is memory-efficient but relies on `has` so may be CPU-inefficient for some kind of collections.
194 fun has_any
(other
: Collection[nullable Object]): Bool
197 if has
(o
) then return true
203 # Iterators generate a series of elements, one at a time.
205 # They are mainly used with collections and obtained from `Collection::iterator`.
206 interface Iterator[E
]
209 fun item
: E
is abstract
211 # Jump to the next item.
215 # Jump to the next item `step` times.
218 # var i = [11, 22, 33, 44].iterator
219 # assert i.item == 11
221 # assert i.item == 33
224 # `next_by` should be used instead of looping on `next` because is takes care
225 # of stopping if the end of iteration is reached prematurely whereas a loop of
226 # `next` will abort because of the precondition on `is_ok`.
233 # If `step` is negative, this method aborts.
234 # But specific subclasses can change this and do something more meaningful instead.
237 fun next_by
(step
: Int)
240 while is_ok
and step
> 0 do
246 # Is there a current item ?
247 fun is_ok
: Bool is abstract
249 # Iterate over `self`
250 fun iterator
: Iterator[E
] do return self
252 # Pre-iteration hook.
254 # Used to inform `self` that the iteration is starting.
255 # Specific iterators can use this to prepare some resources.
257 # Is automatically invoked at the beginning of `for` structures.
259 # Do nothing by default.
262 # Post-iteration hook.
264 # Used to inform `self` that the iteration is over.
265 # Specific iterators can use this to free some resources.
267 # Is automatically invoked at the end of `for` structures.
269 # Do nothing by default.
272 # A decorator around `self` that advance self a given number of steps instead of one.
275 # var i = [11, 22, 33, 44, 55].iterator
276 # var i2 = i.to_step(2)
278 # assert i2.item == 11
280 # assert i2.item == 33
282 # assert i.item == 33
284 fun to_step
(step
: Int): Iterator[E
] do return new StepIterator[E
](self, step
)
287 # A basic helper class to specialize specific Iterator decorators
288 abstract class IteratorDecorator[E
]
291 # The underling iterator
292 protected var real
: Iterator[E
]
294 redef fun is_ok
do return real
.is_ok
295 redef fun item
do return real
.item
296 redef fun finish
do real
.finish
297 redef fun next
do real
.next
298 redef fun next_by
(step
) do real
.next_by
(step
)
301 # A decorator that advance a given number of steps
302 private class StepIterator[E
]
303 super IteratorDecorator[E
]
306 redef fun next
do real
.next_by
(step
)
307 redef fun next_by
(step
) do real
.next_by
(step
* self.step
)
310 # A collection that contains only one item.
312 # Used to pass arguments by reference.
314 # Also used when one want to give a single element when a full
315 # collection is expected
319 redef fun first
do return item
321 redef fun is_empty
do return false
323 redef fun length
do return 1
325 redef fun has
(an_item
) do return item
== an_item
327 redef fun has_only
(an_item
) do return item
== an_item
329 redef fun count
(an_item
)
331 if item
== an_item
then
338 redef fun iterator
do return new RefIterator[E
](self)
341 var item
: E
is writable
344 # This iterator is quite stupid since it is used for only one item.
345 private class RefIterator[E
]
347 redef fun item
do return _container
.item
349 redef fun next
do is_ok
= false
351 redef var is_ok
= true
353 var container
: Ref[E
]
356 # Items can be removed from this collection
357 interface RemovableCollection[E
]
364 # assert a.length == 0
367 fun clear
is abstract
369 # Remove an occurrence of `item`
371 # var a = [1,2,3,1,2,3]
373 # assert a == [1,3,1,2,3]
374 fun remove
(item
: nullable Object) is abstract
376 # Remove all occurrences of `item`
378 # var a = [1,2,3,1,2,3]
380 # assert a == [1,3,1,3]
381 fun remove_all
(item
: nullable Object) do while has
(item
) do remove
(item
)
384 # Items can be added to these collections.
385 interface SimpleCollection[E
]
386 super RemovableCollection[E
]
388 # Add an item in a collection.
392 # assert a.has(3) == true
393 # assert a.has(10) == false
395 # Ensure col.has(item)
396 fun add
(item
: E
) is abstract
398 # Add each item of `coll`.
401 # assert a.has(4) == true
402 # assert a.has(10) == false
403 fun add_all
(coll
: Collection[E
]) do for i
in coll
do add
(i
)
408 # Set is a collection without duplicates (according to `==`)
410 # var s: Set[String] = new ArraySet[String]
412 # var b = "Hel" + "lo"
415 # assert s.has(b) == true
417 super SimpleCollection[E
]
420 redef fun has_only
(item
)
433 redef fun count
(item
)
442 # Synonym of remove since there is only one item
443 redef fun remove_all
(item
) do remove
(item
)
445 # Equality is defined on set and means that each set contains the same elements
448 if not other
isa Set[nullable Object] then return false
449 if other
.length
!= length
then return false
450 return has_all
(other
)
453 # Because of the law between `==` and `hash`, `hash` is redefined to be the sum of the hash of the elements
456 # 23 is a magic number empirically determined to be not so bad.
457 var res
= 23 + length
458 # Note: the order of the elements must not change the hash value.
459 # So, unlike usual hash functions, the accumulator is not combined with itself.
461 if e
!= null then res
+= e
.hash
466 # Returns the union of this set with the `other` set
467 fun union
(other
: Set[E
]): Set[E
]
475 # Returns the intersection of this set with the `other` set
476 fun intersection
(other
: Set[E
]): Set[E
]
479 for v
in self do if other
.has
(v
) then nhs
.add
(v
)
483 redef fun clone
do return union
(self)
485 # Returns a new instance of `Set`.
487 # Depends on the subclass, mainly used for copy services
488 # like `union` or `intersection`.
489 protected fun new_set
: Set[E
] is abstract
492 # MapRead are abstract associative collections: `key` -> `item`.
493 interface MapRead[K
, V
]
494 # Get the item at `key`
496 # var x = new HashMap[String, Int]
498 # assert x["four"] == 4
499 # # assert x["five"] #=> abort
501 # If the key is not in the map, `provide_default_value` is called (that aborts by default)
502 # See `get_or_null` and `get_or_default` for safe variations.
503 fun [](key
: nullable Object): V
is abstract
505 # Get the item at `key` or null if `key` is not in the map.
507 # var x = new HashMap[String, Int]
509 # assert x.get_or_null("four") == 4
510 # assert x.get_or_null("five") == null
512 # Note: use `has_key` and `[]` if you need the distinction between a key associated with null, and no key.
513 fun get_or_null
(key
: nullable Object): nullable V
515 if has_key
(key
) then return self[key
]
519 # Get the item at `key` or return `default` if not in map
521 # var x = new HashMap[String, Int]
523 # assert x.get_or_default("four", 40) == 4
524 # assert x.get_or_default("five", 50) == 50
526 fun get_or_default
(key
: nullable Object, default
: V
): V
528 if has_key
(key
) then return self[key
]
532 # Is there an item associated with `key`?
534 # var x = new HashMap[String, Int]
536 # assert x.has_key("four") == true
537 # assert x.has_key("five") == false
539 # By default it is a synonymous to `keys.has` but could be redefined with a direct implementation.
540 fun has_key
(key
: nullable Object): Bool do return self.keys
.has
(key
)
542 # Get a new iterator on the map.
543 fun iterator
: MapIterator[K
, V
] is abstract
545 # Return the point of view of self on the values only.
546 # Note that `self` and `values` are views on the same data;
547 # therefore any modification of one is visible on the other.
549 # var x = new HashMap[String, Int]
551 # assert x.values.has(4) == true
552 # assert x.values.has(5) == false
553 fun values
: Collection[V
] is abstract
555 # Return the point of view of self on the keys only.
556 # Note that `self` and `keys` are views on the same data;
557 # therefore any modification of one is visible on the other.
559 # var x = new HashMap[String, Int]
561 # assert x.keys.has("four") == true
562 # assert x.keys.has("five") == false
563 fun keys
: Collection[K
] is abstract
565 # Is there no item in the collection?
567 # var x = new HashMap[String, Int]
568 # assert x.is_empty == true
570 # assert x.is_empty == false
571 fun is_empty
: Bool is abstract
573 # Alias for `not is_empty`.
575 # Some people prefer to have conditions grammatically easier to read.
577 # var map = new HashMap[String, Int]
578 # assert map.not_empty == false
580 # assert map.not_empty == true
581 fun not_empty
: Bool do return not self.is_empty
583 # Number of items in the collection.
585 # var x = new HashMap[String, Int]
586 # assert x.length == 0
588 # assert x.length == 1
590 # assert x.length == 2
591 fun length
: Int is abstract
593 # Called by the underling implementation of `[]` to provide a default value when a `key` has no value
594 # By default the behavior is to abort.
596 # Note: the value is returned *as is*, implementations may want to store the value in the map before returning it
598 protected fun provide_default_value
(key
: nullable Object): V
do abort
600 # Does `self` and `other` have the same keys associated with the same values?
603 # var a = new HashMap[String, Int]
604 # var b = new ArrayMap[Object, Numeric]
615 if not other
isa MapRead[nullable Object, nullable Object] then return false
616 if other
.length
!= self.length
then return false
618 if not other
.has_key
(k
) then return false
619 if other
[k
] != v
then return false
624 # A hashcode based on the hashcode of the keys and the values.
627 # var a = new HashMap[String, Int]
628 # var b = new ArrayMap[Object, Numeric]
631 # assert a.hash == b.hash
637 if k
!= null then res
+= k
.hash
* 7
638 if v
!= null then res
+= v
.hash
* 11
644 # Maps are associative collections: `key` -> `item`.
646 # The main operator over maps is [].
648 # var map: Map[String, Int] = new ArrayMap[String, Int]
650 # map["one"] = 1 # Associate 'one' to '1'
651 # map["two"] = 2 # Associate 'two' to '2'
652 # assert map["one"] == 1
653 # assert map["two"] == 2
655 # Instances of maps can be used with the for structure
657 # for key, value in map do
658 # assert (key == "one" and value == 1) or (key == "two" and value == 2)
661 # The keys and values in the map can also be manipulated directly with the `keys` and `values` methods.
663 # assert map.keys.has("one") == true
664 # assert map.keys.has("tree") == false
665 # assert map.values.has(1) == true
666 # assert map.values.has(3) == false
671 # Set the `value` at `key`.
673 # Values can then get retrieved with `[]`.
675 # var x = new HashMap[String, Int]
677 # assert x["four"] == 4
679 # If the key was associated with a value, this old value is discarded
680 # and replaced with the new one.
683 # assert x["four"] == 40
684 # assert x.values.has(4) == false
686 fun []=(key
: K
, value
: V
) is abstract
688 # Add each (key,value) of `map` into `self`.
689 # If a same key exists in `map` and `self`, then the value in self is discarded.
691 # var x = new HashMap[String, Int]
694 # var y = new HashMap[String, Int]
698 # assert x["four"] == 40
699 # assert x["five"] == 5
700 # assert x["nine"] == 90
701 fun add_all
(map
: MapRead[K
, V
])
710 # Alias for `add_all`
711 fun recover_with
(map
: MapRead[K
, V
]) is deprecated
do add_all
(map
)
715 # var x = new HashMap[String, Int]
718 # assert x.keys.has("four") == false
721 fun clear
is abstract
723 redef fun values
: RemovableCollection[V
] is abstract
725 redef fun keys
: RemovableCollection[K
] is abstract
729 interface MapIterator[K
, V
]
732 fun item
: V
is abstract
734 # The key of the current item.
736 fun key
: K
is abstract
738 # Jump to the next item.
742 # Is there a current item ?
743 fun is_ok
: Bool is abstract
745 # Set a new `item` at `key`.
746 #fun item=(item: E) is abstract
748 # Pre-iteration hook.
750 # Used to inform `self` that the iteration is starting.
751 # Specific iterators can use this to prepare some resources.
753 # Is automatically invoked at the beginning of `for` structures.
755 # Do nothing by default.
758 # Post-iteration hook.
760 # Used to inform `self` that the iteration is over.
761 # Specific iterators can use this to free some resources.
763 # Is automatically invoked at the end of `for` structures.
765 # Do nothing by default.
769 # Iterator on a 'keys' point of view of a map
770 class MapKeysIterator[K
, V
]
772 # The original iterator
773 var original_iterator
: MapIterator[K
, V
]
775 redef fun is_ok
do return self.original_iterator
.is_ok
776 redef fun next
do self.original_iterator
.next
777 redef fun item
do return self.original_iterator
.key
780 # Iterator on a 'values' point of view of a map
781 class MapValuesIterator[K
, V
]
783 # The original iterator
784 var original_iterator
: MapIterator[K
, V
]
786 redef fun is_ok
do return self.original_iterator
.is_ok
787 redef fun next
do self.original_iterator
.next
788 redef fun item
do return self.original_iterator
.item
791 # Sequences are indexed collections.
792 # The first item is 0. The last is `length-1`.
794 # The order is the main caracteristic of sequence
795 # and all concrete implementation of sequences are basically interchangeable.
796 interface SequenceRead[E
]
799 # Get the first item.
800 # Is equivalent with `self[0]`.
803 # assert a.first == 1
805 # REQUIRE `not is_empty`
808 assert not_empty
: not is_empty
812 # Return the index-th element of the sequence.
813 # The first element is 0 and the last is `length-1`
814 # If index is invalid, the program aborts
821 # REQUIRE `index >= 0 and index < length`
822 fun [](index
: Int): E
is abstract
824 # Return the index-th element but wrap
826 # Whereas `self[]` requires the index to exists, the `modulo` accessor automatically
827 # wraps overbound and underbouds indexes.
831 # assert a.modulo(1) == 20
832 # assert a.modulo(3) == 10
833 # assert a.modulo(-1) == 30
834 # assert a.modulo(-10) == 30
837 # REQUIRE `not_empty`
838 # ENSURE `result == self[modulo_index(index)]`
839 fun modulo
(index
: Int): E
do return self[modulo_index
(index
)]
841 # Returns the real index for a modulo index.
845 # assert a.modulo_index(1) == 1
846 # assert a.modulo_index(3) == 0
847 # assert a.modulo_index(-1) == 2
848 # assert a.modulo_index(-10) == 2
851 # REQUIRE `not_empty`
852 fun modulo_index
(index
: Int): Int
854 var length
= self.length
856 return index
% length
858 return length
- (-1 - index
) % length
- 1
863 # Is equivalent with `self[length-1]`.
868 # REQUIRE `not is_empty`
871 assert not_empty
: not is_empty
872 return self[length-1
]
875 # The index of the first occurrence of `item`.
876 # Return -1 if `item` is not found.
877 # Comparison is done with `==`.
879 # var a = [10,20,30,10,20,30]
880 # assert a.index_of(20) == 1
881 # assert a.index_of(40) == -1
882 fun index_of
(item
: nullable Object): Int do return index_of_from
(item
, 0)
884 # The index of the last occurrence of `item`.
885 # Return -1 if `item` is not found.
886 # Comparison is done with `==`.
888 # var a = [10,20,30,10,20,30]
889 # assert a.last_index_of(20) == 4
890 # assert a.last_index_of(40) == -1
891 fun last_index_of
(item
: nullable Object): Int do return last_index_of_from
(item
, length-1
)
893 # The index of the first occurrence of `item`, starting from pos.
894 # Return -1 if `item` is not found.
895 # Comparison is done with `==`.
897 # var a = [10,20,30,10,20,30]
898 # assert a.index_of_from(20, 3) == 4
899 # assert a.index_of_from(20, 4) == 4
900 # assert a.index_of_from(20, 5) == -1
901 fun index_of_from
(item
: nullable Object, pos
: Int): Int
906 if p
>=pos
and i
.item
== item
then return i
.index
913 # The index of the last occurrence of `item` starting from `pos` and decrementing.
914 # Return -1 if `item` is not found.
915 # Comparison is done with `==`.
917 # var a = [10,20,30,10,20,30]
918 # assert a.last_index_of_from(20, 2) == 1
919 # assert a.last_index_of_from(20, 1) == 1
920 # assert a.last_index_of_from(20, 0) == -1
921 fun last_index_of_from
(item
: nullable Object, pos
: Int): Int do
924 if self[i
] == item
then return i
930 # Two sequences are equals if they have the same items in the same order.
932 # var a = new List[Int]
936 # assert a == [1,2,3]
937 # assert a != [1,3,2]
940 if not o
isa SequenceRead[nullable Object] then return false
942 if o
.length
!= l
then return false
945 if self[i
] != o
[i
] then return false
951 # Because of the law between `==` and `hash`, `hash` is redefined to be the sum of the hash of the elements
954 # The 17 and 2/3 magic numbers were determined empirically.
955 # Note: the standard hash functions djb2, sbdm and fnv1 were also
956 # tested but were comparable (or worse).
957 var res
= 17 + length
960 if e
!= null then res
+= e
.hash
965 redef fun iterator
: IndexedIterator[E
] is abstract
967 # Gets a new Iterator starting at position `pos`
969 # var iter = [10,20,30,40,50].iterator_from(2)
970 # assert iter.to_a == [30, 40, 50]
971 fun iterator_from
(pos
: Int): IndexedIterator[E
]
974 while pos
> 0 and res
.is_ok
do
981 # Gets an iterator starting at the end and going backwards
983 # var reviter = [1,2,3].reverse_iterator
984 # assert reviter.to_a == [3,2,1]
985 fun reverse_iterator
: IndexedIterator[E
] is abstract
987 # Gets an iterator on the chars of self starting from `pos`
989 # var reviter = [10,20,30,40,50].reverse_iterator_from(2)
990 # assert reviter.to_a == [30,20,10]
991 fun reverse_iterator_from
(pos
: Int): IndexedIterator[E
]
993 var res
= reverse_iterator
994 while pos
> 0 and res
.is_ok
do
1002 # Sequence are indexed collection.
1003 # The first item is 0. The last is `length-1`.
1004 interface Sequence[E
]
1005 super SequenceRead[E
]
1006 super SimpleCollection[E
]
1008 # Set the first item.
1009 # Is equivalent with `self[0] = item`.
1013 # assert a == [10,2,3]
1015 do self[0] = item
end
1017 # Set the last item.
1018 # Is equivalent with `self[length-1] = item`.
1022 # assert a == [1,2,10]
1024 # If the sequence is empty, `last=` is equivalent with `self[0]=` (thus with `first=`)
1026 # var b = new Array[Int]
1039 # A synonym of `push`
1040 redef fun add
(e
) do push
(e
)
1042 # Add an item after the last one.
1047 # assert a == [1,2,3,10,20]
1048 fun push
(e
: E
) is abstract
1050 # Add each item of `coll` after the last.
1054 # assert a == [1,2,3,7,8,9]
1056 # Alias of `add_all`
1057 fun append
(coll
: Collection[E
]) do add_all
(coll
)
1059 # Remove the last item.
1066 # REQUIRE `not is_empty`
1067 fun pop
: E
is abstract
1069 # Add an item before the first one.
1074 # assert a == [20,10,1,2,3]
1075 fun unshift
(e
: E
) is abstract
1077 # Add all items of `coll` before the first one.
1081 # assert a == [7,8,9,1,2,3]
1083 # Alias of `insert_at(coll, 0)`
1084 fun prepend
(coll
: Collection[E
]) do insert_all
(coll
, 0)
1086 # Remove the first item.
1087 # The second item thus become the first.
1090 # assert a.shift == 1
1091 # assert a.shift == 2
1094 # REQUIRE `not is_empty`
1095 fun shift
: E
is abstract
1097 # Set the `item` at `index`.
1099 # var a = [10,20,30]
1101 # assert a == [10,200,30]
1103 # like with `[]`, index should be between `0` and `length-1`
1104 # However, if `index==length`, `[]=` works like `push`.
1107 # assert a == [10,200,30,400]
1109 # REQUIRE `index >= 0 and index <= length`
1110 fun []=(index
: Int, item
: E
) is abstract
1112 # Set the index-th element but wrap
1114 # Whereas `self[]=` requires the index to exists, the `modulo` accessor automatically
1115 # wraps overbound and underbouds indexes.
1118 # var a = [10,20,30]
1121 # a.modulo(-1) = 300
1122 # a.modulo(-10) = 301
1123 # assert a == [100, 200, 301]
1126 # REQUIRE `not_empty`
1127 # ENSURE `self[modulo_index(index)] == value`
1128 fun modulo
=(index
: Int, value
: E
) do self[modulo_index
(index
)] = value
1130 # Insert an element at a given position, following elements are shifted.
1132 # var a = [10, 20, 30, 40]
1134 # assert a == [10, 20, 100, 30, 40]
1136 # REQUIRE `index >= 0 and index <= length`
1137 # ENSURE `self[index] == item`
1138 fun insert
(item
: E
, index
: Int) is abstract
1140 # Insert all elements at a given position, following elements are shifted.
1142 # var a = [10, 20, 30, 40]
1143 # a.insert_all([100..102], 2)
1144 # assert a == [10, 20, 100, 101, 102, 30, 40]
1146 # REQUIRE `index >= 0 and index <= length`
1147 # ENSURE `self[index] == coll.first`
1148 fun insert_all
(coll
: Collection[E
], index
: Int)
1150 assert index
>= 0 and index
< length
1151 if index
== length
then
1160 # Remove the item at `index` and shift all following elements
1162 # var a = [10,20,30]
1164 # assert a == [10,30]
1166 # REQUIRE `index >= 0 and index < length`
1167 fun remove_at
(index
: Int) is abstract
1169 # Rotates the elements of self once to the left
1172 # var a = [12, 23, 34, 45]
1174 # assert a == [23, 34, 45, 12]
1181 # Rotates the elements of self once to the right
1184 # var a = [12, 23, 34, 45]
1186 # assert a == [45, 12, 23, 34]
1194 # Iterators on indexed collections.
1195 interface IndexedIterator[E
]
1197 # The index of the current item.
1198 fun index
: Int is abstract
1201 # Associative arrays that internally uses couples to represent each (key, value) pairs.
1202 # This is an helper class that some specific implementation of Map may implements.
1203 interface CoupleMap[K
, V
]
1206 # Return the couple of the corresponding key
1207 # Return null if the key is no associated element
1208 protected fun couple_at
(key
: nullable Object): nullable Couple[K
, V
] is abstract
1210 # Return a new iteralot on all couples
1211 # Used to provide `iterator` and others
1212 protected fun couple_iterator
: Iterator[Couple[K
,V
]] is abstract
1214 redef fun iterator
do return new CoupleMapIterator[K
,V
](couple_iterator
)
1218 var c
= couple_at
(key
)
1220 return provide_default_value
(key
)
1226 redef fun has_key
(key
) do return couple_at
(key
) != null
1229 # Iterator on CoupleMap
1231 # Actually it is a wrapper around an iterator of the internal array of the map.
1232 private class CoupleMapIterator[K
, V
]
1233 super MapIterator[K
, V
]
1234 redef fun item
do return _iter
.item
.second
1236 #redef fun item=(e) do _iter.item.second = e
1238 redef fun key
do return _iter
.item
.first
1240 redef fun is_ok
do return _iter
.is_ok
1247 var iter
: Iterator[Couple[K
,V
]]
1250 # Some tools ###################################################################
1252 # Two objects in a simple structure.
1255 # The first element of the couple.
1256 var first
: F
is writable
1258 # The second element of the couple.
1259 var second
: S
is writable