Diffing Arrays in ObjC
poop
void DiffArrays(NSMutableArray *newArray, NSMutableArray *oldArray, NSIndexSet **outAddedIndexes, NSIndexSet **outRemovedIndexes) {
NSMutableIndexSet *addedIndexes = [NSMutableIndexSet indexSet];
NSMutableIndexSet *removedIndexes = [NSMutableIndexSet indexSet];
NSInteger newIndex = 0;
for (NSInteger oldIndex = 0; oldIndex < [oldArray count]; oldIndex++) {
id oldValue = oldArray[oldIndex];
NSInteger foundMatch = NSNotFound;
for (NSInteger i = newIndex; i < [newArray count]; i++) {
if ([oldValue updateWithItem:newArray[i]]) {
foundMatch = i;
break;
}
}
if (foundMatch != NSNotFound) {
if (foundMatch != newIndex) {
[addedIndexes addIndexesInRange:NSMakeRange(foundMatch, newIndex - foundMatch)];
}
newIndex = foundMatch + 1;
} else {
[removedIndexes addIndex:oldIndex];
}
}
if (newIndex < [newArray count]) {
[addedIndexes addIndexesInRange:NSMakeRange(newIndex, [newArray count] - newIndex)];
}
if (outAddedIndexes) {
*outAddedIndexes = addedIndexes;
}
if (outRemovedIndexes) {
*outRemovedIndexes = removedIndexes;
}
}