1//
2//  ANTLRFastQueue.m
3//  ANTLR
4//
5//  Created by Ian Michell on 26/04/2010.
6// [The "BSD licence"]
7// Copyright (c) 2010 Ian Michell 2010 Alan Condit
8// All rights reserved.
9//
10// Redistribution and use in source and binary forms, with or without
11// modification, are permitted provided that the following conditions
12// are met:
13// 1. Redistributions of source code must retain the above copyright
14//    notice, this list of conditions and the following disclaimer.
15// 2. Redistributions in binary form must reproduce the above copyright
16//    notice, this list of conditions and the following disclaimer in the
17//    documentation and/or other materials provided with the distribution.
18// 3. The name of the author may not be used to endorse or promote products
19//    derived from this software without specific prior written permission.
20//
21// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32#import "ANTLRFastQueue.h"
33#import "ANTLRError.h"
34#import "ANTLRRuntimeException.h"
35
36@implementation ANTLRFastQueue
37
38//@synthesize pool;
39@synthesize data;
40@synthesize p;
41@synthesize range;
42
43+ (id) newANTLRFastQueue
44{
45    return [[ANTLRFastQueue alloc] init];
46}
47
48- (id) init
49{
50	self = [super init];
51	if ( self != nil ) {
52		data = [[AMutableArray arrayWithCapacity:100] retain];
53		p = 0;
54		range = -1;
55	}
56	return self;
57}
58
59- (void) dealloc
60{
61#ifdef DEBUG_DEALLOC
62    NSLog( @"called dealloc in ANTLRFastQueue" );
63#endif
64	if ( data ) [data release];
65	[super dealloc];
66}
67
68- (id) copyWithZone:(NSZone *)aZone
69{
70    ANTLRFastQueue *copy;
71
72    copy = [[[self class] allocWithZone:aZone] init];
73    copy.data = [data copyWithZone:nil];
74    copy.p = p;
75    copy.range = range;
76    return copy;
77}
78
79// FIXME: Java code has this, it doesn't seem like it needs to be there... Then again a lot of the code in the java runtime is not great...
80- (void) reset
81{
82	[self clear];
83}
84
85- (void) clear
86{
87	p = 0;
88    if ( [data count] )
89        [data removeAllObjects];
90}
91
92- (id) remove
93{
94	id obj = [self objectAtIndex:0];
95	p++;
96	// check to see if we have hit the end of the buffer
97	if ( p == [data count] ) {
98		// if we have, then we need to clear it out
99		[self clear];
100	}
101	return obj;
102}
103
104- (void) addObject:(id) obj
105{
106    [data addObject:obj];
107}
108
109- (NSUInteger) count
110{
111	return [data count];
112}
113
114- (NSUInteger) size
115{
116	return [data count] - p;
117}
118
119- (NSUInteger) range
120{
121    return range;
122}
123
124- (id) head
125{
126	return [self objectAtIndex:0];
127}
128
129- (id) objectAtIndex:(NSUInteger) i
130{
131    NSUInteger absIndex;
132
133    absIndex = p + i;
134	if ( absIndex >= [data count] ) {
135		@throw [ANTLRNoSuchElementException newException:[NSString stringWithFormat:@"queue index %d > last index %d", absIndex, [data count]-1]];
136	}
137	if ( absIndex < 0 ) {
138	    @throw [ANTLRNoSuchElementException newException:[NSString stringWithFormat:@"queue index %d < 0", absIndex]];
139	}
140	if ( absIndex > range ) range = absIndex;
141	return [data objectAtIndex:absIndex];
142}
143
144- (NSString *) toString
145{
146    return [self description];
147}
148
149- (NSString *) description
150{
151	NSMutableString *buf = [NSMutableString stringWithCapacity:30];
152	NSInteger n = [self size];
153	for (NSInteger i = 0; i < n; i++) {
154		[buf appendString:[[self objectAtIndex:i] description]];
155		if ((i + 1) < n) {
156			[buf appendString:@" "];
157		}
158	}
159	return buf;
160}
161
162#ifdef DONTUSENOMO
163- (NSAutoreleasePool *)getPool
164{
165    return pool;
166}
167
168- (void)setPool:(NSAutoreleasePool *)aPool
169{
170    pool = aPool;
171}
172#endif
173
174@end
175