Merge: introduce nit_env.sh to setup the shell environement
[nit.git] / lib / core / collection / abstract_collection.nit
index aca4fdc..ef10cfa 100644 (file)
@@ -177,6 +177,21 @@ interface Collection[E]
                for e in self do if self.count(e) != other.count(e) then return false
                return true
        end
+
+       # Does the collection contain at least one element of `other`?
+       #
+       #     assert [1,3,4,2].has_any([1..10])    == true
+       #     assert [1,3,4,2].has_any([5..10])    == false
+       #
+       # Note that the default implementation is general and correct for any lawful Collections.
+       # It is memory-efficient but relies on `has` so may be CPU-inefficient for some kind of collections.
+       fun has_any(other: Collection[nullable Object]): Bool
+       do
+               for o in other do
+                       if has(o) then return true
+               end
+               return false
+       end
 end
 
 # Iterators generate a series of elements, one at a time.
@@ -853,18 +868,13 @@ interface SequenceRead[E]
        #     assert a.last_index_of_from(20, 2)   == 1
        #     assert a.last_index_of_from(20, 1)   == 1
        #     assert a.last_index_of_from(20, 0)   == -1
-       fun last_index_of_from(item: nullable Object, pos: Int): Int
-       do
-               var res = -1
-               var p = 0
-               var i = iterator
-               while i.is_ok do
-                       if p>pos then break
-                       if i.item == item then res = p
-                       i.next
-                       p += 1
+       fun last_index_of_from(item: nullable Object, pos: Int): Int do
+               var i = pos
+               while i >= 0 do
+                       if self[i] == item then return i
+                       i -= 1
                end
-               return res
+               return -1
        end
 
        # Two sequences are equals if they have the same items in the same order.