lib/coll: move *index_of* in Sequence
authorJean Privat <jean@pryen.org>
Thu, 20 Mar 2014 09:04:07 +0000 (05:04 -0400)
committerJean Privat <jean@pryen.org>
Thu, 20 Mar 2014 20:30:03 +0000 (16:30 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

lib/standard/collection/abstract_collection.nit
lib/standard/collection/array.nit

index 8b72d85..d08560b 100644 (file)
@@ -571,23 +571,66 @@ interface SequenceRead[E]
                return self[length-1]
        end
 
-       # Return the index of the first occurrence of `item`.
-       # Return -1 if `item` is not found
-       # Comparison is done with ==
+       # The index of the first occurrence of `item`.
+       # Return -1 if `item` is not found.
+       # Comparison is done with `==`.
        #
        #     var a = [10,20,30,10,20,30]
        #     assert a.index_of(20)   == 1
        #     assert a.index_of(40)   == -1
-       fun index_of(item: E): Int
+       fun index_of(item: E): Int do return index_of_from(item, 0)
+
+       # The index of the last occurrence of `item`.
+       # Return -1 if `item` is not found.
+       # Comparison is done with `==`.
+       #
+       #     var a = [10,20,30,10,20,30]
+       #     assert a.last_index_of(20)   == 4
+       #     assert a.last_index_of(40)   == -1
+       fun last_index_of(item: E): Int do return last_index_of_from(item, length-1)
+
+       # The index of the first occurrence of `item`, starting from pos.
+       # Return -1 if `item` is not found.
+       # Comparison is done with `==`.
+       #
+       #     var a = [10,20,30,10,20,30]
+       #     assert a.index_of_from(20, 3)   == 4
+       #     assert a.index_of_from(20, 4)   == 4
+       #     assert a.index_of_from(20, 5)   == -1
+       fun index_of_from(item: E, pos: Int): Int
        do
+               var p = 0
                var i = iterator
                while i.is_ok do
-                       if i.item == item then return i.index
+                       if p>pos and i.item == item then return i.index
                        i.next
+                       p += 1
                end
                return -1
        end
 
+       # The index of the last occurrence of `item` starting from `pos` and decrementing.
+       # Return -1 if `item` is not found.
+       # Comparison is done with `==`.
+       #
+       #     var a = [10,20,30,10,20,30]
+       #     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: E, 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
+               end
+               return res
+       end
+
        redef fun iterator: IndexedIterator[E] is abstract
 
        # Two sequences are equals if they have the same items in the same order.
index 558f7c1..e8e814c 100644 (file)
@@ -61,13 +61,9 @@ abstract class AbstractArrayRead[E]
 
        redef fun index_of(item) do return index_of_from(item, 0)
 
-       # The index of the last occurrence of an element.
-       # Return -1 if not found.
-       fun last_index_of(item: E): Int do return last_index_of_from(item, length-1)
+       redef fun last_index_of(item: E): Int do return last_index_of_from(item, length-1)
 
-       # The index of the first occurrence of an element starting from pos.
-       # Return -1 if not found.
-       fun index_of_from(item: E, pos: Int): Int
+       redef fun index_of_from(item: E, pos: Int): Int
        do
                var i = pos
                var len = length
@@ -80,9 +76,7 @@ abstract class AbstractArrayRead[E]
                return -1
        end
 
-       # The index of the first occurrence of an element starting from pos, by decremanting the index
-       # Return -1 if not found.
-       fun last_index_of_from(item: E, pos: Int): Int
+       redef fun last_index_of_from(item: E, pos: Int): Int
        do
                var i = pos
                while i >= 0 do