core: fix typos in doc of CircularArray
[nit.git] / lib / core / collection / abstract_collection.nit
index e226eb6..04dabfa 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
@@ -379,7 +385,7 @@ end
 interface SimpleCollection[E]
        super RemovableCollection[E]
 
-       # Add an item in a collection.
+       # Add `item` to this collection.
        #
        #     var a = [1,2]
        #     a.add 3
@@ -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
 
@@ -851,6 +859,36 @@ interface SequenceRead[E]
                end
        end
 
+       # Try to get an element, return `null` if the `index` is invalid.
+       #
+       # ~~~
+       # var a = [10,20,30]
+       # assert a.get_or_null(1) == 20
+       # assert a.get_or_null(3) == null
+       # assert a.get_or_null(-1) == null
+       # assert a.get_or_null(-10) == null
+       # ~~~
+       fun get_or_null(index: Int): nullable E
+       do
+               if index >= 0 and index < length then return self[index]
+               return null
+       end
+
+       # Try to get an element, return `default` if the `index` is invalid.
+       #
+       # ~~~
+       # var a = [10,20,30]
+       # assert a.get_or_default(1, -1) == 20
+       # assert a.get_or_default(3, -1) == -1
+       # assert a.get_or_default(-1, -1) == -1
+       # assert a.get_or_default(-10, -1) == -1
+       # ~~~
+       fun get_or_default(index: Int, default: E): E
+       do
+               if index >= 0 and index < length then return self[index]
+               return default
+       end
+
        # Get the last item.
        # Is equivalent with `self[length-1]`.
        #
@@ -1157,6 +1195,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.