mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-24 07:05:35 +00:00
Simplify usage of diffing API
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
#import "_ASCoreAnimationExtras.h"
|
||||
#import "ASDisplayNodeExtras.h"
|
||||
#import "ASEqualityHelpers.h"
|
||||
#import "NSArray+Diffing.h"
|
||||
|
||||
#import "ASInternalHelpers.h"
|
||||
#import "ASLayout.h"
|
||||
|
||||
@@ -13,6 +13,6 @@
|
||||
/**
|
||||
* Uses a bottom-up memoized longest common subsequence solution to identify differences. Runs in O(mn) complexity.
|
||||
*/
|
||||
- (void)asdk_diffWithArray:(NSArray *)array insertions:(NSMutableIndexSet **)insertions deletions:(NSMutableIndexSet **)deletions;
|
||||
- (void)asdk_diffWithArray:(NSArray *)array insertions:(NSIndexSet **)insertions deletions:(NSIndexSet **)deletions;
|
||||
|
||||
@end
|
||||
|
||||
@@ -10,28 +10,32 @@
|
||||
|
||||
@implementation NSArray (Diffing)
|
||||
|
||||
- (void)asdk_diffWithArray:(NSArray *)array insertions:(NSMutableIndexSet **)insertions deletions:(NSMutableIndexSet **)deletions
|
||||
- (void)asdk_diffWithArray:(NSArray *)array insertions:(NSIndexSet **)insertions deletions:(NSIndexSet **)deletions
|
||||
{
|
||||
NSIndexSet *commonIndexes = [self _asdk_commonIndexesWithArray:array];
|
||||
|
||||
if (insertions) {
|
||||
NSArray *commonObjects = [self objectsAtIndexes:commonIndexes];
|
||||
NSMutableIndexSet *insertionIndexes = [NSMutableIndexSet indexSet];
|
||||
for (NSInteger i = 0, j = 0; i < commonObjects.count || j < array.count;) {
|
||||
if (i < commonObjects.count && j < array.count && [commonObjects[i] isEqual:array[j]]) {
|
||||
i++; j++;
|
||||
} else {
|
||||
[*insertions addIndex:j];
|
||||
[insertionIndexes addIndex:j];
|
||||
j++;
|
||||
}
|
||||
}
|
||||
*insertions = insertionIndexes;
|
||||
}
|
||||
|
||||
if (deletions) {
|
||||
NSMutableIndexSet *deletionIndexes = [NSMutableIndexSet indexSet];
|
||||
for (NSInteger i = 0; i < self.count; i++) {
|
||||
if (![commonIndexes containsIndex:i]) {
|
||||
[*deletions addIndex:i];
|
||||
[deletionIndexes addIndex:i];
|
||||
}
|
||||
}
|
||||
*deletions = deletionIndexes;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -57,8 +57,7 @@
|
||||
];
|
||||
|
||||
for (NSArray *test in tests) {
|
||||
NSMutableIndexSet *insertions = [NSMutableIndexSet indexSet];
|
||||
NSMutableIndexSet *deletions = [NSMutableIndexSet indexSet];
|
||||
NSIndexSet *insertions, *deletions;
|
||||
[test[0] asdk_diffWithArray:test[1] insertions:&insertions deletions:&deletions];
|
||||
for (NSNumber *index in (NSArray *)test[2]) {
|
||||
XCTAssert([insertions containsIndex:[index integerValue]]);
|
||||
|
||||
Reference in New Issue
Block a user