-
Notifications
You must be signed in to change notification settings - Fork 0
/
SWPointSet.m
164 lines (122 loc) · 4 KB
/
SWPointSet.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//
// SWPointSet.m
// This file is part of the "SWApplicationSupport" project, and is distributed under the MIT License.
//
// Created by Samuel Williams on 13/05/05.
// Copyright 2005 Samuel Williams. All rights reserved.
//
#import "SWPointSet.h"
@implementation NSIndexSet (SWMathematicsOperatorsAddition)
- (NSIndexSet*) intersectWithIndexSet: (NSIndexSet*)set {
NSMutableIndexSet *intersectionSet = [NSMutableIndexSet indexSet];
if ([self count] == 0 || [set count] == 0) return intersectionSet;
NSRange range = NSMakeRange(0, [self lastIndex]);
NSRangePointer rangePointer = ⦥
unsigned int buffer[32];
unsigned int count;
do {
count = [self getIndexes:buffer maxCount:32 inIndexRange:rangePointer];
while (count -= 1) {
if ([set containsIndex:buffer[count]])
[intersectionSet addIndex:buffer[count]];
}
} while (rangePointer != nil);
return intersectionSet;
}
- (NSIndexSet*) unionWithIndexSet: (NSIndexSet*)set {
NSMutableIndexSet *unionSet = [self mutableCopy];
[unionSet addIndexes:set];
return unionSet;
}
- (NSIndexSet*) invertWithinRange: (NSRange)range {
NSMutableIndexSet *inverseSet = [[NSMutableIndexSet alloc] initWithIndexesInRange:range];
if ([self count] == 0) return inverseSet;
NSRangePointer rangePointer = ⦥
unsigned int buffer[32];
unsigned int count;
do {
count = [self getIndexes:buffer maxCount:32 inIndexRange:rangePointer];
while (count -= 1) {
[inverseSet removeIndex:buffer[count]];
}
} while (rangePointer != nil);
return inverseSet;
}
@end
@implementation SWPointSet
+ (SWPoint) pointForIndex:(unsigned int)index withSize:(SWSize)s {
int x = index % (int)(s.width);
return SWPoint (x, (int)((index - x) / s.width));
}
+ (unsigned int) indexForPoint:(SWPoint)point withSize:(SWSize)s {
return point.x + (point.y * s.width);
}
- (id) init {
return nil;
}
- (id) initWithSize: (SWSize)newSize indexSet:(NSIndexSet*)newIndexSet {
if (self = [super init]) {
size = newSize;
indexSet = [newIndexSet mutableCopy];
}
return self;
}
- (id) initWithSize:(SWSize)newSize {
if (self = [super init]) {
size = newSize;
indexSet = [[NSMutableIndexSet alloc] init];
}
return self;
}
- (void) dealloc {
[super dealloc];
[indexSet release];
}
- (NSIndexSet*) indexSet {
return indexSet;
}
- (BOOL) containsPoint:(SWPoint)point {
return [indexSet containsIndex:[[self class] indexForPoint:point withSize:size]];
}
- (SWPoint) pointForIndex:(unsigned int)index {
return [[self class] pointForIndex:index withSize:size];
}
- (unsigned int) indexForPoint:(SWPoint)point {
return [[self class] indexForPoint:point withSize:size];
}
+ (id) pointSetWithSize: (SWSize)size {
SWPointSet *pointSet = [[self class] alloc];
return [pointSet initWithSize:size];
}
- (SWPointSet*) intersectWithPointSet: (SWPointSet*)set {
NSIndexSet *intersectionSet = [[self indexSet] intersectWithIndexSet:[set indexSet]];
return [[[self class] alloc] initWithSize:self->size indexSet:intersectionSet];
}
- (SWPointSet*) subtractPointSet: (SWPointSet*)set {
NSMutableIndexSet *intersectionSet = [[self indexSet] mutableCopy];
[intersectionSet removeIndexes:[set indexSet]];
return [[[self class] alloc] initWithSize:self->size indexSet:intersectionSet];
}
- (SWPointSet*) unionWithPointSet: (SWPointSet*)set {
NSIndexSet *unionSet = [[self indexSet] unionWithIndexSet:[set indexSet]];
return [[[self class] alloc] initWithSize:self->size indexSet:unionSet];
}
- (SWPointSet*) inversePointSet {
NSIndexSet *invertedSet = [[self indexSet] invertWithinRange:NSMakeRange(0, size.width * size.height)];
return [[[self class] alloc] initWithSize:self->size indexSet:invertedSet];
}
- (size_t) count {
return [[self indexSet] count];
}
@end
@implementation SWMutablePointSet
- (void) addPoint:(SWPoint)point {
[indexSet addIndex:[[self class] indexForPoint:point withSize:size]];
}
- (void) removePoint:(SWPoint)point {
[indexSet removeIndex:[[self class] indexForPoint:point withSize:size]];
}
- (NSMutableIndexSet*) indexSet {
return indexSet;
}
@end