# Subclasses often provide a more efficient implementation.
#
# Because of the `iterator` method, Collections instances can use
-# the `for` control structure:
+# the `for` control structure.
#
-# var x: Collection[U]
-# # ...
-# for u in x do
-# # u is a U
-# # ...
-# end
+# ~~~nitish
+# var x: Collection[U]
+# # ...
+# for u in x do
+# # u is a U
+# # ...
+# end
+# ~~~
#
-# that is equivalent with
+# that is equivalent with the following:
#
-# var x: Collection[U]
-# # ...
-# var i = x.iterator
-# while i.is_ok do
-# var u = i.item # u is a U
-# # ...
-# i.next
-# end
+# ~~~nitish
+# var x: Collection[U]
+# # ...
+# var i = x.iterator
+# while i.is_ok do
+# var u = i.item # u is a U
+# # ...
+# i.next
+# end
+# ~~~
interface Collection[E]
# Get a new iterator on the collection.
fun iterator: Iterator[E] is abstract
return iterator.item
end
- # Is the collection contains all the elements of `other`?
+ # Does the collection contain at least each element of `other`?
#
- # assert [1,1,1].has_all([1]) == true
- # assert [1,1,1].has_all([1,2]) == false
# assert [1,3,4,2].has_all([1..2]) == true
# assert [1,3,4,2].has_all([1..5]) == false
+ #
+ # Repeated elements in the collections are not considered.
+ #
+ # assert [1,1,1].has_all([1]) == true
+ # assert [1..5].has_all([1,1,1]) == true
+ #
+ # Note that the default implementation is general and correct for any lawful Collections.
+ # It is memory-efficient but relies on `has` so may be CPU-inefficient for some kind of collections.
fun has_all(other: Collection[E]): Bool
do
for x in other do if not has(x) then return false
return true
end
+
+ # Does the collection contain exactly all the elements of `other`?
+ #
+ # The same elements must be present in both `self` and `other`,
+ # but the order of the elements in the collections are not considered.
+ #
+ # assert [1..3].has_exactly([3,1,2]) == true # the same elements
+ # assert [1..3].has_exactly([3,1]) == false # 2 is not in the array
+ # assert [1..2].has_exactly([3,1,2]) == false # 3 is not in the range
+ #
+ # Repeated elements must be present in both collections in the same amount.
+ # So basically it is a multi-set comparison.
+ #
+ # assert [1,2,3,2].has_exactly([1,2,2,3]) == true # the same elements
+ # assert [1,2,3,2].has_exactly([1,2,3]) == false # more 2 in the first array
+ # assert [1,2,3].has_exactly([1,2,2,3]) == false # more 2 in the second array
+ #
+ # Note that the default implementation is general and correct for any lawful Collections.
+ # It is memory-efficient but relies on `count` so may be CPU-inefficient for some kind of collections.
+ fun has_exactly(other: Collection[E]): Bool
+ do
+ if length != other.length then return false
+ for e in self do if self.count(e) != other.count(e) then return false
+ return true
+ end
end
# Instances of the Iterator class generates a series of elements, one at a time.
# Iterate over `self`
fun iterator: Iterator[E] do return self
+
+ # Post-iteration hook.
+ #
+ # Used to inform `self` that the iteration is over.
+ # Specific iterators can use this to free some resources.
+ #
+ # Is automatically invoked at the end of `for` structures.
+ #
+ # Do nothing by default.
+ fun finish do end
end
# A collection that contains only one item.
redef fun iterator do return new ContainerIterator[E](self)
- # Create a new instance with a given initial value.
- init(e: E) do item = e
-
# The stored item
var item: E is writable
end
redef fun next do is_ok = false
- init(c: Container[E]) do _container = c
-
redef var is_ok: Bool = true
- private var container: Container[E]
+ var container: Container[E]
end
# Items can be removed from this collection
return nhs
end
+ # Returns a new instance of `Set`.
+ #
+ # Depends on the subclass, mainly used for copy services
+ # like `union` or `intersection`.
protected fun new_set: Set[E] is abstract
end
# Set a new `item` at `key`.
#fun item=(item: E) is abstract
+
+ # Post-iteration hook.
+ #
+ # Used to inform `self` that the iteration is over.
+ # Specific iterators can use this to free some resources.
+ #
+ # Is automatically invoked at the end of `for` structures.
+ #
+ # Do nothing by default.
+ fun finish do end
end
# Iterator on a 'keys' point of view of a map
_iter.next
end
- private var iter: Iterator[Couple[K,V]]
-
- init(i: Iterator[Couple[K,V]]) do _iter = i
+ var iter: Iterator[Couple[K,V]]
end
# Some tools ###################################################################
# The second element of the couple.
var second: S is writable
-
- # Create a new instance with a first and a second object.
- init(f: F, s: S)
- do
- first = f
- second = s
- end
end