mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-30 08:21:25 +00:00
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:
parent
e26964a166
commit
f8fd11f3fd
18 changed files with 2024 additions and 188 deletions
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
42
Tests/base/NSArray/random.plist
Normal file
42
Tests/base/NSArray/random.plist
Normal 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"
|
||||
)
|
40
Tests/base/NSArray/sorted.plist
Normal file
40
Tests/base/NSArray/sorted.plist
Normal 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")
|
Loading…
Add table
Add a link
Reference in a new issue