8b72d85c80c1fc795914aa23b3f40a3459f4e408
[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 # Return 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
582 do
583 var i = iterator
584 while i.is_ok do
585 if i.item == item then return i.index
586 i.next
587 end
588 return -1
589 end
590
591 redef fun iterator: IndexedIterator[E] is abstract
592
593 # Two sequences are equals if they have the same items in the same order.
594 #
595 # var a = new List[Int]
596 # a.add(1)
597 # a.add(2)
598 # a.add(3)
599 # assert a == [1,2,3]
600 # assert a != [1,3,2]
601 redef fun ==(o)
602 do
603 if not o isa SequenceRead[nullable Object] then return false
604 var l = length
605 if o.length != l then return false
606 var i = 0
607 while i < l do
608 if self[i] != o[i] then return false
609 i += 1
610 end
611 return true
612 end
613
614 # Because of the law between `==` and `hash`, `hash` is redefined to be the sum of the hash of the elements
615 redef fun hash
616 do
617 var res = 0
618 for e in self do res += res.hash
619 return res
620 end
621 end
622
623 # Sequence are indexed collection.
624 # The first item is 0. The last is `length-1`.
625 interface Sequence[E]
626 super SequenceRead[E]
627 super SimpleCollection[E]
628
629 # Set the first item.
630 # Is equivalent with `self[0] = item`.
631 #
632 # var a = [1,2,3]
633 # a.first = 10
634 # assert a == [10,2,3]
635 fun first=(item: E)
636 do self[0] = item end
637
638 # Set the last item.
639 # Is equivalent with `self[length-1] = item`.
640 #
641 # var a = [1,2,3]
642 # a.last = 10
643 # assert a == [1,2,10]
644 #
645 # If the sequence is empty, `last=` is equivalent with `self[0]=` (thus with `first=`)
646 #
647 # var b = new Array[Int]
648 # b.last = 10
649 # assert b == [10]
650 fun last=(item: E)
651 do
652 var l = length
653 if l > 0 then
654 self[l-1] = item
655 else
656 self[0] = item
657 end
658 end
659
660 # A synonym of `push`
661 redef fun add(e) do push(e)
662
663 # Add an item after the last one.
664 #
665 # var a = [1,2,3]
666 # a.push(10)
667 # a.push(20)
668 # assert a == [1,2,3,10,20]
669 fun push(e: E) is abstract
670
671 # Add each item of `coll` after the last.
672 #
673 # var a = [1,2,3]
674 # a.append([7..9])
675 # assert a == [1,2,3,7,8,9]
676 fun append(coll: Collection[E]) do for i in coll do push(i)
677
678 # Remove the last item.
679 #
680 # var a = [1,2,3]
681 # assert a.pop == 3
682 # assert a.pop == 2
683 # assert a == [1]
684 #
685 # REQUIRE `not is_empty`
686 fun pop: E is abstract
687
688 # Add an item before the first one.
689 #
690 # var a = [1,2,3]
691 # a.unshift(10)
692 # a.unshift(20)
693 # assert a == [20,10,1,2,3]
694 fun unshift(e: E) is abstract
695
696 # Remove the first item.
697 # The second item thus become the first.
698 #
699 # var a = [1,2,3]
700 # assert a.shift == 1
701 # assert a.shift == 2
702 # assert a == [3]
703 #
704 # REQUIRE `not is_empty`
705 fun shift: E is abstract
706
707 # Set the `item` at `index`.
708 #
709 # var a = [10,20,30]
710 # a[1] = 200
711 # assert a == [10,200,30]
712 #
713 # like with `[]`, index should be between `0` and `length-1`
714 # However, if `index==length`, `[]=` works like `push`.
715 #
716 # a[3] = 400
717 # assert a == [10,200,30,400]
718 #
719 # REQUIRE `index >= 0 and index <= length`
720 fun []=(index: Int, item: E) is abstract
721
722 # Remove the item at `index` and shift all following elements
723 #
724 # var a = [10,20,30]
725 # a.remove_at(1)
726 # assert a == [10,30]
727 #
728 # REQUIRE `index >= 0 and index < length`
729 fun remove_at(index: Int) is abstract
730 end
731
732 # Iterators on indexed collections.
733 interface IndexedIterator[E]
734 super Iterator[E]
735 # The index of the current item.
736 fun index: Int is abstract
737 end
738
739 # Associative arrays that internally uses couples to represent each (key, value) pairs.
740 interface CoupleMap[K: Object, E]
741 super Map[K, E]
742 # Return the couple of the corresponding key
743 # Return null if the key is no associated element
744 protected fun couple_at(key: K): nullable Couple[K, E] is abstract
745
746 redef fun [](key)
747 do
748 var c = couple_at(key)
749 if c == null then
750 return provide_default_value(key)
751 else
752 return c.second
753 end
754 end
755 end
756
757 # Iterator on CoupleMap
758 #
759 # Actually is is a wrapper around an iterator of the internal array of the map.
760 class CoupleMapIterator[K: Object, E]
761 super MapIterator[K, E]
762 redef fun item do return _iter.item.second
763
764 #redef fun item=(e) do _iter.item.second = e
765
766 redef fun key do return _iter.item.first
767
768 redef fun is_ok do return _iter.is_ok
769
770 redef fun next
771 do
772 _iter.next
773 end
774
775 var _iter: Iterator[Couple[K,E]]
776
777 init(i: Iterator[Couple[K,E]]) do _iter = i
778 end
779
780 # Some tools ###################################################################
781
782 # Two objects in a simple structure.
783 class Couple[F, S]
784
785 # The first element of the couple.
786 readable writable var _first: F
787
788 # The second element of the couple.
789 readable writable var _second: S
790
791 # Create a new instance with a first and a second object.
792 init(f: F, s: S)
793 do
794 _first = f
795 _second = s
796 end
797 end