lib: keys in maps and sets are not nullable
authorJean Privat <jean@pryen.org>
Mon, 26 Jul 2010 20:40:39 +0000 (16:40 -0400)
committerJean Privat <jean@pryen.org>
Fri, 6 Aug 2010 16:47:10 +0000 (12:47 -0400)
This removes some unsafe code and prepare for a potential strictier nullabe type
implementation.

Signed-off-by: Jean Privat <jean@pryen.org>

# Conflicts:
#
#  lib/standard/collection/hash_collection.nit

lib/standard/collection/abstract_collection.nit
lib/standard/collection/array.nit
lib/standard/collection/hash_collection.nit
src/metamodel/partial_order.nit

index 926585c..a94c7b6 100644 (file)
@@ -209,7 +209,7 @@ end
 #    ...
 #    s.add(a)
 #    s.has(b) # --> true
-interface Set[E]
+interface Set[E: Object]
 special SimpleCollection[E]
 
        redef fun has_only(item)
@@ -238,7 +238,7 @@ special SimpleCollection[E]
        redef fun remove_all(item) do remove(item)
 end
 
-interface MapRead[K, E]
+interface MapRead[K: Object, E]
 special Collection[E]
        # Get the item at `key'.
        fun [](key: K): E is abstract
@@ -261,7 +261,7 @@ end
 #     map[u2]            # -> v2
 #     map.has_key(u1)    # -> true
 #     map.has_key(u3)    # -> false
-interface Map[K, E]
+interface Map[K: Object, E]
 special RemovableCollection[E]
 special MapRead[K, E]
        # Set the`item' at `key'.
@@ -283,7 +283,7 @@ special MapRead[K, E]
 end
 
 # Iterators for Map.
-interface MapIterator[K, E]
+interface MapIterator[K: Object, E]
 special Iterator[E]
        # The key of the current item.
        fun key: K is abstract
@@ -382,7 +382,7 @@ special MapIterator[Int, E]
 end
 
 # Associatives arrays that internally uses couples to represent each (key, value) pairs.
-interface CoupleMap[K, E]
+interface CoupleMap[K: Object, E]
 special Map[K, E]
        # Return the couple of the corresponding key
        # Return null if the key is no associated element
@@ -404,7 +404,7 @@ end
 # Iterator on CoupleMap
 #
 # Actually is is a wrapper around an iterator of the internal array of the map.
-class CoupleMapIterator[K, E]
+class CoupleMapIterator[K: Object, E]
 special MapIterator[K, E]
        redef fun item do return _iter.item.second
        
index 19aa65d..86357ae 100644 (file)
@@ -413,7 +413,7 @@ end
 # Others collections ##########################################################
 
 # A set implemented with an Array.
-class ArraySet[E]
+class ArraySet[E: Object]
 special Set[E]
        # The stored elements.
        var _array: Array[E]
@@ -461,7 +461,7 @@ special Set[E]
 end
 
 # Iterators on sets implemented with arrays.
-class ArraySetIterator[E]
+class ArraySetIterator[E: Object]
 special Iterator[E]
 
        redef fun is_ok do return _iter.is_ok
@@ -477,7 +477,7 @@ end
 
 
 # Associative arrays implemented with an array of (key, value) pairs.
-class ArrayMap[K, E]
+class ArrayMap[K: Object, E]
 special CoupleMap[K, E]
 
        # O(n)
index 66a301d..e30f7e5 100644 (file)
@@ -184,7 +184,7 @@ special ArrayCapable[nullable N]
        end
 end
 
-private class HashNode[K]
+private class HashNode[K: Object]
        var _key: K
        type N: HashNode[K]
        readable writable var _next_item: nullable N = null
@@ -197,7 +197,7 @@ private class HashNode[K]
        end
 end
 
-class HashMap[K, V]
+class HashMap[K: Object, V]
 special Map[K, V]
 special HashCollection[K, HashMapNode[K, V], V]
 
@@ -266,7 +266,6 @@ special HashCollection[K, HashMapNode[K, V], V]
 
        redef fun []=(key, v)
        do
-               assert key != null
                var i = index_at(key)
                var c = node_at_idx(i, key)
                if c != null then
@@ -301,7 +300,7 @@ special HashCollection[K, HashMapNode[K, V], V]
        end
 end
 
-class HashMapNode[K, V]
+class HashMapNode[K: Object, V]
 special HashNode[K]
        redef type N: HashMapNode[K, V]
        var _value: V
@@ -313,7 +312,7 @@ special HashNode[K]
        end
 end
 
-class HashMapIterator[K, V]
+class HashMapIterator[K: Object, V]
 special MapIterator[K, V]
        redef fun is_ok do return _node != null
 
@@ -354,7 +353,7 @@ special MapIterator[K, V]
        end
 end
 
-class HashSet[E]
+class HashSet[E: Object]
 special Set[E]
 special HashCollection[E, HashSetNode[E], E]
 
@@ -396,7 +395,7 @@ special HashCollection[E, HashSetNode[E], E]
        end
 end
 
-class HashSetNode[E]
+class HashSetNode[E: Object]
 special HashNode[E]
        redef type N: HashSetNode[E]
 
@@ -406,7 +405,7 @@ special HashNode[E]
        end
 end
 
-class HashSetIterator[E]
+class HashSetIterator[E: Object]
 special Iterator[E]
        redef fun is_ok do return _node != null
 
index 9fe3ba3..fd9a8bb 100644 (file)
@@ -19,7 +19,7 @@ package partial_order
 
 # Handles partial ordered sets (ie hierarchies)
 # Thez are built by adding new element at the bottom of the hierarchy
-class PartialOrder[E]
+class PartialOrder[E: Object]
 special Collection[E]
        # Elements
        var _elements: Map[E, PartialOrderElement[E]]
@@ -186,7 +186,7 @@ special Collection[E]
        end
 end
 
-class PartialOrderElement[E]
+class PartialOrderElement[E: Object]
        # The partial order where belong self
        readable var _order: PartialOrder[E]