Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enumerate NSArray starting at givven index searching both ways (no wrap around)

Example. I've got an array with 15 objects. I want to start enumerating from a given index. Say start at index 5 and then the index above, the index under, above, under etc... I don't want it to wrap around, but rather stop and continue in unexplored direction.

So the order of indexes in my example would be. 5, 6, 4, 7, 3, 8, 2, 9, 1, 10, 0, 11, 12, 13, 14

How can this be done?

like image 933
hfossli Avatar asked Jan 15 '13 15:01

hfossli


2 Answers

Here's a more compact implementation that doesn't require creating subarrays:

@implementation NSArray (Extensions)

- (void)enumerateFromIndex:(NSUInteger)index goBothWaysUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block
{
    BOOL stop = NO;
    for (NSUInteger i = 0; i < self.count && !stop; i++) {
        if (index + i < self.count) {
            block([self objectAtIndex:index + i], index + i, &stop);
        }
        if (i != 0 && !stop && i <= index) {
            block([self objectAtIndex:index - i], index - i, &stop);
        }
    }
}

@end
like image 99
omz Avatar answered Nov 15 '22 17:11

omz


-(void)enumerateArray:(NSArray *)array inBothDirectionsFromIndex:(int)startIndex
{
    for (int i=0; i<array.count; i++)
    {
        int index = startIndex;
        int indexAfter = startIndex + round(i/2.f) + (i%2 ? 0 : 1);
        int indexBefore = startIndex - round(i/2.f);

        if ((i%2 && indexAfter < array.count) || indexBefore < 0)
        {
            index = indexAfter;
            if (indexBefore < 0)
                index -= indexBefore + 1;
        }
        else if ((i > 0 && indexBefore > -1) || indexAfter > array.count-1)
        {
            index = indexBefore;
            if (indexAfter > array.count-1)
                index -= indexAfter - array.count;;
        }

        id item = [array objectAtIndex:index];

        //Do what you want with the item here

    }
}
like image 21
Basem Saadawy Avatar answered Nov 15 '22 17:11

Basem Saadawy