forked from jerrykrinock/CategoriesObjC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSArray+SimpleMutations.m
196 lines (164 loc) · 4.96 KB
/
NSArray+SimpleMutations.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
#import "NSArray+SimpleMutations.h"
@implementation NSArray (SimpleMutations)
- (NSArray*)arrayByRemovingObject:(id)object {
NSMutableArray* mutant = [self mutableCopy] ;
[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] ;
}
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 {
NSMutableSet* set = [[NSMutableSet alloc] initWithArray:self] ;
NSMutableArray* keepers = [[NSMutableArray alloc] init] ;
// set may have fewer objects than self because if a group of
// objects are -isEqual:, set contains only one of the group.
for (id objectInArray in self) {
id objectInSet = [set member:objectInArray] ;
if (objectInSet) {
// This is the first object in self which equals
// this particular objectInSet. Keep it
[keepers addObject:objectInArray] ;
// And remove it from set, so that subsequent objects
// in self which equal this objectInSet will not
// be keepers.
[set removeObject:objectInSet] ;
}
}
NSArray* answer = [keepers copy] ;
[keepers release] ;
[set release] ;
return [answer 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:(NSMutableSet*)newAdditions
newDeletions:(NSMutableSet*)newDeletions {
NSSet* immuterator ;
NSInteger index ;
// 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) {
[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) {
index = [deletions indexOfObject:object] ;
if (index != NSNotFound) {
[newAdditions removeObject:object] ;
[deletions removeObject:object] ;
}
}
[immuterator release] ;
// Add surviving new additions to existing additions
[additions addObjectsFromArray:[newAdditions allObjects]] ;
// Remove from newDeletions any which cancel out existing additions,
// and do the cancellation
immuterator = [newDeletions copy] ;
for (id object in immuterator) {
index = [additions indexOfObject:object] ;
if (index != NSNotFound) {
[newDeletions removeObject:object] ;
[additions removeObject:object] ;
}
}
[immuterator release] ;
// Add surviving new deletions to existing deletions
[deletions addObjectsFromArray:[newDeletions allObjects]] ;
}
@end