# The root of the collection hierarchy.
#
-# Instances of this class offers an iterator method.
+# Collections modelize finite groups of objects, called elements.
+#
+# The specific behavior and representation of collections is determined
+# by the subclasses of the hierarchy.
+#
+# The main service of Collection is to provide a stable `iterator`
+# method usable to retrieve all the elements of the collection.
+#
+# Additional services are provided.
+# For an implementation point of view, Collection provide a basic
+# implementation of these services using the `iterator` method.
+# Subclasses often provide a more efficient implementation.
+#
+# Because of the `iterator` method, Collections instances can use
+# the `for` control structure:
#
-# Collections instances can use the "for" structure:
# var x: Collection[U]
# # ...
# for u in x do
# # u is a U
# # ...
# end
+#
# that is equivalent with
+#
# var x: Collection[U]
# # ...
# var i = x.iterator
# # ...
# i.next
# end
-#
-# This abstract class implements its others methods with an iterator.
-# Subclasses may redefine them with an efficient implementation.
interface Collection[E]
# Get a new iterator on the collection.
fun iterator: Iterator[E] is abstract
- # Iterate over each element of the collection
- fun iterate
- !each(e: E)
- do
- var i = iterator
- while i.is_ok do
- each(i.item)
- i.next
- end
- end
-
# Is there no item in the collection?
#
# assert [1,2,3].is_empty == false
end
end
-# Naive implementation of collections method
-# You only have to define iterator!
-interface NaiveCollection[E]
- super Collection[E]
-end
-
# Instances of the Iterator class generates a series of elements, one at a time.
# They are mainly used with collections.
interface Iterator[E]
if other.length != length then return false
return has_all(other)
end
+
+ # because of the law between `==` and `hash`, hash is redefined to be the sum of the hash of the elements
+ redef fun hash
+ do
+ var res = 0
+ for e in self do res += res.hash
+ return res
+ end
end
# MapRead are abstract associative collections: `key` -> `item`.
# Get a new iterator on the map.
fun iterator: MapIterator[K, E] is abstract
- # Iterate over each element of the collection
- fun iterate
- !each(k: K, v: E)
- do
- var i = iterator
- while i.is_ok do
- each(i.key, i.item)
- i.next
- end
- end
-
# Return the point of view of self on the values only.
# Note that `self` and `values` are views on the same data;
# therefore any modification of one is visible on the other.
end
redef fun iterator: IndexedIterator[E] is abstract
+
+ # Two sequences are equals if they have the same items in the same order.
+ redef fun ==(o)
+ do
+ if not o isa SequenceRead[nullable Object] or o is null then return false
+ var l = length
+ if o.length != l then return false
+ var i = 0
+ while i < l do
+ if self[i] != o[i] then return false
+ i += 1
+ end
+ return true
+ end
+
+ # because of the law between `==` and `hash`, hash is redefined to be the sum of the hash of the elements
+ redef fun hash
+ do
+ var res = 0
+ for e in self do res += res.hash
+ return res
+ end
end
# Sequence are indexed collection.