Add utils for new shuffle.
parent
a4eb42eab0
commit
232fff4598
|
@ -0,0 +1,34 @@
|
|||
//
|
||||
// Pair.h
|
||||
// Cog
|
||||
//
|
||||
// Created by Eric Hanneken on 2/13/08.
|
||||
// Copyright 2008 Vincent Spader. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Cocoa/Cocoa.h>
|
||||
|
||||
@interface NSArray (ShuffleUtils)
|
||||
|
||||
+ (NSArray *) arrayWithRandomLongs:(NSUInteger) count;
|
||||
|
||||
/*
|
||||
* zip produces a new array by pairing successive objects
|
||||
* from two input arrays until one is exhausted. Only
|
||||
* pointers are copied; the objects are not.
|
||||
*/
|
||||
+ (NSArray *)zipArray:(NSArray*)x withArray:(NSArray *) y;
|
||||
|
||||
/*
|
||||
* Unzip produces a new pair of arrays by separating
|
||||
* an input array of pairs. Only pointers are copied;
|
||||
* the objects are not.
|
||||
*/
|
||||
+ (NSArray *) unzipArray:(NSArray*) pairs;
|
||||
|
||||
- (NSComparisonResult) compareFirsts:(id) y;
|
||||
- (id)first;
|
||||
- (id)second;
|
||||
|
||||
|
||||
@end
|
|
@ -0,0 +1,81 @@
|
|||
//
|
||||
// Pair.m
|
||||
// Cog
|
||||
//
|
||||
// Created by Eric Hanneken on 2/13/08.
|
||||
// Copyright 2008 Vincent Spader. All rights reserved.
|
||||
//
|
||||
|
||||
#import "NSArray+ShuffleUtils.h"
|
||||
|
||||
|
||||
@implementation NSArray (ShuffleUtils)
|
||||
|
||||
/*
|
||||
* Generates an array of random long integers in the range
|
||||
* 0 to (2**31) - 1. The length of the array is determined
|
||||
* by the count parameter.
|
||||
*/
|
||||
+ (NSArray *) arrayWithRandomLongs:(NSUInteger) count
|
||||
{
|
||||
NSMutableArray* randomLongs = [NSMutableArray arrayWithCapacity:count];
|
||||
NSUInteger i;
|
||||
for (i = 0; i < count; ++i)
|
||||
{
|
||||
[randomLongs addObject:[NSNumber numberWithLong:random()]];
|
||||
}
|
||||
|
||||
return randomLongs;
|
||||
}
|
||||
|
||||
+ (NSArray *)zipArray:(NSArray*)x withArray:(NSArray *) y
|
||||
{
|
||||
NSUInteger xCount = [x count];
|
||||
NSUInteger yCount = [y count];
|
||||
NSUInteger minCount = (xCount < yCount) ? xCount : yCount;
|
||||
NSMutableArray* pairs = [NSMutableArray arrayWithCapacity:minCount];
|
||||
NSUInteger i;
|
||||
for (i = 0; i < minCount; ++i)
|
||||
{
|
||||
NSArray* p = [NSArray arrayWithObjects:[x objectAtIndex:i], [y objectAtIndex:i], nil];
|
||||
[pairs addObject:p];
|
||||
}
|
||||
|
||||
return pairs;
|
||||
}
|
||||
|
||||
+ (NSArray *) unzipArray:(NSArray*) pairs
|
||||
{
|
||||
NSMutableArray* first = [NSMutableArray arrayWithCapacity:[pairs count]];
|
||||
NSMutableArray* second = [NSMutableArray arrayWithCapacity:[pairs count]];
|
||||
|
||||
for (NSArray *pair in pairs)
|
||||
{
|
||||
[first addObject:[pair first]];
|
||||
[second addObject:[pair second]];
|
||||
}
|
||||
|
||||
return [NSArray arrayWithObjects:first,second,nil];
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Compares two pairs by their first objects.
|
||||
*/
|
||||
- (NSComparisonResult) compareFirsts:(id) y
|
||||
{
|
||||
return [[self first] compare:[y first]];
|
||||
}
|
||||
|
||||
- (id)first
|
||||
{
|
||||
return [self objectAtIndex:0];
|
||||
}
|
||||
|
||||
- (id)second
|
||||
{
|
||||
return [self objectAtIndex:1];
|
||||
}
|
||||
|
||||
|
||||
@end
|
Loading…
Reference in New Issue