2 docCopyright("Steve Dekorte", 2002)
3 docLicense("BSD revised")
5 Stack - array of void pointers
6 supports setting marks - when a mark is popped,
7 all stack items above it are popped as well
14 #include "Common_inline.h"
15 #ifdef IO_DECLARE_INLINES
17 IOINLINE
void Stack_do_(const Stack
*self
, StackDoCallback
*callback
)
19 void **itemP
= self
->top
;
20 intptr_t mark
= self
->lastMark
;
22 while (itemP
> self
->items
)
24 if (itemP
- self
->items
== mark
)
26 mark
= (intptr_t)(*itemP
);
37 IOINLINE
void Stack_doUntilMark_(const Stack
*self
, StackDoCallback
*callback
)
39 void **itemP
= self
->top
;
40 intptr_t mark
= self
->lastMark
;
42 while (itemP
> self
->items
)
44 if (itemP
- self
->items
== mark
)
57 IOINLINE
void Stack_clear(Stack
*self
)
59 self
->top
= self
->items
;
63 IOINLINE
size_t Stack_totalSize(const Stack
*self
)
65 return (self
->top
- self
->items
);
68 IOINLINE
int Stack_count(const Stack
*self
)
70 return (self
->top
- self
->items
);
73 IOINLINE
void Stack_push_(Stack
*self
, void *item
)
77 if (self
->top
== self
->memEnd
)
85 IOINLINE
void Stack_pushMark(Stack
*self
)
87 Stack_push_(self
, (void *)self
->lastMark
);
88 self
->lastMark
= self
->top
- self
->items
;
91 IOINLINE
intptr_t Stack_pushMarkPoint(Stack
*self
)
93 Stack_push_(self
, (void *)self
->lastMark
);
94 self
->lastMark
= self
->top
- self
->items
;
95 return self
->lastMark
;
98 IOINLINE
void *Stack_pop(Stack
*self
)
100 void *top
= *(self
->top
);
102 if (self
->items
!= self
->top
)
104 #ifdef STACK_POP_CALLBACK
105 if(self
->popCallback
) self
->popCallback(*(self
->top
));
113 IOINLINE
void Stack_popMark(Stack
*self
)
115 #ifdef STACK_POP_CALLBACK
116 if(self
->popCallback
) Stack_doUntilMark_(self
, self
->popCallback
);
119 self
->top
= self
->items
+ self
->lastMark
- 1;
123 self
->lastMark
= (intptr_t)(self
->items
[self
->lastMark
]);
127 IOINLINE
int Stack_popMarkPoint_(Stack
*self
, intptr_t mark
)
129 while (self
->lastMark
&& self
->lastMark
!= mark
)
134 if (self
->lastMark
!= mark
)
143 IOINLINE
void Stack_clearTop(Stack
*self
)
146 Stack_pushMark(self
);
147 //self->top = self->items + self->lastMark;
150 IOINLINE
void *Stack_top(const Stack
*self
)
155 IOINLINE
void *Stack_at_(const Stack
*self
, int i
)
157 return self
->items
[i
+ 1];