forked from gobbledegook/creevey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSIndexSetSymDiffExtension.m
75 lines (67 loc) · 2.01 KB
/
NSIndexSetSymDiffExtension.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//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.
// NSIndexSetSymDiffExtension.m
// Created by d on 2005.06.13.
#import "NSIndexSetSymDiffExtension.h"
@implementation NSMutableIndexSet (SymDiffExtension)
/*
- (NSIndexSet *)indexSetWithSymmetricDifference:(NSIndexSet *)setB
{
unsigned int *a, *b, aCount, bCount, i, j;
NSRange r;
NSMutableIndexSet *diffSet = [NSMutableIndexSet indexSet];
aCount = [self count]; i = 0;
bCount = [setB count]; j = 0;
a = malloc(aCount*sizeof(unsigned int)); if (!a) return nil;
b = malloc(bCount*sizeof(unsigned int)); if (!b) { free(a); return nil; }
[self getIndexes:a maxCount:aCount inIndexRange:&r];
[setB getIndexes:b maxCount:bCount inIndexRange:&r];
while (i < aCount && j < bCount) {
if (a[i] == b[j]) {
++i; ++j;
} else if (a[i] < b[j]) {
[diffSet addIndex:a[i++]];
} else {
[diffSet addIndex:b[j++]];
}
}
while (i < aCount) [diffSet addIndex:a[i++]];
while (j < bCount) [diffSet addIndex:b[j++]];
return diffSet;
}
*/
- (void)symmetricDifference:(NSIndexSet *)setB
{
NSUInteger *a, *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