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