lib: update some modules of standard to new constructors
[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 private 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 protected fun new_set: Set[E] is abstract
338 end
339
340 # MapRead are abstract associative collections: `key` -> `item`.
341 interface MapRead[K: Object, V]
342 # Get the item at `key`
343 #
344 # var x = new HashMap[String, Int]
345 # x["four"] = 4
346 # assert x["four"] == 4
347 # # assert x["five"] #=> abort
348 #
349 # If the key is not in the map, `provide_default_value` is called (that aborts by default)
350 # See `get_or_null` and `get_or_default` for safe variations.
351 fun [](key: K): V is abstract
352
353 # Get the item at `key` or null if `key` is not in the map.
354 #
355 # var x = new HashMap[String, Int]
356 # x["four"] = 4
357 # assert x.get_or_null("four") == 4
358 # assert x.get_or_null("five") == null
359 #
360 # Note: use `has_key` and `[]` if you need the distinction between a key associated with null, and no key.
361 fun get_or_null(key: K): nullable V
362 do
363 if has_key(key) then return self[key]
364 return null
365 end
366
367 # Get the item at `key` or return `default` if not in map
368 #
369 # var x = new HashMap[String, Int]
370 # x["four"] = 4
371 # assert x.get_or_default("four", 40) == 4
372 # assert x.get_or_default("five", 50) == 50
373 #
374 fun get_or_default(key: K, default: V): V
375 do
376 if has_key(key) then return self[key]
377 return default
378 end
379
380 # Alias for `keys.has`
381 fun has_key(key: K): Bool do return self.keys.has(key)
382
383 # Get a new iterator on the map.
384 fun iterator: MapIterator[K, V] is abstract
385
386 # Return the point of view of self on the values only.
387 # Note that `self` and `values` are views on the same data;
388 # therefore any modification of one is visible on the other.
389 #
390 # var x = new HashMap[String, Int]
391 # x["four"] = 4
392 # assert x.values.has(4) == true
393 # assert x.values.has(5) == false
394 fun values: Collection[V] is abstract
395
396 # Return the point of view of self on the keys only.
397 # Note that `self` and `keys` 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.keys.has("four") == true
403 # assert x.keys.has("five") == false
404 fun keys: Collection[K] is abstract
405
406 # Is there no item in the collection?
407 #
408 # var x = new HashMap[String, Int]
409 # assert x.is_empty == true
410 # x["four"] = 4
411 # assert x.is_empty == false
412 fun is_empty: Bool is abstract
413
414 # Number of items in the collection.
415 #
416 # var x = new HashMap[String, Int]
417 # assert x.length == 0
418 # x["four"] = 4
419 # assert x.length == 1
420 # x["five"] = 5
421 # assert x.length == 2
422 fun length: Int is abstract
423
424 # Called by the underling implementation of `[]` to provide a default value when a `key` has no value
425 # By default the behavior is to abort.
426 #
427 # Note: the value is returned *as is*, implementations may want to store the value in the map before returning it
428 # @toimplement
429 protected fun provide_default_value(key: K): V do abort
430 end
431
432 # Maps are associative collections: `key` -> `item`.
433 #
434 # The main operator over maps is [].
435 #
436 # var map: Map[String, Int] = new ArrayMap[String, Int]
437 # # ...
438 # map["one"] = 1 # Associate 'one' to '1'
439 # map["two"] = 2 # Associate 'two' to '2'
440 # assert map["one"] == 1
441 # assert map["two"] == 2
442 #
443 # Instances of maps can be used with the for structure
444 #
445 # for key, value in map do
446 # assert (key == "one" and value == 1) or (key == "two" and value == 2)
447 # end
448 #
449 # The keys and values in the map can also be manipulated directly with the `keys` and `values` methods.
450 #
451 # assert map.keys.has("one") == true
452 # assert map.keys.has("tree") == false
453 # assert map.values.has(1) == true
454 # assert map.values.has(3) == false
455 #
456 interface Map[K: Object, V]
457 super MapRead[K, V]
458
459 # Set the `value` at `key`.
460 #
461 # Values can then get retrieved with `[]`.
462 #
463 # var x = new HashMap[String, Int]
464 # x["four"] = 4
465 # assert x["four"] == 4
466 #
467 # If the key was associated with a value, this old value is discarded
468 # and replaced with the new one.
469 #
470 # x["four"] = 40
471 # assert x["four"] == 40
472 # assert x.values.has(4) == false
473 #
474 fun []=(key: K, value: V) is abstract
475
476 # Add each (key,value) of `map` into `self`.
477 # If a same key exists in `map` and `self`, then the value in self is discarded.
478 #
479 # It is the analogous of `SimpleCollection::add_all`
480 #
481 # var x = new HashMap[String, Int]
482 # x["four"] = 4
483 # x["five"] = 5
484 # var y = new HashMap[String, Int]
485 # y["four"] = 40
486 # y["nine"] = 90
487 # x.recover_with y
488 # assert x["four"] == 40
489 # assert x["five"] == 5
490 # assert x["nine"] == 90
491 fun recover_with(map: MapRead[K, V])
492 do
493 var i = map.iterator
494 while i.is_ok do
495 self[i.key] = i.item
496 i.next
497 end
498 end
499
500 # Remove all items
501 #
502 # var x = new HashMap[String, Int]
503 # x["four"] = 4
504 # x.clear
505 # assert x.keys.has("four") == false
506 #
507 # ENSURE `is_empty`
508 fun clear is abstract
509
510 redef fun values: RemovableCollection[V] is abstract
511
512 redef fun keys: RemovableCollection[K] is abstract
513 end
514
515 # Iterators for Map.
516 interface MapIterator[K: Object, V]
517 # The current item.
518 # Require `is_ok`.
519 fun item: V is abstract
520
521 # The key of the current item.
522 # Require `is_ok`.
523 fun key: K is abstract
524
525 # Jump to the next item.
526 # Require `is_ok`.
527 fun next is abstract
528
529 # Is there a current item ?
530 fun is_ok: Bool is abstract
531
532 # Set a new `item` at `key`.
533 #fun item=(item: E) is abstract
534
535 # Post-iteration hook.
536 #
537 # Used to inform `self` that the iteration is over.
538 # Specific iterators can use this to free some resources.
539 #
540 # Is automatically invoked at the end of `for` structures.
541 #
542 # Do nothing by default.
543 fun finish do end
544 end
545
546 # Iterator on a 'keys' point of view of a map
547 class MapKeysIterator[K: Object, V]
548 super Iterator[K]
549 # The original iterator
550 var original_iterator: MapIterator[K, V]
551
552 redef fun is_ok do return self.original_iterator.is_ok
553 redef fun next do self.original_iterator.next
554 redef fun item do return self.original_iterator.key
555 end
556
557 # Iterator on a 'values' point of view of a map
558 class MapValuesIterator[K: Object, V]
559 super Iterator[V]
560 # The original iterator
561 var original_iterator: MapIterator[K, V]
562
563 redef fun is_ok do return self.original_iterator.is_ok
564 redef fun next do self.original_iterator.next
565 redef fun item do return self.original_iterator.item
566 end
567
568 # Sequences are indexed collections.
569 # The first item is 0. The last is `length-1`.
570 #
571 # The order is the main caracteristic of sequence
572 # and all concrete implementation of sequences are basically interchangeable.
573 interface SequenceRead[E]
574 super Collection[E]
575
576 # Get the first item.
577 # Is equivalent with `self[0]`.
578 #
579 # var a = [1,2,3]
580 # assert a.first == 1
581 #
582 # REQUIRE `not is_empty`
583 redef fun first
584 do
585 assert not_empty: not is_empty
586 return self[0]
587 end
588
589 # Return the index-th element of the sequence.
590 # The first element is 0 and the last is `length-1`
591 # If index is invalid, the program aborts
592 #
593 # var a = [10,20,30]
594 # assert a[0] == 10
595 # assert a[1] == 20
596 # assert a[2] == 30
597 #
598 # REQUIRE `index >= 0 and index < length`
599 fun [](index: Int): E is abstract
600
601 # Get the last item.
602 # Is equivalent with `self[length-1]`.
603 #
604 # var a = [1,2,3]
605 # assert a.last == 3
606 #
607 # REQUIRE `not is_empty`
608 fun last: E
609 do
610 assert not_empty: not is_empty
611 return self[length-1]
612 end
613
614 # The index of the first occurrence of `item`.
615 # Return -1 if `item` is not found.
616 # Comparison is done with `==`.
617 #
618 # var a = [10,20,30,10,20,30]
619 # assert a.index_of(20) == 1
620 # assert a.index_of(40) == -1
621 fun index_of(item: E): Int do return index_of_from(item, 0)
622
623 # The index of the last occurrence of `item`.
624 # Return -1 if `item` is not found.
625 # Comparison is done with `==`.
626 #
627 # var a = [10,20,30,10,20,30]
628 # assert a.last_index_of(20) == 4
629 # assert a.last_index_of(40) == -1
630 fun last_index_of(item: E): Int do return last_index_of_from(item, length-1)
631
632 # The index of the first occurrence of `item`, starting from pos.
633 # Return -1 if `item` is not found.
634 # Comparison is done with `==`.
635 #
636 # var a = [10,20,30,10,20,30]
637 # assert a.index_of_from(20, 3) == 4
638 # assert a.index_of_from(20, 4) == 4
639 # assert a.index_of_from(20, 5) == -1
640 fun index_of_from(item: E, pos: Int): Int
641 do
642 var p = 0
643 var i = iterator
644 while i.is_ok do
645 if p>=pos and i.item == item then return i.index
646 i.next
647 p += 1
648 end
649 return -1
650 end
651
652 # The index of the last occurrence of `item` starting from `pos` and decrementing.
653 # Return -1 if `item` is not found.
654 # Comparison is done with `==`.
655 #
656 # var a = [10,20,30,10,20,30]
657 # assert a.last_index_of_from(20, 2) == 1
658 # assert a.last_index_of_from(20, 1) == 1
659 # assert a.last_index_of_from(20, 0) == -1
660 fun last_index_of_from(item: E, pos: Int): Int
661 do
662 var res = -1
663 var p = 0
664 var i = iterator
665 while i.is_ok do
666 if p>pos then break
667 if i.item == item then res = p
668 i.next
669 p += 1
670 end
671 return res
672 end
673
674 # Two sequences are equals if they have the same items in the same order.
675 #
676 # var a = new List[Int]
677 # a.add(1)
678 # a.add(2)
679 # a.add(3)
680 # assert a == [1,2,3]
681 # assert a != [1,3,2]
682 redef fun ==(o)
683 do
684 if not o isa SequenceRead[nullable Object] then return false
685 var l = length
686 if o.length != l then return false
687 var i = 0
688 while i < l do
689 if self[i] != o[i] then return false
690 i += 1
691 end
692 return true
693 end
694
695 # Because of the law between `==` and `hash`, `hash` is redefined to be the sum of the hash of the elements
696 redef fun hash
697 do
698 # The 17 and 2/3 magic numbers were determined empirically.
699 # Note: the standard hash functions djb2, sbdm and fnv1 were also
700 # tested but were comparable (or worse).
701 var res = 17 + length
702 for e in self do
703 res = res * 3 / 2
704 if e != null then res += e.hash
705 end
706 return res
707 end
708
709 redef fun iterator: IndexedIterator[E] is abstract
710
711 # Gets a new Iterator starting at position `pos`
712 #
713 # var iter = [10,20,30,40,50].iterator_from(2)
714 # assert iter.to_a == [30, 40, 50]
715 fun iterator_from(pos: Int): IndexedIterator[E]
716 do
717 var res = iterator
718 while pos > 0 and res.is_ok do
719 res.next
720 pos -= 1
721 end
722 return res
723 end
724
725 # Gets an iterator starting at the end and going backwards
726 #
727 # var reviter = [1,2,3].reverse_iterator
728 # assert reviter.to_a == [3,2,1]
729 fun reverse_iterator: IndexedIterator[E] is abstract
730
731 # Gets an iterator on the chars of self starting from `pos`
732 #
733 # var reviter = [10,20,30,40,50].reverse_iterator_from(2)
734 # assert reviter.to_a == [30,20,10]
735 fun reverse_iterator_from(pos: Int): IndexedIterator[E]
736 do
737 var res = reverse_iterator
738 while pos > 0 and res.is_ok do
739 res.next
740 pos -= 1
741 end
742 return res
743 end
744 end
745
746 # Sequence are indexed collection.
747 # The first item is 0. The last is `length-1`.
748 interface Sequence[E]
749 super SequenceRead[E]
750 super SimpleCollection[E]
751
752 # Set the first item.
753 # Is equivalent with `self[0] = item`.
754 #
755 # var a = [1,2,3]
756 # a.first = 10
757 # assert a == [10,2,3]
758 fun first=(item: E)
759 do self[0] = item end
760
761 # Set the last item.
762 # Is equivalent with `self[length-1] = item`.
763 #
764 # var a = [1,2,3]
765 # a.last = 10
766 # assert a == [1,2,10]
767 #
768 # If the sequence is empty, `last=` is equivalent with `self[0]=` (thus with `first=`)
769 #
770 # var b = new Array[Int]
771 # b.last = 10
772 # assert b == [10]
773 fun last=(item: E)
774 do
775 var l = length
776 if l > 0 then
777 self[l-1] = item
778 else
779 self[0] = item
780 end
781 end
782
783 # A synonym of `push`
784 redef fun add(e) do push(e)
785
786 # Add an item after the last one.
787 #
788 # var a = [1,2,3]
789 # a.push(10)
790 # a.push(20)
791 # assert a == [1,2,3,10,20]
792 fun push(e: E) is abstract
793
794 # Add each item of `coll` after the last.
795 #
796 # var a = [1,2,3]
797 # a.append([7..9])
798 # assert a == [1,2,3,7,8,9]
799 #
800 # Alias of `add_all`
801 fun append(coll: Collection[E]) do add_all(coll)
802
803 # Remove the last item.
804 #
805 # var a = [1,2,3]
806 # assert a.pop == 3
807 # assert a.pop == 2
808 # assert a == [1]
809 #
810 # REQUIRE `not is_empty`
811 fun pop: E is abstract
812
813 # Add an item before the first one.
814 #
815 # var a = [1,2,3]
816 # a.unshift(10)
817 # a.unshift(20)
818 # assert a == [20,10,1,2,3]
819 fun unshift(e: E) is abstract
820
821 # Add all items of `coll` before the first one.
822 #
823 # var a = [1,2,3]
824 # a.prepend([7..9])
825 # assert a == [7,8,9,1,2,3]
826 #
827 # Alias of `insert_at(coll, 0)`
828 fun prepend(coll: Collection[E]) do insert_all(coll, 0)
829
830 # Remove the first item.
831 # The second item thus become the first.
832 #
833 # var a = [1,2,3]
834 # assert a.shift == 1
835 # assert a.shift == 2
836 # assert a == [3]
837 #
838 # REQUIRE `not is_empty`
839 fun shift: E is abstract
840
841 # Set the `item` at `index`.
842 #
843 # var a = [10,20,30]
844 # a[1] = 200
845 # assert a == [10,200,30]
846 #
847 # like with `[]`, index should be between `0` and `length-1`
848 # However, if `index==length`, `[]=` works like `push`.
849 #
850 # a[3] = 400
851 # assert a == [10,200,30,400]
852 #
853 # REQUIRE `index >= 0 and index <= length`
854 fun []=(index: Int, item: E) is abstract
855
856 # Insert an element at a given position, following elements are shifted.
857 #
858 # var a = [10, 20, 30, 40]
859 # a.insert(100, 2)
860 # assert a == [10, 20, 100, 30, 40]
861 #
862 # REQUIRE `index >= 0 and index <= length`
863 # ENSURE `self[index] == item`
864 fun insert(item: E, index: Int) is abstract
865
866 # Insert all elements at a given position, following elements are shifted.
867 #
868 # var a = [10, 20, 30, 40]
869 # a.insert_all([100..102], 2)
870 # assert a == [10, 20, 100, 101, 102, 30, 40]
871 #
872 # REQUIRE `index >= 0 and index <= length`
873 # ENSURE `self[index] == coll.first`
874 fun insert_all(coll: Collection[E], index: Int)
875 do
876 assert index >= 0 and index < length
877 if index == length then
878 add_all(coll)
879 end
880 for c in coll do
881 insert(c, index)
882 index += 1
883 end
884 end
885
886 # Remove the item at `index` and shift all following elements
887 #
888 # var a = [10,20,30]
889 # a.remove_at(1)
890 # assert a == [10,30]
891 #
892 # REQUIRE `index >= 0 and index < length`
893 fun remove_at(index: Int) is abstract
894 end
895
896 # Iterators on indexed collections.
897 interface IndexedIterator[E]
898 super Iterator[E]
899 # The index of the current item.
900 fun index: Int is abstract
901 end
902
903 # Associative arrays that internally uses couples to represent each (key, value) pairs.
904 # This is an helper class that some specific implementation of Map may implements.
905 interface CoupleMap[K: Object, V]
906 super Map[K, V]
907
908 # Return the couple of the corresponding key
909 # Return null if the key is no associated element
910 protected fun couple_at(key: K): nullable Couple[K, V] is abstract
911
912 # Return a new iteralot on all couples
913 # Used to provide `iterator` and others
914 protected fun couple_iterator: Iterator[Couple[K,V]] is abstract
915
916 redef fun iterator do return new CoupleMapIterator[K,V](couple_iterator)
917
918 redef fun [](key)
919 do
920 var c = couple_at(key)
921 if c == null then
922 return provide_default_value(key)
923 else
924 return c.second
925 end
926 end
927 end
928
929 # Iterator on CoupleMap
930 #
931 # Actually it is a wrapper around an iterator of the internal array of the map.
932 private class CoupleMapIterator[K: Object, V]
933 super MapIterator[K, V]
934 redef fun item do return _iter.item.second
935
936 #redef fun item=(e) do _iter.item.second = e
937
938 redef fun key do return _iter.item.first
939
940 redef fun is_ok do return _iter.is_ok
941
942 redef fun next
943 do
944 _iter.next
945 end
946
947 private var iter: Iterator[Couple[K,V]]
948 end
949
950 # Some tools ###################################################################
951
952 # Two objects in a simple structure.
953 class Couple[F, S]
954
955 # The first element of the couple.
956 var first: F is writable
957
958 # The second element of the couple.
959 var second: S is writable
960 end