Skip to content
Browse files

adding CWPriorityQueue from the objc playground to here

  • Loading branch information...
1 parent 02affc0 commit a5cbfb5aa6e50ea02ad2c58746c652044e790fb3 @Machx committed Jan 9, 2013
View
49 Source/CWPriorityQueue.h
@@ -0,0 +1,49 @@
+//
+// CWPriorityQueue.h
+// ObjC_Playground
+//
+// Created by Colin Wheeler on 12/18/12.
+// Copyright (c) 2012 Colin Wheeler. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+/**
+ experimental priority queue implementation
+ */
+
+@interface CWPriorityQueue : NSObject
+
+/**
+ Adds the item to the queue and sorts all items by their priority
+
+ The item is added to the Queue and then sorted by the priority it was given in
+ this API. The lower an items number is the higher its priority is in the queue.
+
+ @param item to be added to the queue
+ @param priority this number is used to sort the item in the queue
+ */
+-(void)addItem:(id)item
+ withPriority:(NSUInteger)priority;
+
+/**
+ Removes an item with the highest priority off the queue & returns it
+
+ This method grabs a reference to the item with the highest priority on the
+ queue (priority 0 or as close to it as there is), removes it from the queue
+ and then returns it to you.
+
+ @return the item with the highest priority (lowest #) or nil if queue is empty
+ */
+-(id)dequeue;
+
+/**
+ Returns all objects that have a priority in the queue that matches the argument
+
+ @return an NSSet of all objects matching the given priority, or an empty set
+ */
+-(NSSet *)allObjectsOfPriority:(NSUInteger)priority;
+
+-(NSUInteger)countofObjectsWithPriority:(NSUInteger)priority;
+
+@end
View
119 Source/CWPriorityQueue.m
@@ -0,0 +1,119 @@
+//
+// CWPriorityQueue.m
+// ObjC_Playground
+//
+// Created by Colin Wheeler on 12/18/12.
+// Copyright (c) 2012 Colin Wheeler. All rights reserved.
+//
+
+#import "CWPriorityQueue.h"
+
+@interface CWPriorityQueueItem : NSObject
+@property(retain) id item;
+@property(assign) NSUInteger priority;
+@end
+
+@implementation CWPriorityQueueItem
+
+- (id)init
+{
+ self = [super init];
+ if (self) {
+ _item = nil;
+ _priority = NSUIntegerMax; //lowest priority
+ }
+ return self;
+}
+
++(instancetype)itemWithObject:(id)object
+ andPriority:(NSUInteger)priority
+{
+ NSParameterAssert(object);
+ CWPriorityQueueItem *queueItem = [self new];
+ queueItem.item = object;
+ queueItem.priority = priority;
+ return queueItem;
+}
+
+-(NSString *)description
+{
+ return [NSString stringWithFormat:@"%@: Priority: %ld Item: %@",
+ NSStringFromClass([self class]), _priority,_item];
+}
+
+@end
+
+@interface CWPriorityQueue ()
+@property(retain) NSMutableArray *storage;
+@end
+
+@implementation CWPriorityQueue
+
+- (id)init
+{
+ self = [super init];
+ if (self) {
+ _storage = [NSMutableArray array];
+ }
+ return self;
+}
+
+-(void)_sortStorage
+{
+ [self.storage sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
+ NSUInteger obj1Priority = ((CWPriorityQueueItem *)obj1).priority;
+ NSUInteger obj2Priority = ((CWPriorityQueueItem *)obj2).priority;
+ if (obj1Priority < obj2Priority) {
+ return NSOrderedAscending;
+ } else if (obj1Priority == obj2Priority) {
+ return NSOrderedSame;
+ } else {
+ return NSOrderedDescending;
+ }
+ }];
+}
+
+-(void)addItem:(id)item
+ withPriority:(NSUInteger)priority
+{
+ NSParameterAssert(item);
+ CWPriorityQueueItem *container = [CWPriorityQueueItem itemWithObject:item
+ andPriority:priority];
+ [self.storage addObject:container];
+ [self _sortStorage];
+}
+
+-(id)dequeue
+{
+ if(self.storage.count == 0) { return nil; }
+ id obj = ((CWPriorityQueueItem *)self.storage[0]).item;
+ [self.storage removeObjectAtIndex:0];
+ return obj;
+}
+
+-(NSSet *)allObjectsOfPriority:(NSUInteger)priority;
+{
+ NSMutableSet *results = [NSMutableSet set];
+ NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
+ return (((CWPriorityQueueItem *)evaluatedObject).priority == priority);
+ }];
+ NSArray *filteredResults = [self.storage filteredArrayUsingPredicate:predicate];
+ [filteredResults cw_each:^(id object, NSUInteger index, BOOL *stop) {
+ CWPriorityQueueItem *queueItem = (CWPriorityQueueItem *)object;
+ [results addObject:queueItem.item];
+ }];
+ return results;
+}
+
+-(NSUInteger)countofObjectsWithPriority:(NSUInteger)priority
+{
+ __block int32_t count = 0;
+ [self.storage cw_eachConcurrentlyWithBlock:^(id object, NSUInteger index, BOOL *stop) {
+ if (((CWPriorityQueueItem *)object).priority == priority) {
+ OSAtomicIncrement32(&count);
+ }
+ }];
+ return (NSUInteger)count;
+}
+
+@end
View
13 Source/CWPriorityQueueTests.h
@@ -0,0 +1,13 @@
+//
+// CWPriorityQueueTests.h
+// ObjC_Playground
+//
+// Created by Colin Wheeler on 12/19/12.
+// Copyright (c) 2012 Colin Wheeler. All rights reserved.
+//
+
+#import <SenTestingKit/SenTestingKit.h>
+
+@interface CWPriorityQueueTests : SenTestCase
+
+@end
View
78 Source/CWPriorityQueueTests.m
@@ -0,0 +1,78 @@
+//
+// CWPriorityQueueTests.m
+// ObjC_Playground
+//
+// Created by Colin Wheeler on 12/19/12.
+// Copyright (c) 2012 Colin Wheeler. All rights reserved.
+//
+
+#import "CWPriorityQueueTests.h"
+#import "CWPriorityQueue.h"
+
+@implementation CWPriorityQueueTests
+
+-(void)testBasicPushAndPop
+{
+ CWPriorityQueue *queue = [CWPriorityQueue new];
+
+ [queue addItem:@"1" withPriority:1];
+ [queue addItem:@"5" withPriority:5];
+ [queue addItem:@"3" withPriority:3];
+ [queue addItem:@"2" withPriority:2];
+ [queue addItem:@"100" withPriority:100];
+ [queue addItem:@"20" withPriority:20];
+ [queue addItem:@"9" withPriority:9];
+
+ STAssertTrue([@"1" isEqualToString:[queue dequeue]], nil);
+ STAssertTrue([@"2" isEqualToString:[queue dequeue]], nil);
+ STAssertTrue([@"3" isEqualToString:[queue dequeue]], nil);
+ STAssertTrue([@"5" isEqualToString:[queue dequeue]], nil);
+ STAssertTrue([@"9" isEqualToString:[queue dequeue]], nil);
+ STAssertTrue([@"20" isEqualToString:[queue dequeue]], nil);
+ STAssertTrue([@"100" isEqualToString:[queue dequeue]], nil);
+}
+
+-(void)testObjectsOfPriority
+{
+ CWPriorityQueue *queue = [CWPriorityQueue new];
+
+ [queue addItem:@"All" withPriority:0];
+ [queue addItem:@"Glory" withPriority:3];
+ [queue addItem:@"To" withPriority:1];
+ [queue addItem:@"The" withPriority:5];
+ [queue addItem:@"Hypnotoad" withPriority:5];
+ [queue addItem:@"Fry" withPriority:13];
+ [queue addItem:@"Leela" withPriority:2];
+
+ NSSet *results = [queue allObjectsOfPriority:5];
+
+ STAssertTrue(results.count == 2, nil);
+
+ NSSet *expected = [NSSet setWithObjects:@"The",@"Hypnotoad", nil];
+ STAssertTrue([expected isEqualToSet:results], nil);
+}
+
+-(void)testCountOfPriority
+{
+ CWPriorityQueue *queue = [CWPriorityQueue new];
+
+ [queue addItem:@"1" withPriority:1];
+ [queue addItem:@"2" withPriority:2];
+ [queue addItem:@"3" withPriority:3];
+ [queue addItem:@"3" withPriority:3];
+ [queue addItem:@"3" withPriority:3];
+ [queue addItem:@"3" withPriority:3];
+ [queue addItem:@"4" withPriority:4];
+ [queue addItem:@"7" withPriority:7];
+ [queue addItem:@"9" withPriority:9];
+ [queue addItem:@"9" withPriority:9];
+
+ STAssertTrue([queue countofObjectsWithPriority:1] == 1, nil);
+ STAssertTrue([queue countofObjectsWithPriority:2] == 1, nil);
+ STAssertTrue([queue countofObjectsWithPriority:3] == 4, nil);
+ STAssertTrue([queue countofObjectsWithPriority:4] == 1, nil);
+ STAssertTrue([queue countofObjectsWithPriority:7] == 1, nil);
+ STAssertTrue([queue countofObjectsWithPriority:9] == 2, nil);
+}
+
+@end
View
1 Source/ZangetsuTouch.h
@@ -68,6 +68,7 @@
#import "NSURLAdditions.h"
#import "CWBlockTimer.h"
#import "CWFixedQueue.h"
+#import "CWPriorityQueue.h"
/**
What will never work on iOS
View
1 Zangetsu.h
@@ -77,3 +77,4 @@
#import "NSURLAdditions.h"
#import "CWBlockTimer.h"
#import "CWFixedQueue.h"
+#import "CWPriorityQueue.h"
View
18 Zangetsu.xcodeproj/project.pbxproj
@@ -115,6 +115,11 @@
B869BB3814C46D3100ED2908 /* CWRuntimeUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = B89418FD132AF54500313534 /* CWRuntimeUtilities.m */; };
B869BB3914C46D3500ED2908 /* CWRuntimeUtilities.h in Headers */ = {isa = PBXBuildFile; fileRef = B89418FC132AF54500313534 /* CWRuntimeUtilities.h */; settings = {ATTRIBUTES = (Public, ); }; };
B869C85313F6B90400739DE4 /* CWMacroTests.m in Sources */ = {isa = PBXBuildFile; fileRef = B869C85213F6B90400739DE4 /* CWMacroTests.m */; };
+ B86E6101169D1F060071D187 /* CWPriorityQueue.h in Headers */ = {isa = PBXBuildFile; fileRef = B86E60FD169D1F060071D187 /* CWPriorityQueue.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ B86E6102169D1F060071D187 /* CWPriorityQueue.m in Sources */ = {isa = PBXBuildFile; fileRef = B86E60FE169D1F060071D187 /* CWPriorityQueue.m */; };
+ B86E6106169D21620071D187 /* CWPriorityQueueTests.m in Sources */ = {isa = PBXBuildFile; fileRef = B86E6100169D1F060071D187 /* CWPriorityQueueTests.m */; };
+ B86E6107169D21830071D187 /* CWPriorityQueue.h in Headers */ = {isa = PBXBuildFile; fileRef = B86E60FD169D1F060071D187 /* CWPriorityQueue.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ B86E6108169D21BD0071D187 /* CWPriorityQueue.m in Sources */ = {isa = PBXBuildFile; fileRef = B86E60FE169D1F060071D187 /* CWPriorityQueue.m */; };
B86FC33C15BB109F009D018C /* NSURLAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = B86FC33A15BB109F009D018C /* NSURLAdditions.h */; settings = {ATTRIBUTES = (Public, ); }; };
B86FC33D15BB109F009D018C /* NSURLAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = B86FC33B15BB109F009D018C /* NSURLAdditions.m */; };
B86FC33F15BB14B0009D018C /* NSURLAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = B86FC33A15BB109F009D018C /* NSURLAdditions.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -348,6 +353,10 @@
B869BB2714C3B6C800ED2908 /* SenTestingKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SenTestingKit.framework; path = Library/Frameworks/SenTestingKit.framework; sourceTree = DEVELOPER_DIR; };
B869C85113F6B90400739DE4 /* CWMacroTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CWMacroTests.h; path = Source/CWMacroTests.h; sourceTree = "<group>"; };
B869C85213F6B90400739DE4 /* CWMacroTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CWMacroTests.m; path = Source/CWMacroTests.m; sourceTree = "<group>"; };
+ B86E60FD169D1F060071D187 /* CWPriorityQueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CWPriorityQueue.h; path = Source/CWPriorityQueue.h; sourceTree = "<group>"; };
+ B86E60FE169D1F060071D187 /* CWPriorityQueue.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CWPriorityQueue.m; path = Source/CWPriorityQueue.m; sourceTree = "<group>"; };
+ B86E60FF169D1F060071D187 /* CWPriorityQueueTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CWPriorityQueueTests.h; path = Source/CWPriorityQueueTests.h; sourceTree = "<group>"; };
+ B86E6100169D1F060071D187 /* CWPriorityQueueTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CWPriorityQueueTests.m; path = Source/CWPriorityQueueTests.m; sourceTree = "<group>"; };
B86FC33A15BB109F009D018C /* NSURLAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NSURLAdditions.h; path = Source/NSURLAdditions.h; sourceTree = "<group>"; };
B86FC33B15BB109F009D018C /* NSURLAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NSURLAdditions.m; path = Source/NSURLAdditions.m; sourceTree = "<group>"; };
B87A7AF4145D0EDC007A4C0D /* CWQueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CWQueue.h; path = Source/CWQueue.h; sourceTree = "<group>"; };
@@ -611,6 +620,8 @@
B81CADA214FC33A9009F5FD7 /* Queues */ = {
isa = PBXGroup;
children = (
+ B86E60FD169D1F060071D187 /* CWPriorityQueue.h */,
+ B86E60FE169D1F060071D187 /* CWPriorityQueue.m */,
B81CAD9814FBE680009F5FD7 /* CWBlockQueue.h */,
B81CAD9914FBE680009F5FD7 /* CWBlockQueue.m */,
B84C513314F696790046455E /* CWSerialBlockQueue.h */,
@@ -823,6 +834,8 @@
B8590751158787EF00B8B2C4 /* CWNSColorAdditionsTests.m */,
B8CE2C3215FF7CB40004D8D1 /* CWFixedQueueTests.h */,
B8CE2C3315FF7CB40004D8D1 /* CWFixedQueueTests.m */,
+ B86E60FF169D1F060071D187 /* CWPriorityQueueTests.h */,
+ B86E6100169D1F060071D187 /* CWPriorityQueueTests.m */,
);
name = "Unit Tests";
sourceTree = "<group>";
@@ -883,6 +896,7 @@
B86FC33C15BB109F009D018C /* NSURLAdditions.h in Headers */,
B8D2618915D5571100F087C4 /* CWBlockTimer.h in Headers */,
B8CE2C2D15FE2F690004D8D1 /* CWFixedQueue.h in Headers */,
+ B86E6101169D1F060071D187 /* CWPriorityQueue.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -933,6 +947,7 @@
B85908391587D3F300B8B2C4 /* UIImageAdditions.h in Headers */,
B86FC33F15BB14B0009D018C /* NSURLAdditions.h in Headers */,
B8CE2C2E15FE2F690004D8D1 /* CWFixedQueue.h in Headers */,
+ B86E6107169D21830071D187 /* CWPriorityQueue.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1150,6 +1165,7 @@
B86FC33D15BB109F009D018C /* NSURLAdditions.m in Sources */,
B8D2618A15D5571100F087C4 /* CWBlockTimer.m in Sources */,
B8CE2C2F15FE2F690004D8D1 /* CWFixedQueue.m in Sources */,
+ B86E6102169D1F060071D187 /* CWPriorityQueue.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1195,6 +1211,7 @@
B812FF58155E043100BDC6CA /* CWDoublyLinkedList.m in Sources */,
B859083A1587D3F300B8B2C4 /* UIImageAdditions.m in Sources */,
B8CE2C3015FE2F690004D8D1 /* CWFixedQueue.m in Sources */,
+ B86E6108169D21BD0071D187 /* CWPriorityQueue.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1237,6 +1254,7 @@
B86357A11561495400EF09BA /* CWDoublyLinkedListTests.m in Sources */,
B8590752158787F000B8B2C4 /* CWNSColorAdditionsTests.m in Sources */,
B8CE2C3415FF7CB40004D8D1 /* CWFixedQueueTests.m in Sources */,
+ B86E6106169D21620071D187 /* CWPriorityQueueTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

0 comments on commit a5cbfb5

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