Skip to content
This repository has been archived by the owner on Jun 2, 2018. It is now read-only.

Commit

Permalink
Introduce the -apply: method for all collections. Fixes issue #14.
Browse files Browse the repository at this point in the history
-apply: is remarkably similar to -each:, with the primary difference being that enumeration is concurrent under -apply: instead of sequential. This means a noticeable speed increase, but also increased danger for the programmer. Use wisely.
Signed-off-by: Zachary Waldowski <zwaldowski@gmail.com>
  • Loading branch information
zwaldowski committed Dec 20, 2011
1 parent 5438dc7 commit ad5620e
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 1 deletion.
14 changes: 14 additions & 0 deletions BlocksKit/NSArray+BlocksKit.h
Expand Up @@ -30,6 +30,20 @@
*/
- (void)each:(BKSenderBlock)block;

/** Enumerates through an array concurrently and executes
the given block once for each object.
Enumeration will occur on appropriate background queues. This
will have a noticeable speed increase, especially on dual-core
devices, but you *must* be aware of the thread safety of the
objects you message from within the block. Be aware that the
order of objects is not necessarily the order each block will
be called in.
@param block A single-argument, void-returning code block.
*/
- (void)apply:(BKSenderBlock)block;

/** Loops through an array to find the object matching the block.
match: is functionally identical to select:, but will stop and return
Expand Down
8 changes: 7 additions & 1 deletion BlocksKit/NSArray+BlocksKit.m
Expand Up @@ -8,11 +8,17 @@
@implementation NSArray (BlocksKit)

- (void)each:(BKSenderBlock)block {
[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
block(obj);
}];
}

- (void)apply:(BKSenderBlock)block {
[self enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
block(obj);
}];
}

- (id)match:(BKValidationBlock)block {
NSUInteger index = [self indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return block(obj);
Expand Down
13 changes: 13 additions & 0 deletions BlocksKit/NSDictionary+BlocksKit.h
Expand Up @@ -27,6 +27,19 @@
*/
- (void)each:(BKKeyValueBlock)block;

/** Enumerates through the dictionary concurrently and executes
the given block once for each pair.
Enumeration will occur on appropriate background queues;
the system will spawn threads as need for execution. This
will have a noticeable speed increase, especially on dual-core
devices, but you *must* be aware of the thread safety of the
objects you message from within the block.
@param block A block that performs an action using a key/value pair.
*/
- (void)apply:(BKKeyValueBlock)block;

/** Loops through a dictionary to find the key/value pairs matching the block.
@param block A BOOL-returning code block for a key/value pair.
Expand Down
6 changes: 6 additions & 0 deletions BlocksKit/NSDictionary+BlocksKit.m
Expand Up @@ -13,6 +13,12 @@ - (void)each:(BKKeyValueBlock)block {
}];
}

- (void)apply:(BKKeyValueBlock)block {
[self enumerateKeysAndObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id key, id obj, BOOL *stop) {
block(key, obj);
}];
}

- (NSDictionary *)select:(BKKeyValueValidationBlock)block {
NSMutableDictionary *list = [NSMutableDictionary dictionaryWithCapacity:self.count];

Expand Down
11 changes: 11 additions & 0 deletions BlocksKit/NSIndexSet+BlocksKit.h
Expand Up @@ -30,6 +30,17 @@
*/
- (void)each:(BKIndexBlock)block;

/** Enumerates each index in an index set concurrently and executes the
given block once per index.
Enumeration will occur on appropriate background queues.
Be aware that the block will not necessarily be executed
in order for each index.
@param block A single-argument, void-returning code block.
*/
- (void)apply:(BKIndexBlock)block;

/** Loops through an array and returns the index matching the block.
@param block A single-argument, `BOOL`-returning code block.
Expand Down
6 changes: 6 additions & 0 deletions BlocksKit/NSIndexSet+BlocksKit.m
Expand Up @@ -13,6 +13,12 @@ - (void)each:(BKIndexBlock)block {
}];
}

- (void)apply:(BKIndexBlock)block {
[self enumerateIndexesWithOptions:NSEnumerationConcurrent usingBlock:^(NSUInteger idx, BOOL *stop) {
block(idx);
}];
}

- (NSUInteger)match:(BKIndexValidationBlock)block {
return [self indexPassingTest:^BOOL(NSUInteger idx, BOOL *stop) {
return block(idx);
Expand Down
12 changes: 12 additions & 0 deletions BlocksKit/NSSet+BlocksKit.h
Expand Up @@ -30,6 +30,18 @@
*/
- (void)each:(BKSenderBlock)block;

/** Enumerates through a set concurrently and executes
the given block once for each object.
Enumeration will occur on appropriate background queues. This
will have a noticeable speed increase, especially on dual-core
devices, but you *must* be aware of the thread safety of the
objects you message from within the block.
@param block A single-argument, void-returning code block.
*/
- (void)apply:(BKSenderBlock)block;

/** Loops through a set to find the object matching the block.
match: is functionally identical to select:, but will stop and return
Expand Down
6 changes: 6 additions & 0 deletions BlocksKit/NSSet+BlocksKit.m
Expand Up @@ -13,6 +13,12 @@ - (void)each:(BKSenderBlock)block {
}];
}

- (void)apply:(BKSenderBlock)block {
[self enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id obj, BOOL *stop) {
block(obj);
}];
}

- (id)match:(BKValidationBlock)block {
return [[self objectsPassingTest:^BOOL(id obj, BOOL *stop) {
if (block(obj)) {
Expand Down

0 comments on commit ad5620e

Please sign in to comment.