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
|
static struct {
Class isa;
} placeholder;
@interface OFMutableArrayPlaceholder: OFMutableArray
@end
static of_comparison_result_t
compare(id left, id right, SEL selector)
{
of_comparison_result_t (*comparator)(id, SEL, id) =
(of_comparison_result_t (*)(id, SEL, id))
[left methodForSelector: selector];
return comparator(left, selector, right);
}
static void
quicksort(OFMutableArray *array, size_t left, size_t right, SEL selector,
int options)
{
of_comparison_result_t ascending, descending;
if (options & OF_ARRAY_SORT_DESCENDING) {
ascending = OF_ORDERED_DESCENDING;
descending = OF_ORDERED_ASCENDING;
} else {
ascending = OF_ORDERED_ASCENDING;
descending = OF_ORDERED_DESCENDING;
}
while (left < right) {
size_t i = left;
size_t j = right - 1;
id pivot = [array objectAtIndex: right];
|
|
|
|
|
|
|
|
|
|
|
|
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
|
static struct {
Class isa;
} placeholder;
@interface OFMutableArrayPlaceholder: OFMutableArray
@end
static OFComparisonResult
compare(id left, id right, SEL selector)
{
OFComparisonResult (*comparator)(id, SEL, id) =
(OFComparisonResult (*)(id, SEL, id))
[left methodForSelector: selector];
return comparator(left, selector, right);
}
static void
quicksort(OFMutableArray *array, size_t left, size_t right, SEL selector,
OFArraySortOptions options)
{
OFComparisonResult ascending, descending;
if (options & OFArraySortDescending) {
ascending = OFOrderedDescending;
descending = OFOrderedAscending;
} else {
ascending = OFOrderedAscending;
descending = OFOrderedDescending;
}
while (left < right) {
size_t i = left;
size_t j = right - 1;
id pivot = [array objectAtIndex: right];
|
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
left = i + 1;
}
}
#ifdef OF_HAVE_BLOCKS
static void
quicksortWithBlock(OFMutableArray *array, size_t left, size_t right,
of_comparator_t comparator, int options)
{
of_comparison_result_t ascending, descending;
if (options & OF_ARRAY_SORT_DESCENDING) {
ascending = OF_ORDERED_DESCENDING;
descending = OF_ORDERED_ASCENDING;
} else {
ascending = OF_ORDERED_ASCENDING;
descending = OF_ORDERED_DESCENDING;
}
while (left < right) {
size_t i = left;
size_t j = right - 1;
id pivot = [array objectAtIndex: right];
|
|
|
|
|
|
|
|
|
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
left = i + 1;
}
}
#ifdef OF_HAVE_BLOCKS
static void
quicksortWithBlock(OFMutableArray *array, size_t left, size_t right,
OFComparator comparator, OFArraySortOptions options)
{
OFComparisonResult ascending, descending;
if (options & OFArraySortDescending) {
ascending = OFOrderedDescending;
descending = OFOrderedAscending;
} else {
ascending = OFOrderedAscending;
descending = OFOrderedDescending;
}
while (left < right) {
size_t i = left;
size_t j = right - 1;
id pivot = [array objectAtIndex: right];
|
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
|
ret = [[OFMutableAdjacentArray alloc] initWithObject: firstObject
arguments: arguments];
va_end(arguments);
return ret;
}
- (instancetype)initWithObject: (id)firstObject
arguments: (va_list)arguments
{
return (id)[[OFMutableAdjacentArray alloc] initWithObject: firstObject
arguments: arguments];
}
- (instancetype)initWithArray: (OFArray *)array
{
return (id)[[OFMutableAdjacentArray alloc] initWithArray: array];
}
- (instancetype)initWithObjects: (id const *)objects
count: (size_t)count
{
return (id)[[OFMutableAdjacentArray alloc] initWithObjects: objects
count: count];
}
- (instancetype)initWithSerialization: (OFXMLElement *)element
{
|
|
<
|
<
|
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
|
ret = [[OFMutableAdjacentArray alloc] initWithObject: firstObject
arguments: arguments];
va_end(arguments);
return ret;
}
- (instancetype)initWithObject: (id)firstObject arguments: (va_list)arguments
{
return (id)[[OFMutableAdjacentArray alloc] initWithObject: firstObject
arguments: arguments];
}
- (instancetype)initWithArray: (OFArray *)array
{
return (id)[[OFMutableAdjacentArray alloc] initWithArray: array];
}
- (instancetype)initWithObjects: (id const *)objects count: (size_t)count
{
return (id)[[OFMutableAdjacentArray alloc] initWithObjects: objects
count: count];
}
- (instancetype)initWithSerialization: (OFXMLElement *)element
{
|
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
- (id)copy
{
return [[OFArray alloc] initWithArray: self];
}
- (void)addObject: (id)object
{
[self insertObject: object
atIndex: self.count];
}
- (void)addObjectsFromArray: (OFArray *)array
{
[self insertObjectsFromArray: array
atIndex: self.count];
}
- (void)insertObject: (id)object
atIndex: (size_t)idx
{
OF_UNRECOGNIZED_SELECTOR
}
- (void)insertObjectsFromArray: (OFArray *)array
atIndex: (size_t)idx
{
size_t i = 0;
for (id object in array)
[self insertObject: object
atIndex: idx + i++];
}
- (void)replaceObjectAtIndex: (size_t)idx
withObject: (id)object
{
OF_UNRECOGNIZED_SELECTOR
}
- (void)setObject: (id)object
atIndexedSubscript: (size_t)idx
{
[self replaceObjectAtIndex: idx
withObject: object];
}
- (void)replaceObject: (id)oldObject
withObject: (id)newObject
{
size_t count;
if (oldObject == nil || newObject == nil)
@throw [OFInvalidArgumentException exception];
count = self.count;
for (size_t i = 0; i < count; i++) {
if ([[self objectAtIndex: i] isEqual: oldObject]) {
[self replaceObjectAtIndex: i
withObject: newObject];
return;
}
}
}
- (void)replaceObjectIdenticalTo: (id)oldObject
withObject: (id)newObject
{
size_t count;
if (oldObject == nil || newObject == nil)
@throw [OFInvalidArgumentException exception];
count = self.count;
for (size_t i = 0; i < count; i++) {
if ([self objectAtIndex: i] == oldObject) {
[self replaceObjectAtIndex: i
withObject: newObject];
return;
}
}
}
- (void)removeObjectAtIndex: (size_t)idx
|
|
<
|
<
|
<
|
<
|
<
|
<
<
|
|
<
|
<
|
<
|
<
|
<
|
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
- (id)copy
{
return [[OFArray alloc] initWithArray: self];
}
- (void)addObject: (id)object
{
[self insertObject: object atIndex: self.count];
}
- (void)addObjectsFromArray: (OFArray *)array
{
[self insertObjectsFromArray: array atIndex: self.count];
}
- (void)insertObject: (id)object atIndex: (size_t)idx
{
OF_UNRECOGNIZED_SELECTOR
}
- (void)insertObjectsFromArray: (OFArray *)array atIndex: (size_t)idx
{
size_t i = 0;
for (id object in array)
[self insertObject: object atIndex: idx + i++];
}
- (void)replaceObjectAtIndex: (size_t)idx withObject: (id)object
{
OF_UNRECOGNIZED_SELECTOR
}
- (void)setObject: (id)object atIndexedSubscript: (size_t)idx
{
[self replaceObjectAtIndex: idx withObject: object];
}
- (void)replaceObject: (id)oldObject withObject: (id)newObject
{
size_t count;
if (oldObject == nil || newObject == nil)
@throw [OFInvalidArgumentException exception];
count = self.count;
for (size_t i = 0; i < count; i++) {
if ([[self objectAtIndex: i] isEqual: oldObject]) {
[self replaceObjectAtIndex: i withObject: newObject];
return;
}
}
}
- (void)replaceObjectIdenticalTo: (id)oldObject withObject: (id)newObject
{
size_t count;
if (oldObject == nil || newObject == nil)
@throw [OFInvalidArgumentException exception];
count = self.count;
for (size_t i = 0; i < count; i++) {
if ([self objectAtIndex: i] == oldObject) {
[self replaceObjectAtIndex: i withObject: newObject];
return;
}
}
}
- (void)removeObjectAtIndex: (size_t)idx
|
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
[self removeObjectAtIndex: i];
return;
}
}
}
- (void)removeObjectsInRange: (of_range_t)range
{
for (size_t i = 0; i < range.length; i++)
[self removeObjectAtIndex: range.location];
}
- (void)removeLastObject
{
size_t count = self.count;
if (count == 0)
return;
[self removeObjectAtIndex: count - 1];
}
- (void)removeAllObjects
{
[self removeObjectsInRange: of_range(0, self.count)];
}
#ifdef OF_HAVE_BLOCKS
- (void)replaceObjectsUsingBlock: (of_array_replace_block_t)block
{
[self enumerateObjectsUsingBlock: ^ (id object, size_t idx,
bool *stop) {
id new = block(object, idx);
if (new != object)
[self replaceObjectAtIndex: idx
withObject: new];
}];
}
#endif
- (void)exchangeObjectAtIndex: (size_t)idx1
withObjectAtIndex: (size_t)idx2
{
id object1 = [self objectAtIndex: idx1];
id object2 = [self objectAtIndex: idx2];
[object1 retain];
@try {
[self replaceObjectAtIndex: idx1
withObject: object2];
[self replaceObjectAtIndex: idx2
withObject: object1];
} @finally {
[object1 release];
}
}
- (void)sort
{
[self sortUsingSelector: @selector(compare:)
options: 0];
}
- (void)sortUsingSelector: (SEL)selector
options: (int)options
{
size_t count = self.count;
if (count == 0 || count == 1)
return;
quicksort(self, 0, count - 1, selector, options);
}
#ifdef OF_HAVE_BLOCKS
- (void)sortUsingComparator: (of_comparator_t)comparator
options: (int)options
{
size_t count = self.count;
if (count == 0 || count == 1)
return;
quicksortWithBlock(self, 0, count - 1, comparator, options);
}
#endif
- (void)reverse
{
size_t i, j, count = self.count;
if (count == 0 || count == 1)
return;
for (i = 0, j = count - 1; i < j; i++, j--)
[self exchangeObjectAtIndex: i
withObjectAtIndex: j];
}
- (void)makeImmutable
{
}
@end
|
|
|
|
|
<
|
<
|
<
|
<
|
<
|
|
|
|
<
|
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
[self removeObjectAtIndex: i];
return;
}
}
}
- (void)removeObjectsInRange: (OFRange)range
{
for (size_t i = 0; i < range.length; i++)
[self removeObjectAtIndex: range.location];
}
- (void)removeLastObject
{
size_t count = self.count;
if (count == 0)
return;
[self removeObjectAtIndex: count - 1];
}
- (void)removeAllObjects
{
[self removeObjectsInRange: OFRangeMake(0, self.count)];
}
#ifdef OF_HAVE_BLOCKS
- (void)replaceObjectsUsingBlock: (OFArrayReplaceBlock)block
{
[self enumerateObjectsUsingBlock: ^ (id object, size_t idx,
bool *stop) {
id new = block(object, idx);
if (new != object)
[self replaceObjectAtIndex: idx withObject: new];
}];
}
#endif
- (void)exchangeObjectAtIndex: (size_t)idx1 withObjectAtIndex: (size_t)idx2
{
id object1 = [self objectAtIndex: idx1];
id object2 = [self objectAtIndex: idx2];
[object1 retain];
@try {
[self replaceObjectAtIndex: idx1 withObject: object2];
[self replaceObjectAtIndex: idx2 withObject: object1];
} @finally {
[object1 release];
}
}
- (void)sort
{
[self sortUsingSelector: @selector(compare:) options: 0];
}
- (void)sortUsingSelector: (SEL)selector
options: (OFArraySortOptions)options
{
size_t count = self.count;
if (count == 0 || count == 1)
return;
quicksort(self, 0, count - 1, selector, options);
}
#ifdef OF_HAVE_BLOCKS
- (void)sortUsingComparator: (OFComparator)comparator
options: (OFArraySortOptions)options
{
size_t count = self.count;
if (count == 0 || count == 1)
return;
quicksortWithBlock(self, 0, count - 1, comparator, options);
}
#endif
- (void)reverse
{
size_t i, j, count = self.count;
if (count == 0 || count == 1)
return;
for (i = 0, j = count - 1; i < j; i++, j--)
[self exchangeObjectAtIndex: i withObjectAtIndex: j];
}
- (void)makeImmutable
{
}
@end
|