Merge: Added contributing guidelines and link from readme
[nit.git] / lib / core / collection / abstract_collection.nit
index 5f9af05..5ed911f 100644 (file)
@@ -149,6 +149,12 @@ interface Collection[E]
        # It is memory-efficient but relies on `has` so may be CPU-inefficient for some kind of collections.
        fun has_all(other: Collection[nullable Object]): Bool
        do
+               if is_same_instance(other) then return true
+               var ol = other.length
+               var  l = length
+               if ol == 0 then return true
+               if l == 0 then return false
+               if ol == 1 then return has(other.first)
                for x in other do if not has(x) then return false
                return true
        end
@@ -451,7 +457,9 @@ interface Set[E]
                var res = 23 + length
                # Note: the order of the elements must not change the hash value.
                # So, unlike usual hash functions, the accumulator is not combined with itself.
-               for e in self do res += e.hash
+               for e in self do
+                       if e != null then res += e.hash
+               end
                return res
        end
 
@@ -680,19 +688,17 @@ interface Map[K, V]
        # Add each (key,value) of `map` into `self`.
        # If a same key exists in `map` and `self`, then the value in self is discarded.
        #
-       # It is the analogous of `SimpleCollection::add_all`
-       #
        #     var x = new HashMap[String, Int]
        #     x["four"] = 4
        #     x["five"] = 5
        #     var y = new HashMap[String, Int]
        #     y["four"] = 40
        #     y["nine"] = 90
-       #     x.recover_with y
+       #     x.add_all y
        #     assert x["four"]  == 40
        #     assert x["five"]  == 5
        #     assert x["nine"]  == 90
-       fun recover_with(map: MapRead[K, V])
+       fun add_all(map: MapRead[K, V])
        do
                var i = map.iterator
                while i.is_ok do
@@ -701,6 +707,9 @@ interface Map[K, V]
                end
        end
 
+       # Alias for `add_all`
+       fun recover_with(map: MapRead[K, V]) is deprecated do add_all(map)
+
        # Remove all items
        #
        #     var x = new HashMap[String, Int]
@@ -1156,6 +1165,30 @@ interface Sequence[E]
        #
        # REQUIRE `index >= 0 and index < length`
        fun remove_at(index: Int) is abstract
+
+       # Rotates the elements of self once to the left
+       #
+       # ~~~nit
+       # var a = [12, 23, 34, 45]
+       # a.rotate_left
+       # assert a == [23, 34, 45, 12]
+       # ~~~
+       fun rotate_left do
+               var fst = shift
+               push fst
+       end
+
+       # Rotates the elements of self once to the right
+       #
+       # ~~~nit
+       # var a = [12, 23, 34, 45]
+       # a.rotate_right
+       # assert a == [45, 12, 23, 34]
+       # ~~~
+       fun rotate_right do
+               var lst = pop
+               unshift lst
+       end
 end
 
 # Iterators on indexed collections.