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