NSMutableArray+QueueAdditions.m
861 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//
// NSMutableArray+QueueAdditions.m
// YHBarrage
//
// Created by Arthur on 16/6/6.
// Copyright © 2016年 YOHO!. All rights reserved.
//
#import "NSMutableArray+QueueAdditions.h"
@implementation NSMutableArray (QueueAdditions)
- (id)dequeue
{
@autoreleasepool {
@synchronized(self) {
id headObject = [self objectAtIndex:0];
if (headObject != nil) {
[self removeObjectAtIndex:0];
// [self removeObjectsInRange:NSMakeRange(0, MaxQueueCapacity/2-1)];
}
return headObject;
}
}
}
- (void)enqueue:(id)obj
{
@synchronized(self) {
if (self.count < MaxQueueCapacity) {
[self addObject:obj];
} else {
[self dequeue];
[self addObject:obj];
}
}
}
@end