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
|
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
static OFComparisonResult
compare(id left, id right, SEL selector)
{
of_comparison_result_t (*comparator)(id, SEL, id) =
(of_comparison_result_t (*)(id, SEL, id))
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,
int options)
OFArraySortOptions options)
{
of_comparison_result_t ascending, descending;
OFComparisonResult ascending, descending;
if (options & OF_ARRAY_SORT_DESCENDING) {
ascending = OF_ORDERED_DESCENDING;
descending = OF_ORDERED_ASCENDING;
if (options & OFArraySortDescending) {
ascending = OFOrderedDescending;
descending = OFOrderedAscending;
} else {
ascending = OF_ORDERED_ASCENDING;
descending = OF_ORDERED_DESCENDING;
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
|
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)
OFComparator comparator, OFArraySortOptions options)
{
of_comparison_result_t ascending, descending;
OFComparisonResult ascending, descending;
if (options & OF_ARRAY_SORT_DESCENDING) {
ascending = OF_ORDERED_DESCENDING;
descending = OF_ORDERED_ASCENDING;
if (options & OFArraySortDescending) {
ascending = OFOrderedDescending;
descending = OFOrderedAscending;
} else {
ascending = OF_ORDERED_ASCENDING;
descending = OF_ORDERED_DESCENDING;
ascending = OFOrderedAscending;
descending = OFOrderedDescending;
}
while (left < right) {
size_t i = left;
size_t j = right - 1;
id pivot = [array objectAtIndex: right];
|
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
|
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
|
-
+
-
+
-
+
|
[self removeObjectAtIndex: i];
return;
}
}
}
- (void)removeObjectsInRange: (of_range_t)range
- (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: of_range(0, self.count)];
[self removeObjectsInRange: OFMakeRange(0, self.count)];
}
#ifdef OF_HAVE_BLOCKS
- (void)replaceObjectsUsingBlock: (of_array_replace_block_t)block
- (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];
|
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
|
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
|
-
+
-
+
+
|
- (void)sort
{
[self sortUsingSelector: @selector(compare:) options: 0];
}
- (void)sortUsingSelector: (SEL)selector
options: (int)options
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: (of_comparator_t)comparator options: (int)options
- (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);
|