lib/core: Improve performances of last_index_of_from
authorLucas Bajolet <r4pass@hotmail.com>
Fri, 25 Sep 2015 15:00:48 +0000 (11:00 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Fri, 25 Sep 2015 15:00:48 +0000 (11:00 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

lib/core/collection/abstract_collection.nit
lib/core/text/abstract_text.nit

index 5192797..b65db67 100644 (file)
@@ -833,18 +833,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.
index 63fb920..1fcb0d9 100644 (file)
@@ -146,15 +146,7 @@ abstract class Text
        # Returns -1 if not found
        #
        # DEPRECATED : Use self.chars.last_index_of_from instead
-       fun last_index_of_from(item: Char, pos: Int): Int
-       do
-               var iter = self.chars.reverse_iterator_from(pos)
-               while iter.is_ok do
-                       if iter.item == item then return iter.index
-                       iter.next
-               end
-               return -1
-       end
+       fun last_index_of_from(item: Char, pos: Int): Int do return chars.last_index_of_from(item, pos)
 
        # Gets an iterator on the chars of self
        #