From 232fff45987b27e851ebf1898ac206db5cf81bc1 Mon Sep 17 00:00:00 2001 From: vspader Date: Sat, 16 Feb 2008 02:46:37 +0000 Subject: [PATCH] Add utils for new shuffle. --- Utils/NSArray+ShuffleUtils.h | 34 +++++++++++++++ Utils/NSArray+ShuffleUtils.m | 81 ++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 Utils/NSArray+ShuffleUtils.h create mode 100644 Utils/NSArray+ShuffleUtils.m diff --git a/Utils/NSArray+ShuffleUtils.h b/Utils/NSArray+ShuffleUtils.h new file mode 100644 index 000000000..8b5a31c98 --- /dev/null +++ b/Utils/NSArray+ShuffleUtils.h @@ -0,0 +1,34 @@ +// +// Pair.h +// Cog +// +// Created by Eric Hanneken on 2/13/08. +// Copyright 2008 Vincent Spader. All rights reserved. +// + +#import + +@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 \ No newline at end of file diff --git a/Utils/NSArray+ShuffleUtils.m b/Utils/NSArray+ShuffleUtils.m new file mode 100644 index 000000000..cc06e76e5 --- /dev/null +++ b/Utils/NSArray+ShuffleUtils.m @@ -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 \ No newline at end of file