New doc system done for core
[io.git] / libs / basekit / source / Stack_inline.h
blob3d21dc2ef485742988cc653e9fd450a4379d384c
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
7 */
9 #ifdef STACK_C
10 #define IO_IN_C_FILE
11 #endif
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);
26 else
28 (*callback)(*itemP);
31 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)
44 return;
46 else
48 (*callback)(*itemP);
51 itemP --;
55 IOINLINE void Stack_clear(Stack *self)
57 self->top = self->items;
58 self->lastMark = 0;
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)
73 self->top ++;
75 if (self->top == self->memEnd)
77 Stack_resize(self);
80 *(self->top) = item;
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));
104 #endif
105 self->top --;
108 return top;
111 IOINLINE void Stack_popMark(Stack *self)
113 #ifdef STACK_POP_CALLBACK
114 if(self->popCallback) Stack_doUntilMark_(self, self->popCallback);
115 #endif
117 self->top = self->items + self->lastMark - 1;
119 if (self->lastMark)
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)
129 Stack_popMark(self);
132 if (self->lastMark != mark)
134 return 0;
137 Stack_popMark(self);
138 return 1;
141 IOINLINE void Stack_clearTop(Stack *self)
143 Stack_popMark(self);
144 Stack_pushMark(self);
145 //self->top = self->items + self->lastMark;
148 IOINLINE void *Stack_top(const Stack *self)
150 return *(self->top);
153 IOINLINE void *Stack_at_(const Stack *self, int i)
155 return self->items[i + 1];
160 #undef IO_IN_C_FILE
161 #endif