-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathNSIndexSet+Extensions.m
43 lines (36 loc) · 1.12 KB
/
NSIndexSet+Extensions.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
#import "NSIndexSet+Extensions.h"
@implementation NSIndexSet (Extensions)
- (NSUInteger)positionOfIndex:(NSUInteger)index {
return [self countOfIndexesInRange:NSMakeRange(0, index+1)];
}
- (NSUInteger)indexAtPosition:(NSUInteger)position {
NSUInteger count = 0;
NSUInteger index = [self firstIndex];
while (index != NSNotFound) {
if (count == position) {
return index;
}
index = [self indexGreaterThanIndex:index];
count++;
}
return NSNotFound;
}
+ (NSIndexSet *)indexSetWithArray:(NSArray *)array {
NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet];
for (NSNumber *i in array) {
[indexes addIndex:[i intValue]];
}
return indexes;
}
- (NSIndexSet *)intersectionWithIndexSet:(NSIndexSet *)indexSet {
NSMutableIndexSet *intersection = [NSMutableIndexSet indexSet];
NSUInteger index = [self firstIndex];
while (index != NSNotFound) {
if ([indexSet containsIndex:index]) {
[intersection addIndex:index];
}
index = [self indexGreaterThanIndex:index];
}
return intersection;
}
@end