2 Copyright (C) 2004 eXtrapola Srl
4 This file is part of SOPE.
6 SOPE is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #import "StructuredStack.h"
25 @implementation StructuredStack
28 [self->_stack release];
34 - (NSMutableArray *)stack {
35 if (self->_stack == nil)
36 // TODO: find a proper capacity
37 self->_stack = [[NSMutableArray alloc] initWithCapacity:16];
44 - (void)removeAllObjects {
45 [[self stack] removeAllObjects];
49 - (void)push:(id)anObject {
50 NSMutableArray *stack;
56 [stack addObject:anObject];
58 if (![self cursorFollowsFIFO])
62 self->pos = ([stack count] - 1);
66 NSMutableArray *stack;
73 count = [stack count];
76 object = [stack objectAtIndex:--count];
78 [stack removeLastObject];
81 if ([self cursorFollowsFIFO]) {
94 pos = [[self stack] count];
101 NSMutableArray *stack;
104 stack = [self stack];
105 count = [stack count];
107 if (count == 0 || pos >= count) {
121 return [stack objectAtIndex:pos];
124 - (id)currentObject {
125 NSMutableArray *stack;
128 stack = [self stack];
129 count = [stack count];
131 if (count == 0 || pos >= count)
136 return [[self stack] objectAtIndex:pos];
140 NSMutableArray *stack;
143 stack = [self stack];
145 result = ((start || pos == 0) && [stack count])
147 : [stack objectAtIndex:--pos];
151 - (void)setCursorFollowsFIFO:(BOOL)aValue {
152 self->cursorFIFO = aValue;
154 - (BOOL)cursorFollowsFIFO {
155 return self->cursorFIFO;
158 - (id)objectRelativeToCursorAtIndex:(int)anIndex {
159 NSMutableArray *stack;
162 stack = [self stack];
163 count = [stack count];
167 if (count == 0 || i < 0 || i >= count)
170 return [[self stack] objectAtIndex:i];
173 @end /* StructuredStack */