-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCueSyncArray.mm
263 lines (205 loc) · 5.54 KB
/
CueSyncArray.mm
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
/*
* Copyright 2013 TheKitchenSync Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "CueSyncArray.h"
#import "CueSyncCollectionsPriv.h"
@implementation CueSyncArray {
NSMutableArray *_array;
dispatch_queue_t _queue;
}
#pragma mark - Static Factory
+ (instancetype)from:(NSArray *)array;
{
return [[[self alloc] initWithArray:array] autorelease];
}
+ (id)array;
{
return [[[self alloc] init] autorelease];
}
#pragma mark - Init
- (id)init;
{
return [self initWithArray:@[]];
}
- (id)initWithArray:(NSArray *)array;
{
self = [super init];
if (self) {
_array = array ? [array mutableCopy] : [@[] mutableCopy];
_queue = dispatch_queue_create("com.cueup.CueSyncArray", DISPATCH_QUEUE_CONCURRENT);
}
return self;
}
#pragma mark - Query
- (BOOL)containsObject:(id)anObject;
{
READ_TYPE(BOOL, [_array containsObject:anObject]);
}
- (NSUInteger)indexOfObject:(id)anObject;
{
READ_TYPE(NSUInteger, [_array indexOfObject:anObject]);
}
- (NSUInteger)count;
{
READ_TYPE(NSUInteger, _array.count);
}
- (id)objectAtIndex:(NSUInteger)index;
{
READ_ID([[[_array objectAtIndex:index] retain] autorelease]);
}
- (id)objectAtIndexedSubscript:(NSUInteger)idx;
{
READ_ID([[[_array objectAtIndexedSubscript:idx] retain] autorelease]);
}
- (NSString *)description;
{
READ_ID(_array.description);
}
#pragma mark - Add
- (void)addObject:(id)object;
{
WRITE([_array addObject:object]);
}
- (void)addObjectsFromArray:(NSArray *)array;
{
WRITE([_array addObjectsFromArray:array]);
}
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
{
WRITE([_array insertObject:anObject atIndex:index]);
}
- (void)setObject:(id)anObject atIndexedSubscript:(NSUInteger)index;
{
WRITE([_array setObject:anObject atIndexedSubscript:index]);
}
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
{
WRITE([_array replaceObjectAtIndex:index withObject:anObject]);
}
- (void)setArray:(NSArray *)otherArray;
{
WRITE([_array setArray:otherArray]);
}
#pragma mark - Remove
- (void)removeObject:(id)object;
{
WRITE([_array removeObject:object]);
}
- (void)removeObjectAtIndex:(NSUInteger)index;
{
WRITE([_array removeObjectAtIndex:index]);
}
- (void)removeFirstInstanceOfObject:(id)object;
{
WRITE(NSInteger index = [_array indexOfObject:object];
if (index != NSNotFound) {
[_array removeObjectAtIndex:index];
});
}
- (void)removeLastObject;
{
WRITE([_array removeLastObject]);
}
- (void)removeAllObjects;
{
WRITE([_array removeAllObjects]);
}
#pragma mark - Filter and Sort
- (CueSyncArray *)filteredArrayUsingBlock:(BOOL (^)(id evaluatedObject, NSDictionary *bindings))block;
{
READ_ID([[_array filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:block]] cueConcurrent]);
}
- (void)filterUsingBlock:(BOOL (^)(id evaluatedObject, NSDictionary *bindings))block;
{
WRITE([_array filterUsingPredicate:[NSPredicate predicateWithBlock:block]]);
}
- (CueSyncArray *)sortedArrayUsingDescriptors:(NSArray *)sortDescriptors;
{
READ_ID([[_array sortedArrayUsingDescriptors:sortDescriptors] cueConcurrent]);
}
- (void)sortUsingDescriptors:(NSArray *)sortDescriptors;
{
WRITE([_array sortUsingDescriptors:sortDescriptors]);
}
#pragma mark - Array
- (NSMutableArray *)unsafeArray;
{
return _array;
}
- (NSArray *)array;
{
READ_ID([[_array copy] autorelease]);
}
#pragma mark - NSCoding
- (id)initWithCoder:(NSCoder *)aDecoder;
{
NSArray *array = [aDecoder decodeObjectForKey:@"array"];
return [self initWithArray:array];
}
- (void)encodeWithCoder:(NSCoder *)aCoder;
{
READ([aCoder encodeObject:_array forKey:@"array"]);
}
#pragma mark - NSCopying
- (id)copyWithZone:(NSZone *)zone;
{
return [self mutableCopyWithZone:zone];
}
- (id)mutableCopyWithZone:(NSZone *)zone;
{
READ_ID([[self.class allocWithZone:zone] initWithArray:_array]);
}
- (void)withArray:(void (^)(NSArray *array))block;
{
READ(if (block) { block(_array); });
}
- (BOOL)isEqual:(id)object;
{
if (object == self) {
return YES;
}
if ([object isKindOfClass:[CueSyncArray class]]) {
__block BOOL retval = NO;
CueSyncArray *other = object;
[other withArray:^(NSArray *array) {
[self withArray:^(NSArray *array_) {
retval = [array isEqual:array_];
}];
}];
return retval;
}
return NO;
}
// We don't want it to hash the same as an NSArray
// because we can't reliably do the right thing
// with isEqual, so we offset by one.
- (NSUInteger)hash;
{
READ_TYPE(NSUInteger, _array.hash + 1);
}
#pragma mark - NSFastEnumeration
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
objects:(id *)stackbuf
count:(NSUInteger)len;
{
READ_TYPE(NSUInteger, [[[_array copy] autorelease] countByEnumeratingWithState:state objects:stackbuf count:len]);
}
- (void)dealloc;
{
[_array release];
dispatch_release(_queue);
[super dealloc];
}
@end