1 //metadoc Stack copyright Steve Dekorte 2002
2 //metadoc Stack license BSD revised
3 /*metadoc Stack description
4 Stack - array of void pointers
5 supports setting marks - when a mark is popped,
6 all stack items above it are popped as well
12 #include "Common_inline.h"
13 #ifdef IO_DECLARE_INLINES
15 IOINLINE
void Stack_do_(const Stack
*self
, StackDoCallback
*callback
)
17 void **itemP
= self
->top
;
18 intptr_t mark
= self
->lastMark
;
20 while (itemP
> self
->items
)
22 if (itemP
- self
->items
== mark
)
24 mark
= (intptr_t)(*itemP
);
35 IOINLINE
void Stack_doUntilMark_(const Stack
*self
, StackDoCallback
*callback
)
37 void **itemP
= self
->top
;
38 intptr_t mark
= self
->lastMark
;
40 while (itemP
> self
->items
)
42 if (itemP
- self
->items
== mark
)
55 IOINLINE
void Stack_clear(Stack
*self
)
57 self
->top
= self
->items
;
61 IOINLINE
size_t Stack_totalSize(const Stack
*self
)
63 return (self
->top
- self
->items
);
66 IOINLINE
int Stack_count(const Stack
*self
)
68 return (self
->top
- self
->items
);
71 IOINLINE
void Stack_push_(Stack
*self
, void *item
)
75 if (self
->top
== self
->memEnd
)
83 IOINLINE
void Stack_pushMark(Stack
*self
)
85 Stack_push_(self
, (void *)self
->lastMark
);
86 self
->lastMark
= self
->top
- self
->items
;
89 IOINLINE
intptr_t Stack_pushMarkPoint(Stack
*self
)
91 Stack_push_(self
, (void *)self
->lastMark
);
92 self
->lastMark
= self
->top
- self
->items
;
93 return self
->lastMark
;
96 IOINLINE
void *Stack_pop(Stack
*self
)
98 void *top
= *(self
->top
);
100 if (self
->items
!= self
->top
)
102 #ifdef STACK_POP_CALLBACK
103 if(self
->popCallback
) self
->popCallback(*(self
->top
));
111 IOINLINE
void Stack_popMark(Stack
*self
)
113 #ifdef STACK_POP_CALLBACK
114 if(self
->popCallback
) Stack_doUntilMark_(self
, self
->popCallback
);
117 self
->top
= self
->items
+ self
->lastMark
- 1;
121 self
->lastMark
= (intptr_t)(self
->items
[self
->lastMark
]);
125 IOINLINE
int Stack_popMarkPoint_(Stack
*self
, intptr_t mark
)
127 while (self
->lastMark
&& self
->lastMark
!= mark
)
132 if (self
->lastMark
!= mark
)
141 IOINLINE
void Stack_clearTop(Stack
*self
)
144 Stack_pushMark(self
);
145 //self->top = self->items + self->lastMark;
148 IOINLINE
void *Stack_top(const Stack
*self
)
153 IOINLINE
void *Stack_at_(const Stack
*self
, int i
)
155 return self
->items
[i
+ 1];