lib/coll: make CoupleMapIterator private
[nit.git] / lib / standard / collection / abstract_collection.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 #
5 # This file is free software, which comes along with NIT. This software is
6 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
7 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
8 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
9 # is kept unaltered, and a notification of the changes is added.
10 # You are allowed to redistribute it and sell it, alone or is a part of
11 # another product.
12
13 # Abstract collection classes and services.
14 #
15 # TODO specify the behavior on iterators when collections are modified.
16 module abstract_collection
17
18 import kernel
19
20 # The root of the collection hierarchy.
21 #
22 # Collections modelize finite groups of objects, called elements.
23 #
24 # The specific behavior and representation of collections is determined
25 # by the subclasses of the hierarchy.
26 #
27 # The main service of Collection is to provide a stable `iterator`
28 # method usable to retrieve all the elements of the collection.
29 #
30 # Additional services are provided.
31 # For an implementation point of view, Collection provide a basic
32 # implementation of these services using the `iterator` method.
33 # Subclasses often provide a more efficient implementation.
34 #
35 # Because of the `iterator` method, Collections instances can use
36 # the `for` control structure:
37 #
38 # var x: Collection[U]
39 # # ...
40 # for u in x do
41 # # u is a U
42 # # ...
43 # end
44 #
45 # that is equivalent with
46 #
47 # var x: Collection[U]
48 # # ...
49 # var i = x.iterator
50 # while i.is_ok do
51 # var u = i.item # u is a U
52 # # ...
53 # i.next
54 # end
55 interface Collection[E]
56 # Get a new iterator on the collection.
57 fun iterator: Iterator[E] is abstract
58
59 # Is there no item in the collection?
60 #
61 # assert [1,2,3].is_empty == false
62 # assert [1..1[.is_empty == true
63 fun is_empty: Bool do return length == 0
64
65 # Number of items in the collection.
66 #
67 # assert [10,20,30].length == 3
68 # assert [20..30[.length == 10
69 fun length: Int
70 do
71 var nb = 0
72 for i in self do nb += 1
73 return nb
74 end
75
76 # Is `item` in the collection ?
77 # Comparisons are done with ==
78 #
79 # assert [1,2,3].has(2) == true
80 # assert [1,2,3].has(9) == false
81 # assert [1..5[.has(2) == true
82 # assert [1..5[.has(9) == false
83 fun has(item: E): Bool
84 do
85 for i in self do if i == item then return true
86 return false
87 end
88
89 # Is the collection contain only `item`?
90 # Comparisons are done with ==
91 # Return true if the collection is empty.
92 #
93 # assert [1,1,1].has_only(1) == true
94 # assert [1,2,3].has_only(1) == false
95 # assert [1..1].has_only(1) == true
96 # assert [1..3].has_only(1) == false
97 # assert [3..3[.has_only(1) == true # empty collection
98 #
99 # ENSURE `is_empty implies result == true`
100 fun has_only(item: E): Bool
101 do
102 for i in self do if i != item then return false
103 return true
104 end
105
106 # How many occurrences of `item` are in the collection?
107 # Comparisons are done with ==
108 #
109 # assert [10,20,10].count(10) == 2
110 fun count(item: E): Int
111 do
112 var nb = 0
113 for i in self do if i == item then nb += 1
114 return nb
115 end
116
117 # Return the first item of the collection
118 #
119 # assert [1,2,3].first == 1
120 fun first: E
121 do
122 assert length > 0
123 return iterator.item
124 end
125
126 # Is the collection contains all the elements of `other`?
127 #
128 # assert [1,1,1].has_all([1]) == true
129 # assert [1,1,1].has_all([1,2]) == false
130 # assert [1,3,4,2].has_all([1..2]) == true
131 # assert [1,3,4,2].has_all([1..5]) == false
132 fun has_all(other: Collection[E]): Bool
133 do
134 for x in other do if not has(x) then return false
135 return true
136 end
137 end
138
139 # Instances of the Iterator class generates a series of elements, one at a time.
140 # They are mainly used with collections.
141 interface Iterator[E]
142 # The current item.
143 # Require `is_ok`.
144 fun item: E is abstract
145
146 # Jump to the next item.
147 # Require `is_ok`.
148 fun next is abstract
149
150 # Is there a current item ?
151 fun is_ok: Bool is abstract
152 end
153
154 # A collection that contains only one item.
155 #
156 # Used to pass arguments by reference.
157 #
158 # Also used when one want to give asingle element when a full
159 # collection is expected
160 class Container[E]
161 super Collection[E]
162
163 redef fun first do return _item
164
165 redef fun is_empty do return false
166
167 redef fun length do return 1
168
169 redef fun has(an_item) do return _item == an_item
170
171 redef fun has_only(an_item) do return _item == an_item
172
173 redef fun count(an_item)
174 do
175 if _item == an_item then
176 return 1
177 else
178 return 0
179 end
180 end
181
182 redef fun iterator do return new ContainerIterator[E](self)
183
184 # Create a new instance with a given initial value.
185 init(e: E) do _item = e
186
187 # The stored item
188 readable writable var _item: E
189 end
190
191 # This iterator is quite stupid since it is used for only one item.
192 class ContainerIterator[E]
193 super Iterator[E]
194 redef fun item do return _container.item
195
196 redef fun next do _is_ok = false
197
198 init(c: Container[E]) do _container = c
199
200 redef readable var _is_ok: Bool = true
201
202 var _container: Container[E]
203 end
204
205 # Items can be removed from this collection
206 interface RemovableCollection[E]
207 super Collection[E]
208
209 # Remove all items
210 #
211 # var a = [1,2,3]
212 # a.clear
213 # assert a.length == 0
214 #
215 # ENSURE `is_empty`
216 fun clear is abstract
217
218 # Remove an occucence of `item`
219 #
220 # var a = [1,2,3,1,2,3]
221 # a.remove 2
222 # assert a == [1,3,1,2,3]
223 fun remove(item: E) is abstract
224
225 # Remove all occurences of `item`
226 #
227 # var a = [1,2,3,1,2,3]
228 # a.remove_all 2
229 # assert a == [1,3,1,3]
230 fun remove_all(item: E) do while has(item) do remove(item)
231 end
232
233 # Items can be added to these collections.
234 interface SimpleCollection[E]
235 super RemovableCollection[E]
236
237 # Add an item in a collection.
238 #
239 # var a = [1,2]
240 # a.add 3
241 # assert a.has(3) == true
242 # assert a.has(10) == false
243 #
244 # Ensure col.has(item)
245 fun add(item: E) is abstract
246
247 # Add each item of `coll`.
248 # var a = [1,2]
249 # a.add_all [3..5]
250 # assert a.has(4) == true
251 # assert a.has(10) == false
252 fun add_all(coll: Collection[E]) do for i in coll do add(i)
253 end
254
255 # Abstract sets.
256 #
257 # Set is a collection without duplicates (according to `==`)
258 #
259 # var s: Set[String] = new ArraySet[String]
260 # var a = "Hello"
261 # var b = "Hel" + "lo"
262 # # ...
263 # s.add(a)
264 # assert s.has(b) == true
265 interface Set[E: Object]
266 super SimpleCollection[E]
267
268 redef fun has_only(item)
269 do
270 var l = length
271 if l == 1 then
272 return has(item)
273 else if l == 0 then
274 return true
275 else
276 return false
277 end
278 end
279
280 # Only 0 or 1
281 redef fun count(item)
282 do
283 if has(item) then
284 return 1
285 else
286 return 0
287 end
288 end
289
290 # Synonym of remove since there is only one item
291 redef fun remove_all(item) do remove(item)
292
293 # Equality is defined on set and means that each set contains the same elements
294 redef fun ==(other)
295 do
296 if not other isa Set[Object] then return false
297 if other.length != length then return false
298 return has_all(other)
299 end
300
301 # Because of the law between `==` and `hash`, `hash` is redefined to be the sum of the hash of the elements
302 redef fun hash
303 do
304 var res = 0
305 for e in self do res += res.hash
306 return res
307 end
308 end
309
310 # MapRead are abstract associative collections: `key` -> `item`.
311 interface MapRead[K: Object, E]
312 # Get the item at `key`
313 #
314 # var x = new HashMap[String, Int]
315 # x["four"] = 4
316 # assert x["four"] == 4
317 # # assert x["five"] #=> abort
318 #
319 # If the key is not in the map, `provide_default_value` is called (that aborts by default)
320 # See `get_or_null` and `get_or_default` for safe variations.
321 fun [](key: K): E is abstract
322
323 # Get the item at `key` or null if `key` is not in the map.
324 #
325 # var x = new HashMap[String, Int]
326 # x["four"] = 4
327 # assert x.get_or_null("four") == 4
328 # assert x.get_or_null("five") == null
329 #
330 # Note: use `has_key` and `[]` if you need the distinction bewteen a key associated with null, and no key.
331 fun get_or_null(key: K): nullable E
332 do
333 if has_key(key) then return self[key]
334 return null
335 end
336
337 # Get the item at `key` or return `default` if not in map
338 #
339 # var x = new HashMap[String, Int]
340 # x["four"] = 4
341 # assert x.get_or_default("four", 40) == 4
342 # assert x.get_or_default("five", 50) == 50
343 #
344 fun get_or_default(key: K, default: E): E
345 do
346 if has_key(key) then return self[key]
347 return default
348 end
349
350 # Depreciated alias for `keys.has`
351 fun has_key(key: K): Bool do return self.keys.has(key)
352
353 # Get a new iterator on the map.
354 fun iterator: MapIterator[K, E] is abstract
355
356 # Return the point of view of self on the values only.
357 # Note that `self` and `values` are views on the same data;
358 # therefore any modification of one is visible on the other.
359 #
360 # var x = new HashMap[String, Int]
361 # x["four"] = 4
362 # assert x.values.has(4) == true
363 # assert x.values.has(5) == false
364 fun values: Collection[E] is abstract
365
366 # Return the point of view of self on the keys only.
367 # Note that `self` and `keys` are views on the same data;
368 # therefore any modification of one is visible on the other.
369 #
370 # var x = new HashMap[String, Int]
371 # x["four"] = 4
372 # assert x.keys.has("four") == true
373 # assert x.keys.has("five") == false
374 fun keys: Collection[K] is abstract
375
376 # Is there no item in the collection?
377 #
378 # var x = new HashMap[String, Int]
379 # assert x.is_empty == true
380 # x["four"] = 4
381 # assert x.is_empty == false
382 fun is_empty: Bool is abstract
383
384 # Number of items in the collection.
385 #
386 # var x = new HashMap[String, Int]
387 # assert x.length == 0
388 # x["four"] = 4
389 # assert x.length == 1
390 # x["five"] = 5
391 # assert x.length == 2
392 fun length: Int is abstract
393
394 # Called by the underling implementation of `[]` to provide a default value when a `key` has no value
395 # By default the behavior is to abort.
396 #
397 # Note: the value is returned *as is*, implementations may want to store the value in the map before returning it
398 # @toimplement
399 protected fun provide_default_value(key: K): E do abort
400 end
401
402 # Maps are associative collections: `key` -> `item`.
403 #
404 # The main operator over maps is [].
405 #
406 # var map: Map[String, Int] = new ArrayMap[String, Int]
407 # # ...
408 # map["one"] = 1 # Associate 'one' to '1'
409 # map["two"] = 2 # Associate 'two' to '2'
410 # assert map["one"] == 1
411 # assert map["two"] == 2
412 #
413 # Instances of maps can be used with the for structure
414 #
415 # for key, value in map do
416 # assert (key == "one" and value == 1) or (key == "two" and value == 2)
417 # end
418 #
419 # The keys and values in the map can also be manipulated directly with the `keys` and `values` methods.
420 #
421 # assert map.keys.has("one") == true
422 # assert map.keys.has("tree") == false
423 # assert map.values.has(1) == true
424 # assert map.values.has(3) == false
425 #
426 interface Map[K: Object, E]
427 super MapRead[K, E]
428
429 # Set the `value` at `key`.
430 #
431 # Values can then get retrieved with `[]`.
432 #
433 # var x = new HashMap[String, Int]
434 # x["four"] = 4
435 # assert x["four"] == 4
436 #
437 # If the key was associated with a value, this old value is discarted
438 # and replaced with the new one.
439 #
440 # x["four"] = 40
441 # assert x["four"] == 40
442 # assert x.values.has(4) == false
443 #
444 fun []=(key: K, value: E) is abstract
445
446 # Add each (key,value) of `map` into `self`.
447 # If a same key exists in `map` and `self`, then the value in self is discarded.
448 #
449 # It is the analogous of `SimpleCollection::add_all`
450 #
451 # var x = new HashMap[String, Int]
452 # x["four"] = 4
453 # x["five"] = 5
454 # var y = new HashMap[String, Int]
455 # y["four"] = 40
456 # y["nine"] = 90
457 # x.recover_with y
458 # assert x["four"] == 40
459 # assert x["five"] == 5
460 # assert x["nine"] == 90
461 fun recover_with(map: Map[K, E])
462 do
463 var i = map.iterator
464 while i.is_ok do
465 self[i.key] = i.item
466 i.next
467 end
468 end
469
470 # Remove all items
471 #
472 # var x = new HashMap[String, Int]
473 # x["four"] = 4
474 # x.clear
475 # x.keys.has("four") == false
476 #
477 # ENSURE `is_empty`
478 fun clear is abstract
479
480 redef fun values: RemovableCollection[E] is abstract
481
482 redef fun keys: RemovableCollection[K] is abstract
483 end
484
485 # Iterators for Map.
486 interface MapIterator[K: Object, E]
487 # The current item.
488 # Require `is_ok`.
489 fun item: E is abstract
490
491 # The key of the current item.
492 # Require `is_ok`.
493 fun key: K is abstract
494
495 # Jump to the next item.
496 # Require `is_ok`.
497 fun next is abstract
498
499 # Is there a current item ?
500 fun is_ok: Bool is abstract
501
502 # Set a new `item` at `key`.
503 #fun item=(item: E) is abstract
504 end
505
506 # Iterator on a 'keys' point of view of a map
507 class MapKeysIterator[K: Object, V]
508 super Iterator[K]
509 # The original iterator
510 var iterator: MapIterator[K, V]
511
512 redef fun is_ok do return self.iterator.is_ok
513 redef fun next do self.iterator.next
514 redef fun item do return self.iterator.key
515 end
516
517 # Iterator on a 'values' point of view of a map
518 class MapValuesIterator[K: Object, V]
519 super Iterator[V]
520 # The original iterator
521 var iterator: MapIterator[K, V]
522
523 redef fun is_ok do return self.iterator.is_ok
524 redef fun next do self.iterator.next
525 redef fun item do return self.iterator.item
526 end
527
528 # Sequences are indexed collections.
529 # The first item is 0. The last is `length-1`.
530 #
531 # The order is the main caracteristic of sequence
532 # and all concrete implementation of sequences are basically interchangeable.
533 interface SequenceRead[E]
534 super Collection[E]
535
536 # Get the first item.
537 # Is equivalent with `self[0]`.
538 #
539 # var a = [1,2,3]
540 # assert a.first == 1
541 #
542 # REQUIRE `not is_empty`
543 redef fun first
544 do
545 assert not_empty: not is_empty
546 return self[0]
547 end
548
549 # Return the index-th element of the sequence.
550 # The first element is 0 and the last is `length-1`
551 # If index is invalid, the program aborts
552 #
553 # var a = [10,20,30]
554 # assert a[0] == 10
555 # assert a[1] == 20
556 # assert a[2] == 30
557 #
558 # REQUIRE `index >= 0 and index < length`
559 fun [](index: Int): E is abstract
560
561 # Get the last item.
562 # Is equivalent with `self[length-1]`.
563 #
564 # var a = [1,2,3]
565 # assert a.last == 3
566 #
567 # REQUIRE `not is_empty`
568 fun last: E
569 do
570 assert not_empty: not is_empty
571 return self[length-1]
572 end
573
574 # The index of the first occurrence of `item`.
575 # Return -1 if `item` is not found.
576 # Comparison is done with `==`.
577 #
578 # var a = [10,20,30,10,20,30]
579 # assert a.index_of(20) == 1
580 # assert a.index_of(40) == -1
581 fun index_of(item: E): Int do return index_of_from(item, 0)
582
583 # The index of the last occurrence of `item`.
584 # Return -1 if `item` is not found.
585 # Comparison is done with `==`.
586 #
587 # var a = [10,20,30,10,20,30]
588 # assert a.last_index_of(20) == 4
589 # assert a.last_index_of(40) == -1
590 fun last_index_of(item: E): Int do return last_index_of_from(item, length-1)
591
592 # The index of the first occurrence of `item`, starting from pos.
593 # Return -1 if `item` is not found.
594 # Comparison is done with `==`.
595 #
596 # var a = [10,20,30,10,20,30]
597 # assert a.index_of_from(20, 3) == 4
598 # assert a.index_of_from(20, 4) == 4
599 # assert a.index_of_from(20, 5) == -1
600 fun index_of_from(item: E, pos: Int): Int
601 do
602 var p = 0
603 var i = iterator
604 while i.is_ok do
605 if p>pos and i.item == item then return i.index
606 i.next
607 p += 1
608 end
609 return -1
610 end
611
612 # The index of the last occurrence of `item` starting from `pos` and decrementing.
613 # Return -1 if `item` is not found.
614 # Comparison is done with `==`.
615 #
616 # var a = [10,20,30,10,20,30]
617 # assert a.last_index_of_from(20, 2) == 1
618 # assert a.last_index_of_from(20, 1) == 1
619 # assert a.last_index_of_from(20, 0) == -1
620 fun last_index_of_from(item: E, pos: Int): Int
621 do
622 var res = -1
623 var p = 0
624 var i = iterator
625 while i.is_ok do
626 if p>pos then break
627 if i.item == item then res = p
628 i.next
629 p += 1
630 end
631 return res
632 end
633
634 redef fun iterator: IndexedIterator[E] is abstract
635
636 # Two sequences are equals if they have the same items in the same order.
637 #
638 # var a = new List[Int]
639 # a.add(1)
640 # a.add(2)
641 # a.add(3)
642 # assert a == [1,2,3]
643 # assert a != [1,3,2]
644 redef fun ==(o)
645 do
646 if not o isa SequenceRead[nullable Object] then return false
647 var l = length
648 if o.length != l then return false
649 var i = 0
650 while i < l do
651 if self[i] != o[i] then return false
652 i += 1
653 end
654 return true
655 end
656
657 # Because of the law between `==` and `hash`, `hash` is redefined to be the sum of the hash of the elements
658 redef fun hash
659 do
660 var res = 0
661 for e in self do res += res.hash
662 return res
663 end
664 end
665
666 # Sequence are indexed collection.
667 # The first item is 0. The last is `length-1`.
668 interface Sequence[E]
669 super SequenceRead[E]
670 super SimpleCollection[E]
671
672 # Set the first item.
673 # Is equivalent with `self[0] = item`.
674 #
675 # var a = [1,2,3]
676 # a.first = 10
677 # assert a == [10,2,3]
678 fun first=(item: E)
679 do self[0] = item end
680
681 # Set the last item.
682 # Is equivalent with `self[length-1] = item`.
683 #
684 # var a = [1,2,3]
685 # a.last = 10
686 # assert a == [1,2,10]
687 #
688 # If the sequence is empty, `last=` is equivalent with `self[0]=` (thus with `first=`)
689 #
690 # var b = new Array[Int]
691 # b.last = 10
692 # assert b == [10]
693 fun last=(item: E)
694 do
695 var l = length
696 if l > 0 then
697 self[l-1] = item
698 else
699 self[0] = item
700 end
701 end
702
703 # A synonym of `push`
704 redef fun add(e) do push(e)
705
706 # Add an item after the last one.
707 #
708 # var a = [1,2,3]
709 # a.push(10)
710 # a.push(20)
711 # assert a == [1,2,3,10,20]
712 fun push(e: E) is abstract
713
714 # Add each item of `coll` after the last.
715 #
716 # var a = [1,2,3]
717 # a.append([7..9])
718 # assert a == [1,2,3,7,8,9]
719 fun append(coll: Collection[E]) do for i in coll do push(i)
720
721 # Remove the last item.
722 #
723 # var a = [1,2,3]
724 # assert a.pop == 3
725 # assert a.pop == 2
726 # assert a == [1]
727 #
728 # REQUIRE `not is_empty`
729 fun pop: E is abstract
730
731 # Add an item before the first one.
732 #
733 # var a = [1,2,3]
734 # a.unshift(10)
735 # a.unshift(20)
736 # assert a == [20,10,1,2,3]
737 fun unshift(e: E) is abstract
738
739 # Remove the first item.
740 # The second item thus become the first.
741 #
742 # var a = [1,2,3]
743 # assert a.shift == 1
744 # assert a.shift == 2
745 # assert a == [3]
746 #
747 # REQUIRE `not is_empty`
748 fun shift: E is abstract
749
750 # Set the `item` at `index`.
751 #
752 # var a = [10,20,30]
753 # a[1] = 200
754 # assert a == [10,200,30]
755 #
756 # like with `[]`, index should be between `0` and `length-1`
757 # However, if `index==length`, `[]=` works like `push`.
758 #
759 # a[3] = 400
760 # assert a == [10,200,30,400]
761 #
762 # REQUIRE `index >= 0 and index <= length`
763 fun []=(index: Int, item: E) is abstract
764
765 # Insert an element at a given position, following elements are shifted.
766 #
767 # var a = [10, 20, 30, 40]
768 # a.insert(100, 2)
769 # assert a == [10, 20, 100, 30, 40]
770 #
771 # REQUIRE `index >= 0 and index < length`
772 # ENSURE `self[index] == item`
773 fun insert(item: E, index: Int) is abstract
774
775 # Remove the item at `index` and shift all following elements
776 #
777 # var a = [10,20,30]
778 # a.remove_at(1)
779 # assert a == [10,30]
780 #
781 # REQUIRE `index >= 0 and index < length`
782 fun remove_at(index: Int) is abstract
783 end
784
785 # Iterators on indexed collections.
786 interface IndexedIterator[E]
787 super Iterator[E]
788 # The index of the current item.
789 fun index: Int is abstract
790 end
791
792 # Associative arrays that internally uses couples to represent each (key, value) pairs.
793 # This is an helper class that some specific implementation of Map may implements.
794 interface CoupleMap[K: Object, E]
795 super Map[K, E]
796
797 # Return the couple of the corresponding key
798 # Return null if the key is no associated element
799 protected fun couple_at(key: K): nullable Couple[K, E] is abstract
800
801 # Return a new iteralot on all couples
802 # Used to provide `iterator` and others
803 protected fun couple_iterator: Iterator[Couple[K,E]] is abstract
804
805 redef fun iterator do return new CoupleMapIterator[K,E](couple_iterator)
806
807 redef fun [](key)
808 do
809 var c = couple_at(key)
810 if c == null then
811 return provide_default_value(key)
812 else
813 return c.second
814 end
815 end
816 end
817
818 # Iterator on CoupleMap
819 #
820 # Actually it is a wrapper around an iterator of the internal array of the map.
821 private class CoupleMapIterator[K: Object, E]
822 super MapIterator[K, E]
823 redef fun item do return _iter.item.second
824
825 #redef fun item=(e) do _iter.item.second = e
826
827 redef fun key do return _iter.item.first
828
829 redef fun is_ok do return _iter.is_ok
830
831 redef fun next
832 do
833 _iter.next
834 end
835
836 var _iter: Iterator[Couple[K,E]]
837
838 init(i: Iterator[Couple[K,E]]) do _iter = i
839 end
840
841 # Some tools ###################################################################
842
843 # Two objects in a simple structure.
844 class Couple[F, S]
845
846 # The first element of the couple.
847 readable writable var _first: F
848
849 # The second element of the couple.
850 readable writable var _second: S
851
852 # Create a new instance with a first and a second object.
853 init(f: F, s: S)
854 do
855 _first = f
856 _second = s
857 end
858 end