-
Notifications
You must be signed in to change notification settings - Fork 41
/
NSArray+SSYDisjoint.m
60 lines (50 loc) · 1.72 KB
/
NSArray+SSYDisjoint.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#import "NSArray+SSYDisjoint.h"
@interface SSYDisjoiningPlaceholder : NSObject
@end
@implementation SSYDisjoiningPlaceholder
@end
@implementation NSMutableArray (SSYDisjoint)
- (void)putObject:(id)object
atIndex:(NSUInteger)index {
NSInteger count = [self count] ;
if (index < count) {
[self replaceObjectAtIndex:index
withObject:object] ;
}
else {
if (index > count) {
SSYDisjoiningPlaceholder* placeholder = [[SSYDisjoiningPlaceholder alloc] init] ;
for (NSInteger i = [self count] ; i < index ; i++) {
[self addObject:placeholder] ;
}
[placeholder release] ;
}
[self addObject:object] ;
}
}
- (void)cleanObjectAtIndex:(NSInteger)index {
SSYDisjoiningPlaceholder* placeholder = [[SSYDisjoiningPlaceholder alloc] init] ;
[self replaceObjectAtIndex:index
withObject:placeholder] ;
[placeholder release] ;
NSInteger count = [self count] ;
NSInteger lastIndexToKeep = count - 1 ;
for ( ; lastIndexToKeep>=0; lastIndexToKeep--) {
if (![[self objectAtIndex:lastIndexToKeep] isKindOfClass:[SSYDisjoiningPlaceholder class]]) {
break ;
}
}
NSInteger lengthToRemove = count - lastIndexToKeep - 1;
if ((lengthToRemove > 0) && (lastIndexToKeep < count - 1)) {
NSInteger firstIndexToRemove = lastIndexToKeep + 1 ;
NSRange range = NSMakeRange(firstIndexToRemove, lengthToRemove) ;
[self removeObjectsInRange:range] ;
}
}
- (void)cleanObject:(id)object {
NSInteger index = [self indexOfObject:object] ;
if (index != NSNotFound) {
[self cleanObjectAtIndex:index] ;
}
}
@end