mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-22 16:33:29 +00:00
Fix for bug #18107
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@23965 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
d99f06f398
commit
3481a3d002
3 changed files with 83 additions and 57 deletions
|
@ -8,6 +8,10 @@
|
|||
* Source/GSPrivate.h: Only use the visibility attribute if it is
|
||||
available. Avoids warnings (and link errors in at least one case)
|
||||
on systems where it is not fully supported by gcc yet.
|
||||
* Headers/Foundation/NSSortDescriptor.h: Tidy and fix macros marking
|
||||
macos compatibility.
|
||||
* Source/NSSortDescriptor.m: Fix to have usable hash and isEqual
|
||||
(bug #18107).
|
||||
|
||||
2006-10-23 Richard Frith-Macdonald <rfm@gnu.org>
|
||||
|
||||
|
|
|
@ -32,41 +32,38 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if OS_API_VERSION(100300,GS_API_LATEST)
|
||||
|
||||
@class NSString;
|
||||
|
||||
@interface NSSortDescriptor : NSObject <NSCopying, NSCoding>
|
||||
{
|
||||
NSString * _key;
|
||||
BOOL _ascending;
|
||||
SEL _selector;
|
||||
NSString *_key;
|
||||
BOOL _ascending;
|
||||
SEL _selector;
|
||||
}
|
||||
|
||||
// initialization
|
||||
- (id) initWithKey: (NSString *) key ascending: (BOOL) ascending;
|
||||
- (id) initWithKey: (NSString *) key
|
||||
ascending: (BOOL) ascending
|
||||
selector: (SEL) selector;
|
||||
|
||||
// getting information about a sort descriptor's setup
|
||||
- (BOOL) ascending;
|
||||
- (NSComparisonResult) compareObject: (id) object1 toObject: (id) object2;
|
||||
- (id) initWithKey: (NSString *)key
|
||||
ascending: (BOOL)ascending;
|
||||
- (id) initWithKey: (NSString *)key
|
||||
ascending: (BOOL)ascending
|
||||
selector: (SEL)selector;
|
||||
- (NSString *) key;
|
||||
- (SEL) selector;
|
||||
|
||||
// using sort descriptors
|
||||
- (NSComparisonResult) compareObject: (id) object1 toObject: (id) object2;
|
||||
- (id) reversedSortDescriptor;
|
||||
|
||||
@end
|
||||
|
||||
@interface NSArray (NSSortDescriptorSorting)
|
||||
|
||||
- (NSArray *) sortedArrayUsingDescriptors: (NSArray *) sortDescriptors;
|
||||
- (NSArray *) sortedArrayUsingDescriptors: (NSArray *)sortDescriptors;
|
||||
|
||||
@end
|
||||
|
||||
@interface NSMutableArray (NSSortDescriptorSorting)
|
||||
|
||||
- (void) sortUsingDescriptors: (NSArray *) sortDescriptors;
|
||||
- (void) sortUsingDescriptors: (NSArray *)sortDescriptors;
|
||||
|
||||
@end
|
||||
|
||||
|
@ -74,4 +71,6 @@ extern "C" {
|
|||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* __NSSortDescriptor_h_GNUSTEP_BASE_INCLUDE */
|
||||
|
|
|
@ -32,13 +32,49 @@
|
|||
|
||||
@implementation NSSortDescriptor
|
||||
|
||||
- (BOOL) ascending
|
||||
{
|
||||
return _ascending;
|
||||
}
|
||||
|
||||
- (NSComparisonResult) compareObject: (id) object1 toObject: (id) object2
|
||||
{
|
||||
NSComparisonResult result;
|
||||
id comparedKey1 = [object1 valueForKeyPath: _key],
|
||||
comparedKey2 = [object2 valueForKeyPath: _key];
|
||||
|
||||
result = (NSComparisonResult) [comparedKey1 performSelector: _selector
|
||||
withObject: comparedKey2];
|
||||
|
||||
if (_ascending != YES)
|
||||
{
|
||||
result = -result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
- (id) copyWithZone: (NSZone*)zone
|
||||
{
|
||||
if (NSShouldRetainWithZone(self, zone))
|
||||
{
|
||||
return RETAIN(self);
|
||||
}
|
||||
return [[NSSortDescriptor allocWithZone: zone]
|
||||
initWithKey: _key ascending: _ascending selector: _selector];
|
||||
}
|
||||
|
||||
- (void) dealloc
|
||||
{
|
||||
TEST_RELEASE(_key);
|
||||
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (unsigned) hash
|
||||
{
|
||||
return _ascending + (unsigned)(uintptr_t)_selector + [_key hash];
|
||||
}
|
||||
|
||||
- (id) initWithKey: (NSString *) key ascending: (BOOL) ascending
|
||||
{
|
||||
return [self initWithKey: key ascending: ascending selector: NULL];
|
||||
|
@ -73,9 +109,25 @@
|
|||
}
|
||||
}
|
||||
|
||||
- (BOOL) ascending
|
||||
- (BOOL) isEqual: (id)other
|
||||
{
|
||||
return _ascending;
|
||||
if (other == self)
|
||||
{
|
||||
return YES;
|
||||
}
|
||||
if ([other isKindOfClass: [NSSortDescriptor class]] == NO)
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
if (((NSSortDescriptor*)other)->_ascending != _ascending)
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
if (!sel_eq(((NSSortDescriptor*)other)->_selector, _selector))
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
return [((NSSortDescriptor*)other)->_key isEqualToString: _key];
|
||||
}
|
||||
|
||||
- (NSString *) key
|
||||
|
@ -83,35 +135,17 @@
|
|||
return _key;
|
||||
}
|
||||
|
||||
- (id) reversedSortDescriptor
|
||||
{
|
||||
return AUTORELEASE([[NSSortDescriptor alloc]
|
||||
initWithKey: _key ascending: !_ascending selector: _selector]);
|
||||
}
|
||||
|
||||
- (SEL) selector
|
||||
{
|
||||
return _selector;
|
||||
}
|
||||
|
||||
- (NSComparisonResult) compareObject: (id) object1 toObject: (id) object2
|
||||
{
|
||||
NSComparisonResult result;
|
||||
id comparedKey1 = [object1 valueForKeyPath: _key],
|
||||
comparedKey2 = [object2 valueForKeyPath: _key];
|
||||
|
||||
result = (NSComparisonResult) [comparedKey1 performSelector: _selector
|
||||
withObject: comparedKey2];
|
||||
|
||||
if (_ascending != YES)
|
||||
{
|
||||
result = -result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
- (id) reversedSortDescriptor
|
||||
{
|
||||
return [[[NSSortDescriptor alloc]
|
||||
initWithKey: _key ascending: !_ascending selector: _selector]
|
||||
autorelease];
|
||||
}
|
||||
|
||||
- (void) encodeWithCoder: (NSCoder *) coder
|
||||
{
|
||||
if ([coder allowsKeyedCoding])
|
||||
|
@ -129,9 +163,9 @@
|
|||
}
|
||||
}
|
||||
|
||||
- initWithCoder: (NSCoder *) decoder
|
||||
- (id) initWithCoder: (NSCoder *)decoder
|
||||
{
|
||||
if ([super init])
|
||||
if ((self = [super init]) != nil)
|
||||
{
|
||||
if ([decoder allowsKeyedCoding])
|
||||
{
|
||||
|
@ -146,19 +180,8 @@
|
|||
[decoder decodeValueOfObjCType: @encode(BOOL) at: &_ascending];
|
||||
[decoder decodeValueOfObjCType: @encode(SEL) at: &_selector];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
else
|
||||
{
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
- (id) copyWithZone: (NSZone*) zone
|
||||
{
|
||||
return [[NSSortDescriptor allocWithZone: zone]
|
||||
initWithKey: _key ascending: _ascending selector: _selector];
|
||||
return self;
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
Loading…
Reference in a new issue