-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSSortDescriptor.m
More file actions
568 lines (484 loc) · 13.9 KB
/
NSSortDescriptor.m
File metadata and controls
568 lines (484 loc) · 13.9 KB
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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/* Implementation for NSSortDescriptor for GNUStep
Copyright (C) 2005-2011 Free Software Foundation, Inc.
Written by: Saso Kiselkov <diablos@manga.sk>
Date: 2005
This file is part of the GNUstep Base Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
*/
#import "common.h"
#define EXPOSE_NSSortDescriptor_IVARS 1
#import "Foundation/NSSortDescriptor.h"
#import "Foundation/NSCoder.h"
#import "Foundation/NSException.h"
#import "Foundation/NSKeyValueCoding.h"
#import "Foundation/NSNotification.h"
#import "Foundation/NSUserDefaults.h"
#import "GNUstepBase/GSObjCRuntime.h"
#import "GSPrivate.h"
#import "GSSorting.h"
static BOOL initialized = NO;
#ifdef __clang__
#pragma clang diagnostic ignored "-Wreceiver-forward-class"
#endif
@interface GSTimSortPlaceHolder : NSObject
+ (void) setUnstable;
@end
@interface GSQuickSortPlaceHolder : NSObject
+ (void) setUnstable;
@end
@interface GSShellSortPlaceHolder : NSObject
+ (void) setUnstable;
@end
@implementation NSSortDescriptor
+ (void) defaultsChanged: (NSNotification*)n
{
NSUserDefaults *defs = (NSUserDefaults*)[n object];
NSString *algorithm;
algorithm = [defs stringForKey: @"GSSortAlgorithm"];
if ([algorithm isEqual: @"QuickSort"])
{
[GSQuickSortPlaceHolder setUnstable];
}
else if ([algorithm isEqual: @"ShellSort"])
{
[GSShellSortPlaceHolder setUnstable];
}
else if ([algorithm isEqual: @"TimSort"])
{
[GSTimSortPlaceHolder setUnstable];
}
else
{
[GSTimSortPlaceHolder setUnstable];
if (nil != algorithm)
{
NSLog(@"GSSortAlgorithm default unknown value (%@)", algorithm);
}
}
}
+ (void) initialize
{
if (NO == initialized)
{
NSNotificationCenter *nc;
NSUserDefaults *defs;
[GSTimSortPlaceHolder class]; // default stable sort
nc = [NSNotificationCenter defaultCenter];
defs = [NSUserDefaults standardUserDefaults];
[nc addObserver: self
selector: @selector(defaultsChanged:)
name: NSUserDefaultsDidChangeNotification
object: defs];
[self defaultsChanged: nil]; // set unstable sort
initialized = YES;
}
}
- (BOOL) ascending
{
return _ascending;
}
- (NSComparisonResult) compareObject: (id) object1 toObject: (id) object2
{
NSComparisonResult result;
id comparedKey1 = [object1 valueForKeyPath: _key];
id comparedKey2 = [object2 valueForKeyPath: _key];
if (_comparator == NULL)
{
result = (NSComparisonResult) [comparedKey1 performSelector: _selector
withObject: comparedKey2];
}
else
{
result = CALL_BLOCK(((NSComparator)_comparator), comparedKey1, comparedKey2);
}
if (_ascending == NO)
{
if (result == NSOrderedAscending)
{
result = NSOrderedDescending;
}
else if (result == NSOrderedDescending)
{
result = NSOrderedAscending;
}
}
return result;
}
- (id) copyWithZone: (NSZone*)zone
{
NSSortDescriptor *copy = nil;
if (NSShouldRetainWithZone(self, zone))
{
return RETAIN(self);
}
if (_comparator == NULL)
{
copy = [[NSSortDescriptor allocWithZone: zone]
initWithKey: _key ascending: _ascending selector: _selector];
}
else
{
copy = [[NSSortDescriptor allocWithZone: zone]
initWithKey: _key ascending: _ascending comparator: _comparator];
}
return copy;
}
- (void) dealloc
{
TEST_RELEASE(_key);
TEST_RELEASE(_comparator);
[super dealloc];
}
- (NSUInteger) hash
{
const char *sel = sel_getName(_selector);
return _ascending + GSPrivateHash(0, sel, strlen(sel)) + [_key hash];
}
+ (id) sortDescriptorWithKey: (NSString *)aKey ascending: (BOOL)ascending
{
return AUTORELEASE([[self alloc] initWithKey: aKey ascending: ascending]);
}
+ (id) sortDescriptorWithKey: (NSString *)aKey
ascending: (BOOL)ascending
selector: (SEL)aSelector
{
return AUTORELEASE([[self alloc] initWithKey: aKey
ascending: ascending
selector: aSelector]);
}
+ (id)sortDescriptorWithKey: (NSString *)key
ascending: (BOOL)ascending
comparator: (NSComparator)cmptr
{
return AUTORELEASE([[self alloc] initWithKey: key
ascending: ascending
comparator: cmptr]);
}
- (id) initWithKey: (NSString *) key ascending: (BOOL) ascending
{
return [self initWithKey: key ascending: ascending selector: NULL];
}
- (id) initWithKey: (NSString *) key
ascending: (BOOL) ascending
comparator: (NSComparator) cmptr
{
if ([self init])
{
if (key == nil)
{
[NSException raise: NSInvalidArgumentException
format: @"%@", _(@"Passed nil key when initializing "
@"an NSSortDescriptor.")];
}
if (cmptr == NULL)
{
[NSException raise: NSInvalidArgumentException
format: @"%@", _(@"Passed NULL comparator when initializing "
@"an NSSortDescriptor.")];
}
ASSIGN(_key, key);
_ascending = ascending;
ASSIGN(_comparator, (id)cmptr);
return self;
}
else
{
return nil;
}
}
- (id) initWithKey: (NSString *) key
ascending: (BOOL) ascending
selector: (SEL) selector
{
if ([self init])
{
if (key == nil)
{
[NSException raise: NSInvalidArgumentException
format: @"%@", _(@"Passed nil key when initializing "
@"an NSSortDescriptor.")];
}
if (selector == NULL)
{
selector = @selector(compare:);
}
ASSIGN(_key, key);
_ascending = ascending;
_selector = selector;
return self;
}
else
{
return nil;
}
}
- (BOOL) isEqual: (id)other
{
if (other == self)
{
return YES;
}
if ([other isKindOfClass: [NSSortDescriptor class]] == NO)
{
return NO;
}
if (((NSSortDescriptor*)other)->_ascending != _ascending)
{
return NO;
}
if (!sel_isEqual(((NSSortDescriptor*)other)->_selector, _selector))
{
return NO;
}
return [((NSSortDescriptor*)other)->_key isEqualToString: _key];
}
- (NSString *) key
{
return _key;
}
- (id) reversedSortDescriptor
{
return AUTORELEASE([[NSSortDescriptor alloc]
initWithKey: _key ascending: !_ascending selector: _selector]);
}
- (SEL) selector
{
return _selector;
}
- (void) encodeWithCoder: (NSCoder *) coder
{
if ([coder allowsKeyedCoding])
{
[coder encodeObject: _key forKey: @"NSKey"];
[coder encodeBool: _ascending forKey: @"NSAscending"];
[coder encodeObject: NSStringFromSelector(_selector)
forKey: @"NSSelector"];
}
else
{
[coder encodeObject: _key];
[coder encodeValueOfObjCType: @encode(BOOL) at: &_ascending];
[coder encodeValueOfObjCType: @encode(SEL) at: &_selector];
}
}
- (id) initWithCoder: (NSCoder *)decoder
{
if ((self = [super init]) != nil)
{
if ([decoder allowsKeyedCoding])
{
ASSIGN(_key, [decoder decodeObjectForKey: @"NSKey"]);
_ascending = [decoder decodeBoolForKey: @"NSAscending"];
_selector = NSSelectorFromString([decoder
decodeObjectForKey: @"NSSelector"]);
}
else
{
ASSIGN(_key, [decoder decodeObject]);
[decoder decodeValueOfObjCType: @encode(BOOL) at: &_ascending];
[decoder decodeValueOfObjCType: @encode(SEL) at: &_selector];
}
}
return self;
}
@end
/* Symbols for the sorting functions, the actual algorithms fill these. */
void
(*_GSSortUnstable)(id* buffer, NSRange range,
id comparisonEntity, GSComparisonType cmprType, void *context) = NULL;
void
(*_GSSortStable)(id* buffer, NSRange range,
id comparisonEntity, GSComparisonType cmprType, void *context) = NULL;
void
(*_GSSortUnstableConcurrent)(id* buffer, NSRange range,
id comparisonEntity, GSComparisonType cmprType, void *context) = NULL;
void
(*_GSSortStableConcurrent)(id* buffer, NSRange range,
id comparisonEntity, GSComparisonType cmprType, void *context) = NULL;
// Sorting functions that select the adequate algorithms
void
GSSortUnstable(id* buffer, NSRange range, id descriptorOrComparator,
GSComparisonType type, void* context)
{
if (NO == initialized) [NSSortDescriptor class];
if (NULL != _GSSortUnstable)
{
_GSSortUnstable(buffer, range, descriptorOrComparator, type, context);
}
else if (NULL != _GSSortStable)
{
_GSSortStable(buffer, range, descriptorOrComparator, type, context);
}
else
{
[NSException raise: @"NSInternalInconsistencyException" format:
@"The GNUstep-base library was compiled without sorting support."];
}
}
void
GSSortStable(id* buffer, NSRange range, id descriptorOrComparator,
GSComparisonType type, void* context)
{
if (NO == initialized) [NSSortDescriptor class];
if (NULL != _GSSortStable)
{
_GSSortStable(buffer, range, descriptorOrComparator, type, context);
}
else
{
[NSException raise: @"NSInternalInconsistencyException" format:
@"The GNUstep-base library was compiled without a"
@" stable sorting algorithm."];
}
}
void
GSSortStableConcurrent(id* buffer, NSRange range, id descriptorOrComparator,
GSComparisonType type, void* context)
{
if (NO == initialized) [NSSortDescriptor class];
if (NULL != _GSSortStableConcurrent)
{
_GSSortStableConcurrent(buffer, range, descriptorOrComparator,
type, context);
}
else
{
GSSortStable(buffer, range, descriptorOrComparator, type, context);
}
}
void
GSSortUnstableConcurrent(id* buffer, NSRange range, id descriptorOrComparator,
GSComparisonType type, void* context)
{
if (NO == initialized) [NSSortDescriptor class];
if (NULL != _GSSortUnstableConcurrent)
{
_GSSortUnstableConcurrent(buffer, range, descriptorOrComparator,
type, context);
}
else if (NULL != _GSSortStableConcurrent)
{
_GSSortStableConcurrent(buffer, range, descriptorOrComparator,
type, context);
}
else
{
GSSortUnstable(buffer, range, descriptorOrComparator, type, context);
}
}
@implementation NSArray (NSSortDescriptorSorting)
- (NSArray *) sortedArrayUsingDescriptors: (NSArray *)sortDescriptors
{
NSMutableArray *sortedArray = [GSMutableArray arrayWithArray: self];
[sortedArray sortUsingDescriptors: sortDescriptors];
return GS_IMMUTABLE(sortedArray);
}
@end
/* Sort the objects in range using the first descriptor and, if there
* are more descriptors, recursively call the function to sort each range
* of adhacent equal objects using the remaining descriptors.
*/
static void
SortRange(id *objects, NSRange range, id *descriptors,
NSUInteger numDescriptors)
{
NSSortDescriptor *sd = (NSSortDescriptor*)descriptors[0];
GSSortUnstable(objects, range, sd, GSComparisonTypeSortDescriptor, NULL);
if (numDescriptors > 1)
{
NSUInteger start = range.location;
NSUInteger finish = NSMaxRange(range);
while (start < finish)
{
NSUInteger pos = start + 1;
/* Find next range of adjacent objects.
*/
while (pos < finish
&& [sd compareObject: objects[start]
toObject: objects[pos]] == NSOrderedSame)
{
pos++;
}
/* Sort the range using remaining descriptors.
*/
if (pos - start > 1)
{
SortRange(objects, NSMakeRange(start, pos - start),
descriptors + 1, numDescriptors - 1);
}
start = pos;
}
}
}
@implementation NSMutableArray (NSSortDescriptorSorting)
- (void) sortUsingDescriptors: (NSArray *)sortDescriptors
{
NSUInteger count = [self count];
NSUInteger numDescriptors = [sortDescriptors count];
if (count > 1 && numDescriptors > 0)
{
id descriptors[numDescriptors];
NSArray *a;
GS_BEGINIDBUF(objects, count);
[self getObjects: objects];
if ([sortDescriptors isProxy])
{
NSUInteger i;
for (i = 0; i < numDescriptors; i++)
{
descriptors[i] = [sortDescriptors objectAtIndex: i];
}
}
else
{
[sortDescriptors getObjects: descriptors];
}
SortRange(objects, NSMakeRange(0, count), descriptors, numDescriptors);
a = [[NSArray alloc] initWithObjects: objects count: count];
[self setArray: a];
RELEASE(a);
GS_ENDIDBUF();
}
}
@end
@implementation GSMutableArray (NSSortDescriptorSorting)
- (void) sortUsingDescriptors: (NSArray *)sortDescriptors
{
NSUInteger dCount = [sortDescriptors count];
if (_count > 1 && dCount > 0)
{
GS_BEGINIDBUF(descriptors, dCount);
if ([sortDescriptors isProxy])
{
NSUInteger i;
for (i = 0; i < dCount; i++)
{
descriptors[i] = [sortDescriptors objectAtIndex: i];
}
}
else
{
[sortDescriptors getObjects: descriptors];
}
SortRange(_contents_array, NSMakeRange(0, _count), descriptors, dCount);
GS_ENDIDBUF();
}
}
@end
@implementation NSSet (NSSortDescriptorSorting)
- (NSArray *) sortedArrayUsingDescriptors: (NSArray *)sortDescriptors
{
return [[self allObjects] sortedArrayUsingDescriptors: sortDescriptors];
}
@end