nullable: convert lib, tools and tests
[nit.git] / lib / standard / abstract_collection.nit
index a1ad3f8..10fafb7 100644 (file)
@@ -67,7 +67,7 @@ end
 
 # 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
 
@@ -186,7 +186,7 @@ special RemovableCollection[E]
        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.
@@ -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 [].
@@ -241,15 +252,10 @@ end
 #     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
 
@@ -263,8 +269,6 @@ special RemovableCollection[E]
                        i.next
                end
        end
-
-       redef meth iterator: MapIterator[K, E] is abstract
 end
 
 # Iterators for Map.
@@ -274,35 +278,55 @@ special Iterator[E]
        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) 
@@ -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,19 +358,6 @@ 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.
@@ -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]]