mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-25 01:31:08 +00:00
Merge pull request #59 from gnustep/NSSortDescriptor_branch
NSSortDescriptor branch
This commit is contained in:
commit
deda3fada9
2 changed files with 86 additions and 5 deletions
|
@ -50,6 +50,7 @@ extern "C" {
|
||||||
NSString *_key;
|
NSString *_key;
|
||||||
BOOL _ascending;
|
BOOL _ascending;
|
||||||
SEL _selector;
|
SEL _selector;
|
||||||
|
NSComparator _comparator;
|
||||||
#endif
|
#endif
|
||||||
#if GS_NONFRAGILE
|
#if GS_NONFRAGILE
|
||||||
#else
|
#else
|
||||||
|
@ -92,6 +93,24 @@ extern "C" {
|
||||||
+ (id) sortDescriptorWithKey: (NSString *)aKey
|
+ (id) sortDescriptorWithKey: (NSString *)aKey
|
||||||
ascending: (BOOL)ascending
|
ascending: (BOOL)ascending
|
||||||
selector: (SEL)aSelector;
|
selector: (SEL)aSelector;
|
||||||
|
|
||||||
|
/** <p>Returns an autoreleased sort descriptor initialized to perform
|
||||||
|
* comparisons in the specified order using the comparator to compare
|
||||||
|
* the property aKey of each object.</p>
|
||||||
|
*
|
||||||
|
* <p>See also -initWithKey:ascending:selector:.</p>
|
||||||
|
*/
|
||||||
|
+ (id)sortDescriptorWithKey: (NSString *)key
|
||||||
|
ascending: (BOOL)ascending
|
||||||
|
comparator: (NSComparator)cmptr;
|
||||||
|
|
||||||
|
/** <init />
|
||||||
|
* Initialises the receiver to perform comparisons in the specified order
|
||||||
|
* using the comparator to compare the property key of each object.
|
||||||
|
*/
|
||||||
|
- (id) initWithKey: (NSString *)key
|
||||||
|
ascending: (BOOL)ascending
|
||||||
|
comparator: (NSComparator)cmptr;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/** Initialises the receiver for comparisons using the 'compare:' selector
|
/** Initialises the receiver for comparisons using the 'compare:' selector
|
||||||
|
@ -102,12 +121,13 @@ extern "C" {
|
||||||
|
|
||||||
/** <init />
|
/** <init />
|
||||||
* Initialises the receiver to perform comparisons in the specified order
|
* Initialises the receiver to perform comparisons in the specified order
|
||||||
* using selector to compar the property key of each object.
|
* using selector to compare the property key of each object.
|
||||||
*/
|
*/
|
||||||
- (id) initWithKey: (NSString *)key
|
- (id) initWithKey: (NSString *)key
|
||||||
ascending: (BOOL)ascending
|
ascending: (BOOL)ascending
|
||||||
selector: (SEL)selector;
|
selector: (SEL)selector;
|
||||||
|
|
||||||
|
|
||||||
/** Returns the key used to obtain the property on which comparisons are based.
|
/** Returns the key used to obtain the property on which comparisons are based.
|
||||||
*/
|
*/
|
||||||
- (NSString *) key;
|
- (NSString *) key;
|
||||||
|
|
|
@ -113,8 +113,16 @@ static BOOL initialized = NO;
|
||||||
id comparedKey1 = [object1 valueForKeyPath: _key];
|
id comparedKey1 = [object1 valueForKeyPath: _key];
|
||||||
id comparedKey2 = [object2 valueForKeyPath: _key];
|
id comparedKey2 = [object2 valueForKeyPath: _key];
|
||||||
|
|
||||||
result = (NSComparisonResult) [comparedKey1 performSelector: _selector
|
if (_comparator == NULL)
|
||||||
withObject: comparedKey2];
|
{
|
||||||
|
result = (NSComparisonResult) [comparedKey1 performSelector: _selector
|
||||||
|
withObject: comparedKey2];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result = CALL_BLOCK(((NSComparator)_comparator), comparedKey1, comparedKey2);
|
||||||
|
}
|
||||||
|
|
||||||
if (_ascending == NO)
|
if (_ascending == NO)
|
||||||
{
|
{
|
||||||
if (result == NSOrderedAscending)
|
if (result == NSOrderedAscending)
|
||||||
|
@ -132,17 +140,29 @@ static BOOL initialized = NO;
|
||||||
|
|
||||||
- (id) copyWithZone: (NSZone*)zone
|
- (id) copyWithZone: (NSZone*)zone
|
||||||
{
|
{
|
||||||
|
NSSortDescriptor *copy = nil;
|
||||||
if (NSShouldRetainWithZone(self, zone))
|
if (NSShouldRetainWithZone(self, zone))
|
||||||
{
|
{
|
||||||
return RETAIN(self);
|
return RETAIN(self);
|
||||||
}
|
}
|
||||||
return [[NSSortDescriptor allocWithZone: zone]
|
|
||||||
initWithKey: _key ascending: _ascending selector: _selector];
|
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
|
- (void) dealloc
|
||||||
{
|
{
|
||||||
TEST_RELEASE(_key);
|
TEST_RELEASE(_key);
|
||||||
|
TEST_RELEASE(_comparator);
|
||||||
[super dealloc];
|
[super dealloc];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -167,11 +187,52 @@ static BOOL initialized = NO;
|
||||||
selector: aSelector]);
|
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
|
- (id) initWithKey: (NSString *) key ascending: (BOOL) ascending
|
||||||
{
|
{
|
||||||
return [self initWithKey: key ascending: ascending selector: NULL];
|
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, cmptr);
|
||||||
|
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
- (id) initWithKey: (NSString *) key
|
- (id) initWithKey: (NSString *) key
|
||||||
ascending: (BOOL) ascending
|
ascending: (BOOL) ascending
|
||||||
selector: (SEL) selector
|
selector: (SEL) selector
|
||||||
|
|
Loading…
Reference in a new issue