Completely overhaul how we do sorting in -base. GSSorting.h now defines an

interface that can be used for all sorting tasks in the library. The actual sort
algorithms to use are now plugable. Timsort is the new default sorting
algorithm, the existing algorithms, shellsort and quicksort, can still be
selected using a configure switch.

Also implement the new NSComparator (blocks) based sorting and insertion index
searching methods for NSMutableArray and NSArray.



git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@35573 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Niels Grewe 2012-09-19 13:31:09 +00:00
parent e26964a166
commit f8fd11f3fd
18 changed files with 2024 additions and 188 deletions

View file

@ -1,3 +1,45 @@
2012-09-19 Niels Grewe <niels.grewe@halbordnung.de>
* Source/GSSorting.h
* Source/NSSortDescriptor.m:
Add new generic interface for sorting.
* Source/GSTimSort.m: Implement timsort sorting algorithm.
* Source/GSQuickSort.m
* Source/GSShellSort.m:
Factor out previously used sorting algorithms.
* Source/GSArray.m
* Source/NSArray.m:
Modify to use the new sorting interface for sorting using
functions, NSSortDescriptor, and NSComparator. Implement
NSComparator based sorting and insertion index searching
methods.
* Source/GNUmakefile: Connect new files to the build.
* configure.ac: Add configure switch to allow selection of sort
algorithm.
* Headers/GNUstepBase/GSConfig.h.in: Add defines for sort
algorithm selection.
* configure: Regenerate
* Headers/Foundation/NSObjCRuntime.h
* Headers/Foundation/NSArray.h:
Declarations for new NSComparator based sorting methods.
* Tests/base/NSArray/general.m
* Tests/base/NSArray/blocks.m:
Add tests for new functionality.
* Tests/base/NSArray/random.plist
* Tests/base/NSArray/sorted.plist:
Add sorting example data that is large enough to test more
sophisticated sorting algorithms.
Completely overhaul how we do sorting in -base. GSSorting.h now
defines an interface that can be used for all sorting tasks in
the library. The actual sort algorithms to use are now plugable.
Timsort is the new default sorting algorithm, the existing
algorithms, shellsort and quicksort can still be selected using
a configure switch, but they do not override timsort completely
because they are unstable.
Also implement the new NSComparator (blocks) based sorting and
insertion index searching methods for NSMutableArray and
NSArray.
2012-09-17 12:03-EDT Gregory John Casamento <greg.casamento@gmail.com>
Merged from the testplant branch.

View file

@ -212,6 +212,47 @@ DEFINE_BLOCK_TYPE(GSPredicateBlock, BOOL, id, NSUInteger, BOOL*);
- (NSUInteger) indexOfObjectAtIndexes: (NSIndexSet*)indexSet
options: (NSEnumerationOptions)opts
passingTest: (GSPredicateBlock)predicate;
/**
* Returns a sorted array using the block to determine the order of objects.
*/
- (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr;
/**
* Returns a sorted array using the block to determine the order of objects.
*
* The opts argument is a bitfield. Setting the NSSortConcurrent flag
* specifies that it is thread-safe. The NSSortStable bit specifies that
* it should keep equal objects in the same order.
*/
- (NSArray *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr;
enum
{
NSBinarySearchingFirstEqual = (1UL << 8), /** Specifies that the binary
* search should find the first object equal in the array.
*/
NSBinarySearchingLastEqual = (1UL << 9), /** Specifies that the binary
* search should find the last object equal in the array.
*/
NSBinarySearchingInsertionIndex = (1UL << 10), /** Specifies that the binary
* search should find the index at which an equal object should be inserted
* in order to keep the array sorted
*/
};
typedef NSUInteger NSBinarySearchingOptions;
/**
* Performs a binary search of the array within the specified range for the
* index of an object equal to obj according to cmp.
* If NSBinarySearchingInsertionIndex is specified, searches for the index
* at which such an object should be inserted.
*/
- (NSUInteger)indexOfObject:(id)obj
inSortedRange:(NSRange)r
options:(NSBinarySearchingOptions)opts
usingComparator:(NSComparator)cmp;
#endif
/**
* Accessor for subscripting. This is called by the compiler when you write
@ -266,6 +307,19 @@ DEFINE_BLOCK_TYPE(GSPredicateBlock, BOOL, id, NSUInteger, BOOL*);
context: (void*)context;
- (void) sortUsingSelector: (SEL)comparator;
#if OS_API_VERSION(100600, GS_API_LATEST)
/**
* Sorts the array using the specified comparator block.
*/
- (void) sortUsingComparator: (NSComparator)comparator;
/**
* Sorts the array using the specified comparator block and options.
*/
- (void) sortWithOptions: (NSSortOptions)options
usingComparator: (NSComparator)comparator;
#endif
#if OS_API_VERSION(GS_API_MACOSX, GS_API_LATEST)
- (void) setValue: (id)value forKey: (NSString*)key;
#endif

View file

@ -42,6 +42,7 @@
#import <GNUstepBase/GSVersionMacros.h>
#import <GNUstepBase/GSConfig.h>
#import <GNUstepBase/GSBlocks.h>
/* These typedefs must be in place before GSObjCRuntime.h is imported.
*/
@ -99,6 +100,21 @@ enum
*/
typedef NSUInteger NSEnumerationOptions;
enum
{
NSSortConcurrent = (1UL << 0), /** Specifies that the sort
* is concurrency-safe. Note that this does not mean that it will be
* carried out in a concurrent manner, only that it can be.
*/
NSSortStable = (1UL << 4), /** Specifies that the sort should keep
* equal objects in the same order in the collection.
*/
};
/** Bitfield used to specify options to control the sorting of collections.
*/
typedef NSUInteger NSSortOptions;
#import <GNUstepBase/GSObjCRuntime.h>
#if OS_API_VERSION(100500,GS_API_LATEST)
@ -153,6 +169,7 @@ NSComparisonResult;
enum {NSNotFound = NSIntegerMax};
DEFINE_BLOCK_TYPE(NSComparator, NSComparisonResult, id, id);
#ifdef __clang__
#define NS_REQUIRES_NIL_TERMINATION __attribute__((sentinel))

View file

@ -233,6 +233,27 @@ typedef struct {
#define GS_USE_LIBDISPATCH @HAVE_LIBDISPATCH@
#define GS_HAVE_OBJC_ROOT_CLASS_ATTR @GS_HAVE_OBJC_ROOT_CLASS_ATTR@
#define GS_USE_TIMSORT @GS_USE_TIMSORT@
#define GS_USE_QUICKSORT @GS_USE_QUICKSORT@
#define GS_USE_SHELLSORT @GS_USE_SHELLSORT@
#if GS_USE_TIMSORT
#define GS_DISABLE_QUICKSORT
#define GS_DISABLE_SHELLSORT
#endif
/*
* QuickSort and ShellSort cannot override TimSort because that would leave us
* without a stable sorting algorithm.
*/
#if GS_USE_QUICKSORT
#define GS_DISABLE_SHELLSORT
#endif
#if GS_USE_SHELLSORT
#define GS_DISABLE_QUICKSORT
#endif
#if defined(__WIN32__) || defined(_WIN32) || defined(__MS_WIN32__)
# if !defined(__WIN32__)
# define __WIN32__

View file

@ -168,6 +168,9 @@ GSSocketStream.m \
GSStream.m \
GSString.m \
GSICUString.m \
GSTimSort.m \
GSQuickSort.m \
GSShellSort.m \
GSValue.m \
NSAffineTransform.m \
NSArchiver.m \

View file

@ -37,6 +37,7 @@
#import "Foundation/NSKeyedArchiver.h"
#import "GSPrivate.h"
#import "GSSorting.h"
static SEL eqSel;
static SEL oaiSel;
@ -375,7 +376,7 @@ static Class GSInlineArrayClass;
}
}
- (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState*)state
- (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState*)state
objects: (__unsafe_unretained id[])stackbuf
count: (NSUInteger)len
{
@ -791,81 +792,50 @@ static Class GSInlineArrayClass;
_version++;
}
- (void) sortUsingFunction: (NSComparisonResult(*)(id,id,void*))compare
- (void) sortUsingFunction: (NSComparisonResult (*)(id,id,void*))compare
context: (void*)context
{
/* Shell sort algorithm taken from SortingInAction - a NeXT example */
#define STRIDE_FACTOR 3 // good value for stride factor is not well-understood
// 3 is a fairly good choice (Sedgewick)
NSUInteger c;
NSUInteger d;
NSUInteger stride = 1;
BOOL found;
NSUInteger count = _count;
#ifdef GSWARN
BOOL badComparison = NO;
#endif
_version++;
while (stride <= count)
{
stride = stride * STRIDE_FACTOR + 1;
}
if ((1 < _count) && (NULL != compare))
{
GSSortUnstable(_contents_array, NSMakeRange(0,_count), (id)compare, GSComparisonTypeFunction, context);
}
_version++;
}
while (stride > (STRIDE_FACTOR - 1))
- (void) sortWithOptions: (NSSortOptions)options
usingComparator: (NSComparator)comparator
{
_version++;
if ((1 < _count) && (NULL != comparator))
{
if (options & NSSortStable)
{
// loop to sort for each value of stride
stride = stride / STRIDE_FACTOR;
for (c = stride; c < count; c++)
{
found = NO;
if (stride > c)
{
break;
}
d = c - stride;
while (!found) /* move to left until correct place */
{
id a = _contents_array[d + stride];
id b = _contents_array[d];
NSComparisonResult r;
r = (*compare)(a, b, context);
if (r < 0)
{
#ifdef GSWARN
if (r != NSOrderedAscending)
{
badComparison = YES;
}
#endif
_contents_array[d+stride] = b;
_contents_array[d] = a;
if (stride > d)
{
break;
}
d -= stride; // jump by stride factor
}
else
{
#ifdef GSWARN
if (r != NSOrderedDescending && r != NSOrderedSame)
{
badComparison = YES;
}
#endif
found = YES;
}
}
}
if (options & NSSortConcurrent)
{
GSSortStableConcurrent(_contents_array, NSMakeRange(0,_count),
(id)comparator, GSComparisonTypeComparatorBlock, NULL);
}
else
{
GSSortStable(_contents_array, NSMakeRange(0,_count),
(id)comparator, GSComparisonTypeComparatorBlock, NULL);
}
}
#ifdef GSWARN
if (badComparison == YES)
else
{
NSWarnMLog(@"Detected bad return value from comparison");
if (options & NSSortConcurrent)
{
GSSortUnstableConcurrent(_contents_array, NSMakeRange(0,_count),
(id)comparator, GSComparisonTypeComparatorBlock, NULL);
}
else
{
GSSortUnstable(_contents_array, NSMakeRange(0,_count),
(id)comparator, GSComparisonTypeComparatorBlock, NULL);
}
}
#endif
}
_version++;
}
@ -885,7 +855,7 @@ static Class GSInlineArrayClass;
return AUTORELEASE([enumerator initWithArray: (GSArray*)self]);
}
- (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState*)state
- (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState*)state
objects: (__unsafe_unretained id[])stackbuf
count: (NSUInteger)len
{

92
Source/GSQuickSort.m Normal file
View file

@ -0,0 +1,92 @@
/* Implementation of QuickSort for GNUStep
Copyright (C) 2005-2012 Free Software Foundation, Inc.
Written by: Saso Kiselkov <diablos@manga.sk>
Date: 2005
Modified by: Niels Grewe <niels.grewe@halbordnung.de>
Date: September 2012
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
Library 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 02111 USA.
*/
#import "Foundation/NSSortDescriptor.h"
#import "Foundation/NSArray.h"
#import "Foundation/NSObjCRuntime.h"
#import "GSSorting.h"
/// 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 :-).
#ifndef GS_DISABLE_QUICKSORT
static void
_GSQuickSort(id *objects,
NSRange sortRange,
id comparisonEntity,
GSComparisonType type,
void *context)
{
if (sortRange.length > 1)
{
id pivot = objects[sortRange.location];
unsigned int left = sortRange.location + 1;
unsigned int right = NSMaxRange(sortRange);
while (left < right)
{
if (GSCompareUsingDescriptorOrComparator(left, right,
comparisonEntity, type, context) == 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);
}
}
@interface GSQuickSortPlaceHolder : NSObject
@end
@implementation GSQuickSortPlaceHolder
+ (void)load
{
_GSSortUnstable = _GSQuickSort;
}
@end
#endif

125
Source/GSShellSort.m Normal file
View file

@ -0,0 +1,125 @@
/* Implementation of ShellSort for GNUStep
Copyright (C) 1995-2012 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <rfm@gnu.org>
Modified by: Niels Grewe <niels.grewe@halbordnung.de>
Date: September 2012
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
Library 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 02111 USA.
*/
#import "Foundation/NSSortDescriptor.h"
#import "Foundation/NSArray.h"
#import "Foundation/NSObjCRuntime.h"
#import "GSSorting.h"
#ifndef GS_DISABLE_SHELLSORT
void
_GSShellSort(id *objects,
NSRange sortRange,
id comparisonEntity,
GSComparisonType type,
void *context)
{
/* Shell sort algorithm taken from SortingInAction - a NeXT example */
#define STRIDE_FACTOR 3 // good value for stride factor is not well-understood
// 3 is a fairly good choice (Sedgewick)
NSUInteger c;
NSUInteger d;
NSUInteger stride = 1;
BOOL found;
NSUInteger count = NSMaxRange(sortRange);
#ifdef GSWARN
BOOL badComparison = NO;
#endif
while (stride <= count)
{
stride = stride * STRIDE_FACTOR + 1;
}
while (stride > (STRIDE_FACTOR - 1))
{
// loop to sort for each value of stride
stride = stride / STRIDE_FACTOR;
for (c = (sortRange.location + stride); c < count; c++)
{
found = NO;
if (stride > c)
{
break;
}
d = c - stride;
while (!found) /* move to left until correct place */
{
id a = objects[d + stride];
id b = objects[d];
NSComparisonResult r;
r = GSCompareUsingDescriptorOrComparator(a, b, comparisonEntity, context);
if (r < 0)
{
#ifdef GSWARN
if (r != NSOrderedAscending)
{
badComparison = YES;
}
#endif
objects[d+stride] = b;
objects[d] = a;
if (stride > d)
{
break;
}
d -= stride; // jump by stride factor
}
else
{
#ifdef GSWARN
if (r != NSOrderedDescending && r != NSOrderedSame)
{
badComparison = YES;
}
#endif
found = YES;
}
}
}
}
#ifdef GSWARN
if (badComparison == YES)
{
NSWarnFLog(@"Detected bad return value from comparison");
}
#endif
}
@interface GSShellSortPlaceHolder : NSObject
@end
@implementation GSShellSortPlaceHolder
+ (void)load
{
_GSSortUnstable = _GSShellSort;
}
@end
#endif

148
Source/GSSorting.h Normal file
View file

@ -0,0 +1,148 @@
/* Header file for sorting functions in GNUstep
Copyright (C) 2012 Free Software Foundation, Inc.
Written by: Niels Grewe <niels.grewe@halbordnung.de>
Date: September 2012
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
Library 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 02111 USA.
*/
#import "Foundation/NSSortDescriptor.h"
#import "GNUstepBase/GSObjCRuntime.h"
#import "GNUstepBase/NSObject+GNUstepBase.h"
#import "Foundation/NSException.h"
#import "GSPrivate.h"
enum
{
GSComparisonTypeSortDescriptor = 0, /** Comparison using an NSSortDescriptor */
GSComparisonTypeComparatorBlock, /** Comparison using an NSComparator */
GSComparisonTypeFunction, /** Comparison using a comparison function of type
* NSInteger(*)(id,id,void*) */
GSComparisonTypeMax
};
typedef NSUInteger GSComparisonType;
/**
* This is the internal prototype of an unstable, non-concurrency safe sorting
* function that can be used either through NSComparator or NSSortDescriptor. It
* may or may not be implemented by one of the sorting implementations in
* GNUstep.
*/
extern void (*_GSSortUnstable)(id* buffer, NSRange range, id comparisonEntity, GSComparisonType cmprType, void *context);
/**
* This is the internal prototype of an stable, non-concurrency safe sorting
* function that can be used either through NSComparator or NSSortDescriptor.
* It may or may not be implemented by one of the sorting implementations in
* GNUstep.
*/
extern void (*_GSSortStable)(id* buffer, NSRange range, id comparisonEntity, GSComparisonType cmprType, void *context);
/**
* This is the internal prototype of an unstable, concurrency safe sorting
* function that can be used either through NSComparator or NSSortDescriptor.
* It may or may not be implemented by one of the sorting implementations in
* GNUstep.
*/
extern void (*_GSSortUnstableConcurrent)(id* buffer, NSRange range, id comparisonEntity, GSComparisonType cmprType, void *context);
/**
* This is the internal prototype of an stable, concurrency safe sorting
* function that can be used either through NSComparator or NSSortDescriptor.
* It may or may not be implemented by one of the sorting implementations in
* GNUstep.
*/
extern void (*_GSSortStableConcurrent)(id* buffer, NSRange range, id comparisonEntity, GSComparisonType cmprType, void *context);
/**
* GSSortUnstable() uses the above prototypes to provide sorting that does not
* make any specific guarantees. If no explicit unstable sorting algorithm is
* available, it will fall through to stable sorting.
*/
void
GSSortUnstable(id* buffer, NSRange range, id sortDecriptorOrCompatator, GSComparisonType cmprType, void *context);
/**
* GSSortStable() uses one of the internal sorting algorithms to provide stable
* sorting. If no stable sorting method is available, it raises an exception.
*/
void
GSSortStable(id* buffer, NSRange range, id sortDecriptorOrCompatator, GSComparisonType cmprType, void *context);
/**
* GSSortUnstableConcurrent() uses the above prototypes to provide sorting that
* does not make guarantees about stability, but allows for concurrent sorting.
* If no such sorting algorithm is available, it first falls through to stable
* concurrent sorting, then unstable non-concurrent sorting and finally stable
* concurrent sorting.
*/
void
GSSortUnstableConcurrent(id* buffer, NSRange range, id sortDecriptorOrCompatator, GSComparisonType cmprType, void *context);
/**
* GSSortStableConcurrent() uses one of the internal sorting algorithms to
* provide stable sorting that may be executed concurrently. If no such
* algorithm is available, it falls through to non-concurrent GSSortStable().
*/
void
GSSortStableConcurrent(id* buffer, NSRange range, id sortDecriptorOrCompatator, GSComparisonType cmprType, void *context);
/**
* This function finds the proper point for inserting a new key into a sorted
* range, placing the new key at the rightmost position of all equal keys.
*
* This function is provided using the implementation of the timsort algorithm.
*/
NSUInteger
GSRightInsertionPointForKeyInSortedRange(id key, id* buffer, NSRange range, NSComparator comparator);
/**
* This function finds the proper point for inserting a new key into a sorted
* range, placing the new key at the leftmost position of all equal keys.
*
* This function is provided using the implementation of the timsort algorithm.
*/
NSUInteger
GSLeftInsertionPointForKeyInSortedRange(id key, id* buffer, NSRange range, NSComparator comparator);
/**
* Convenience function to operate with sort descriptors, comparator blocks and functions.
*/
static inline NSComparisonResult
GSCompareUsingDescriptorOrComparator(id first, id second, id descOrComp, GSComparisonType cmprType, void* context)
{
switch (cmprType)
{
case GSComparisonTypeSortDescriptor:
return [(NSSortDescriptor*)descOrComp compareObject: first toObject: second];
case GSComparisonTypeComparatorBlock:
return CALL_BLOCK(((NSComparator)descOrComp), first, second);
case GSComparisonTypeFunction:
return ((NSInteger (*)(id, id, void *))descOrComp)(first, second, context);
default:
[NSException raise: @"NSInternalInconstitencyException"
format: @"Invalid comparison type"];
}
// Not reached:
return 0;
}

1090
Source/GSTimSort.m Normal file

File diff suppressed because it is too large Load diff

View file

@ -53,6 +53,7 @@
#import "GSPrivate.h"
#import "GSFastEnumeration.h"
#import "GSDispatch.h"
#import "GSSorting.h"
static BOOL GSMacOSXCompatiblePropertyLists(void)
{
if (GSPrivateDefaultsFlag(NSWriteOldStylePropertyLists) == YES)
@ -1113,6 +1114,96 @@ compare(id elem1, id elem2, void* context)
return [sortedArray makeImmutableCopyOnFail: NO];
}
- (NSArray*) sortedArrayWithOptions: (NSSortOptions)options
usingComparator:(NSComparator)comparator
{
NSMutableArray *sortedArray;
sortedArray = [[[NSMutableArrayClass allocWithZone:
NSDefaultMallocZone()] initWithArray: self copyItems: NO] autorelease];
[sortedArray sortWithOptions: options usingComparator: comparator];
return [sortedArray makeImmutableCopyOnFail: NO];
}
- (NSArray*) sortedArrayUsingComparator: (NSComparator)comparator
{
return [self sortedArrayWithOptions: 0 usingComparator: comparator];
}
- (NSUInteger) indexOfObject: (id)key
inSortedRange: (NSRange)range
options: (NSBinarySearchingOptions)options
usingComparator: (NSComparator)comparator
{
if (range.length == 0)
{
return options & NSBinarySearchingInsertionIndex ? range.location : NSNotFound;
}
if (range.length == 1)
{
switch (CALL_BLOCK(comparator, key, [self objectAtIndex: range.location]))
{
case NSOrderedSame:
return range.location;
case NSOrderedAscending:
return options & NSBinarySearchingInsertionIndex ? range.location : NSNotFound;
case NSOrderedDescending:
return options & NSBinarySearchingInsertionIndex ? (range.location + 1) : NSNotFound;
default:
// Shouldn't happen
return NSNotFound;
}
}
else
{
NSUInteger index = NSNotFound;
NSUInteger count = [self count];
GS_BEGINIDBUF(objects, count);
[self getObjects: objects];
// We use the timsort galloping to find the insertion index:
if (options & NSBinarySearchingLastEqual)
{
index = GSRightInsertionPointForKeyInSortedRange(key, objects, range, comparator);
}
else
{
// Left insertion is our default
index = GSLeftInsertionPointForKeyInSortedRange(key, objects, range, comparator);
}
GS_ENDIDBUF()
// If we were looking for the insertion point, we are done here
if (options & NSBinarySearchingInsertionIndex)
{
return index;
}
// Otherwise, we need need another equality check in order to know whether
// we need return NSNotFound.
if (options & NSBinarySearchingLastEqual)
{
// For search from the right, the equal object would be the one before the
// index, but only if it's not at the very beginning of the range (though
// that might not actually be possible, it's better to check nonetheless).
if (index > range.location)
{
index--;
}
}
/*
* For a search from the left, we'd have the correct index anyways. Check
* whether it's equal to the key and return NSNotFound otherwise
*/
return (NSOrderedSame == CALL_BLOCK(comparator, key, [self objectAtIndex: index]) ? index : NSNotFound);
}
// Never reached
return NSNotFound;
}
/**
* Returns a string formed by concatenating the objects in the receiver,
* with the specified separator string inserted between each part.
@ -2424,79 +2515,64 @@ compare(id elem1, id elem2, void* context)
- (void) sortUsingFunction: (NSComparisonResult (*)(id,id,void*))compare
context: (void*)context
{
/* Shell sort algorithm taken from SortingInAction - a NeXT example */
#define STRIDE_FACTOR 3 // good value for stride factor is not well-understood
// 3 is a fairly good choice (Sedgewick)
unsigned int c;
unsigned int d;
unsigned int stride = 1;
BOOL found;
unsigned int count = [self count];
#ifdef GSWARN
BOOL badComparison = NO;
#endif
NSUInteger count = [self count];
if ((1 < count) && (NULL != compare))
{
NSArray *res = nil;
GS_BEGINIDBUF(objects, count);
[self getObjects: objects];
while (stride <= count)
{
stride = stride * STRIDE_FACTOR + 1;
}
GSSortUnstable(objects, NSMakeRange(0,count), (id)compare, GSComparisonTypeFunction, context);
while (stride > (STRIDE_FACTOR - 1))
{
// loop to sort for each value of stride
stride = stride / STRIDE_FACTOR;
for (c = stride; c < count; c++)
{
found = NO;
if (stride > c)
{
break;
}
d = c - stride;
while (!found) /* move to left until correct place */
{
id a = [self objectAtIndex: d + stride];
id b = [self objectAtIndex: d];
NSComparisonResult r;
res = [[NSArray alloc] initWithObjects: objects count: count];
[self setArray: res];
RELEASE(res);
GS_ENDIDBUF();
}
}
r = (*compare)(a, b, context);
if (r < 0)
{
#ifdef GSWARN
if (r != NSOrderedAscending)
{
badComparison = YES;
}
#endif
IF_NO_GC(RETAIN(a));
[self replaceObjectAtIndex: d + stride withObject: b];
[self replaceObjectAtIndex: d withObject: a];
RELEASE(a);
if (stride > d)
{
break;
}
d -= stride; // jump by stride factor
}
else
{
#ifdef GSWARN
if (r != NSOrderedDescending && r != NSOrderedSame)
{
badComparison = YES;
}
#endif
found = YES;
}
}
}
}
#ifdef GSWARN
if (badComparison == YES)
- (void) sortWithOptions: (NSSortOptions)options
usingComparator: (NSComparator)comparator
{
NSUInteger count = [self count];
if ((1 < count) && (NULL != comparator))
{
NSArray *res = nil;
GS_BEGINIDBUF(objects, count);
[self getObjects: objects];
if (options & NSSortStable)
{
NSWarnMLog(@"Detected bad return value from comparison");
if (options & NSSortConcurrent)
{
GSSortStableConcurrent(objects, NSMakeRange(0,count), (id)comparator, GSComparisonTypeComparatorBlock, NULL);
}
else
{
GSSortStable(objects, NSMakeRange(0,count), (id)comparator, GSComparisonTypeComparatorBlock, NULL);
}
}
#endif
else
{
if (options & NSSortConcurrent)
{
GSSortUnstableConcurrent(objects, NSMakeRange(0,count), (id)comparator, GSComparisonTypeComparatorBlock, NULL);
}
else
{
GSSortUnstable(objects, NSMakeRange(0,count), (id)comparator, GSComparisonTypeComparatorBlock, NULL);
}
}
res = [[NSArray alloc] initWithObjects: objects count: count];
[self setArray: res];
RELEASE(res);
GS_ENDIDBUF();
}
}
- (void)sortUsingComparator: (NSComparator)comparator
{
[self sortWithOptions: 0 usingComparator: comparator];
}
/**

View file

@ -34,6 +34,7 @@
#import "GNUstepBase/GSObjCRuntime.h"
#import "GNUstepBase/NSObject+GNUstepBase.h"
#import "GSPrivate.h"
#import "GSSorting.h"
@implementation NSSortDescriptor
@ -199,53 +200,87 @@
@end
/// Swaps the two provided objects.
static inline void
SwapObjects(id * o1, id * o2)
{
id temp;
temp = *o1;
*o1 = *o2;
*o2 = temp;
/* 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 (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."];
}
}
/**
* Sorts the provided object array's sortRange according to sortDescriptor.
*/
// Quicksort algorithm copied from Wikipedia :-).
static void
SortObjectsWithDescriptor(id *objects,
NSRange sortRange,
NSSortDescriptor *sortDescriptor)
void
GSSortStable(id* buffer, NSRange range, id descriptorOrComparator, GSComparisonType type, void* context)
{
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);
}
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 (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 (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
@ -269,7 +304,7 @@ SortRange(id *objects, NSRange range, id *descriptors,
{
NSSortDescriptor *sd = (NSSortDescriptor*)descriptors[0];
SortObjectsWithDescriptor(objects, range, sd);
GSSortUnstable(objects, range, sd, GSComparisonTypeSortDescriptor, NULL);
if (numDescriptors > 1)
{
unsigned start = range.location;
@ -366,4 +401,6 @@ SortRange(id *objects, NSRange range, id *descriptors,
}
}
@end

View file

@ -4,7 +4,7 @@
#import <Foundation/NSIndexSet.h>
#import <Foundation/NSString.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSValue.h>
static NSUInteger fooCount = 0;
static NSUInteger lastIndex = NSNotFound;
@ -50,6 +50,14 @@ int main()
&& (NO == [set containsIndex: 1])),
"Can select object indices based on block predicate.");
[arp release]; arp = nil;
array = [NSArray arrayWithObjects:[NSNumber numberWithInteger:2], [NSNumber numberWithInteger:5], [NSNumber numberWithInteger:3], [NSNumber numberWithInteger:2], [NSNumber numberWithInteger:10], nil];
NSArray *sortedArray = [NSArray arrayWithObjects:[NSNumber numberWithInteger:2], [NSNumber numberWithInteger:2], [NSNumber numberWithInteger:3], [NSNumber numberWithInteger:5], [NSNumber numberWithInteger:10], nil];
PASS([sortedArray isEqualToArray:[array sortedArrayUsingComparator:^ NSComparisonResult (NSNumber *a, NSNumber *b) { return [a compare:b]; }]], "Can sort arrays with NSComparators.");
PASS(0 == [sortedArray indexOfObject:[NSNumber numberWithInteger:2] inSortedRange:NSMakeRange(0, [sortedArray count]) options:NSBinarySearchingFirstEqual usingComparator:^ NSComparisonResult (NSNumber *a, NSNumber *b) { return [a compare:b]; }], "Can find index of first object in sorted array");
PASS(1 == [sortedArray indexOfObject:[NSNumber numberWithInteger:2] inSortedRange:NSMakeRange(0, [sortedArray count]) options:NSBinarySearchingLastEqual usingComparator:^ NSComparisonResult (NSNumber *a, NSNumber *b) { return [a compare:b]; }], "Can find index of first object in sorted array");
PASS(3 == [sortedArray indexOfObject:[NSNumber numberWithInteger:4] inSortedRange:NSMakeRange(0, [sortedArray count]) options:NSBinarySearchingInsertionIndex usingComparator:^ NSComparisonResult (NSNumber *a, NSNumber *b) { return [a compare:b]; }], "Can find insertion index in sorted array");
PASS(NSNotFound == [sortedArray indexOfObject:[NSNumber numberWithInteger:4] inSortedRange:NSMakeRange(0, [sortedArray count]) options:0 usingComparator:^ NSComparisonResult (NSNumber *a, NSNumber *b) { return [a compare:b]; }], "Can not find non existant object in sorted array");
# else
SKIP("No Blocks support in the compiler.")
# endif

View file

@ -13,7 +13,7 @@ int main()
vals1 = [[[NSArray arrayWithObject:val1] arrayByAddingObject:val2] retain];
vals2 = [[vals1 arrayByAddingObject:val2] retain];
vals3 = [[vals1 arrayByAddingObject:val3] retain];
obj = [NSArray new];
arr = obj;
PASS(obj != nil && [obj isKindOfClass:[NSArray class]] && [obj count] == 0,
@ -30,16 +30,16 @@ int main()
e = [arr objectEnumerator];
v1 = [e nextObject];
v2 = [e nextObject];
PASS(e != nil && v1 == nil && v2 == nil,
PASS(e != nil && v1 == nil && v2 == nil,
"-objectEnumerator: is ok for empty array");
e = [vals1 objectEnumerator];
v1 = [e nextObject];
v2 = [e nextObject];
v3 = [e nextObject];
PASS(v1 != nil && v2 != nil && v3 == nil && [vals1 containsObject:v1] &&
PASS(v1 != nil && v2 != nil && v3 == nil && [vals1 containsObject:v1] &&
[vals1 containsObject:v2] && [v1 isEqual:val1] && [v2 isEqual: val2],
"-objectEnumerator: enumerates the array");
}
}
{
obj = [arr description];
obj = [obj propertyList];
@ -53,14 +53,14 @@ int main()
PASS(vals1 != nil && [vals1 isKindOfClass: [NSArray class]] &&
[vals1 count] == 2, "-count returns two for an array with two objects");
PASS([vals1 hash] == 2, "-hash returns two for an array with two objects");
PASS([vals1 indexOfObject:nil] == NSNotFound,
PASS([vals1 indexOfObject:nil] == NSNotFound,
"-indexOfObject: gives NSNotFound for a nil object");
PASS([vals1 indexOfObject:val3] == NSNotFound,
"-indexOfObject: gives NSNotFound for a object not in the array");
PASS([vals1 isEqualToArray:vals1],
"Array is equal to itself using -isEqualToArray:");
PASS(![vals1 isEqualToArray:vals2],"Similar arrays are not equal using -isEqualToArray:");
{
NSArray *a;
NSRange r = NSMakeRange(0,2);
@ -71,7 +71,7 @@ int main()
r = NSMakeRange(1,2);
PASS_EXCEPTION([arr subarrayWithRange:r];,@"NSRangeException","-subarrayWithRange with invalid range");
}
{
NSString *c = @"/";
NSString *s = @"Hello/A Goodbye";
@ -86,6 +86,15 @@ int main()
"-sortedArrayUsingSelector: seems ok");
}
{
NSMutableArray *unsorted = [NSMutableArray arrayWithContentsOfFile: @"random.plist"];
NSArray *expected = [NSArray arrayWithContentsOfFile: @"sorted.plist"];
[unsorted sortUsingSelector: @selector(compare:)];
PASS_EQUAL(unsorted, expected, "Sorting larger arrays works.");
}
[arp release]; arp = nil;
return 0;
}

View file

@ -0,0 +1,42 @@
(
"ahTeiKio0a",
"queeBiuja6",
"aeFienge4a",
"uDuw0TeYoo",
"ix2HiZah1u",
"iXoJoo7Iec",
"iebeeY6ech",
"uiNgu0EiX9",
"Ya0ao7eich",
"ahch5Edizu",
"aoPh8en1wo",
"IeZoo9gahN",
"AG1PheeLie",
"Gai9beiChu",
"aireik7Eth",
"aeN3Xoh1ae",
"xahGh1Reif",
"ov4YoreiFi",
"es2aiJaesu",
"eGhol6eifa",
"ein9aeLies",
"aiJahf8sha",
"aip3Boophi",
"haeyahV9iY",
"Othee6Anga",
"zeid5eey7I",
"shok5Eevah",
"Ohth6aWaey",
"teV9aw2jae",
"WeDeeRu5te",
"Nu7eop1chu",
"eeXaeDae5y",
"OoChiez5ae",
"ioSh5ooh2d",
"aiMai5koox",
"Hae0ohJa1e",
"ahCh5Re1Ee",
"Su2xuof9Ta",
"loo1Leophe",
"lie2iCuiho"
)

View file

@ -0,0 +1,40 @@
("AG1PheeLie",
"Gai9beiChu",
"Hae0ohJa1e",
"IeZoo9gahN",
"Nu7eop1chu",
"Ohth6aWaey",
"OoChiez5ae",
"Othee6Anga",
"Su2xuof9Ta",
"WeDeeRu5te",
"Ya0ao7eich",
"aeFienge4a",
"aeN3Xoh1ae",
"ahCh5Re1Ee",
"ahTeiKio0a",
"ahch5Edizu",
"aiJahf8sha",
"aiMai5koox",
"aip3Boophi",
"aireik7Eth",
"aoPh8en1wo",
"eGhol6eifa",
"eeXaeDae5y",
"ein9aeLies",
"es2aiJaesu",
"haeyahV9iY",
"iXoJoo7Iec",
"iebeeY6ech",
"ioSh5ooh2d",
"ix2HiZah1u",
"lie2iCuiho",
"loo1Leophe",
"ov4YoreiFi",
"queeBiuja6",
"shok5Eevah",
"teV9aw2jae",
"uDuw0TeYoo",
"uiNgu0EiX9",
"xahGh1Reif",
"zeid5eey7I")

39
configure vendored
View file

@ -744,6 +744,9 @@ ICU_LIBS
HAVE_ICU
HAVE_LIBDISPATCH
USE_GMP
GS_USE_TIMSORT
GS_USE_QUICKSORT
GS_USE_SHELLSORT
INCLUDE_FLAGS
LDIR_FLAGS
WARN_FLAGS
@ -1468,6 +1471,10 @@ Optional Packages:
if not using icu-config)
--with-gmp-include=PATH include path for gmp headers
--with-gmp-library=PATH library path for gmp libraries
--with-sort-algorithm=ALG force use of a specific sorting algorithm.
Possible values are timsort, quicksort, and shellsort.
Defaults to timsort. Timsort cannot be completely
disabled because it is required for stable sorting.
--with-gdomap-port=PORT alternative port for gdomap
--with-openssl-include=PATH include path for openssl headers
--with-openssl-library=PATH library path for openssl libraries
@ -29482,6 +29489,33 @@ fi
#--------------------------------------------------------------------
# Check which sorting algorithm to use.
#--------------------------------------------------------------------
# Check whether --with-sort-algorithm was given.
if test "${with_sort_algorithm+set}" = set; then
withval=$with_sort_algorithm; sort_algorithm="$withval"
else
sort_algorithm="timsort"
fi
GS_USE_TIMSORT=1
GS_USE_QUICKSORT=0
GS_USE_SHELLSORT=0
if test "$sort_algorithm" = "quicksort"; then
use_quicksort=1
else
if test "$srot_algorithm" = "shellsort"; then
use_shellsort=1
fi
fi
#--------------------------------------------------------------------
# Check whether nl_langinfo(CODESET) is supported, needed by Unicode.m.
#--------------------------------------------------------------------
@ -30533,6 +30567,9 @@ ICU_LIBS!$ICU_LIBS$ac_delim
HAVE_ICU!$HAVE_ICU$ac_delim
HAVE_LIBDISPATCH!$HAVE_LIBDISPATCH$ac_delim
USE_GMP!$USE_GMP$ac_delim
GS_USE_TIMSORT!$GS_USE_TIMSORT$ac_delim
GS_USE_QUICKSORT!$GS_USE_QUICKSORT$ac_delim
GS_USE_SHELLSORT!$GS_USE_SHELLSORT$ac_delim
INCLUDE_FLAGS!$INCLUDE_FLAGS$ac_delim
LDIR_FLAGS!$LDIR_FLAGS$ac_delim
WARN_FLAGS!$WARN_FLAGS$ac_delim
@ -30548,7 +30585,7 @@ LIBOBJS!$LIBOBJS$ac_delim
LTLIBOBJS!$LTLIBOBJS$ac_delim
_ACEOF
if test `sed -n "s/.*$ac_delim\$/X/p" conf$$subs.sed | grep -c X` = 62; then
if test `sed -n "s/.*$ac_delim\$/X/p" conf$$subs.sed | grep -c X` = 65; then
break
elif $ac_last_try; then
{ { echo "$as_me:$LINENO: error: could not make $CONFIG_STATUS" >&5

View file

@ -3266,6 +3266,31 @@ fi
AC_SUBST(USE_GMP)
#--------------------------------------------------------------------
# Check which sorting algorithm to use.
#--------------------------------------------------------------------
AC_ARG_WITH(sort-algorithm,
[ --with-sort-algorithm=ALG force use of a specific sorting algorithm.
Possible values are timsort, quicksort, and shellsort.
Defaults to timsort. Timsort cannot be completely
disabled because it is required for stable sorting.],
sort_algorithm="$withval", sort_algorithm="timsort")
GS_USE_TIMSORT=1
GS_USE_QUICKSORT=0
GS_USE_SHELLSORT=0
if test "$sort_algorithm" = "quicksort"; then
use_quicksort=1
else
if test "$srot_algorithm" = "shellsort"; then
use_shellsort=1
fi
fi
AC_SUBST(GS_USE_TIMSORT)
AC_SUBST(GS_USE_QUICKSORT)
AC_SUBST(GS_USE_SHELLSORT)
#--------------------------------------------------------------------
# Check whether nl_langinfo(CODESET) is supported, needed by Unicode.m.
#--------------------------------------------------------------------