PageRenderTime 109ms CodeModel.GetById 83ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 1ms

/BlocksKit/NSIndexSet+BlocksKit.m

http://github.com/zwaldowski/BlocksKit
Objective C | 75 lines | 49 code | 22 blank | 4 comment | 9 complexity | d220b9e8ebb24c1f53bcd7527f3f353e MD5 | raw file
 1//
 2//  NSIndexSet+BlocksKit.m
 3//  BlocksKit
 4//
 5
 6#import "NSIndexSet+BlocksKit.h"
 7
 8@implementation NSIndexSet (BlocksKit)
 9
10- (void)each:(BKIndexBlock)block {
11	NSParameterAssert(block != nil);
12	
13	[self enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
14		block(idx);
15	}];
16}
17
18- (void)apply:(BKIndexBlock)block {
19	NSParameterAssert(block != nil);
20	
21	[self enumerateIndexesWithOptions:NSEnumerationConcurrent usingBlock:^(NSUInteger idx, BOOL *stop) {
22		block(idx);
23	}];
24}
25
26- (NSUInteger)match:(BKIndexValidationBlock)block {
27	NSParameterAssert(block != nil);
28	
29	return [self indexPassingTest:^BOOL(NSUInteger idx, BOOL *stop) {
30		return block(idx);
31	}];
32}
33
34- (NSIndexSet *)select:(BKIndexValidationBlock)block {
35	NSParameterAssert(block != nil);
36	
37	NSIndexSet *list = [self indexesPassingTest:^BOOL(NSUInteger idx, BOOL *stop) {
38		return block(idx);
39	}];
40	
41	if (!list.count)
42		return nil;
43	
44	return list;
45}
46
47- (NSIndexSet *)reject:(BKIndexValidationBlock)block {  
48	NSParameterAssert(block != nil);
49
50	NSIndexSet *list = [self indexesPassingTest:^BOOL(NSUInteger idx, BOOL *stop) {
51		return !block(idx);
52	}];
53	
54	if (!list.count)
55		return nil;
56	
57	return list;	 
58}
59
60- (NSIndexSet *)map:(BKIndexTransformBlock)block {
61	NSParameterAssert(block != nil);
62	
63	NSMutableIndexSet *list = [NSMutableIndexSet indexSet];
64	
65	[self enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
66		[list addIndex:block(idx)];
67	}];
68	
69	if (!list.count)
70		return nil;
71	
72	return list;
73}
74
75@end