1 | /* |
---|
2 | * Copyright (C) 2005,2007,2009-2010 Timothy D. Morgan |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation; version 3 of the License. |
---|
7 | * |
---|
8 | * This program is distributed in the hope that it will be useful, |
---|
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
11 | * GNU General Public License for more details. |
---|
12 | * |
---|
13 | * You should have received a copy of the GNU General Public License |
---|
14 | * along with this program; if not, write to the Free Software |
---|
15 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
16 | * |
---|
17 | * $Id: void_stack.c 169 2010-03-03 19:24:58Z tim $ |
---|
18 | */ |
---|
19 | |
---|
20 | /** |
---|
21 | * @file |
---|
22 | */ |
---|
23 | |
---|
24 | |
---|
25 | #include "void_stack.h" |
---|
26 | |
---|
27 | void_stack* void_stack_new(unsigned short max_size) |
---|
28 | { |
---|
29 | void_stack* ret_val = talloc(NULL, void_stack); |
---|
30 | |
---|
31 | if (ret_val != NULL) |
---|
32 | { |
---|
33 | memset(ret_val, 0, sizeof(*ret_val)); |
---|
34 | ret_val->elements = talloc_array(ret_val, void*, max_size); |
---|
35 | if (ret_val->elements == NULL) |
---|
36 | { |
---|
37 | talloc_free(ret_val); |
---|
38 | ret_val = NULL; |
---|
39 | } |
---|
40 | else |
---|
41 | { |
---|
42 | memset(ret_val->elements, 0, max_size*sizeof(void*)); |
---|
43 | |
---|
44 | ret_val->max_size = max_size; |
---|
45 | ret_val->top = 0; |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | return ret_val; |
---|
50 | } |
---|
51 | |
---|
52 | |
---|
53 | void_stack* void_stack_copy(const void_stack* v) |
---|
54 | { |
---|
55 | unsigned int i; |
---|
56 | void_stack* ret_val; |
---|
57 | if(v == NULL) |
---|
58 | return NULL; |
---|
59 | |
---|
60 | ret_val = void_stack_new(v->max_size); |
---|
61 | if(ret_val == NULL) |
---|
62 | return NULL; |
---|
63 | |
---|
64 | for(i = 0; i < v->top; i++) |
---|
65 | ret_val->elements[i] = v->elements[i]; |
---|
66 | ret_val->top = v->top; |
---|
67 | |
---|
68 | return ret_val; |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | void_stack* void_stack_copy_reverse(const void_stack* v) |
---|
73 | { |
---|
74 | unsigned int i; |
---|
75 | void_stack* ret_val; |
---|
76 | if(v == NULL) |
---|
77 | return NULL; |
---|
78 | |
---|
79 | ret_val = void_stack_new(v->max_size); |
---|
80 | if(ret_val == NULL) |
---|
81 | return NULL; |
---|
82 | |
---|
83 | for(i = 0; i < v->top; i++) |
---|
84 | ret_val->elements[i] = v->elements[v->top-i-1]; |
---|
85 | ret_val->top = v->top; |
---|
86 | |
---|
87 | return ret_val; |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | void void_stack_free(void_stack* stack) |
---|
92 | { |
---|
93 | talloc_free(stack); |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | void void_stack_free_deep(void_stack* stack) |
---|
98 | { |
---|
99 | unsigned short i; |
---|
100 | for(i=0; i < stack->top; i++) |
---|
101 | free(stack->elements[i]); |
---|
102 | talloc_free(stack); |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | unsigned short void_stack_size(const void_stack* stack) |
---|
107 | { |
---|
108 | return stack->top; |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | void* void_stack_pop(void_stack* stack) |
---|
113 | { |
---|
114 | void* ret_val = NULL; |
---|
115 | |
---|
116 | if(stack->top > 0) |
---|
117 | { |
---|
118 | ret_val = stack->elements[--(stack->top)]; |
---|
119 | stack->elements[stack->top] = NULL; |
---|
120 | } |
---|
121 | |
---|
122 | return ret_val; |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | bool void_stack_push(void_stack* stack, void* e) |
---|
127 | { |
---|
128 | if(stack->top < stack->max_size) |
---|
129 | { |
---|
130 | stack->elements[stack->top++] = e; |
---|
131 | return true; |
---|
132 | } |
---|
133 | else |
---|
134 | return false; |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | const void* void_stack_cur(const void_stack* stack) |
---|
139 | { |
---|
140 | void* ret_val = NULL; |
---|
141 | |
---|
142 | if(stack->top > 0) |
---|
143 | ret_val = stack->elements[stack->top-1]; |
---|
144 | |
---|
145 | return ret_val; |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | void_stack_iterator* void_stack_iterator_new(const void_stack* stack) |
---|
150 | { |
---|
151 | void_stack_iterator* ret_val = NULL; |
---|
152 | |
---|
153 | if(stack != NULL) |
---|
154 | { |
---|
155 | ret_val = talloc(stack, void_stack_iterator); |
---|
156 | if (ret_val != NULL) |
---|
157 | { |
---|
158 | ret_val->stack = stack; |
---|
159 | ret_val->cur = 0; |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | return ret_val; |
---|
164 | } |
---|
165 | |
---|
166 | |
---|
167 | void void_stack_iterator_free(void_stack_iterator* iter) |
---|
168 | { |
---|
169 | talloc_free(iter); |
---|
170 | } |
---|
171 | |
---|
172 | |
---|
173 | const void* void_stack_iterator_next(void_stack_iterator* iter) |
---|
174 | { |
---|
175 | if(iter->cur < iter->stack->top) |
---|
176 | return iter->stack->elements[iter->cur++]; |
---|
177 | else |
---|
178 | return NULL; |
---|
179 | } |
---|