From 8a61b9a1a0dec7a11eba92e255dce86f0df3f5c7 Mon Sep 17 00:00:00 2001 From: Aparajita Fishman Date: Fri, 23 Jul 2010 12:13:49 -0400 Subject: [PATCH] Added -indexOfObjectPassingTest: and related methods --- Foundation/CPArray.j | 57 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/Foundation/CPArray.j b/Foundation/CPArray.j index 27acbd1f2..d3abf2b14 100755 --- a/Foundation/CPArray.j +++ b/Foundation/CPArray.j @@ -397,6 +397,63 @@ return CPNotFound; } +- (unsigned)indexOfObjectPassingTest:(Function)predicate +{ + return [self indexOfObjectWithOptions:CPEnumerationNormal passingTest:predicate context:nil]; +} + +- (unsigned)indexOfObjectPassingTest:(Function)predicate context:(id)aContext +{ + return [self indexOfObjectWithOptions:CPEnumerationNormal passingTest:predicate context:aContext]; +} + +- (unsigned)indexOfObjectWithOptions:(CPEnumerationOptions)opts passingTest:(Function)predicate +{ + return [self indexOfObjectWithOptions:opts passingTest:predicate context:nil]; +} + +- (unsigned)indexOfObjectWithOptions:(CPEnumerationOptions)opts passingTest:(Function)predicate context:(id)aContext +{ + // We don't use an enumerator because they return nil to indicate end of enumeration, + // but nil may actually be the value we are looking for, so we have to loop over the array. + + var start, stop, increment; + + if (opts & CPEnumerationReverse) + { + start = [self count] - 1; + stop = -1; + increment = -1; + } + else + { + start = 0; + stop = [self count]; + increment = 1; + } + + for (var i = start; i != stop; i += increment) + { + var result = predicate([self objectAtIndex:i], i, aContext); + + if (typeof result === 'boolean' && result) + { + return i; + } + else if (typeof result === 'object') + { + if (result == nil) + continue + else if (result.pass) + return i; + else if (result.stop) + return CPNotFound; + } + } + + return CPNotFound; +} + /*! Returns the index of \c anObject in the array, which must be sorted in the same order as calling sortUsingSelector: with the selector passed to this method would result in.