-
Notifications
You must be signed in to change notification settings - Fork 41
/
NSArray+SSYMutations.m
282 lines (241 loc) · 7.33 KB
/
NSArray+SSYMutations.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#import "NSArray+SSYMutations.h"
@implementation NSArray (SSYMutations)
- (NSArray*)arrayByRemovingObject:(id)object {
NSMutableArray* mutant = [self mutableCopy] ;
[mutant removeObject:object] ;
NSArray* newArray = [NSArray arrayWithArray:mutant] ;
[mutant release] ;
return newArray ;
}
- (NSArray*)arrayByRemovingObjectsFromSet:(NSSet*)set {
NSMutableArray* mutant = [self mutableCopy] ;
for (id object in set) {
[mutant removeObject:object] ;
}
NSArray* newArray = [NSArray arrayWithArray:mutant] ;
[mutant release] ;
return newArray ;
}
- (NSArray*)arrayByInsertingObject:(id)object
atIndex:(NSInteger)index {
NSArray* answer ;
if (object) {
NSMutableArray* mutant = [self mutableCopy] ;
[mutant insertObject:object
atIndex:index] ;
answer = [NSArray arrayWithArray:mutant] ;
[mutant release] ;
}
else {
answer = self ;
}
return answer ;
}
- (NSArray*)arrayByRemovingObjectAtIndex:(NSUInteger)index {
NSMutableArray* mutant = [self mutableCopy] ;
[mutant removeObjectAtIndex:index] ;
NSArray* newArray = [NSArray arrayWithArray:mutant] ;
[mutant release] ;
return newArray ;
}
- (NSArray*)arrayByAddingUniqueObject:(id)object {
NSArray* array ;
if ([self indexOfObject:object] == NSNotFound) {
array = [self arrayByAddingObject:object] ;
}
else {
array = self ;
}
return array ;
}
- (NSArray*)arrayByAddingUniqueObjectsFromArray:(NSArray*)array {
if (!array) {
return self ;
}
NSMutableArray* newArray = [self mutableCopy] ;
for (id object in array) {
if ([self indexOfObject:object] == NSNotFound) {
[newArray addObject:object] ;
}
}
NSArray* answer = [newArray copy] ;
[newArray release] ;
return [answer autorelease] ;
}
- (NSArray*)arrayByRemovingObjectsEqualPerSelector:(SEL)isEqualSelector {
NSMutableArray* keepers = [[NSMutableArray alloc] init] ;
for (id object in self) {
BOOL isUnique = YES ;
for (id uniqueObject in keepers) {
if ([object performSelector:isEqualSelector
withObject:uniqueObject]) {
isUnique = NO ;
break ;
}
}
if (isUnique) {
[keepers addObject:object] ;
}
}
NSArray* answer = [keepers copy] ;
[keepers release] ;
return [answer autorelease] ;
}
- (NSArray*)arrayByRemovingEqualObjects {
NSMutableArray* mutableCopy = [self mutableCopy] ;
[mutableCopy removeEqualObjects] ;
NSArray* copy = [mutableCopy copy] ;
[mutableCopy release] ;
return [copy autorelease] ;
}
- (NSArray*)arrayIntersectingCollection:(NSObject <NSFastEnumeration> *)collection {
NSMutableIndexSet* keepers = [[NSMutableIndexSet alloc] init] ;
for (id object in collection) {
NSInteger index = [self indexOfObject:object] ;
if (index != NSNotFound) {
[keepers addIndex:index] ;
}
}
NSArray* newArray = [self objectsAtIndexes:keepers] ;
[keepers release] ;
return newArray ;
}
- (NSArray*)arrayMinusCollection:(NSObject <NSFastEnumeration> *)collection {
NSMutableArray* keepers = [self mutableCopy] ;
for (id object in collection) {
NSInteger index = [keepers indexOfObject:object] ;
if (index != NSNotFound) {
[keepers removeObjectAtIndex:index] ;
}
}
NSArray* newArray = [[keepers copy] autorelease] ;
[keepers release] ;
return newArray ;
}
+ (void)mutateAdditions:(NSMutableArray*)additions
deletions:(NSMutableArray*)deletions
newAdditions:(NSCountedSet*)newAdditions
newDeletions:(NSCountedSet*)newDeletions {
// A copy of a collection which we use to enumerate upon
// so that we can simultaneously modify the copied collection…
NSCountedSet* immuterator ;
NSInteger index, nCancellations, i ;
// Remove from newAdditions and newDeletions any members
// in these new inputs which cancel one another out
immuterator = [newAdditions copy] ;
for (id object in immuterator) {
id member = [newDeletions member:object] ;
if (member) {
nCancellations = MIN([newAdditions countForObject:object], [newDeletions countForObject:object]) ;
for (i=0; i<nCancellations; i++) {
[newAdditions removeObject:object] ;
[newDeletions removeObject:member] ;
}
}
}
[immuterator release] ;
// Remove from newAdditions any which cancel out existing deletions,
// and do the cancellation
immuterator = [newAdditions copy] ;
for (id object in immuterator) {
// The following loop will cycle M times, where M is the count
// of object in newAdditions, or the count of object in deletions,
// whichever is less. It's the same as the previous loop, except
// you do it a little differently because one subject is an array
// instead of a counted set
for (i=0; i<[immuterator countForObject:object]; i++) {
index = [deletions indexOfObject:object] ;
if (index == NSNotFound) {
break ;
}
[newAdditions removeObject:object] ;
[deletions removeObjectAtIndex:index] ;
}
}
[immuterator release] ;
// Add surviving new additions to existing additions
for (id object in newAdditions) {
for (i=0; i<[newAdditions countForObject:object]; i++) {
[additions addObject:object] ;
}
}
// Remove from newDeletions any which cancel out existing additions,
// and do the cancellation
immuterator = [newDeletions copy] ;
for (id object in immuterator) {
for (i=0; i<[immuterator countForObject:object]; i++) {
index = [additions indexOfObject:object] ;
if (index == NSNotFound) {
break ;
}
[newDeletions removeObject:object] ;
[additions removeObjectAtIndex:index] ;
}
}
[immuterator release] ;
// Add surviving new deletions to existing deletions
for (id object in newDeletions) {
for (i=0; i<[newDeletions countForObject:object]; i++) {
[deletions addObject:object] ;
}
}
}
@end
@implementation NSMutableArray (SSYMutations)
- (void)moveObject:(id)object
toIndex:(NSInteger)newIndex {
NSInteger currentIndex = [self indexOfObject:object] ;
if (currentIndex != NSNotFound) {
id actualObject = [self objectAtIndex:currentIndex] ;
[self removeObject:actualObject] ;
newIndex = MIN(newIndex, [self count]) ;
[self insertObject:actualObject
atIndex:newIndex] ;
}
}
- (void)trimToStartIndex:(NSInteger)startIndex
count:(NSInteger)count {
if (count < 1) {
[self removeAllObjects] ;
}
else {
NSRange deadRange ;
// Remove lower range
// Use the ? operator instead of MIN, because the two parameters are of different
// types (NSInteger, NSUInteger) which causes MIN to behave unpredictably.
NSInteger minLength = (startIndex < (NSInteger)[self count]) ? startIndex : [self count] ;
NSInteger deadLength = MAX(minLength, 0) ;
deadRange = NSMakeRange(0, deadLength) ;
[self removeObjectsInRange:deadRange] ;
// Remove upper range
NSInteger excess = [self count] - count ;
if (excess > 0) {
deadRange = NSMakeRange(count, excess) ;
[self removeObjectsInRange:deadRange] ;
}
}
}
- (void)removeEqualObjects {
NSArray* copy = [self copy] ;
NSInteger i = 0 ;
NSInteger offset = 0 ;
for (id object in copy) {
NSInteger j = 0 ;
for (id priorObject in copy) {
if (j >= i) {
// We only look for earlier objects
break ;
}
if ([object isEqual:priorObject]) {
NSInteger removeIndex = i - offset ;
[self removeObjectAtIndex:removeIndex] ;
offset++ ;
break ;
}
j++ ;
}
i++ ;
}
[copy release] ;
}
@end