Skip to content

Instantly share code, notes, and snippets.

@hasanadil
Created August 14, 2015 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hasanadil/7c4734ab8fb0542c4d7f to your computer and use it in GitHub Desktop.
Save hasanadil/7c4734ab8fb0542c4d7f to your computer and use it in GitHub Desktop.
//Divide up the work among workers
NSUInteger numberOfWorkers = 4;
NSUInteger pixelsPerWorker = pixelCount/numberOfWorkers;
for (NSUInteger worker = 0; worker < numberOfWorkers; worker++) {
//Submit work blocks to be executed concurrently
dispatch_async(weakMe.concurrentQueue, ^{
//Add the colors result to a trie data strucuture whose leaf is the counter, O(1) baby
HATrie *trie = [[HATrie alloc] init];
//Get the pixels to work on
NSUInteger workerOffset = worker * pixelsPerWorker;
UInt32 * workerPixels;
workerPixels = (UInt32 *) calloc(pixelsPerWorker, sizeof(UInt32));
memcpy(workerPixels, pixels + workerOffset, pixelsPerWorker);
//Loop over the pixels of interest
UInt32 * currentPixel = workerPixels;
for (NSUInteger j = 0; j < pixelsPerWorker; j++) {
UInt32 color = *currentPixel;
float red = R(color);
float green = G(color);
float blue = B(color);
if ((red == 0 && green == 0 && blue == 0) || (red == 1 && green == 1 && blue == 1)) {
continue;
}
HAColorComponents *components = [[HAColorComponents alloc] initWithRed:red green:green blue:blue];
[trie addColorComponents:components];
currentPixel++;
}
HAColorComponentsCount *maxColorComponentsCount = trie.maxCountColorComponents;
[workerResults setObject:maxColorComponentsCount forKey:@(worker)];
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment