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