-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathNSMutableArray+DYMovable.m
27 lines (25 loc) · 1.01 KB
/
NSMutableArray+DYMovable.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
#import "NSMutableArray+DYMovable.h"
@implementation NSMutableArray (DYMovable)
- (void)moveObjectAtIndex:(NSUInteger)fromIdx toIndex:(NSUInteger)toIdx {
id obj = [self objectAtIndex:fromIdx];
[self removeObjectAtIndex:fromIdx];
[self insertObject:obj atIndex:toIdx];
}
- (NSUInteger)updateIndexOfObject:(id)obj usingComparator:(NSComparator)cmp oldIndex:(NSUInteger *)outIdx {
NSUInteger idx = [self indexOfObject:obj]; // linear search to find object with outdated index
if (idx == NSNotFound) return NSNotFound;
if (outIdx) *outIdx = idx;
[self removeObjectAtIndex:idx];
idx = [self indexOfObject:obj inSortedRange:(NSRange){0,self.count} options:NSBinarySearchingInsertionIndex usingComparator:cmp];
[self insertObject:obj atIndex:idx];
return idx;
}
- (void)changeBase:(NSString *)basePath toPath:(NSString *)newBase {
NSUInteger i, n = self.count;
NSRange r = {0, basePath.length};
for (i=0; i<n; ++i) {
NSString *s = self[i];
self[i] = [s stringByReplacingCharactersInRange:r withString:newBase];
}
}
@end