Mutable LIFO and FIFO in Objective-C
Branch: master
Clone or download
Latest commit 0f67fac May 26, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Documentation basics May 21, 2018
LICENSE Create LICENSE May 21, 2018
Queue.h basics May 21, 2018
Queue.m basics May 21, 2018
README.md Update README.md May 25, 2018
Stack.h basics May 21, 2018
Stack.m basics May 21, 2018

README.md

Stack + Queue

language license documentation

I find myself building these classes all the time, so I decided to create them in a feature complete way once and for all. They're pretty simple, but they're designed to behave as similarly to foundation-level collections as possible.

Usage

Stack

Stack *myStack = [[Stack alloc] init];              // []

[myStack push:@"A"];                                // ["A"]
[myStack push:@"B"];                                // ["A", "B"]

[myStack pushObjects:@[@"C", @"D", @"E"]];          // ["A", "B", "C", "D", "E"]

NSString *last = [myStack peek];                    // "E"

[myStack pop];                                      // ["A", "B", "C", "D"]

Queue

Queue *myQueue = [[Queue alloc] init];              // []

[myStack enqueue:@"A"];                             // ["A"]
[myStack enqueue:@"B"];                             // ["A", "B"]

[myStack enqueueObjects:@[@"C", @"D", @"E"]];       // ["A", "B", "C", "D", "E"]

NSString *first = [myStack peek];                   // "A"

[myStack dequeue];                                  // ["B", "C", "D", "E"]

Features

Objective-C Lightweight Generics

Stack<NSString *> *myStack = [Stack<NSString *> stack];     // Generic String Stack

[stack push:@"1"];                                          // OK
[stack push:@1];                                            // NOT OK

Objective-C Fast Enumeration

Queue<NSNumber *> *myQueue = [Queue queueWithObjects:@1, @2, @3, @4, nil];

for (NSNumber *number in myQueue) {

    // iterate

}

Subscripting

Stack<NSString *> *myStack = [Stack stackWithArray:@[@"A", @"B", @"C", @"D"]];

NSString *first = myStack[0]                        // "A"
NSString *second = myStack[1]                       // "B"

Equality, Hashability, Encodability

Queue<NSNumber *> *myQueue = [Queue queueWithArray:@[@1, @2, @3, @4]];
Queue<NSNumber *> *myQueue2 = [Queue queueWithArray:@[@1, @2, @3, @4]];

[myQueue isEqualToQueue:myQueue2]                   // YES
[myQueue2 isEqual:myQueue]                          // YES
myQueue.hash == myQueue2.hash                       // YES

NSData *myQueueData = [NSKeyedArchiver archivedDataWithRootObject:myQueue];
Queue<NSNumber *> *myQueue3 = (Queue<NSNumber *> *)[NSKeyedUnarchiver unarchiveObjectWithData:myQueueData];

[myQueue3 isEqual:myQueue]                          // YES

Mass Key-Value Observation

You can observe every object in a stack or a queue using: -addObserver:toObjectsAtIndexes:forKeyPath:options:context:

You can remove observers using: -removeObserver:fromObjectsAtIndexes:forKeyPath:

Sorting & Filtering

See the documentation for details on the various methods for deriving or mutating sorted / filtered stacks & queues.

Documentation

Documentation is made with Jazzy, and is hosted on GitHub pages. You can find it here