Skip to content
Browse files

Improve efficiency of shuffledArray to O(n). Add shuffle method to NS…

…MutableArray.
  • Loading branch information...
1 parent 61f33ab commit bba7d18a25ae82e1808e170945e2f9ec9783a43a Justin Prieto committed Sep 14, 2012
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

0 comments on commit bba7d18

Please sign in to comment.
Something went wrong with that request. Please try again.