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