Order operations category on NSArray
Objective-C
Pull request Compare This branch is even with FriskyElectrocat:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
FESOrderOpsDemo
NSArray+FESOrderOps.h
NSArray+FESOrderOps.m
README.md

README.md

NSArray+FESOrderOps Category

This is a simple category on NSArray to provide a few ordering operations. The basic premise is that you don't always want to sequentially work through an array's contents.

Most of these methods were written during film production where rendering a sequence of frames sequentially (say 1-100) was not ideal. Being able to start work on frames across the entire sequence would allow the artist to gauge more readily if the entire render job should continue or if it needed to be stopped and corrected. If the job was fine after the first few frames finish and are checked then the remainder of the frames could continue and the rendered sequence would fill in as frames finished.

But that's not why you're here, on to the category.

Ordering your NSArray

The category adds the following to NSArray:

@interface NSArray (FESOrderOps)
- (NSArray *)fes_shuffle;
- (NSArray *)fes_halfOrder;
- (NSArray *)fes_dice;
- (NSArray *)fes_rollWithPowerOfTwo:(int)power;
- (NSArray *)fes_roll;
@end

fes_shuffle:

A basic Fisher-Yates Shuffle using the arc4random_uniform(3) function for psuedorandom values. Returns a new NSArray with the new order. While not strictly putting an array in a known order this is included for completeness in case the user does want to shuffle, say for a deck of cards.

fes_halfOrder:

Takes the half-way point of an array, and then keeps taking the half-way point until the array is exhausted. Returns a new NSArray with the new order.

fes_dice:

Takes the first and last element of the array and then runs fes_halfOrder: on the remainder. This provides a slightly faster glimpse of the bounds of a sequence of operations than a pure halfOrder: would. Returns an NSArray with the new order.

fes_rollWithPowerOfTwo:

Starts at 0 position and rolls across the array selecting values at a specified interval that is a power of two (2). When the source array is no longer long enough to allow that size of a roll it will step down to the next power of two until the list is exhausted. Returns an NSArray with the new order.

fes_roll:

Uses fes_rollWithPowerOfTwo: to return a roll with a power of five (5) for an initial roll size of 32. Consider this the default parameter version of fes_rollWithPowerOfTwo:. Returns an NSArray with the new order.

License

This code is under the MIT license:

Copyright © 2012 Dan Weeks/Frisky Electrocat Studio

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

The Future

While this is pretty straight forward I know more work can go into this. Blocks for one would be nice. This version is a straight port from Python code that I have used for quite a while.