# Naive implementation of collections method
# You only have to define iterator!
-interface NaiveCollection[E: Object]
+interface NaiveCollection[E]
special Collection[E]
redef meth is_empty do return length == 0
meth add(item: E) is abstract
# Add each item of `coll`.
- meth add_all(coll: Collection[E]) do if coll != null then for i in coll do add(i)
+ meth add_all(coll: Collection[E]) do for i in coll do add(i)
end
# Abstract sets.
redef meth remove_all(item) do remove(item)
end
+interface MapRead[K, E]
+special Collection[E]
+ # Get the item at `key'.
+ meth [](key: K): E is abstract
+
+ # Is there an item at `key'.
+ meth has_key(key: K): Bool is abstract
+
+ redef meth iterator: MapIterator[K, E] is abstract
+end
+
# Maps are associative collections: `key' -> `item'.
#
# The main operator over maps is [].
# map.has_key(u3) # -> false
interface Map[K, E]
special RemovableCollection[E]
- # Get the item at `key'.
- meth [](key: K): E is abstract
-
+special MapRead[K, E]
# Set the`item' at `key'.
meth []=(key: K, item: E) is abstract
- # Is there an item at `key'.
- meth has_key(key: K): Bool is abstract
-
# Remove the item at `key'
meth remove_at(key: K) is abstract
i.next
end
end
-
- redef meth iterator: MapIterator[K, E] is abstract
end
# Iterators for Map.
meth key: K is abstract
# Set a new `item' at `key'.
- meth item=(item: E) is abstract
+ #meth item=(item: E) is abstract
end
# Indexed collection are ordoned collections.
# The first item is 0. The last is `length'-1.
-interface IndexedCollection[E]
-special Map[Int, E]
-special SimpleCollection[E]
+interface IndexedCollectionRead[E]
+special MapRead[Int, E]
# Get the first item.
# Is equivalent with `self'[0].
- redef meth first
- do
+ redef meth first
+ do
assert not_empty: not is_empty
- return self[0]
+ return self[0]
end
- # Set the first item.
- # Is equivalent with `self'[0] = `item'.
- meth first=(item: E)
- do self[0] = item end
-
# Get the last item.
# Is equivalent with `self'[`length'-1].
meth last: E
- do
+ do
assert not_empty: not is_empty
- return self[length-1]
+ return self[length-1]
end
+ # Return the index of the first occurence of `item'.
+ # Return -1 if `item' is not found
+ meth index_of(item: E): Int
+ do
+ var i = iterator
+ while i.is_ok do
+ if i.item == item then return i.index
+ i.next
+ end
+ return -1
+ end
+
+ redef meth iterator: IndexedIterator[E] is abstract
+end
+
+# Indexed collection are ordoned collections.
+# The first item is 0. The last is `length'-1.
+interface IndexedCollection[E]
+special IndexedCollectionRead[E]
+special Map[Int, E]
+special SimpleCollection[E]
+ # Set the first item.
+ # Is equivalent with `self'[0] = `item'.
+ meth first=(item: E)
+ do self[0] = item end
+
# Set the last item.
# Is equivalent with `self'[length-1] = `item'.
meth last=(item: E)
meth push(e: E) is abstract
# Add each item of `coll` after the last.
- meth append(coll: Collection[E]) do if coll != null then for i in coll do push(i)
+ meth append(coll: Collection[E]) do for i in coll do push(i)
# Remove the last item.
meth pop: E is abstract
# The second item become the first.
meth shift: E is abstract
- # Return the index of the first occurence of `item'.
- # Return -1 if `item' is not found
- meth index_of(item: E): Int
- do
- var i = iterator
- while i.is_ok do
- if i.item == item then return i.index
- i.next
- end
- return -1
- end
-
- redef meth iterator: IndexedIterator[E] is abstract
end
# Iterators on indexed collections.
special Map[K, E]
# Return the couple of the corresponding key
# Return null if the key is no associated element
- protected meth couple_at(key: K): Couple[K, E] is abstract
+ protected meth couple_at(key: K): nullable Couple[K, E] is abstract
redef meth [](key)
do
var c = couple_at(key)
if c == null then
- return null
+ abort
else
return c.second
end
special MapIterator[K, E]
redef meth item do return _iter.item.second
- redef meth item=(e) do _iter.item.second = e
+ #redef meth item=(e) do _iter.item.second = e
redef meth key do return _iter.item.first
redef meth next
do
_iter.next
- while _iter.is_ok and _iter.item == null do _iter.next
end
attr _iter: Iterator[Couple[K,E]]