Skip to content

Commit

Permalink
Tidy up the whole NSArray+EKStuff
Browse files Browse the repository at this point in the history
- Add some tests
- Correct counts "To optimize each loop." (#49)
- Other minor cleanups
  • Loading branch information
stanislaw committed Mar 31, 2014
1 parent 84b7d5c commit 5064b26
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 74 deletions.
2 changes: 1 addition & 1 deletion EKAlgorithms/NSArray+EKStuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
*/
- (NSNumber *)sumOfElements;

- (NSDictionary *)occurencesOfEachElementInArray_naive;
- (NSDictionary *)occurencesOfEachElementInArray;
- (NSDictionary *)occurencesOfEachElementInArrayByUsingDictionary;
- (NSDictionary *)CocoaImplementationOfOccurencesOfEachElementInArray;

//search
Expand Down
100 changes: 49 additions & 51 deletions EKAlgorithms/NSArray+EKStuff.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

#import "NSArray+EKStuff.h"
#import "NSMutableArray+EKStuff.h"

@implementation NSArray (EKStuff);

Expand All @@ -18,8 +17,10 @@ - (NSUInteger)indexOfMaximumElement
{
NSInteger maximumValue = [[self objectAtIndex:0] integerValue];
NSUInteger indexOfMaximumValue = 0;

for (NSUInteger i = 1; i < [self count]; i++) {

NSUInteger count = [self count];

for (NSUInteger i = 1; i < count; i++) {
NSInteger value = [[self objectAtIndex:i] integerValue];

if (value > maximumValue) {
Expand Down Expand Up @@ -114,7 +115,7 @@ - (NSString *)longestString
NSString *returnValue = nil;

for (NSString *string in self) {
if (returnValue == nil || [string length] > [returnValue length]) {
if (returnValue == nil || ([string length] > [returnValue length])) {
returnValue = string;
}
}
Expand All @@ -129,7 +130,7 @@ - (NSString *)shortestString
NSString *returnValue = nil;

for (NSString *string in self) {
if (returnValue == nil || [string length] < [returnValue length]) {
if (returnValue == nil || ([string length] < [returnValue length])) {
returnValue = string;
}
}
Expand All @@ -143,7 +144,7 @@ - (NSArray *)intersectionWithArray:(NSArray *)secondArray
{
NSMutableSet *intersection = [NSMutableSet setWithArray:self];
[intersection intersectSet:[NSSet setWithArray:secondArray]];

return [intersection allObjects];
}

Expand Down Expand Up @@ -203,9 +204,11 @@ + (NSArray *)randomObjectsWithArraySize:(NSUInteger)arraySize maxRandomValue:(NS

- (NSNumber *)sumOfElements
{
NSUInteger count = [self count];

long long int sum = 0;

for (NSUInteger i = 0; i < [self count]; i++) {
for (NSUInteger i = 0; i < count; i++) {
sum = sum + [self[i] longLongValue];
}

Expand All @@ -214,45 +217,47 @@ - (NSNumber *)sumOfElements

#pragma mark - Occurrences of each element in array

- (NSDictionary *)occurencesOfEachElementInArray
- (NSDictionary *)occurencesOfEachElementInArray_naive
{
NSMutableDictionary *result = [@{} mutableCopy];

for (NSUInteger i = 0; i < [self count]; i++) {
NSParameterAssert(self[i] != nil);
NSUInteger count = [self count];

NSMutableDictionary *registry = [NSMutableDictionary dictionaryWithCapacity:count];

for (NSUInteger i = 0; i < count; i++) {
NSUInteger counter = 0;
for (NSUInteger j = 0; j < [self count]; j++) {
NSParameterAssert(self[j] != nil);

for (NSUInteger j = 0; j < count; j++) {
if ([self[i] isEqual:self[j]]) {
counter++;
}
}
[result setObject:[NSNumber numberWithUnsignedInteger:counter]

[registry setObject:@(counter)
forKey:self[i]];
}

return result;
return registry;
}

- (NSDictionary *)occurencesOfEachElementInArrayByUsingDictionary
- (NSDictionary *)occurencesOfEachElementInArray
{
NSMutableDictionary *result = [@{} mutableCopy];

for (NSUInteger i = 0; i < [self count]; i++) {
NSUInteger count = [self count];

NSMutableDictionary *registry = [NSMutableDictionary dictionaryWithCapacity:count];

for (NSUInteger i = 0; i < count; i++) {
id currentElement = self[i];

NSParameterAssert(currentElement != nil);

NSNumber *existingElementCounter = result[currentElement];

NSNumber *existingElementCounter = registry[currentElement];

NSUInteger currentCount = existingElementCounter ? existingElementCounter.unsignedIntegerValue : 0;

currentCount++;

result[currentElement] = [NSNumber numberWithUnsignedInteger:currentCount];
registry[currentElement] = @(currentCount);
}

return result;
return registry;
}

- (NSDictionary *)CocoaImplementationOfOccurencesOfEachElementInArray
Expand All @@ -261,7 +266,6 @@ - (NSDictionary *)CocoaImplementationOfOccurencesOfEachElementInArray
NSMutableDictionary *dictionary = [@{} mutableCopy];

for (id object in [countedSet allObjects]) {
NSParameterAssert(object != nil);
[dictionary setObject:@([countedSet countForObject:object])
forKey:object];
}
Expand All @@ -274,19 +278,15 @@ - (NSDictionary *)CocoaImplementationOfOccurencesOfEachElementInArray

- (NSInteger)indexOfObjectViaLinearSearch:(id)object
{
NSInteger i = 0, indexOfFoundedObject = 0;

for (i = 0; i < [self count]; i++) {
if (object == [self objectAtIndex:i]) {
indexOfFoundedObject = i;
break;
NSUInteger count = [self count];

for (int i = 0; i < count; i++) {
if ([object isEqual:[self objectAtIndex:i]]) {
return i;
}
}
if (i == [self count]) {
indexOfFoundedObject = -1;
}

return indexOfFoundedObject;

return NSNotFound;
}

#pragma mark - Binary search
Expand All @@ -295,24 +295,22 @@ - (NSInteger)indexOfObjectViaBinarySearch:(id)object
{
NSUInteger firstIndex = 0;
NSUInteger uptoIndex = [self count];
NSUInteger indexOfFoundedObject = 0;


while (firstIndex < uptoIndex) {
NSUInteger mid = (firstIndex + uptoIndex) / 2;
if ([object isKindOfClass:[NSNumber class]]) {
if ([object integerValue] < [[self objectAtIndex:mid] integerValue]) {
uptoIndex = mid;
}
else if ([object integerValue] > [[self objectAtIndex:mid] integerValue]) {
firstIndex = mid + 1;
}
else {
return indexOfFoundedObject = mid;
}

if ([object integerValue] < [[self objectAtIndex:mid] integerValue]) {
uptoIndex = mid;
}
else if ([object integerValue] > [[self objectAtIndex:mid] integerValue]) {
firstIndex = mid + 1;
}
else {
return mid;
}
}

return indexOfFoundedObject = -1;
return NSNotFound;
}


Expand Down
9 changes: 0 additions & 9 deletions EKAlgorithms/NSMutableArray+EKStuff.h

This file was deleted.

Empty file.
8 changes: 0 additions & 8 deletions EKAlgorithmsApp/EKAlgorithmsApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@
860B78821858133A0071079A /* EKLinkedList.m in Sources */ = {isa = PBXBuildFile; fileRef = 860B78701858133A0071079A /* EKLinkedList.m */; };
860B78831858133A0071079A /* EKNode.m in Sources */ = {isa = PBXBuildFile; fileRef = 860B78721858133A0071079A /* EKNode.m */; };
860B78841858133A0071079A /* NSArray+EKStuff.m in Sources */ = {isa = PBXBuildFile; fileRef = 860B78741858133A0071079A /* NSArray+EKStuff.m */; };
860B78851858133A0071079A /* NSMutableArray+EKStuff.m in Sources */ = {isa = PBXBuildFile; fileRef = 860B78761858133A0071079A /* NSMutableArray+EKStuff.m */; };
860B78861858133A0071079A /* NSNumber+EKStuff.m in Sources */ = {isa = PBXBuildFile; fileRef = 860B78781858133A0071079A /* NSNumber+EKStuff.m */; };
860B78871858133A0071079A /* NSString+EKStuff.m in Sources */ = {isa = PBXBuildFile; fileRef = 860B787A1858133A0071079A /* NSString+EKStuff.m */; };
860B7889185814810071079A /* NSArray.m in Sources */ = {isa = PBXBuildFile; fileRef = 860B7888185814810071079A /* NSArray.m */; };
860B788A185815BC0071079A /* NSArray+EKStuff.m in Sources */ = {isa = PBXBuildFile; fileRef = 860B78741858133A0071079A /* NSArray+EKStuff.m */; };
8641D23F18C88D6700B109E4 /* NSMutableArray+EKStuff.m in Sources */ = {isa = PBXBuildFile; fileRef = 860B78761858133A0071079A /* NSMutableArray+EKStuff.m */; };
8641D24618C89B7100B109E4 /* NSArray+Selection.m in Sources */ = {isa = PBXBuildFile; fileRef = 8641D24518C89B7100B109E4 /* NSArray+Selection.m */; };
8641D24718C89BC900B109E4 /* NSArray+Selection.m in Sources */ = {isa = PBXBuildFile; fileRef = 8641D24518C89B7100B109E4 /* NSArray+Selection.m */; };
8692312418E983A2002F7CBA /* NSArray+EKSorting.m in Sources */ = {isa = PBXBuildFile; fileRef = 86A39FCE18E97F3300124215 /* NSArray+EKSorting.m */; };
Expand Down Expand Up @@ -116,8 +114,6 @@
860B78721858133A0071079A /* EKNode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EKNode.m; sourceTree = "<group>"; };
860B78731858133A0071079A /* NSArray+EKStuff.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSArray+EKStuff.h"; sourceTree = "<group>"; };
860B78741858133A0071079A /* NSArray+EKStuff.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSArray+EKStuff.m"; sourceTree = "<group>"; };
860B78751858133A0071079A /* NSMutableArray+EKStuff.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSMutableArray+EKStuff.h"; sourceTree = "<group>"; };
860B78761858133A0071079A /* NSMutableArray+EKStuff.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSMutableArray+EKStuff.m"; sourceTree = "<group>"; };
860B78771858133A0071079A /* NSNumber+EKStuff.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSNumber+EKStuff.h"; sourceTree = "<group>"; };
860B78781858133A0071079A /* NSNumber+EKStuff.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSNumber+EKStuff.m"; sourceTree = "<group>"; };
860B78791858133A0071079A /* NSString+EKStuff.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSString+EKStuff.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -264,8 +260,6 @@
860B785B1858133A0071079A /* Data Structures */,
860B78731858133A0071079A /* NSArray+EKStuff.h */,
860B78741858133A0071079A /* NSArray+EKStuff.m */,
860B78751858133A0071079A /* NSMutableArray+EKStuff.h */,
860B78761858133A0071079A /* NSMutableArray+EKStuff.m */,
860B78771858133A0071079A /* NSNumber+EKStuff.h */,
860B78781858133A0071079A /* NSNumber+EKStuff.m */,
860B78791858133A0071079A /* NSString+EKStuff.h */,
Expand Down Expand Up @@ -502,7 +496,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
860B78851858133A0071079A /* NSMutableArray+EKStuff.m in Sources */,
860B787D1858133A0071079A /* EKDeque.m in Sources */,
8FF772EF18E6E24300DF6B3A /* EKAVLTree.m in Sources */,
8F8F83FE18E75E5A0009D125 /* EKBHeap.m in Sources */,
Expand Down Expand Up @@ -545,7 +538,6 @@
86A2D37C188D2705004A908F /* NSArray+EKGeometry.m in Sources */,
8F8F83FF18E75E5A0009D125 /* EKBHeap.m in Sources */,
86A2D378188D2440004A908F /* EKGeometry.m in Sources */,
8641D23F18C88D6700B109E4 /* NSMutableArray+EKStuff.m in Sources */,
860B7850185811420071079A /* main.m in Sources */,
869FE65F186419E600509FE7 /* NSNumber.m in Sources */,
8FF772F418E6E53F00DF6B3A /* EKAVLTreeNode.m in Sources */,
Expand Down
31 changes: 26 additions & 5 deletions EKAlgorithmsSpecs/NSArray.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#import <Kiwi/Kiwi.h>

#import "NSArray+EKStuff.h"
#import "NSMutableArray+EKStuff.h"
#import "NSArray+Selection.h"
#import "NSArray+EKSorting.h"

Expand Down Expand Up @@ -245,7 +244,7 @@
});

describe(@"Occurrences of each element in array", ^{
describe(@"occurencesOfEachElementInArray", ^{
describe(@"occurencesOfEachElementInArray_naive", ^{
specify(^{
NSMutableArray *originalArray = [NSMutableArray array];

Expand All @@ -258,6 +257,27 @@
}
}

NSDictionary *result = [originalArray occurencesOfEachElementInArray_naive];

for (int i = 1; i <= N; i++) {
[[result[@(i)] should] equal:@(i)];
}
});
});

describe(@"occurencesOfEachElementInArray", ^{
specify(^{
NSMutableArray *originalArray = [NSMutableArray array];

// Just the array to test the method against
// [@(1), @(2), @(2), @(3), @(3), @(3), ...]
int N = 100;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= i; j++) {
[originalArray addObject:@(i)];
}
}

NSDictionary *result = [originalArray occurencesOfEachElementInArray];

for (int i = 1; i <= N; i++) {
Expand All @@ -266,26 +286,27 @@
});
});

describe(@"occurencesOfEachElementInArrayByUsingDictionary", ^{
describe(@"CocoaImplementationOfOccurencesOfEachElementInArray", ^{
specify(^{
NSMutableArray *originalArray = [NSMutableArray array];

// Just the array to test the method against
// [@(1), @(2), @(2), @(3), @(3), @(3), ...]
int N = 10;
int N = 100;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= i; j++) {
[originalArray addObject:@(i)];
}
}

NSDictionary *result = [originalArray occurencesOfEachElementInArrayByUsingDictionary];
NSDictionary *result = [originalArray CocoaImplementationOfOccurencesOfEachElementInArray];

for (int i = 1; i <= N; i++) {
[[result[@(i)] should] equal:@(i)];
}
});
});

});

describe(@"Selection algorithms", ^{
Expand Down

0 comments on commit 5064b26

Please sign in to comment.