-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathNSIndexSetSymDiffExtension.m
More file actions
46 lines (40 loc) · 1.22 KB
/
NSIndexSetSymDiffExtension.m
File metadata and controls
46 lines (40 loc) · 1.22 KB
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
//Copyright 2005 Dominic Yu. Some rights reserved.
//This work is licensed under the Creative Commons
//Attribution-NonCommercial-ShareAlike License. To view a copy of this
//license, visit http://creativecommons.org/licenses/by-nc-sa/2.0/ or send
//a letter to Creative Commons, 559 Nathan Abbott Way, Stanford,
//California 94305, USA.
// Created by d on 2005.06.13.
#import "NSIndexSetSymDiffExtension.h"
@implementation NSMutableIndexSet (SymDiffExtension)
- (void)symmetricDifference:(NSIndexSet *)setB
{
NSUInteger * restrict a, * restrict b, aCount, bCount, i, j;
aCount = self.count; i = 0;
bCount = setB.count; j = 0;
// some trivial cases
if (bCount == 0) return;
if (aCount == 0) {
[self addIndexes:setB];
return;
}
// now the meat
a = malloc(aCount*sizeof(NSUInteger)); if (!a) return;
b = malloc(bCount*sizeof(NSUInteger)); if (!b) { free(a); return; }
[self getIndexes:a maxCount:aCount inIndexRange:NULL];
[setB getIndexes:b maxCount:bCount inIndexRange:NULL];
while (i < aCount && j < bCount) {
if (a[i] == b[j]) {
[self removeIndex:a[i++]];
++j;
} else if (a[i] < b[j]) {
++i;
} else {
[self addIndex:b[j++]];
}
}
while (j < bCount) [self addIndex:b[j++]];
free(a);
free(b);
}
@end