From: Lucas Bajolet Date: Fri, 25 Sep 2015 15:00:48 +0000 (-0400) Subject: lib/core: Improve performances of last_index_of_from X-Git-Tag: v0.7.8~1^2~3 X-Git-Url: http://nitlanguage.org lib/core: Improve performances of last_index_of_from Signed-off-by: Lucas Bajolet --- diff --git a/lib/core/collection/abstract_collection.nit b/lib/core/collection/abstract_collection.nit index 5192797..b65db67 100644 --- a/lib/core/collection/abstract_collection.nit +++ b/lib/core/collection/abstract_collection.nit @@ -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. diff --git a/lib/core/text/abstract_text.nit b/lib/core/text/abstract_text.nit index 63fb920..1fcb0d9 100644 --- a/lib/core/text/abstract_text.nit +++ b/lib/core/text/abstract_text.nit @@ -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 #