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 for x
in other
do if not has
(x
) then return false
156 # Does the collection contain exactly all the elements of `other`?
158 # The same elements must be present in both `self` and `other`,
159 # but the order of the elements in the collections are not considered.
161 # assert [1..3].has_exactly([3,1,2]) == true # the same elements
162 # assert [1..3].has_exactly([3,1]) == false # 2 is not in the array
163 # assert [1..2].has_exactly([3,1,2]) == false # 3 is not in the range
165 # Repeated elements must be present in both collections in the same amount.
166 # So basically it is a multi-set comparison.
168 # assert [1,2,3,2].has_exactly([1,2,2,3]) == true # the same elements
169 # assert [1,2,3,2].has_exactly([1,2,3]) == false # more 2 in the first array
170 # assert [1,2,3].has_exactly([1,2,2,3]) == false # more 2 in the second array
172 # Note that the default implementation is general and correct for any lawful Collections.
173 # It is memory-efficient but relies on `count` so may be CPU-inefficient for some kind of collections.
174 fun has_exactly
(other
: Collection[nullable Object]): Bool
176 if length
!= other
.length
then return false
177 for e
in self do if self.count
(e
) != other
.count
(e
) then return false
181 # Does the collection contain at least one element of `other`?
183 # assert [1,3,4,2].has_any([1..10]) == true
184 # assert [1,3,4,2].has_any([5..10]) == false
186 # Note that the default implementation is general and correct for any lawful Collections.
187 # It is memory-efficient but relies on `has` so may be CPU-inefficient for some kind of collections.
188 fun has_any
(other
: Collection[nullable Object]): Bool
191 if has
(o
) then return true
197 # Iterators generate a series of elements, one at a time.
199 # They are mainly used with collections and obtained from `Collection::iterator`.
200 interface Iterator[E
]
203 fun item
: E
is abstract
205 # Jump to the next item.
209 # Jump to the next item `step` times.
212 # var i = [11, 22, 33, 44].iterator
213 # assert i.item == 11
215 # assert i.item == 33
218 # `next_by` should be used instead of looping on `next` because is takes care
219 # of stopping if the end of iteration is reached prematurely whereas a loop of
220 # `next` will abort because of the precondition on `is_ok`.
227 # If `step` is negative, this method aborts.
228 # But specific subclasses can change this and do something more meaningful instead.
231 fun next_by
(step
: Int)
234 while is_ok
and step
> 0 do
240 # Is there a current item ?
241 fun is_ok
: Bool is abstract
243 # Iterate over `self`
244 fun iterator
: Iterator[E
] do return self
246 # Pre-iteration hook.
248 # Used to inform `self` that the iteration is starting.
249 # Specific iterators can use this to prepare some resources.
251 # Is automatically invoked at the beginning of `for` structures.
253 # Do nothing by default.
256 # Post-iteration hook.
258 # Used to inform `self` that the iteration is over.
259 # Specific iterators can use this to free some resources.
261 # Is automatically invoked at the end of `for` structures.
263 # Do nothing by default.
266 # A decorator around `self` that advance self a given number of steps instead of one.
269 # var i = [11, 22, 33, 44, 55].iterator
270 # var i2 = i.to_step(2)
272 # assert i2.item == 11
274 # assert i2.item == 33
276 # assert i.item == 33
278 fun to_step
(step
: Int): Iterator[E
] do return new StepIterator[E
](self, step
)
281 # A basic helper class to specialize specific Iterator decorators
282 abstract class IteratorDecorator[E
]
285 # The underling iterator
286 protected var real
: Iterator[E
]
288 redef fun is_ok
do return real
.is_ok
289 redef fun item
do return real
.item
290 redef fun finish
do real
.finish
291 redef fun next
do real
.next
292 redef fun next_by
(step
) do real
.next_by
(step
)
295 # A decorator that advance a given number of steps
296 private class StepIterator[E
]
297 super IteratorDecorator[E
]
300 redef fun next
do real
.next_by
(step
)
301 redef fun next_by
(step
) do real
.next_by
(step
* self.step
)
304 # A collection that contains only one item.
306 # Used to pass arguments by reference.
308 # Also used when one want to give a single element when a full
309 # collection is expected
313 redef fun first
do return item
315 redef fun is_empty
do return false
317 redef fun length
do return 1
319 redef fun has
(an_item
) do return item
== an_item
321 redef fun has_only
(an_item
) do return item
== an_item
323 redef fun count
(an_item
)
325 if item
== an_item
then
332 redef fun iterator
do return new RefIterator[E
](self)
335 var item
: E
is writable
338 # This iterator is quite stupid since it is used for only one item.
339 private class RefIterator[E
]
341 redef fun item
do return _container
.item
343 redef fun next
do is_ok
= false
345 redef var is_ok
= true
347 var container
: Ref[E
]
350 # Items can be removed from this collection
351 interface RemovableCollection[E
]
358 # assert a.length == 0
361 fun clear
is abstract
363 # Remove an occurrence of `item`
365 # var a = [1,2,3,1,2,3]
367 # assert a == [1,3,1,2,3]
368 fun remove
(item
: nullable Object) is abstract
370 # Remove all occurrences of `item`
372 # var a = [1,2,3,1,2,3]
374 # assert a == [1,3,1,3]
375 fun remove_all
(item
: nullable Object) do while has
(item
) do remove
(item
)
378 # Items can be added to these collections.
379 interface SimpleCollection[E
]
380 super RemovableCollection[E
]
382 # Add an item in a collection.
386 # assert a.has(3) == true
387 # assert a.has(10) == false
389 # Ensure col.has(item)
390 fun add
(item
: E
) is abstract
392 # Add each item of `coll`.
395 # assert a.has(4) == true
396 # assert a.has(10) == false
397 fun add_all
(coll
: Collection[E
]) do for i
in coll
do add
(i
)
402 # Set is a collection without duplicates (according to `==`)
404 # var s: Set[String] = new ArraySet[String]
406 # var b = "Hel" + "lo"
409 # assert s.has(b) == true
411 super SimpleCollection[E
]
414 redef fun has_only
(item
)
427 redef fun count
(item
)
436 # Synonym of remove since there is only one item
437 redef fun remove_all
(item
) do remove
(item
)
439 # Equality is defined on set and means that each set contains the same elements
442 if not other
isa Set[nullable Object] then return false
443 if other
.length
!= length
then return false
444 return has_all
(other
)
447 # Because of the law between `==` and `hash`, `hash` is redefined to be the sum of the hash of the elements
450 # 23 is a magic number empirically determined to be not so bad.
451 var res
= 23 + length
452 # Note: the order of the elements must not change the hash value.
453 # So, unlike usual hash functions, the accumulator is not combined with itself.
454 for e
in self do res
+= e
.hash
458 # Returns the union of this set with the `other` set
459 fun union
(other
: Set[E
]): Set[E
]
467 # Returns the intersection of this set with the `other` set
468 fun intersection
(other
: Set[E
]): Set[E
]
471 for v
in self do if other
.has
(v
) then nhs
.add
(v
)
475 redef fun clone
do return union
(self)
477 # Returns a new instance of `Set`.
479 # Depends on the subclass, mainly used for copy services
480 # like `union` or `intersection`.
481 protected fun new_set
: Set[E
] is abstract
484 # MapRead are abstract associative collections: `key` -> `item`.
485 interface MapRead[K
, V
]
486 # Get the item at `key`
488 # var x = new HashMap[String, Int]
490 # assert x["four"] == 4
491 # # assert x["five"] #=> abort
493 # If the key is not in the map, `provide_default_value` is called (that aborts by default)
494 # See `get_or_null` and `get_or_default` for safe variations.
495 fun [](key
: nullable Object): V
is abstract
497 # Get the item at `key` or null if `key` is not in the map.
499 # var x = new HashMap[String, Int]
501 # assert x.get_or_null("four") == 4
502 # assert x.get_or_null("five") == null
504 # Note: use `has_key` and `[]` if you need the distinction between a key associated with null, and no key.
505 fun get_or_null
(key
: nullable Object): nullable V
507 if has_key
(key
) then return self[key
]
511 # Get the item at `key` or return `default` if not in map
513 # var x = new HashMap[String, Int]
515 # assert x.get_or_default("four", 40) == 4
516 # assert x.get_or_default("five", 50) == 50
518 fun get_or_default
(key
: nullable Object, default
: V
): V
520 if has_key
(key
) then return self[key
]
524 # Is there an item associated with `key`?
526 # var x = new HashMap[String, Int]
528 # assert x.has_key("four") == true
529 # assert x.has_key("five") == false
531 # By default it is a synonymous to `keys.has` but could be redefined with a direct implementation.
532 fun has_key
(key
: nullable Object): Bool do return self.keys
.has
(key
)
534 # Get a new iterator on the map.
535 fun iterator
: MapIterator[K
, V
] is abstract
537 # Return the point of view of self on the values only.
538 # Note that `self` and `values` are views on the same data;
539 # therefore any modification of one is visible on the other.
541 # var x = new HashMap[String, Int]
543 # assert x.values.has(4) == true
544 # assert x.values.has(5) == false
545 fun values
: Collection[V
] is abstract
547 # Return the point of view of self on the keys only.
548 # Note that `self` and `keys` are views on the same data;
549 # therefore any modification of one is visible on the other.
551 # var x = new HashMap[String, Int]
553 # assert x.keys.has("four") == true
554 # assert x.keys.has("five") == false
555 fun keys
: Collection[K
] is abstract
557 # Is there no item in the collection?
559 # var x = new HashMap[String, Int]
560 # assert x.is_empty == true
562 # assert x.is_empty == false
563 fun is_empty
: Bool is abstract
565 # Alias for `not is_empty`.
567 # Some people prefer to have conditions grammatically easier to read.
569 # var map = new HashMap[String, Int]
570 # assert map.not_empty == false
572 # assert map.not_empty == true
573 fun not_empty
: Bool do return not self.is_empty
575 # Number of items in the collection.
577 # var x = new HashMap[String, Int]
578 # assert x.length == 0
580 # assert x.length == 1
582 # assert x.length == 2
583 fun length
: Int is abstract
585 # Called by the underling implementation of `[]` to provide a default value when a `key` has no value
586 # By default the behavior is to abort.
588 # Note: the value is returned *as is*, implementations may want to store the value in the map before returning it
590 protected fun provide_default_value
(key
: nullable Object): V
do abort
592 # Does `self` and `other` have the same keys associated with the same values?
595 # var a = new HashMap[String, Int]
596 # var b = new ArrayMap[Object, Numeric]
607 if not other
isa MapRead[nullable Object, nullable Object] then return false
608 if other
.length
!= self.length
then return false
610 if not other
.has_key
(k
) then return false
611 if other
[k
] != v
then return false
616 # A hashcode based on the hashcode of the keys and the values.
619 # var a = new HashMap[String, Int]
620 # var b = new ArrayMap[Object, Numeric]
623 # assert a.hash == b.hash
629 if k
!= null then res
+= k
.hash
* 7
630 if v
!= null then res
+= v
.hash
* 11
636 # Maps are associative collections: `key` -> `item`.
638 # The main operator over maps is [].
640 # var map: Map[String, Int] = new ArrayMap[String, Int]
642 # map["one"] = 1 # Associate 'one' to '1'
643 # map["two"] = 2 # Associate 'two' to '2'
644 # assert map["one"] == 1
645 # assert map["two"] == 2
647 # Instances of maps can be used with the for structure
649 # for key, value in map do
650 # assert (key == "one" and value == 1) or (key == "two" and value == 2)
653 # The keys and values in the map can also be manipulated directly with the `keys` and `values` methods.
655 # assert map.keys.has("one") == true
656 # assert map.keys.has("tree") == false
657 # assert map.values.has(1) == true
658 # assert map.values.has(3) == false
663 # Set the `value` at `key`.
665 # Values can then get retrieved with `[]`.
667 # var x = new HashMap[String, Int]
669 # assert x["four"] == 4
671 # If the key was associated with a value, this old value is discarded
672 # and replaced with the new one.
675 # assert x["four"] == 40
676 # assert x.values.has(4) == false
678 fun []=(key
: K
, value
: V
) is abstract
680 # Add each (key,value) of `map` into `self`.
681 # If a same key exists in `map` and `self`, then the value in self is discarded.
683 # var x = new HashMap[String, Int]
686 # var y = new HashMap[String, Int]
690 # assert x["four"] == 40
691 # assert x["five"] == 5
692 # assert x["nine"] == 90
693 fun add_all
(map
: MapRead[K
, V
])
702 # Alias for `add_all`
703 fun recover_with
(map
: MapRead[K
, V
]) is deprecated
do add_all
(map
)
707 # var x = new HashMap[String, Int]
710 # assert x.keys.has("four") == false
713 fun clear
is abstract
715 redef fun values
: RemovableCollection[V
] is abstract
717 redef fun keys
: RemovableCollection[K
] is abstract
721 interface MapIterator[K
, V
]
724 fun item
: V
is abstract
726 # The key of the current item.
728 fun key
: K
is abstract
730 # Jump to the next item.
734 # Is there a current item ?
735 fun is_ok
: Bool is abstract
737 # Set a new `item` at `key`.
738 #fun item=(item: E) is abstract
740 # Pre-iteration hook.
742 # Used to inform `self` that the iteration is starting.
743 # Specific iterators can use this to prepare some resources.
745 # Is automatically invoked at the beginning of `for` structures.
747 # Do nothing by default.
750 # Post-iteration hook.
752 # Used to inform `self` that the iteration is over.
753 # Specific iterators can use this to free some resources.
755 # Is automatically invoked at the end of `for` structures.
757 # Do nothing by default.
761 # Iterator on a 'keys' point of view of a map
762 class MapKeysIterator[K
, V
]
764 # The original iterator
765 var original_iterator
: MapIterator[K
, V
]
767 redef fun is_ok
do return self.original_iterator
.is_ok
768 redef fun next
do self.original_iterator
.next
769 redef fun item
do return self.original_iterator
.key
772 # Iterator on a 'values' point of view of a map
773 class MapValuesIterator[K
, V
]
775 # The original iterator
776 var original_iterator
: MapIterator[K
, V
]
778 redef fun is_ok
do return self.original_iterator
.is_ok
779 redef fun next
do self.original_iterator
.next
780 redef fun item
do return self.original_iterator
.item
783 # Sequences are indexed collections.
784 # The first item is 0. The last is `length-1`.
786 # The order is the main caracteristic of sequence
787 # and all concrete implementation of sequences are basically interchangeable.
788 interface SequenceRead[E
]
791 # Get the first item.
792 # Is equivalent with `self[0]`.
795 # assert a.first == 1
797 # REQUIRE `not is_empty`
800 assert not_empty
: not is_empty
804 # Return the index-th element of the sequence.
805 # The first element is 0 and the last is `length-1`
806 # If index is invalid, the program aborts
813 # REQUIRE `index >= 0 and index < length`
814 fun [](index
: Int): E
is abstract
816 # Return the index-th element but wrap
818 # Whereas `self[]` requires the index to exists, the `modulo` accessor automatically
819 # wraps overbound and underbouds indexes.
823 # assert a.modulo(1) == 20
824 # assert a.modulo(3) == 10
825 # assert a.modulo(-1) == 30
826 # assert a.modulo(-10) == 30
829 # REQUIRE `not_empty`
830 # ENSURE `result == self[modulo_index(index)]`
831 fun modulo
(index
: Int): E
do return self[modulo_index
(index
)]
833 # Returns the real index for a modulo index.
837 # assert a.modulo_index(1) == 1
838 # assert a.modulo_index(3) == 0
839 # assert a.modulo_index(-1) == 2
840 # assert a.modulo_index(-10) == 2
843 # REQUIRE `not_empty`
844 fun modulo_index
(index
: Int): Int
846 var length
= self.length
848 return index
% length
850 return length
- (-1 - index
) % length
- 1
855 # Is equivalent with `self[length-1]`.
860 # REQUIRE `not is_empty`
863 assert not_empty
: not is_empty
864 return self[length-1
]
867 # The index of the first occurrence of `item`.
868 # Return -1 if `item` is not found.
869 # Comparison is done with `==`.
871 # var a = [10,20,30,10,20,30]
872 # assert a.index_of(20) == 1
873 # assert a.index_of(40) == -1
874 fun index_of
(item
: nullable Object): Int do return index_of_from
(item
, 0)
876 # The index of the last occurrence of `item`.
877 # Return -1 if `item` is not found.
878 # Comparison is done with `==`.
880 # var a = [10,20,30,10,20,30]
881 # assert a.last_index_of(20) == 4
882 # assert a.last_index_of(40) == -1
883 fun last_index_of
(item
: nullable Object): Int do return last_index_of_from
(item
, length-1
)
885 # The index of the first occurrence of `item`, starting from pos.
886 # Return -1 if `item` is not found.
887 # Comparison is done with `==`.
889 # var a = [10,20,30,10,20,30]
890 # assert a.index_of_from(20, 3) == 4
891 # assert a.index_of_from(20, 4) == 4
892 # assert a.index_of_from(20, 5) == -1
893 fun index_of_from
(item
: nullable Object, pos
: Int): Int
898 if p
>=pos
and i
.item
== item
then return i
.index
905 # The index of the last occurrence of `item` starting from `pos` and decrementing.
906 # Return -1 if `item` is not found.
907 # Comparison is done with `==`.
909 # var a = [10,20,30,10,20,30]
910 # assert a.last_index_of_from(20, 2) == 1
911 # assert a.last_index_of_from(20, 1) == 1
912 # assert a.last_index_of_from(20, 0) == -1
913 fun last_index_of_from
(item
: nullable Object, pos
: Int): Int do
916 if self[i
] == item
then return i
922 # Two sequences are equals if they have the same items in the same order.
924 # var a = new List[Int]
928 # assert a == [1,2,3]
929 # assert a != [1,3,2]
932 if not o
isa SequenceRead[nullable Object] then return false
934 if o
.length
!= l
then return false
937 if self[i
] != o
[i
] then return false
943 # Because of the law between `==` and `hash`, `hash` is redefined to be the sum of the hash of the elements
946 # The 17 and 2/3 magic numbers were determined empirically.
947 # Note: the standard hash functions djb2, sbdm and fnv1 were also
948 # tested but were comparable (or worse).
949 var res
= 17 + length
952 if e
!= null then res
+= e
.hash
957 redef fun iterator
: IndexedIterator[E
] is abstract
959 # Gets a new Iterator starting at position `pos`
961 # var iter = [10,20,30,40,50].iterator_from(2)
962 # assert iter.to_a == [30, 40, 50]
963 fun iterator_from
(pos
: Int): IndexedIterator[E
]
966 while pos
> 0 and res
.is_ok
do
973 # Gets an iterator starting at the end and going backwards
975 # var reviter = [1,2,3].reverse_iterator
976 # assert reviter.to_a == [3,2,1]
977 fun reverse_iterator
: IndexedIterator[E
] is abstract
979 # Gets an iterator on the chars of self starting from `pos`
981 # var reviter = [10,20,30,40,50].reverse_iterator_from(2)
982 # assert reviter.to_a == [30,20,10]
983 fun reverse_iterator_from
(pos
: Int): IndexedIterator[E
]
985 var res
= reverse_iterator
986 while pos
> 0 and res
.is_ok
do
994 # Sequence are indexed collection.
995 # The first item is 0. The last is `length-1`.
996 interface Sequence[E
]
997 super SequenceRead[E
]
998 super SimpleCollection[E
]
1000 # Set the first item.
1001 # Is equivalent with `self[0] = item`.
1005 # assert a == [10,2,3]
1007 do self[0] = item
end
1009 # Set the last item.
1010 # Is equivalent with `self[length-1] = item`.
1014 # assert a == [1,2,10]
1016 # If the sequence is empty, `last=` is equivalent with `self[0]=` (thus with `first=`)
1018 # var b = new Array[Int]
1031 # A synonym of `push`
1032 redef fun add
(e
) do push
(e
)
1034 # Add an item after the last one.
1039 # assert a == [1,2,3,10,20]
1040 fun push
(e
: E
) is abstract
1042 # Add each item of `coll` after the last.
1046 # assert a == [1,2,3,7,8,9]
1048 # Alias of `add_all`
1049 fun append
(coll
: Collection[E
]) do add_all
(coll
)
1051 # Remove the last item.
1058 # REQUIRE `not is_empty`
1059 fun pop
: E
is abstract
1061 # Add an item before the first one.
1066 # assert a == [20,10,1,2,3]
1067 fun unshift
(e
: E
) is abstract
1069 # Add all items of `coll` before the first one.
1073 # assert a == [7,8,9,1,2,3]
1075 # Alias of `insert_at(coll, 0)`
1076 fun prepend
(coll
: Collection[E
]) do insert_all
(coll
, 0)
1078 # Remove the first item.
1079 # The second item thus become the first.
1082 # assert a.shift == 1
1083 # assert a.shift == 2
1086 # REQUIRE `not is_empty`
1087 fun shift
: E
is abstract
1089 # Set the `item` at `index`.
1091 # var a = [10,20,30]
1093 # assert a == [10,200,30]
1095 # like with `[]`, index should be between `0` and `length-1`
1096 # However, if `index==length`, `[]=` works like `push`.
1099 # assert a == [10,200,30,400]
1101 # REQUIRE `index >= 0 and index <= length`
1102 fun []=(index
: Int, item
: E
) is abstract
1104 # Set the index-th element but wrap
1106 # Whereas `self[]=` requires the index to exists, the `modulo` accessor automatically
1107 # wraps overbound and underbouds indexes.
1110 # var a = [10,20,30]
1113 # a.modulo(-1) = 300
1114 # a.modulo(-10) = 301
1115 # assert a == [100, 200, 301]
1118 # REQUIRE `not_empty`
1119 # ENSURE `self[modulo_index(index)] == value`
1120 fun modulo
=(index
: Int, value
: E
) do self[modulo_index
(index
)] = value
1122 # Insert an element at a given position, following elements are shifted.
1124 # var a = [10, 20, 30, 40]
1126 # assert a == [10, 20, 100, 30, 40]
1128 # REQUIRE `index >= 0 and index <= length`
1129 # ENSURE `self[index] == item`
1130 fun insert
(item
: E
, index
: Int) is abstract
1132 # Insert all elements at a given position, following elements are shifted.
1134 # var a = [10, 20, 30, 40]
1135 # a.insert_all([100..102], 2)
1136 # assert a == [10, 20, 100, 101, 102, 30, 40]
1138 # REQUIRE `index >= 0 and index <= length`
1139 # ENSURE `self[index] == coll.first`
1140 fun insert_all
(coll
: Collection[E
], index
: Int)
1142 assert index
>= 0 and index
< length
1143 if index
== length
then
1152 # Remove the item at `index` and shift all following elements
1154 # var a = [10,20,30]
1156 # assert a == [10,30]
1158 # REQUIRE `index >= 0 and index < length`
1159 fun remove_at
(index
: Int) is abstract
1162 # Iterators on indexed collections.
1163 interface IndexedIterator[E
]
1165 # The index of the current item.
1166 fun index
: Int is abstract
1169 # Associative arrays that internally uses couples to represent each (key, value) pairs.
1170 # This is an helper class that some specific implementation of Map may implements.
1171 interface CoupleMap[K
, V
]
1174 # Return the couple of the corresponding key
1175 # Return null if the key is no associated element
1176 protected fun couple_at
(key
: nullable Object): nullable Couple[K
, V
] is abstract
1178 # Return a new iteralot on all couples
1179 # Used to provide `iterator` and others
1180 protected fun couple_iterator
: Iterator[Couple[K
,V
]] is abstract
1182 redef fun iterator
do return new CoupleMapIterator[K
,V
](couple_iterator
)
1186 var c
= couple_at
(key
)
1188 return provide_default_value
(key
)
1194 redef fun has_key
(key
) do return couple_at
(key
) != null
1197 # Iterator on CoupleMap
1199 # Actually it is a wrapper around an iterator of the internal array of the map.
1200 private class CoupleMapIterator[K
, V
]
1201 super MapIterator[K
, V
]
1202 redef fun item
do return _iter
.item
.second
1204 #redef fun item=(e) do _iter.item.second = e
1206 redef fun key
do return _iter
.item
.first
1208 redef fun is_ok
do return _iter
.is_ok
1215 var iter
: Iterator[Couple[K
,V
]]
1218 # Some tools ###################################################################
1220 # Two objects in a simple structure.
1223 # The first element of the couple.
1224 var first
: F
is writable
1226 # The second element of the couple.
1227 var second
: S
is writable