libs-base/Source/NSSortDescriptor.m

377 lines
9.8 KiB
Mathematica
Raw Normal View History

/* Implementation for NSSortDescriptor for GNUStep
Copyright (C) 2005 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 Library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02111 USA.
*/
#include "Foundation/NSSortDescriptor.h"
#include "Foundation/NSBundle.h"
#include "Foundation/NSCoder.h"
#include "Foundation/NSException.h"
#include "Foundation/NSKeyValueCoding.h"
#include "Foundation/NSString.h"
@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];
}
- (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;
}
/* FIXME ... we should use sel_eq to compare selectors, but if we do
* our implementation of -hash will be wrong ... so we will need to
* store all instances in a set to ensure uniqueness.
*/
// if (!sel_eq(((NSSortDescriptor*)other)->_selector, _selector))
if (((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: @"Key"];
[coder encodeBool: _ascending forKey: @"Ascending"];
[coder encodeObject: NSStringFromSelector(_selector)
forKey: @"Selector"];
}
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: @"Key"]);
_ascending = [decoder decodeBoolForKey: @"Ascending"];
_selector = NSSelectorFromString([decoder
decodeObjectForKey: @"Selector"]);
}
else
{
ASSIGN(_key, [decoder decodeObject]);
[decoder decodeValueOfObjCType: @encode(BOOL) at: &_ascending];
[decoder decodeValueOfObjCType: @encode(SEL) at: &_selector];
}
}
return self;
}
@end
/// Swaps the two provided objects.
static inline void
SwapObjects(id * o1, id * o2)
{
id temp;
temp = *o1;
*o1 = *o2;
*o2 = temp;
}
/**
* Sorts the provided object array's sortRange according to sortDescriptor.
*/
// Quicksort algorithm copied from Wikipedia :-).
static void
SortObjectsWithDescriptor(id *objects,
NSRange sortRange,
NSSortDescriptor *sortDescriptor)
{
if (sortRange.length > 1)
{
id pivot = objects[sortRange.location];
unsigned int left = sortRange.location + 1;
unsigned int right = NSMaxRange(sortRange);
while (left < right)
{
if ([sortDescriptor compareObject: objects[left] toObject: pivot] ==
NSOrderedDescending)
{
SwapObjects(&objects[left], &objects[--right]);
}
else
{
left++;
}
}
SwapObjects(&objects[--left], &objects[sortRange.location]);
SortObjectsWithDescriptor(objects, NSMakeRange(sortRange.location, left
- sortRange.location), sortDescriptor);
SortObjectsWithDescriptor(objects, NSMakeRange(right,
NSMaxRange(sortRange) - right), sortDescriptor);
}
}
/**
* Finds all objects in the provided range of the objects array that
* are next to each other and evaluate with the provided sortDescriptor
* as being NSOrderedSame, records their ranges in the provided
* recordedRanges array (enlarging it as necessary) and adjusts the
* numRanges argument to indicate the new size of the range array.
* A pointer to the new location of the array of ranges is returned.
*/
static NSRange *
FindEqualityRanges(id *objects,
NSRange searchRange,
NSSortDescriptor *sortDescriptor,
NSRange *ranges,
unsigned int *numRanges)
{
unsigned int i = searchRange.location;
unsigned int n = NSMaxRange(searchRange);
if (n > 1)
{
while (i < n - 1)
{
unsigned int j;
for (j = i + 1;
j < n &&
[sortDescriptor compareObject: objects[i] toObject: objects[j]]
== NSOrderedSame;
j++);
if (j - i > 1)
{
(*numRanges)++;
ranges = (NSRange *) objc_realloc(ranges, (*numRanges) *
sizeof(NSRange));
ranges[(*numRanges)-1].location = i;
ranges[(*numRanges)-1].length = j - i;
i = j;
}
else
{
i++;
}
}
}
return ranges;
}
@implementation NSArray (NSSortDescriptorSorting)
- (NSArray *) sortedArrayUsingDescriptors: (NSArray *) sortDescriptors
{
NSMutableArray * sortedArray = [NSMutableArray arrayWithArray: self];
[sortedArray sortUsingDescriptors: sortDescriptors];
return [sortedArray makeImmutableCopyOnFail:NO];
}
@end
@implementation NSMutableArray (NSSortDescriptorSorting)
/**
* This method works like this: first, it sorts the entire
* contents of the array using the first sort descriptor. Then,
* after each sort-run, it looks whether there are sort
* descriptors left to process, and if yes, looks at the partially
* sorted array, finds all portions in it which are equal
* (evaluate to NSOrderedSame) and applies the following
* descriptor onto them. It repeats this either until all
* descriptors have been applied or there are no more equal
* portions (equality ranges) left in the array.
*/
- (void) sortUsingDescriptors: (NSArray *) sortDescriptors
{
id *objects;
unsigned int count;
NSRange *equalityRanges;
unsigned int numEqualityRanges;
unsigned int i, n;
count = [self count];
objects = (id *) objc_calloc(count, sizeof(id));
[self getObjects: objects];
equalityRanges = (NSRange *) objc_calloc(1, sizeof(NSRange));
equalityRanges[0].location = 0;
equalityRanges[0].length = count;
numEqualityRanges = 1;
for (i = 0, n = [sortDescriptors count]; i < n && equalityRanges != NULL; i++)
{
unsigned int j;
NSSortDescriptor *sortDescriptor = [sortDescriptors objectAtIndex: i];
// pass through all equality ranges and sort each of them
for (j = 0; j < numEqualityRanges; j++)
{
SortObjectsWithDescriptor(objects, equalityRanges[j],
sortDescriptor);
}
// then, if there are sort descriptors left to process
if (i < n - 1)
// reconstruct the equality ranges anew.
{
NSRange *newRanges = NULL;
unsigned newNumRanges = 0;
// process only contents of old equality ranges
for (j = 0; j < numEqualityRanges; j++)
{
newRanges = FindEqualityRanges(objects, equalityRanges[j],
sortDescriptor, newRanges, &newNumRanges);
}
objc_free(equalityRanges);
equalityRanges = newRanges;
numEqualityRanges = newNumRanges;
}
}
objc_free(equalityRanges);
// now, reconstruct our contents according to the sorted object buffer
[self setArray: [NSArray arrayWithObjects: objects count: count]];
objc_free(objects);
}
@end