Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Improvements to shuffledArray #135

Merged
merged 1 commit into from

2 participants

@density

Added a more efficient shuffle method to NSMutableArray, and changed NSArray's shuffledArray method to use it.

@soffes
Owner

Looks great! Thanks!

@soffes soffes merged commit f62b00f into soffes:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 14, 2012
  1. Improve efficiency of shuffledArray to O(n). Add shuffle method to NS…

    Justin Prieto authored
    …MutableArray.
This page is out of date. Refresh to see the latest.
View
8 SSToolkit.xcodeproj/project.pbxproj
@@ -7,6 +7,8 @@
objects = {
/* Begin PBXBuildFile section */
+ 5841B48416013CDE000C05BD /* NSMutableArray+SSToolkitAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 5841B48216013CDE000C05BD /* NSMutableArray+SSToolkitAdditions.h */; };
+ 5841B48516013CDE000C05BD /* NSMutableArray+SSToolkitAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 5841B48316013CDE000C05BD /* NSMutableArray+SSToolkitAdditions.m */; };
962F5F5E156CCD1200408648 /* NSNumber+SSToolkitAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 962F5F5B156CCD1200408648 /* NSNumber+SSToolkitAdditions.h */; };
962F5F5F156CCD1200408648 /* NSNumber+SSToolkitAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 962F5F5C156CCD1200408648 /* NSNumber+SSToolkitAdditions.m */; };
9EB278B114BBC4360077194C /* SSConcurrentOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = B24E9E11121DC29A0085F81E /* SSConcurrentOperation.m */; };
@@ -169,6 +171,8 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
+ 5841B48216013CDE000C05BD /* NSMutableArray+SSToolkitAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSMutableArray+SSToolkitAdditions.h"; sourceTree = "<group>"; };
+ 5841B48316013CDE000C05BD /* NSMutableArray+SSToolkitAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSMutableArray+SSToolkitAdditions.m"; sourceTree = "<group>"; };
962F5F5B156CCD1200408648 /* NSNumber+SSToolkitAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSNumber+SSToolkitAdditions.h"; sourceTree = "<group>"; };
962F5F5C156CCD1200408648 /* NSNumber+SSToolkitAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSNumber+SSToolkitAdditions.m"; sourceTree = "<group>"; };
962F5F5D156CCD1200408648 /* UIColor+SSToolkitAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIColor+SSToolkitAdditions.m"; sourceTree = "<group>"; };
@@ -488,6 +492,8 @@
B2601A711223079A005506D5 /* Foundation */ = {
isa = PBXGroup;
children = (
+ 5841B48216013CDE000C05BD /* NSMutableArray+SSToolkitAdditions.h */,
+ 5841B48316013CDE000C05BD /* NSMutableArray+SSToolkitAdditions.m */,
962F5F5B156CCD1200408648 /* NSNumber+SSToolkitAdditions.h */,
962F5F5C156CCD1200408648 /* NSNumber+SSToolkitAdditions.m */,
962F5F5D156CCD1200408648 /* UIColor+SSToolkitAdditions.m */,
@@ -735,6 +741,7 @@
B2F72A131534D54C00556B66 /* SSRateLimit.h in Headers */,
B2C7ED6C153A5624006ABE73 /* SSButton.h in Headers */,
962F5F5E156CCD1200408648 /* NSNumber+SSToolkitAdditions.h in Headers */,
+ 5841B48416013CDE000C05BD /* NSMutableArray+SSToolkitAdditions.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -962,6 +969,7 @@
B2F72A141534D54C00556B66 /* SSRateLimit.m in Sources */,
B2C7ED6D153A5624006ABE73 /* SSButton.m in Sources */,
962F5F5F156CCD1200408648 /* NSNumber+SSToolkitAdditions.m in Sources */,
+ 5841B48516013CDE000C05BD /* NSMutableArray+SSToolkitAdditions.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
17 SSToolkit/NSArray+SSToolkitAdditions.m
@@ -7,6 +7,7 @@
//
#import "NSArray+SSToolkitAdditions.h"
+#import "NSMutableArray+SSToolkitAdditions.h"
#import "NSData+SSToolkitAdditions.h"
@interface NSArray (SSToolkitPrivateAdditions)
@@ -28,20 +29,10 @@ - (id)randomObject {
return [self objectAtIndex:arc4random() % [self count]];
}
-
- (NSArray *)shuffledArray {
-
- NSMutableArray *array = [NSMutableArray arrayWithCapacity:[self count]];
-
- NSMutableArray *copy = [self mutableCopy];
- while ([copy count] > 0) {
- NSUInteger index = arc4random() % [copy count];
- id objectToMove = [copy objectAtIndex:index];
- [array addObject:objectToMove];
- [copy removeObjectAtIndex:index];
- }
-
- return array;
+ NSMutableArray *array = [self mutableCopy];
+ [array shuffle];
+ return array;
}
View
16 SSToolkit/NSMutableArray+SSToolkitAdditions.h
@@ -0,0 +1,16 @@
+/**
+ Provides extensions to `NSMutableArray` for various common tasks.
+ */
+@interface NSMutableArray (SSToolkitAdditions)
+
+///--------------------------
+/// @name Changing the Array
+///--------------------------
+
+/**
+ Shuffles the elements of this array in-place using the Fisher-Yates algorithm
+
+ */
+- (void)shuffle;
+
+@end
View
13 SSToolkit/NSMutableArray+SSToolkitAdditions.m
@@ -0,0 +1,13 @@
+#import "NSMutableArray+SSToolkitAdditions.h"
+
+@implementation NSMutableArray (SSToolkitAdditions)
+
+- (void)shuffle
+{
+ for (NSUInteger i = [self count] - 1; i > 0; i--) {
+ [self exchangeObjectAtIndex:arc4random_uniform(i + 1)
+ withObjectAtIndex:i];
+ }
+}
+
+@end
Something went wrong with that request. Please try again.