Skip to content

Commit

Permalink
Implement stopping support for CPArray enumerateObjectsUsingBlock:.
Browse files Browse the repository at this point in the history
  • Loading branch information
aljungberg committed Mar 23, 2012
1 parent e19a782 commit 3a00681
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
14 changes: 11 additions & 3 deletions Foundation/CPArray/CPArray.j
Original file line number Diff line number Diff line change
Expand Up @@ -536,13 +536,21 @@ var concat = Array.prototype.concat,
objj_msgSend([self objectAtIndex:index], aSelector);
}

- (void)enumerateObjectsUsingBlock:(Function)aFunction
- (void)enumerateObjectsUsingBlock:(Function /*(id anObject, int idx, @ref BOOL stop)*/)aFunction
{
// This could have been [self enumerateObjectsWithOptions:CPEnumerationNormal usingBlock:aFunction]
// but this method should be as fast as possible.
var index = 0,
count = [self count];
count = [self count],
shouldStop = NO,
shouldStopRef = AT_REF(shouldStop);

for (; index < count; ++index)
aFunction([self objectAtIndex:index], index);
{
aFunction([self objectAtIndex:index], index, shouldStopRef);
if (shouldStop)
return;
}
}

- (void)enumerateObjectsWithOptions:(CPEnumerationOptions)options usingBlock:(Function /*(id anObject, int idx, @ref BOOL stop)*/)aFunction
Expand Down
14 changes: 14 additions & 0 deletions Tests/Foundation/CPArrayTest.j
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,22 @@
[self assert:input1[0] equals:[output valueForKey:"0"] message:@"output[0]"];
[self assert:input1[1] equals:[output valueForKey:"1"] message:@"output[0]"];
[self assert:input1[2] equals:[output valueForKey:"2"] message:@"output[0]"];

stoppingFunction = function(anObject, idx, stop)
{
[output setValue:anObject forKey:"" + idx];
if ([output count] > 1)
stop(YES); // AT_DEREF(stop, YES) - FIXME Replace with proper @ref @deref when in ObjJ.
}
output = [CPMutableDictionary dictionary];

[input1 enumerateObjectsUsingBlock:stoppingFunction];
[self assert:2 equals:[output count] message:@"output when enumerating input1 and stopping after 2"];
[self assert:input1[0] equals:[output valueForKey:"0"] message:@"output[0]"];
[self assert:input1[1] equals:[output valueForKey:"1"] message:@"output[0]"];
}


@end

@implementation AlwaysEqual : CPObject
Expand Down

0 comments on commit 3a00681

Please sign in to comment.