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