From 62b293caa12a07104ed9d291fa7f819a304dfd1d Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Thu, 20 Mar 2014 05:04:07 -0400 Subject: [PATCH] lib/coll: move *index_of* in Sequence Signed-off-by: Jean Privat --- lib/standard/collection/abstract_collection.nit | 53 ++++++++++++++++++++--- lib/standard/collection/array.nit | 12 ++--- 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/lib/standard/collection/abstract_collection.nit b/lib/standard/collection/abstract_collection.nit index 8b72d85..d08560b 100644 --- a/lib/standard/collection/abstract_collection.nit +++ b/lib/standard/collection/abstract_collection.nit @@ -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. diff --git a/lib/standard/collection/array.nit b/lib/standard/collection/array.nit index 558f7c1..e8e814c 100644 --- a/lib/standard/collection/array.nit +++ b/lib/standard/collection/array.nit @@ -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 -- 1.7.9.5