Skip to content

Commit

Permalink
Possible to add duplicates during enumeration - rewriting to be a bit…
Browse files Browse the repository at this point in the history
… clearer
  • Loading branch information
iccir committed Jan 30, 2012
1 parent 40dd67e commit 37a19ac
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions Source/SwiffSparseArray.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,30 @@ @implementation SwiffSparseArray

- (NSUInteger) countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)count
{
NSInteger highByte = state->state >> 8;
NSInteger lowByte = state->state & 0xFF;

NSUInteger i = 0;

for ( ; highByte < 256; highByte++) {
SwiffSparseArrayBucket *bucket = m_buckets[highByte];

if (bucket) {
for ( ; lowByte < 256; lowByte++) {
id object = bucket->m_objects[lowByte];
if (object) {
buffer[i++] = object;
if (i == count) goto end;
}
}
}
NSUInteger i = state->state;
NSUInteger o = 0;

while ((o < count) && (i < 65536)) {
id object = SwiffSparseArrayGetObjectAtIndex(self, i);

if (highByte != 255) {
lowByte = 0;
if (object) {
buffer[o++] = object;

// Did we fail because we have no bucket?
// If so, skip over the entire non-existant bucket
} else if (!m_buckets[i >> 8]) {
i += 256;
continue;
}

i++;
}

end:
state->state = (highByte << 8) | lowByte;
state->state = i;
state->itemsPtr = buffer;
state->mutationsPtr = &state->extra[0];

return i;
return o;
}


Expand Down

0 comments on commit 37a19ac

Please sign in to comment.