mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-11-26 00:15:52 +00:00
Added -indexOfObjectPassingTest: and related methods
This commit is contained in:
1 parent
fe2bc9f523
commit
27a20b54e8
1 file changed
+62
@@ -27,6 +27,11 @@
|
||||
@import "CPSortDescriptor.j"
|
||||
|
||||
|
||||
CPEnumerationNormal = 0;
|
||||
CPEnumerationConcurrent = 1 << 0;
|
||||
CPEnumerationReverse = 1 << 1;
|
||||
|
||||
|
||||
/* @ignore */
|
||||
@implementation _CPArrayEnumerator : CPEnumerator
|
||||
{
|
||||
@@ -397,6 +402,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.
|
||||
|
||||
Reference in new issue
Block a user