X-Git-Url: http://nitlanguage.org diff --git a/lib/standard/abstract_collection.nit b/lib/standard/abstract_collection.nit index ee2acfb..10fafb7 100644 --- a/lib/standard/abstract_collection.nit +++ b/lib/standard/abstract_collection.nit @@ -38,7 +38,7 @@ import kernel # # This abstract class implements its others methods with an iterator. # Subclasses may redefine them with an efficient implementation. -class Collection[E] +interface Collection[E] # Get a new iterator on the collection. meth iterator: Iterator[E] is abstract @@ -67,7 +67,7 @@ end # Naive implementation of collections method # You only have to define iterator! -class NaiveCollection[E: Object] +interface NaiveCollection[E] special Collection[E] redef meth is_empty do return length == 0 @@ -106,7 +106,7 @@ end # Instances of the Iterator class generates a series of elements, one at a time. # They are mainly used with collections. -class Iterator[E] +interface Iterator[E] # The current item. # Require `is_ok'. meth item: E is abstract @@ -166,7 +166,7 @@ special Iterator[E] end # Items can be removed from this collection -class RemovableCollection[E] +interface RemovableCollection[E] special Collection[E] # Remove all items meth clear is abstract @@ -179,14 +179,14 @@ special Collection[E] end # Items can be added to these collections. -class SimpleCollection[E] +interface SimpleCollection[E] special RemovableCollection[E] # Add an item in a collection. # Ensure col.has(item) 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. @@ -198,7 +198,7 @@ end # ... # s.add(a) # s.has(b) # --> true -class Set[E] +interface Set[E] special SimpleCollection[E] redef meth has_only(item) @@ -227,6 +227,17 @@ special SimpleCollection[E] 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 []. @@ -239,17 +250,12 @@ end # map[u2] # -> v2 # map.has_key(u1) # -> true # map.has_key(u3) # -> false -class Map[K, E] +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 @@ -263,46 +269,64 @@ special RemovableCollection[E] i.next end end - - redef meth iterator: MapIterator[K, E] is abstract end # Iterators for Map. -class MapIterator[K, E] +interface MapIterator[K, E] special Iterator[E] # The key of the current item. 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. -class 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) @@ -322,7 +346,7 @@ special SimpleCollection[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 @@ -334,23 +358,10 @@ special SimpleCollection[E] # 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. -class IndexedIterator[E] +interface IndexedIterator[E] special MapIterator[Int, E] # The index of the current item. meth index: Int is abstract @@ -360,17 +371,17 @@ special MapIterator[Int, E] end # Associatives arrays that internally uses couples to represent each (key, value) pairs. -class CoupleMap[K, E] +interface CoupleMap[K, E] 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 @@ -386,7 +397,7 @@ class CoupleMapIterator[K, E] 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 @@ -395,7 +406,6 @@ special MapIterator[K, E] redef meth next do _iter.next - while _iter.is_ok and _iter.item == null do _iter.next end attr _iter: Iterator[Couple[K,E]]