/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. #import "NSIndexSet+BlocksKit.h"
  6. @implementation NSIndexSet (BlocksKit)
  7. - (void)each:(BKIndexBlock)block {
  8. NSParameterAssert(block != nil);
  9. [self enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
  10. block(idx);
  11. }];
  12. }
  13. - (void)apply:(BKIndexBlock)block {
  14. NSParameterAssert(block != nil);
  15. [self enumerateIndexesWithOptions:NSEnumerationConcurrent usingBlock:^(NSUInteger idx, BOOL *stop) {
  16. block(idx);
  17. }];
  18. }
  19. - (NSUInteger)match:(BKIndexValidationBlock)block {
  20. NSParameterAssert(block != nil);
  21. return [self indexPassingTest:^BOOL(NSUInteger idx, BOOL *stop) {
  22. return block(idx);
  23. }];
  24. }
  25. - (NSIndexSet *)select:(BKIndexValidationBlock)block {
  26. NSParameterAssert(block != nil);
  27. NSIndexSet *list = [self indexesPassingTest:^BOOL(NSUInteger idx, BOOL *stop) {
  28. return block(idx);
  29. }];
  30. if (!list.count)
  31. return nil;
  32. return list;
  33. }
  34. - (NSIndexSet *)reject:(BKIndexValidationBlock)block {
  35. NSParameterAssert(block != nil);
  36. NSIndexSet *list = [self indexesPassingTest:^BOOL(NSUInteger idx, BOOL *stop) {
  37. return !block(idx);
  38. }];
  39. if (!list.count)
  40. return nil;
  41. return list;
  42. }
  43. - (NSIndexSet *)map:(BKIndexTransformBlock)block {
  44. NSParameterAssert(block != nil);
  45. NSMutableIndexSet *list = [NSMutableIndexSet indexSet];
  46. [self enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
  47. [list addIndex:block(idx)];
  48. }];
  49. if (!list.count)
  50. return nil;
  51. return list;
  52. }
  53. @end