From 4004ca9491c5a4b49b97440aa36f6f1070488b0e Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Mon, 24 Sep 2012 11:36:57 +0000 Subject: Implement fast enumeration for PostgresKit's result class. Based on SPMySQL's implementation by Rowan. --- Frameworks/PostgresKit/Source/FLXPostgresResult.m | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'Frameworks/PostgresKit/Source/FLXPostgresResult.m') diff --git a/Frameworks/PostgresKit/Source/FLXPostgresResult.m b/Frameworks/PostgresKit/Source/FLXPostgresResult.m index 06950a3b..02375650 100644 --- a/Frameworks/PostgresKit/Source/FLXPostgresResult.m +++ b/Frameworks/PostgresKit/Source/FLXPostgresResult.m @@ -174,6 +174,36 @@ return data; } +#pragma mark - +#pragma mark Fast enumeration implementation + +- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len +{ + if (state->state >= _numberOfRows) return 0; + + if (state->state != _row) [self seekToRow:state->state]; + + // Determine how many objects to return - 128, len, or all items remaining + NSUInteger itemsToReturn = 128; + + if (len < 128) itemsToReturn = len; + + if (_numberOfRows - state->state < itemsToReturn) { + itemsToReturn = (unsigned long)_numberOfRows - state->state; + } + + for (NSUInteger i = 0; i < itemsToReturn; i++) + { + stackbuf[i] = [self rowAsType:_defaultRowType]; + } + + state->state += itemsToReturn; + state->itemsPtr = stackbuf; + state->mutationsPtr = (unsigned long *)self; + + return itemsToReturn; +} + #pragma mark - #pragma mark Private API -- cgit v1.2.3