2014-05-06 19:41:08 +02:00
|
|
|
#import "Queue.h"
|
|
|
|
#import "Constraints.h"
|
|
|
|
|
|
|
|
@implementation Queue {
|
|
|
|
@private NSMutableArray* items;
|
|
|
|
}
|
|
|
|
-(id) init {
|
|
|
|
if (self = [super init]) {
|
|
|
|
self->items = [NSMutableArray array];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
-(void) enqueue:(id)item {
|
|
|
|
[items addObject:item];
|
|
|
|
}
|
|
|
|
-(id) tryDequeue {
|
2014-08-14 03:13:24 +02:00
|
|
|
if (self.count == 0) return nil;
|
2014-05-06 19:41:08 +02:00
|
|
|
return [self dequeue];
|
|
|
|
}
|
|
|
|
-(id) dequeue {
|
2014-08-14 03:13:24 +02:00
|
|
|
requireState(self.count > 0);
|
2014-08-13 02:02:29 +02:00
|
|
|
id result = items[0];
|
2014-05-06 19:41:08 +02:00
|
|
|
[items removeObjectAtIndex:0];
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
-(id) peek {
|
2014-08-14 03:13:24 +02:00
|
|
|
requireState(self.count > 0);
|
2014-08-13 02:02:29 +02:00
|
|
|
return items[0];
|
2014-05-06 19:41:08 +02:00
|
|
|
}
|
|
|
|
-(id) peekAt:(NSUInteger)offset {
|
2014-08-14 03:13:24 +02:00
|
|
|
require(offset < self.count);
|
2014-08-13 02:02:29 +02:00
|
|
|
return items[offset];
|
2014-05-06 19:41:08 +02:00
|
|
|
}
|
|
|
|
-(NSUInteger) count {
|
2014-08-14 03:13:24 +02:00
|
|
|
return items.count;
|
2014-05-06 19:41:08 +02:00
|
|
|
}
|
|
|
|
@end
|