1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
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
13 # Abstract collection classes and services.
15 # TODO specify the behavior on iterators when collections are modified.
16 module abstract_collection
20 # The root of the collection hierarchy.
22 # Collections modelize finite groups of objects, called elements.
24 # The specific behavior and representation of collections is determined
25 # by the subclasses of the hierarchy.
27 # The main service of Collection is to provide a stable `iterator`
28 # method usable to retrieve all the elements of the collection.
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.
35 # Because of the `iterator` method, Collections instances can use
36 # the `for` control structure.
39 # var x: Collection[U]
47 # that is equivalent with the following:
50 # var x: Collection[U]
54 # var u = i.item # u is a U
59 interface Collection[E
]
60 # Get a new iterator on the collection.
61 fun iterator
: Iterator[E
] is abstract
63 # Is there no item in the collection?
65 # assert [1,2,3].is_empty == false
66 # assert [1..1[.is_empty == true
67 fun is_empty
: Bool do return length
== 0
69 # Number of items in the collection.
71 # assert [10,20,30].length == 3
72 # assert [20..30[.length == 10
76 for i
in self do nb
+= 1
80 # Is `item` in the collection ?
81 # Comparisons are done with ==
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
89 for i
in self do if i
== item
then return true
93 # Is the collection contain only `item`?
94 # Comparisons are done with ==
95 # Return true if the collection is empty.
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
103 # ENSURE `is_empty implies result == true`
104 fun has_only
(item
: E
): Bool
106 for i
in self do if i
!= item
then return false
110 # How many occurrences of `item` are in the collection?
111 # Comparisons are done with ==
113 # assert [10,20,10].count(10) == 2
114 fun count
(item
: E
): Int
117 for i
in self do if i
== item
then nb
+= 1
121 # Return the first item of the collection
123 # assert [1,2,3].first == 1
130 # Does the collection contain at least each element of `other`?
132 # assert [1,3,4,2].has_all([1..2]) == true
133 # assert [1,3,4,2].has_all([1..5]) == false
135 # Repeated elements in the collections are not considered.
137 # assert [1,1,1].has_all([1]) == true
138 # assert [1..5].has_all([1,1,1]) == true
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
144 for x
in other
do if not has
(x
) then return false
148 # Does the collection contain exactly all the elements of `other`?
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.
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
157 # Repeated elements must be present in both collections in the same amount.
158 # So basically it is a multi-set comparison.
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
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
168 if length
!= other
.length
then return false
169 for e
in self do if self.count
(e
) != other
.count
(e
) then return false
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
]
179 fun item
: E
is abstract
181 # Jump to the next item.
185 # Is there a current item ?
186 fun is_ok
: Bool is abstract
188 # Iterate over `self`
189 fun iterator
: Iterator[E
] do return self
191 # Post-iteration hook.
193 # Used to inform `self` that the iteration is over.
194 # Specific iterators can use this to free some resources.
196 # Is automatically invoked at the end of `for` structures.
198 # Do nothing by default.
202 # A collection that contains only one item.
204 # Used to pass arguments by reference.
206 # Also used when one want to give a single element when a full
207 # collection is expected
211 redef fun first
do return item
213 redef fun is_empty
do return false
215 redef fun length
do return 1
217 redef fun has
(an_item
) do return item
== an_item
219 redef fun has_only
(an_item
) do return item
== an_item
221 redef fun count
(an_item
)
223 if item
== an_item
then
230 redef fun iterator
do return new ContainerIterator[E
](self)
233 var item
: E
is writable
236 # This iterator is quite stupid since it is used for only one item.
237 private class ContainerIterator[E
]
239 redef fun item
do return _container
.item
241 redef fun next
do is_ok
= false
243 redef var is_ok
: Bool = true
245 var container
: Container[E
]
248 # Items can be removed from this collection
249 interface RemovableCollection[E
]
256 # assert a.length == 0
259 fun clear
is abstract
261 # Remove an occucence of `item`
263 # var a = [1,2,3,1,2,3]
265 # assert a == [1,3,1,2,3]
266 fun remove
(item
: E
) is abstract
268 # Remove all occurences of `item`
270 # var a = [1,2,3,1,2,3]
272 # assert a == [1,3,1,3]
273 fun remove_all
(item
: E
) do while has
(item
) do remove
(item
)
276 # Items can be added to these collections.
277 interface SimpleCollection[E
]
278 super RemovableCollection[E
]
280 # Add an item in a collection.
284 # assert a.has(3) == true
285 # assert a.has(10) == false
287 # Ensure col.has(item)
288 fun add
(item
: E
) is abstract
290 # Add each item of `coll`.
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
)
300 # Set is a collection without duplicates (according to `==`)
302 # var s: Set[String] = new ArraySet[String]
304 # var b = "Hel" + "lo"
307 # assert s.has(b) == true
309 super SimpleCollection[E
]
311 redef fun has_only
(item
)
324 redef fun count
(item
)
333 # Synonym of remove since there is only one item
334 redef fun remove_all
(item
) do remove
(item
)
336 # Equality is defined on set and means that each set contains the same elements
339 if not other
isa Set[Object] then return false
340 if other
.length
!= length
then return false
341 return has_all
(other
)
344 # Because of the law between `==` and `hash`, `hash` is redefined to be the sum of the hash of the elements
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
355 # Returns the union of this set with the `other` set
356 fun union
(other
: Set[E
]): Set[E
]
364 # Returns the intersection of this set with the `other` set
365 fun intersection
(other
: Set[E
]): Set[E
]
368 for v
in self do if other
.has
(v
) then nhs
.add
(v
)
372 # Returns a new instance of `Set`.
374 # Depends on the subclass, mainly used for copy services
375 # like `union` or `intersection`.
376 protected fun new_set
: Set[E
] is abstract
379 # MapRead are abstract associative collections: `key` -> `item`.
380 interface MapRead[K
, V
]
381 # Get the item at `key`
383 # var x = new HashMap[String, Int]
385 # assert x["four"] == 4
386 # # assert x["five"] #=> abort
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
392 # Get the item at `key` or null if `key` is not in the map.
394 # var x = new HashMap[String, Int]
396 # assert x.get_or_null("four") == 4
397 # assert x.get_or_null("five") == null
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
402 if has_key
(key
) then return self[key
]
406 # Get the item at `key` or return `default` if not in map
408 # var x = new HashMap[String, Int]
410 # assert x.get_or_default("four", 40) == 4
411 # assert x.get_or_default("five", 50) == 50
413 fun get_or_default
(key
: K
, default
: V
): V
415 if has_key
(key
) then return self[key
]
419 # Is there an item associated with `key`?
421 # var x = new HashMap[String, Int]
423 # assert x.has_key("four") == true
424 # assert x.has_key("five") == false
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
)
429 # Get a new iterator on the map.
430 fun iterator
: MapIterator[K
, V
] is abstract
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.
436 # var x = new HashMap[String, Int]
438 # assert x.values.has(4) == true
439 # assert x.values.has(5) == false
440 fun values
: Collection[V
] is abstract
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.
446 # var x = new HashMap[String, Int]
448 # assert x.keys.has("four") == true
449 # assert x.keys.has("five") == false
450 fun keys
: Collection[K
] is abstract
452 # Is there no item in the collection?
454 # var x = new HashMap[String, Int]
455 # assert x.is_empty == true
457 # assert x.is_empty == false
458 fun is_empty
: Bool is abstract
460 # Number of items in the collection.
462 # var x = new HashMap[String, Int]
463 # assert x.length == 0
465 # assert x.length == 1
467 # assert x.length == 2
468 fun length
: Int is abstract
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.
473 # Note: the value is returned *as is*, implementations may want to store the value in the map before returning it
475 protected fun provide_default_value
(key
: K
): V
do abort
477 # Does `self` and `other` have the same keys associated with the same values?
480 # var a = new HashMap[String, Int]
481 # var b = new ArrayMap[Object, Numeric]
492 if not other
isa MapRead[nullable Object, nullable Object] then return false
493 if other
.length
!= self.length
then return false
495 if not other
.has_key
(k
) then return false
496 if other
[k
] != v
then return false
501 # A hashcode based on the hashcode of the keys and the values.
504 # var a = new HashMap[String, Int]
505 # var b = new ArrayMap[Object, Numeric]
508 # assert a.hash == b.hash
514 if k
!= null then res
+= k
.hash
* 7
515 if v
!= null then res
+= v
.hash
* 11
521 # Maps are associative collections: `key` -> `item`.
523 # The main operator over maps is [].
525 # var map: Map[String, Int] = new ArrayMap[String, Int]
527 # map["one"] = 1 # Associate 'one' to '1'
528 # map["two"] = 2 # Associate 'two' to '2'
529 # assert map["one"] == 1
530 # assert map["two"] == 2
532 # Instances of maps can be used with the for structure
534 # for key, value in map do
535 # assert (key == "one" and value == 1) or (key == "two" and value == 2)
538 # The keys and values in the map can also be manipulated directly with the `keys` and `values` methods.
540 # assert map.keys.has("one") == true
541 # assert map.keys.has("tree") == false
542 # assert map.values.has(1) == true
543 # assert map.values.has(3) == false
548 # Set the `value` at `key`.
550 # Values can then get retrieved with `[]`.
552 # var x = new HashMap[String, Int]
554 # assert x["four"] == 4
556 # If the key was associated with a value, this old value is discarded
557 # and replaced with the new one.
560 # assert x["four"] == 40
561 # assert x.values.has(4) == false
563 fun []=(key
: K
, value
: V
) is abstract
565 # Add each (key,value) of `map` into `self`.
566 # If a same key exists in `map` and `self`, then the value in self is discarded.
568 # It is the analogous of `SimpleCollection::add_all`
570 # var x = new HashMap[String, Int]
573 # var y = new HashMap[String, Int]
577 # assert x["four"] == 40
578 # assert x["five"] == 5
579 # assert x["nine"] == 90
580 fun recover_with
(map
: MapRead[K
, V
])
591 # var x = new HashMap[String, Int]
594 # assert x.keys.has("four") == false
597 fun clear
is abstract
599 redef fun values
: RemovableCollection[V
] is abstract
601 redef fun keys
: RemovableCollection[K
] is abstract
605 interface MapIterator[K
, V
]
608 fun item
: V
is abstract
610 # The key of the current item.
612 fun key
: K
is abstract
614 # Jump to the next item.
618 # Is there a current item ?
619 fun is_ok
: Bool is abstract
621 # Set a new `item` at `key`.
622 #fun item=(item: E) is abstract
624 # Post-iteration hook.
626 # Used to inform `self` that the iteration is over.
627 # Specific iterators can use this to free some resources.
629 # Is automatically invoked at the end of `for` structures.
631 # Do nothing by default.
635 # Iterator on a 'keys' point of view of a map
636 class MapKeysIterator[K
, V
]
638 # The original iterator
639 var original_iterator
: MapIterator[K
, V
]
641 redef fun is_ok
do return self.original_iterator
.is_ok
642 redef fun next
do self.original_iterator
.next
643 redef fun item
do return self.original_iterator
.key
646 # Iterator on a 'values' point of view of a map
647 class MapValuesIterator[K
, V
]
649 # The original iterator
650 var original_iterator
: MapIterator[K
, V
]
652 redef fun is_ok
do return self.original_iterator
.is_ok
653 redef fun next
do self.original_iterator
.next
654 redef fun item
do return self.original_iterator
.item
657 # Sequences are indexed collections.
658 # The first item is 0. The last is `length-1`.
660 # The order is the main caracteristic of sequence
661 # and all concrete implementation of sequences are basically interchangeable.
662 interface SequenceRead[E
]
665 # Get the first item.
666 # Is equivalent with `self[0]`.
669 # assert a.first == 1
671 # REQUIRE `not is_empty`
674 assert not_empty
: not is_empty
678 # Return the index-th element of the sequence.
679 # The first element is 0 and the last is `length-1`
680 # If index is invalid, the program aborts
687 # REQUIRE `index >= 0 and index < length`
688 fun [](index
: Int): E
is abstract
691 # Is equivalent with `self[length-1]`.
696 # REQUIRE `not is_empty`
699 assert not_empty
: not is_empty
700 return self[length-1
]
703 # The index of the first occurrence of `item`.
704 # Return -1 if `item` is not found.
705 # Comparison is done with `==`.
707 # var a = [10,20,30,10,20,30]
708 # assert a.index_of(20) == 1
709 # assert a.index_of(40) == -1
710 fun index_of
(item
: E
): Int do return index_of_from
(item
, 0)
712 # The index of the last occurrence of `item`.
713 # Return -1 if `item` is not found.
714 # Comparison is done with `==`.
716 # var a = [10,20,30,10,20,30]
717 # assert a.last_index_of(20) == 4
718 # assert a.last_index_of(40) == -1
719 fun last_index_of
(item
: E
): Int do return last_index_of_from
(item
, length-1
)
721 # The index of the first occurrence of `item`, starting from pos.
722 # Return -1 if `item` is not found.
723 # Comparison is done with `==`.
725 # var a = [10,20,30,10,20,30]
726 # assert a.index_of_from(20, 3) == 4
727 # assert a.index_of_from(20, 4) == 4
728 # assert a.index_of_from(20, 5) == -1
729 fun index_of_from
(item
: E
, pos
: Int): Int
734 if p
>=pos
and i
.item
== item
then return i
.index
741 # The index of the last occurrence of `item` starting from `pos` and decrementing.
742 # Return -1 if `item` is not found.
743 # Comparison is done with `==`.
745 # var a = [10,20,30,10,20,30]
746 # assert a.last_index_of_from(20, 2) == 1
747 # assert a.last_index_of_from(20, 1) == 1
748 # assert a.last_index_of_from(20, 0) == -1
749 fun last_index_of_from
(item
: E
, pos
: Int): Int
756 if i
.item
== item
then res
= p
763 # Two sequences are equals if they have the same items in the same order.
765 # var a = new List[Int]
769 # assert a == [1,2,3]
770 # assert a != [1,3,2]
773 if not o
isa SequenceRead[nullable Object] then return false
775 if o
.length
!= l
then return false
778 if self[i
] != o
[i
] then return false
784 # Because of the law between `==` and `hash`, `hash` is redefined to be the sum of the hash of the elements
787 # The 17 and 2/3 magic numbers were determined empirically.
788 # Note: the standard hash functions djb2, sbdm and fnv1 were also
789 # tested but were comparable (or worse).
790 var res
= 17 + length
793 if e
!= null then res
+= e
.hash
798 redef fun iterator
: IndexedIterator[E
] is abstract
800 # Gets a new Iterator starting at position `pos`
802 # var iter = [10,20,30,40,50].iterator_from(2)
803 # assert iter.to_a == [30, 40, 50]
804 fun iterator_from
(pos
: Int): IndexedIterator[E
]
807 while pos
> 0 and res
.is_ok
do
814 # Gets an iterator starting at the end and going backwards
816 # var reviter = [1,2,3].reverse_iterator
817 # assert reviter.to_a == [3,2,1]
818 fun reverse_iterator
: IndexedIterator[E
] is abstract
820 # Gets an iterator on the chars of self starting from `pos`
822 # var reviter = [10,20,30,40,50].reverse_iterator_from(2)
823 # assert reviter.to_a == [30,20,10]
824 fun reverse_iterator_from
(pos
: Int): IndexedIterator[E
]
826 var res
= reverse_iterator
827 while pos
> 0 and res
.is_ok
do
835 # Sequence are indexed collection.
836 # The first item is 0. The last is `length-1`.
837 interface Sequence[E
]
838 super SequenceRead[E
]
839 super SimpleCollection[E
]
841 # Set the first item.
842 # Is equivalent with `self[0] = item`.
846 # assert a == [10,2,3]
848 do self[0] = item
end
851 # Is equivalent with `self[length-1] = item`.
855 # assert a == [1,2,10]
857 # If the sequence is empty, `last=` is equivalent with `self[0]=` (thus with `first=`)
859 # var b = new Array[Int]
872 # A synonym of `push`
873 redef fun add
(e
) do push
(e
)
875 # Add an item after the last one.
880 # assert a == [1,2,3,10,20]
881 fun push
(e
: E
) is abstract
883 # Add each item of `coll` after the last.
887 # assert a == [1,2,3,7,8,9]
890 fun append
(coll
: Collection[E
]) do add_all
(coll
)
892 # Remove the last item.
899 # REQUIRE `not is_empty`
900 fun pop
: E
is abstract
902 # Add an item before the first one.
907 # assert a == [20,10,1,2,3]
908 fun unshift
(e
: E
) is abstract
910 # Add all items of `coll` before the first one.
914 # assert a == [7,8,9,1,2,3]
916 # Alias of `insert_at(coll, 0)`
917 fun prepend
(coll
: Collection[E
]) do insert_all
(coll
, 0)
919 # Remove the first item.
920 # The second item thus become the first.
923 # assert a.shift == 1
924 # assert a.shift == 2
927 # REQUIRE `not is_empty`
928 fun shift
: E
is abstract
930 # Set the `item` at `index`.
934 # assert a == [10,200,30]
936 # like with `[]`, index should be between `0` and `length-1`
937 # However, if `index==length`, `[]=` works like `push`.
940 # assert a == [10,200,30,400]
942 # REQUIRE `index >= 0 and index <= length`
943 fun []=(index
: Int, item
: E
) is abstract
945 # Insert an element at a given position, following elements are shifted.
947 # var a = [10, 20, 30, 40]
949 # assert a == [10, 20, 100, 30, 40]
951 # REQUIRE `index >= 0 and index <= length`
952 # ENSURE `self[index] == item`
953 fun insert
(item
: E
, index
: Int) is abstract
955 # Insert all elements at a given position, following elements are shifted.
957 # var a = [10, 20, 30, 40]
958 # a.insert_all([100..102], 2)
959 # assert a == [10, 20, 100, 101, 102, 30, 40]
961 # REQUIRE `index >= 0 and index <= length`
962 # ENSURE `self[index] == coll.first`
963 fun insert_all
(coll
: Collection[E
], index
: Int)
965 assert index
>= 0 and index
< length
966 if index
== length
then
975 # Remove the item at `index` and shift all following elements
979 # assert a == [10,30]
981 # REQUIRE `index >= 0 and index < length`
982 fun remove_at
(index
: Int) is abstract
985 # Iterators on indexed collections.
986 interface IndexedIterator[E
]
988 # The index of the current item.
989 fun index
: Int is abstract
992 # Associative arrays that internally uses couples to represent each (key, value) pairs.
993 # This is an helper class that some specific implementation of Map may implements.
994 interface CoupleMap[K
, V
]
997 # Return the couple of the corresponding key
998 # Return null if the key is no associated element
999 protected fun couple_at
(key
: K
): nullable Couple[K
, V
] is abstract
1001 # Return a new iteralot on all couples
1002 # Used to provide `iterator` and others
1003 protected fun couple_iterator
: Iterator[Couple[K
,V
]] is abstract
1005 redef fun iterator
do return new CoupleMapIterator[K
,V
](couple_iterator
)
1009 var c
= couple_at
(key
)
1011 return provide_default_value
(key
)
1017 redef fun has_key
(key
) do return couple_at
(key
) != null
1020 # Iterator on CoupleMap
1022 # Actually it is a wrapper around an iterator of the internal array of the map.
1023 private class CoupleMapIterator[K
, V
]
1024 super MapIterator[K
, V
]
1025 redef fun item
do return _iter
.item
.second
1027 #redef fun item=(e) do _iter.item.second = e
1029 redef fun key
do return _iter
.item
.first
1031 redef fun is_ok
do return _iter
.is_ok
1038 var iter
: Iterator[Couple[K
,V
]]
1041 # Some tools ###################################################################
1043 # Two objects in a simple structure.
1046 # The first element of the couple.
1047 var first
: F
is writable
1049 # The second element of the couple.
1050 var second
: S
is writable