2008-02-16 02:46:37 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
*/
|
2022-02-07 05:49:27 +00:00
|
|
|
+ (NSArray*)arrayWithRandomLongs:(NSUInteger)count {
|
2008-02-16 02:46:37 +00:00
|
|
|
NSMutableArray* randomLongs = [NSMutableArray arrayWithCapacity:count];
|
|
|
|
NSUInteger i;
|
2022-02-07 05:49:27 +00:00
|
|
|
for(i = 0; i < count; ++i) {
|
2022-06-17 13:39:02 +00:00
|
|
|
[randomLongs addObject:@(random())];
|
2008-02-16 02:46:37 +00:00
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2008-02-16 02:46:37 +00:00
|
|
|
return randomLongs;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
+ (NSArray*)zipArray:(NSArray*)x withArray:(NSArray*)y {
|
2008-02-16 02:46:37 +00:00
|
|
|
NSUInteger xCount = [x count];
|
|
|
|
NSUInteger yCount = [y count];
|
|
|
|
NSUInteger minCount = (xCount < yCount) ? xCount : yCount;
|
|
|
|
NSMutableArray* pairs = [NSMutableArray arrayWithCapacity:minCount];
|
|
|
|
NSUInteger i;
|
2022-02-07 05:49:27 +00:00
|
|
|
for(i = 0; i < minCount; ++i) {
|
2022-01-19 02:12:57 +00:00
|
|
|
NSArray* p = @[[x objectAtIndex:i], [y objectAtIndex:i]];
|
2008-02-16 02:46:37 +00:00
|
|
|
[pairs addObject:p];
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2008-02-16 02:46:37 +00:00
|
|
|
return pairs;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
+ (NSArray*)unzipArray:(NSArray*)pairs {
|
2008-02-16 02:46:37 +00:00
|
|
|
NSMutableArray* first = [NSMutableArray arrayWithCapacity:[pairs count]];
|
|
|
|
NSMutableArray* second = [NSMutableArray arrayWithCapacity:[pairs count]];
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
for(NSArray* pair in pairs) {
|
2008-02-16 02:46:37 +00:00
|
|
|
[first addObject:[pair first]];
|
|
|
|
[second addObject:[pair second]];
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
return @[first, second];
|
|
|
|
}
|
2008-02-16 02:46:37 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Compares two pairs by their first objects.
|
|
|
|
*/
|
2022-02-07 05:49:27 +00:00
|
|
|
- (NSComparisonResult)compareFirsts:(id)y {
|
2008-02-16 02:46:37 +00:00
|
|
|
return [[self first] compare:[y first]];
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (id)first {
|
2008-02-16 02:46:37 +00:00
|
|
|
return [self objectAtIndex:0];
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (id)second {
|
2008-02-16 02:46:37 +00:00
|
|
|
return [self objectAtIndex:1];
|
|
|
|
}
|
|
|
|
|
2022-01-19 02:12:57 +00:00
|
|
|
@end
|