Skip to content

Instantly share code, notes, and snippets.

@skahack
Created July 12, 2012 06:50
Show Gist options
  • Save skahack/3096340 to your computer and use it in GitHub Desktop.
Save skahack/3096340 to your computer and use it in GitHub Desktop.
@interface Range : NSObject <NSFastEnumeration>
@property (readwrite, nonatomic, retain) NSNumber *start;
@property (readwrite, nonatomic, retain) NSNumber *end;
+ (Range *)createWithStart:(NSUInteger)start End:(NSUInteger)end;
- (id)initWithStart:(NSUInteger)start End:(NSUInteger)end;
@end
@implementation Range
@synthesize start = _start;
@synthesize end = _end;
+ (Range *)createWithStart:(NSUInteger)s End:(NSUInteger)e {
return [[[self alloc] initWithStart:s End:e] autorelease];
}
- (id)initWithStart:(NSUInteger)s End:(NSUInteger)e {
self = [super init];
if (!self) {
return nil;
}
self.start = [NSNumber numberWithInteger:s];
self.end = [NSNumber numberWithInteger:e];
return self;
}
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len {
NSInteger size = [_end intValue] - [_start intValue];
if (state->state >= size) {
return 0;
}
NSInteger count = 0;
while (state->state < size && count < len) {
stackbuf[count] = [NSNumber numberWithUnsignedLong:state->state];
state->state++;
count++;
}
state->itemsPtr = stackbuf;
state->mutationsPtr = (unsigned long*)self;
return count;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment