lib/collection: improve documentation of `has_all`
[nit.git] / lib / standard / collection / abstract_collection.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 #
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
11 # another product.
12
13 # Abstract collection classes and services.
14 #
15 # TODO specify the behavior on iterators when collections are modified.
16 module abstract_collection
17
18 import kernel
19
20 # The root of the collection hierarchy.
21 #
22 # Collections modelize finite groups of objects, called elements.
23 #
24 # The specific behavior and representation of collections is determined
25 # by the subclasses of the hierarchy.
26 #
27 # The main service of Collection is to provide a stable `iterator`
28 # method usable to retrieve all the elements of the collection.
29 #
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.
34 #
35 # Because of the `iterator` method, Collections instances can use
36 # the `for` control structure:
37 #
38 # var x: Collection[U]
39 # # ...
40 # for u in x do
41 # # u is a U
42 # # ...
43 # end
44 #
45 # that is equivalent with
46 #
47 # var x: Collection[U]
48 # # ...
49 # var i = x.iterator
50 # while i.is_ok do
51 # var u = i.item # u is a U
52 # # ...
53 # i.next
54 # end
55 interface Collection[E]
56 # Get a new iterator on the collection.
57 fun iterator: Iterator[E] is abstract
58
59 # Is there no item in the collection?
60 #
61 # assert [1,2,3].is_empty == false
62 # assert [1..1[.is_empty == true
63 fun is_empty: Bool do return length == 0
64
65 # Number of items in the collection.
66 #
67 # assert [10,20,30].length == 3
68 # assert [20..30[.length == 10
69 fun length: Int
70 do
71 var nb = 0
72 for i in self do nb += 1
73 return nb
74 end
75
76 # Is `item` in the collection ?
77 # Comparisons are done with ==
78 #
79 # assert [1,2,3].has(2) == true
80 # assert [1,2,3].has(9) == false
81 # assert [1..5[.has(2) == true
82 # assert [1..5[.has(9) == false
83 fun has(item: E): Bool
84 do
85 for i in self do if i == item then return true
86 return false
87 end
88
89 # Is the collection contain only `item`?
90 # Comparisons are done with ==
91 # Return true if the collection is empty.
92 #
93 # assert [1,1,1].has_only(1) == true
94 # assert [1,2,3].has_only(1) == false
95 # assert [1..1].has_only(1) == true
96 # assert [1..3].has_only(1) == false
97 # assert [3..3[.has_only(1) == true # empty collection
98 #
99 # ENSURE `is_empty implies result == true`
100 fun has_only(item: E): Bool
101 do
102 for i in self do if i != item then return false
103 return true
104 end
105
106 # How many occurrences of `item` are in the collection?
107 # Comparisons are done with ==
108 #
109 # assert [10,20,10].count(10) == 2
110 fun count(item: E): Int
111 do
112 var nb = 0
113 for i in self do if i == item then nb += 1
114 return nb
115 end
116
117 # Return the first item of the collection
118 #
119 # assert [1,2,3].first == 1
120 fun first: E
121 do
122 assert length > 0
123 return iterator.item
124 end
125
126 # Does the collection contain at least each element of `other`?
127 #
128 # assert [1,3,4,2].has_all([1..2]) == true
129 # assert [1,3,4,2].has_all([1..5]) == false
130 #
131 # Repeated elements in the collections are not considered.
132 #
133 # assert [1,1,1].has_all([1]) == true
134 # assert [1..5].has_all([1,1,1]) == true
135 #
136 # Note that the default implementation is general and correct for any lawful Collections.
137 # It is memory-efficient but relies on `has` so may be CPU-inefficient for some kind of collections.
138 fun has_all(other: Collection[E]): Bool
139 do
140 for x in other do if not has(x) then return false
141 return true
142 end
143 end
144
145 # Instances of the Iterator class generates a series of elements, one at a time.
146 # They are mainly used with collections.
147 interface Iterator[E]
148 # The current item.
149 # Require `is_ok`.
150 fun item: E is abstract
151
152 # Jump to the next item.
153 # Require `is_ok`.
154 fun next is abstract
155
156 # Is there a current item ?
157 fun is_ok: Bool is abstract
158
159 # Iterate over `self`
160 fun iterator: Iterator[E] do return self
161
162 # Post-iteration hook.
163 #
164 # Used to inform `self` that the iteration is over.
165 # Specific iterators can use this to free some resources.
166 #
167 # Is automatically invoked at the end of `for` structures.
168 #
169 # Do nothing by default.
170 fun finish do end
171 end
172
173 # A collection that contains only one item.
174 #
175 # Used to pass arguments by reference.
176 #
177 # Also used when one want to give asingle element when a full
178 # collection is expected
179 class Container[E]
180 super Collection[E]
181
182 redef fun first do return item
183
184 redef fun is_empty do return false
185
186 redef fun length do return 1
187
188 redef fun has(an_item) do return item == an_item
189
190 redef fun has_only(an_item) do return item == an_item
191
192 redef fun count(an_item)
193 do
194 if item == an_item then
195 return 1
196 else
197 return 0
198 end
199 end
200
201 redef fun iterator do return new ContainerIterator[E](self)
202
203 # The stored item
204 var item: E is writable
205 end
206
207 # This iterator is quite stupid since it is used for only one item.
208 private class ContainerIterator[E]
209 super Iterator[E]
210 redef fun item do return _container.item
211
212 redef fun next do is_ok = false
213
214 redef var is_ok: Bool = true
215
216 var container: Container[E]
217 end
218
219 # Items can be removed from this collection
220 interface RemovableCollection[E]
221 super Collection[E]
222
223 # Remove all items
224 #
225 # var a = [1,2,3]
226 # a.clear
227 # assert a.length == 0
228 #
229 # ENSURE `is_empty`
230 fun clear is abstract
231
232 # Remove an occucence of `item`
233 #
234 # var a = [1,2,3,1,2,3]
235 # a.remove 2
236 # assert a == [1,3,1,2,3]
237 fun remove(item: E) is abstract
238
239 # Remove all occurences of `item`
240 #
241 # var a = [1,2,3,1,2,3]
242 # a.remove_all 2
243 # assert a == [1,3,1,3]
244 fun remove_all(item: E) do while has(item) do remove(item)
245 end
246
247 # Items can be added to these collections.
248 interface SimpleCollection[E]
249 super RemovableCollection[E]
250
251 # Add an item in a collection.
252 #
253 # var a = [1,2]
254 # a.add 3
255 # assert a.has(3) == true
256 # assert a.has(10) == false
257 #
258 # Ensure col.has(item)
259 fun add(item: E) is abstract
260
261 # Add each item of `coll`.
262 # var a = [1,2]
263 # a.add_all([3..5])
264 # assert a.has(4) == true
265 # assert a.has(10) == false
266 fun add_all(coll: Collection[E]) do for i in coll do add(i)
267 end
268
269 # Abstract sets.
270 #
271 # Set is a collection without duplicates (according to `==`)
272 #
273 # var s: Set[String] = new ArraySet[String]
274 # var a = "Hello"
275 # var b = "Hel" + "lo"
276 # # ...
277 # s.add(a)
278 # assert s.has(b) == true
279 interface Set[E: Object]
280 super SimpleCollection[E]
281
282 redef fun has_only(item)
283 do
284 var l = length
285 if l == 1 then
286 return has(item)
287 else if l == 0 then
288 return true
289 else
290 return false
291 end
292 end
293
294 # Only 0 or 1
295 redef fun count(item)
296 do
297 if has(item) then
298 return 1
299 else
300 return 0
301 end
302 end
303
304 # Synonym of remove since there is only one item
305 redef fun remove_all(item) do remove(item)
306
307 # Equality is defined on set and means that each set contains the same elements
308 redef fun ==(other)
309 do
310 if not other isa Set[Object] then return false
311 if other.length != length then return false
312 return has_all(other)
313 end
314
315 # Because of the law between `==` and `hash`, `hash` is redefined to be the sum of the hash of the elements
316 redef fun hash
317 do
318 # 23 is a magic number empirically determined to be not so bad.
319 var res = 23 + length
320 # Note: the order of the elements must not change the hash value.
321 # So, unlike usual hash functions, the accumulator is not combined with itself.
322 for e in self do res += e.hash
323 return res
324 end
325
326 # Returns the union of this set with the `other` set
327 fun union(other: Set[E]): Set[E]
328 do
329 var nhs = new_set
330 nhs.add_all self
331 nhs.add_all other
332 return nhs
333 end
334
335 # Returns the intersection of this set with the `other` set
336 fun intersection(other: Set[E]): Set[E]
337 do
338 var nhs = new_set
339 for v in self do if other.has(v) then nhs.add(v)
340 return nhs
341 end
342
343 # Returns a new instance of `Set`.
344 #
345 # Depends on the subclass, mainly used for copy services
346 # like `union` or `intersection`.
347 protected fun new_set: Set[E] is abstract
348 end
349
350 # MapRead are abstract associative collections: `key` -> `item`.
351 interface MapRead[K: Object, V]
352 # Get the item at `key`
353 #
354 # var x = new HashMap[String, Int]
355 # x["four"] = 4
356 # assert x["four"] == 4
357 # # assert x["five"] #=> abort
358 #
359 # If the key is not in the map, `provide_default_value` is called (that aborts by default)
360 # See `get_or_null` and `get_or_default` for safe variations.
361 fun [](key: K): V is abstract
362
363 # Get the item at `key` or null if `key` is not in the map.
364 #
365 # var x = new HashMap[String, Int]
366 # x["four"] = 4
367 # assert x.get_or_null("four") == 4
368 # assert x.get_or_null("five") == null
369 #
370 # Note: use `has_key` and `[]` if you need the distinction between a key associated with null, and no key.
371 fun get_or_null(key: K): nullable V
372 do
373 if has_key(key) then return self[key]
374 return null
375 end
376
377 # Get the item at `key` or return `default` if not in map
378 #
379 # var x = new HashMap[String, Int]
380 # x["four"] = 4
381 # assert x.get_or_default("four", 40) == 4
382 # assert x.get_or_default("five", 50) == 50
383 #
384 fun get_or_default(key: K, default: V): V
385 do
386 if has_key(key) then return self[key]
387 return default
388 end
389
390 # Alias for `keys.has`
391 fun has_key(key: K): Bool do return self.keys.has(key)
392
393 # Get a new iterator on the map.
394 fun iterator: MapIterator[K, V] is abstract
395
396 # Return the point of view of self on the values only.
397 # Note that `self` and `values` are views on the same data;
398 # therefore any modification of one is visible on the other.
399 #
400 # var x = new HashMap[String, Int]
401 # x["four"] = 4
402 # assert x.values.has(4) == true
403 # assert x.values.has(5) == false
404 fun values: Collection[V] is abstract
405
406 # Return the point of view of self on the keys only.
407 # Note that `self` and `keys` are views on the same data;
408 # therefore any modification of one is visible on the other.
409 #
410 # var x = new HashMap[String, Int]
411 # x["four"] = 4
412 # assert x.keys.has("four") == true
413 # assert x.keys.has("five") == false
414 fun keys: Collection[K] is abstract
415
416 # Is there no item in the collection?
417 #
418 # var x = new HashMap[String, Int]
419 # assert x.is_empty == true
420 # x["four"] = 4
421 # assert x.is_empty == false
422 fun is_empty: Bool is abstract
423
424 # Number of items in the collection.
425 #
426 # var x = new HashMap[String, Int]
427 # assert x.length == 0
428 # x["four"] = 4
429 # assert x.length == 1
430 # x["five"] = 5
431 # assert x.length == 2
432 fun length: Int is abstract
433
434 # Called by the underling implementation of `[]` to provide a default value when a `key` has no value
435 # By default the behavior is to abort.
436 #
437 # Note: the value is returned *as is*, implementations may want to store the value in the map before returning it
438 # @toimplement
439 protected fun provide_default_value(key: K): V do abort
440 end
441
442 # Maps are associative collections: `key` -> `item`.
443 #
444 # The main operator over maps is [].
445 #
446 # var map: Map[String, Int] = new ArrayMap[String, Int]
447 # # ...
448 # map["one"] = 1 # Associate 'one' to '1'
449 # map["two"] = 2 # Associate 'two' to '2'
450 # assert map["one"] == 1
451 # assert map["two"] == 2
452 #
453 # Instances of maps can be used with the for structure
454 #
455 # for key, value in map do
456 # assert (key == "one" and value == 1) or (key == "two" and value == 2)
457 # end
458 #
459 # The keys and values in the map can also be manipulated directly with the `keys` and `values` methods.
460 #
461 # assert map.keys.has("one") == true
462 # assert map.keys.has("tree") == false
463 # assert map.values.has(1) == true
464 # assert map.values.has(3) == false
465 #
466 interface Map[K: Object, V]
467 super MapRead[K, V]
468
469 # Set the `value` at `key`.
470 #
471 # Values can then get retrieved with `[]`.
472 #
473 # var x = new HashMap[String, Int]
474 # x["four"] = 4
475 # assert x["four"] == 4
476 #
477 # If the key was associated with a value, this old value is discarded
478 # and replaced with the new one.
479 #
480 # x["four"] = 40
481 # assert x["four"] == 40
482 # assert x.values.has(4) == false
483 #
484 fun []=(key: K, value: V) is abstract
485
486 # Add each (key,value) of `map` into `self`.
487 # If a same key exists in `map` and `self`, then the value in self is discarded.
488 #
489 # It is the analogous of `SimpleCollection::add_all`
490 #
491 # var x = new HashMap[String, Int]
492 # x["four"] = 4
493 # x["five"] = 5
494 # var y = new HashMap[String, Int]
495 # y["four"] = 40
496 # y["nine"] = 90
497 # x.recover_with y
498 # assert x["four"] == 40
499 # assert x["five"] == 5
500 # assert x["nine"] == 90
501 fun recover_with(map: MapRead[K, V])
502 do
503 var i = map.iterator
504 while i.is_ok do
505 self[i.key] = i.item
506 i.next
507 end
508 end
509
510 # Remove all items
511 #
512 # var x = new HashMap[String, Int]
513 # x["four"] = 4
514 # x.clear
515 # assert x.keys.has("four") == false
516 #
517 # ENSURE `is_empty`
518 fun clear is abstract
519
520 redef fun values: RemovableCollection[V] is abstract
521
522 redef fun keys: RemovableCollection[K] is abstract
523 end
524
525 # Iterators for Map.
526 interface MapIterator[K: Object, V]
527 # The current item.
528 # Require `is_ok`.
529 fun item: V is abstract
530
531 # The key of the current item.
532 # Require `is_ok`.
533 fun key: K is abstract
534
535 # Jump to the next item.
536 # Require `is_ok`.
537 fun next is abstract
538
539 # Is there a current item ?
540 fun is_ok: Bool is abstract
541
542 # Set a new `item` at `key`.
543 #fun item=(item: E) is abstract
544
545 # Post-iteration hook.
546 #
547 # Used to inform `self` that the iteration is over.
548 # Specific iterators can use this to free some resources.
549 #
550 # Is automatically invoked at the end of `for` structures.
551 #
552 # Do nothing by default.
553 fun finish do end
554 end
555
556 # Iterator on a 'keys' point of view of a map
557 class MapKeysIterator[K: Object, V]
558 super Iterator[K]
559 # The original iterator
560 var original_iterator: MapIterator[K, V]
561
562 redef fun is_ok do return self.original_iterator.is_ok
563 redef fun next do self.original_iterator.next
564 redef fun item do return self.original_iterator.key
565 end
566
567 # Iterator on a 'values' point of view of a map
568 class MapValuesIterator[K: Object, V]
569 super Iterator[V]
570 # The original iterator
571 var original_iterator: MapIterator[K, V]
572
573 redef fun is_ok do return self.original_iterator.is_ok
574 redef fun next do self.original_iterator.next
575 redef fun item do return self.original_iterator.item
576 end
577
578 # Sequences are indexed collections.
579 # The first item is 0. The last is `length-1`.
580 #
581 # The order is the main caracteristic of sequence
582 # and all concrete implementation of sequences are basically interchangeable.
583 interface SequenceRead[E]
584 super Collection[E]
585
586 # Get the first item.
587 # Is equivalent with `self[0]`.
588 #
589 # var a = [1,2,3]
590 # assert a.first == 1
591 #
592 # REQUIRE `not is_empty`
593 redef fun first
594 do
595 assert not_empty: not is_empty
596 return self[0]
597 end
598
599 # Return the index-th element of the sequence.
600 # The first element is 0 and the last is `length-1`
601 # If index is invalid, the program aborts
602 #
603 # var a = [10,20,30]
604 # assert a[0] == 10
605 # assert a[1] == 20
606 # assert a[2] == 30
607 #
608 # REQUIRE `index >= 0 and index < length`
609 fun [](index: Int): E is abstract
610
611 # Get the last item.
612 # Is equivalent with `self[length-1]`.
613 #
614 # var a = [1,2,3]
615 # assert a.last == 3
616 #
617 # REQUIRE `not is_empty`
618 fun last: E
619 do
620 assert not_empty: not is_empty
621 return self[length-1]
622 end
623
624 # The index of the first occurrence of `item`.
625 # Return -1 if `item` is not found.
626 # Comparison is done with `==`.
627 #
628 # var a = [10,20,30,10,20,30]
629 # assert a.index_of(20) == 1
630 # assert a.index_of(40) == -1
631 fun index_of(item: E): Int do return index_of_from(item, 0)
632
633 # The index of the last occurrence of `item`.
634 # Return -1 if `item` is not found.
635 # Comparison is done with `==`.
636 #
637 # var a = [10,20,30,10,20,30]
638 # assert a.last_index_of(20) == 4
639 # assert a.last_index_of(40) == -1
640 fun last_index_of(item: E): Int do return last_index_of_from(item, length-1)
641
642 # The index of the first occurrence of `item`, starting from pos.
643 # Return -1 if `item` is not found.
644 # Comparison is done with `==`.
645 #
646 # var a = [10,20,30,10,20,30]
647 # assert a.index_of_from(20, 3) == 4
648 # assert a.index_of_from(20, 4) == 4
649 # assert a.index_of_from(20, 5) == -1
650 fun index_of_from(item: E, pos: Int): Int
651 do
652 var p = 0
653 var i = iterator
654 while i.is_ok do
655 if p>=pos and i.item == item then return i.index
656 i.next
657 p += 1
658 end
659 return -1
660 end
661
662 # The index of the last occurrence of `item` starting from `pos` and decrementing.
663 # Return -1 if `item` is not found.
664 # Comparison is done with `==`.
665 #
666 # var a = [10,20,30,10,20,30]
667 # assert a.last_index_of_from(20, 2) == 1
668 # assert a.last_index_of_from(20, 1) == 1
669 # assert a.last_index_of_from(20, 0) == -1
670 fun last_index_of_from(item: E, pos: Int): Int
671 do
672 var res = -1
673 var p = 0
674 var i = iterator
675 while i.is_ok do
676 if p>pos then break
677 if i.item == item then res = p
678 i.next
679 p += 1
680 end
681 return res
682 end
683
684 # Two sequences are equals if they have the same items in the same order.
685 #
686 # var a = new List[Int]
687 # a.add(1)
688 # a.add(2)
689 # a.add(3)
690 # assert a == [1,2,3]
691 # assert a != [1,3,2]
692 redef fun ==(o)
693 do
694 if not o isa SequenceRead[nullable Object] then return false
695 var l = length
696 if o.length != l then return false
697 var i = 0
698 while i < l do
699 if self[i] != o[i] then return false
700 i += 1
701 end
702 return true
703 end
704
705 # Because of the law between `==` and `hash`, `hash` is redefined to be the sum of the hash of the elements
706 redef fun hash
707 do
708 # The 17 and 2/3 magic numbers were determined empirically.
709 # Note: the standard hash functions djb2, sbdm and fnv1 were also
710 # tested but were comparable (or worse).
711 var res = 17 + length
712 for e in self do
713 res = res * 3 / 2
714 if e != null then res += e.hash
715 end
716 return res
717 end
718
719 redef fun iterator: IndexedIterator[E] is abstract
720
721 # Gets a new Iterator starting at position `pos`
722 #
723 # var iter = [10,20,30,40,50].iterator_from(2)
724 # assert iter.to_a == [30, 40, 50]
725 fun iterator_from(pos: Int): IndexedIterator[E]
726 do
727 var res = iterator
728 while pos > 0 and res.is_ok do
729 res.next
730 pos -= 1
731 end
732 return res
733 end
734
735 # Gets an iterator starting at the end and going backwards
736 #
737 # var reviter = [1,2,3].reverse_iterator
738 # assert reviter.to_a == [3,2,1]
739 fun reverse_iterator: IndexedIterator[E] is abstract
740
741 # Gets an iterator on the chars of self starting from `pos`
742 #
743 # var reviter = [10,20,30,40,50].reverse_iterator_from(2)
744 # assert reviter.to_a == [30,20,10]
745 fun reverse_iterator_from(pos: Int): IndexedIterator[E]
746 do
747 var res = reverse_iterator
748 while pos > 0 and res.is_ok do
749 res.next
750 pos -= 1
751 end
752 return res
753 end
754 end
755
756 # Sequence are indexed collection.
757 # The first item is 0. The last is `length-1`.
758 interface Sequence[E]
759 super SequenceRead[E]
760 super SimpleCollection[E]
761
762 # Set the first item.
763 # Is equivalent with `self[0] = item`.
764 #
765 # var a = [1,2,3]
766 # a.first = 10
767 # assert a == [10,2,3]
768 fun first=(item: E)
769 do self[0] = item end
770
771 # Set the last item.
772 # Is equivalent with `self[length-1] = item`.
773 #
774 # var a = [1,2,3]
775 # a.last = 10
776 # assert a == [1,2,10]
777 #
778 # If the sequence is empty, `last=` is equivalent with `self[0]=` (thus with `first=`)
779 #
780 # var b = new Array[Int]
781 # b.last = 10
782 # assert b == [10]
783 fun last=(item: E)
784 do
785 var l = length
786 if l > 0 then
787 self[l-1] = item
788 else
789 self[0] = item
790 end
791 end
792
793 # A synonym of `push`
794 redef fun add(e) do push(e)
795
796 # Add an item after the last one.
797 #
798 # var a = [1,2,3]
799 # a.push(10)
800 # a.push(20)
801 # assert a == [1,2,3,10,20]
802 fun push(e: E) is abstract
803
804 # Add each item of `coll` after the last.
805 #
806 # var a = [1,2,3]
807 # a.append([7..9])
808 # assert a == [1,2,3,7,8,9]
809 #
810 # Alias of `add_all`
811 fun append(coll: Collection[E]) do add_all(coll)
812
813 # Remove the last item.
814 #
815 # var a = [1,2,3]
816 # assert a.pop == 3
817 # assert a.pop == 2
818 # assert a == [1]
819 #
820 # REQUIRE `not is_empty`
821 fun pop: E is abstract
822
823 # Add an item before the first one.
824 #
825 # var a = [1,2,3]
826 # a.unshift(10)
827 # a.unshift(20)
828 # assert a == [20,10,1,2,3]
829 fun unshift(e: E) is abstract
830
831 # Add all items of `coll` before the first one.
832 #
833 # var a = [1,2,3]
834 # a.prepend([7..9])
835 # assert a == [7,8,9,1,2,3]
836 #
837 # Alias of `insert_at(coll, 0)`
838 fun prepend(coll: Collection[E]) do insert_all(coll, 0)
839
840 # Remove the first item.
841 # The second item thus become the first.
842 #
843 # var a = [1,2,3]
844 # assert a.shift == 1
845 # assert a.shift == 2
846 # assert a == [3]
847 #
848 # REQUIRE `not is_empty`
849 fun shift: E is abstract
850
851 # Set the `item` at `index`.
852 #
853 # var a = [10,20,30]
854 # a[1] = 200
855 # assert a == [10,200,30]
856 #
857 # like with `[]`, index should be between `0` and `length-1`
858 # However, if `index==length`, `[]=` works like `push`.
859 #
860 # a[3] = 400
861 # assert a == [10,200,30,400]
862 #
863 # REQUIRE `index >= 0 and index <= length`
864 fun []=(index: Int, item: E) is abstract
865
866 # Insert an element at a given position, following elements are shifted.
867 #
868 # var a = [10, 20, 30, 40]
869 # a.insert(100, 2)
870 # assert a == [10, 20, 100, 30, 40]
871 #
872 # REQUIRE `index >= 0 and index <= length`
873 # ENSURE `self[index] == item`
874 fun insert(item: E, index: Int) is abstract
875
876 # Insert all elements at a given position, following elements are shifted.
877 #
878 # var a = [10, 20, 30, 40]
879 # a.insert_all([100..102], 2)
880 # assert a == [10, 20, 100, 101, 102, 30, 40]
881 #
882 # REQUIRE `index >= 0 and index <= length`
883 # ENSURE `self[index] == coll.first`
884 fun insert_all(coll: Collection[E], index: Int)
885 do
886 assert index >= 0 and index < length
887 if index == length then
888 add_all(coll)
889 end
890 for c in coll do
891 insert(c, index)
892 index += 1
893 end
894 end
895
896 # Remove the item at `index` and shift all following elements
897 #
898 # var a = [10,20,30]
899 # a.remove_at(1)
900 # assert a == [10,30]
901 #
902 # REQUIRE `index >= 0 and index < length`
903 fun remove_at(index: Int) is abstract
904 end
905
906 # Iterators on indexed collections.
907 interface IndexedIterator[E]
908 super Iterator[E]
909 # The index of the current item.
910 fun index: Int is abstract
911 end
912
913 # Associative arrays that internally uses couples to represent each (key, value) pairs.
914 # This is an helper class that some specific implementation of Map may implements.
915 interface CoupleMap[K: Object, V]
916 super Map[K, V]
917
918 # Return the couple of the corresponding key
919 # Return null if the key is no associated element
920 protected fun couple_at(key: K): nullable Couple[K, V] is abstract
921
922 # Return a new iteralot on all couples
923 # Used to provide `iterator` and others
924 protected fun couple_iterator: Iterator[Couple[K,V]] is abstract
925
926 redef fun iterator do return new CoupleMapIterator[K,V](couple_iterator)
927
928 redef fun [](key)
929 do
930 var c = couple_at(key)
931 if c == null then
932 return provide_default_value(key)
933 else
934 return c.second
935 end
936 end
937 end
938
939 # Iterator on CoupleMap
940 #
941 # Actually it is a wrapper around an iterator of the internal array of the map.
942 private class CoupleMapIterator[K: Object, V]
943 super MapIterator[K, V]
944 redef fun item do return _iter.item.second
945
946 #redef fun item=(e) do _iter.item.second = e
947
948 redef fun key do return _iter.item.first
949
950 redef fun is_ok do return _iter.is_ok
951
952 redef fun next
953 do
954 _iter.next
955 end
956
957 var iter: Iterator[Couple[K,V]]
958 end
959
960 # Some tools ###################################################################
961
962 # Two objects in a simple structure.
963 class Couple[F, S]
964
965 # The first element of the couple.
966 var first: F is writable
967
968 # The second element of the couple.
969 var second: S is writable
970 end