Skip to content

Instantly share code, notes, and snippets.

@rnystrom
Created December 19, 2016 19:17
Show Gist options
  • Select an option

  • Save rnystrom/5c1e200b4ec9185bdbe84daa5f9fce0b to your computer and use it in GitHub Desktop.

Select an option

Save rnystrom/5c1e200b4ec9185bdbe84daa5f9fce0b to your computer and use it in GitHub Desktop.
// iterate newResultsArray in ascending order
// if na[i] == oa[j] and na[i+1] == oa[j+1], assign them to the opposite index+1
// pass 4 is for ascending and 5 descending, block covers logic for both
void (^pass4And5Block)(BOOL, vector<IGListRecord>&, vector<IGListRecord>&) = ^(BOOL ascending, vector<IGListRecord> &newResults, vector<IGListRecord> &oldResults) {
const NSInteger offset = ascending ? 1 : -1;
for (NSInteger i = ascending ? 0 : newCount - 1;
(ascending && i < newCount - 1) || (!ascending && i > 0);
ascending ? i++ : i--) {
// don't check block moves for new elements
if (newResults[i].index == NSNotFound) {
continue;
}
const NSInteger nj = newResults[i].index + offset;
const NSInteger ni = i + offset;
const IGListRecord next = newResults[ni];
const IGListEntry *entry = next.entry;
// if we are comparing the same entry, it hasn't been updated, and its identity count hasn't changed
// we can mark the record as part of a block so it isn't counted as an insert/delete
if (nj < oldCount
&& nj > 0
&& entry == oldResults[nj].entry
&& !entry->updated
&& entry->oldCounter == entry->newCounter) {
oldResults[nj].index = ni;
newResults[ni].index = nj;
}
}
};
// pass 4
pass4And5Block(YES, newResultsArray, oldResultsArray);
// pass 5
pass4And5Block(NO, newResultsArray, oldResultsArray);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment