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