1 /************************
2 A Cocoa DataStructuresFramework
3 Copyright (C) 2002 Phillip Morelock in the United States
4 http://www.phillipmorelock.com
5 Other copyrights for this specific file as acknowledged herein.
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *******************************/
22 /////SEE LICENSE FILE FOR LICENSE INFORMATION///////
24 #import "ArrayQueue.h"
26 @implementation ArrayQueue
30 return [self initWithCapacity:10];
33 -initWithCapacity:(unsigned)capacity
37 theQ = [[NSMutableArray alloc] initWithCapacity:capacity];
43 niller = [[NSString stringWithFormat:@"nothing"] retain];
55 -(BOOL) enqueue: (id)enqueuedObj
57 if ( enqueuedObj == nil )
61 [theQ insertObject:enqueuedObj atIndex:backIndex];
73 //decrement the size of the Q
76 //get it and retain it
77 theObj = [[theQ objectAtIndex:frontIndex] retain];
78 [theQ replaceObjectAtIndex:frontIndex withObject:niller];
80 //now increment front -- if we have large array and we've "caught up" with
81 //the back, then let's dealloc and start over.
83 if (frontIndex > 25 && qSize < 0)
85 [self removeAllObjects];
88 return [theObj autorelease];
96 //simple BOOL for whether the queue is empty or not.
105 -(void) removeAllObjects
110 theQ = [[NSMutableArray alloc] initWithCapacity:10];
117 +(ArrayQueue *)queueWithArray:(NSArray *)array
118 ofOrder:(BOOL)direction
123 q = [[ArrayQueue alloc] init];
129 else if (direction)//so the order to dequeue will be from 0...n
132 [q enqueue: [array objectAtIndex: i++]];
134 else //order to dequeue will be n...0
137 [q enqueue: [array objectAtIndex: --s]];
140 return [q autorelease];