NSMutableArray+QueueAdditions.m 861 Bytes
//
//  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