1 | // =============================================================== // |
---|
2 | // // |
---|
3 | // File : AP_buffer.cxx // |
---|
4 | // Purpose : // |
---|
5 | // // |
---|
6 | // Institute of Microbiology (Technical University Munich) // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // =============================================================== // |
---|
10 | |
---|
11 | #include "AP_buffer.hxx" |
---|
12 | #include "AP_error.hxx" |
---|
13 | #include "ap_tree_nlen.hxx" |
---|
14 | |
---|
15 | #include <iostream> |
---|
16 | |
---|
17 | using namespace std; |
---|
18 | |
---|
19 | #define MAX_SPECIES 250000L |
---|
20 | #define MAX_DSTACKSIZE (MAX_SPECIES*2) // defaultwert fuer dynamischen stack |
---|
21 | |
---|
22 | AP_STACK::AP_STACK() { |
---|
23 | first = 0; |
---|
24 | pointer = 0; |
---|
25 | stacksize = 0; |
---|
26 | max_stacksize = MAX_DSTACKSIZE; |
---|
27 | } |
---|
28 | |
---|
29 | AP_STACK::~AP_STACK() { |
---|
30 | if (stacksize > 0) { |
---|
31 | new AP_ERR("~AP_STACK()", "Stack is not empty !", 0); |
---|
32 | } |
---|
33 | } |
---|
34 | |
---|
35 | void AP_STACK::push(void * element) { |
---|
36 | AP_STACK_ELEM * stackelem = new AP_STACK_ELEM; |
---|
37 | if (stacksize > max_stacksize) { |
---|
38 | new AP_ERR("AP_STACK:push()", "Stack owerflow!", 0); |
---|
39 | } |
---|
40 | stackelem->node = element; |
---|
41 | stackelem->next = first; |
---|
42 | first = stackelem; |
---|
43 | stacksize ++; |
---|
44 | } |
---|
45 | |
---|
46 | void * AP_STACK::pop() { |
---|
47 | void * pntr; |
---|
48 | AP_STACK_ELEM * stackelem; |
---|
49 | if (!first) return 0; |
---|
50 | stackelem = first; |
---|
51 | pntr = first->node; |
---|
52 | first = first->next; |
---|
53 | stacksize --; |
---|
54 | delete stackelem; |
---|
55 | return pntr; |
---|
56 | } |
---|
57 | |
---|
58 | void AP_STACK::clear() { |
---|
59 | while (stacksize > 0) { |
---|
60 | AP_STACK_ELEM *pntr = first; |
---|
61 | first = first->next; |
---|
62 | stacksize --; |
---|
63 | delete pntr; |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | void AP_STACK::get_init() { |
---|
68 | pointer = 0; |
---|
69 | } |
---|
70 | void * AP_STACK::get_first() { |
---|
71 | if (first != 0) { |
---|
72 | return first->node; |
---|
73 | } |
---|
74 | else { |
---|
75 | return 0; |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | void * AP_STACK::get() { |
---|
80 | if (0 == pointer) { |
---|
81 | pointer = first; |
---|
82 | } |
---|
83 | else { |
---|
84 | if (pointer->next == 0) { |
---|
85 | new AP_ERR("AP_STACK: get()", " more get() than elements in stack"); |
---|
86 | pointer = 0; |
---|
87 | return 0; |
---|
88 | } |
---|
89 | else { |
---|
90 | pointer = pointer->next; |
---|
91 | } |
---|
92 | } |
---|
93 | return pointer->node; |
---|
94 | } |
---|
95 | |
---|
96 | unsigned long AP_STACK::size() { |
---|
97 | return stacksize; |
---|
98 | } |
---|
99 | |
---|
100 | // ---------------- |
---|
101 | // AP_LIST |
---|
102 | |
---|
103 | AP_list_elem * AP_LIST::element(void * elem) { |
---|
104 | AP_list_elem *pntr = first; |
---|
105 | while (pntr != 0) { |
---|
106 | if (pntr->node == elem) |
---|
107 | return pntr; |
---|
108 | pntr = pntr->next; |
---|
109 | } |
---|
110 | return pntr; |
---|
111 | } |
---|
112 | |
---|
113 | int AP_LIST::len() { |
---|
114 | return list_len; |
---|
115 | } |
---|
116 | |
---|
117 | int AP_LIST::is_element(void * node) { |
---|
118 | if (element(node) == 0) return 0; |
---|
119 | return 1; |
---|
120 | } |
---|
121 | |
---|
122 | int AP_LIST::eof() { |
---|
123 | if (akt == list_len) return 1; |
---|
124 | return 0; |
---|
125 | } |
---|
126 | |
---|
127 | void AP_LIST::insert(void * new_one) { |
---|
128 | AP_list_elem * newelem = new AP_list_elem; |
---|
129 | if (first == 0) { |
---|
130 | first = newelem; |
---|
131 | last = newelem; |
---|
132 | newelem->next = 0; |
---|
133 | newelem->prev = 0; |
---|
134 | pointer = first; |
---|
135 | } |
---|
136 | else { |
---|
137 | first->prev = newelem; |
---|
138 | newelem->prev = 0; |
---|
139 | newelem->next = first; |
---|
140 | first = newelem; |
---|
141 | } |
---|
142 | newelem->node = new_one; |
---|
143 | list_len++; |
---|
144 | return; |
---|
145 | } |
---|
146 | |
---|
147 | void AP_LIST::append(void * new_one) { |
---|
148 | AP_list_elem * newelem = new AP_list_elem; |
---|
149 | if (last == 0) { |
---|
150 | first = newelem; |
---|
151 | last = newelem; |
---|
152 | newelem->prev = 0; |
---|
153 | newelem->next = 0; |
---|
154 | pointer = first; |
---|
155 | } |
---|
156 | else { |
---|
157 | last->next = newelem; |
---|
158 | newelem->prev = last; |
---|
159 | last = newelem; |
---|
160 | newelem->next = 0; |
---|
161 | } |
---|
162 | newelem->node = new_one; |
---|
163 | list_len++; |
---|
164 | return; |
---|
165 | } |
---|
166 | |
---|
167 | void AP_LIST::remove(void * object) { |
---|
168 | AP_list_elem *elem = element(object); |
---|
169 | if (elem) { |
---|
170 | if (elem->prev) { |
---|
171 | elem->prev->next = elem->next; |
---|
172 | } |
---|
173 | else { |
---|
174 | first = elem->next; |
---|
175 | elem->next->prev = 0; |
---|
176 | } |
---|
177 | if (elem->next) { |
---|
178 | elem->next->prev = elem->prev; |
---|
179 | } |
---|
180 | else { |
---|
181 | last = elem->prev; |
---|
182 | elem->prev->next = 0; |
---|
183 | } |
---|
184 | if (elem == pointer) pointer = 0; |
---|
185 | delete elem; |
---|
186 | list_len --; |
---|
187 | return; |
---|
188 | } |
---|
189 | new AP_ERR("AP_LIST::remove(void * object)", "no buffer element !\n"); |
---|
190 | return; |
---|
191 | } |
---|
192 | |
---|
193 | void AP_LIST::push(void *elem) { |
---|
194 | AP_list_elem * newelem = new AP_list_elem; |
---|
195 | if (first == 0) { |
---|
196 | first = newelem; |
---|
197 | last = newelem; |
---|
198 | newelem->next = 0; |
---|
199 | newelem->prev = 0; |
---|
200 | pointer = first; |
---|
201 | } |
---|
202 | else { |
---|
203 | first->prev = newelem; |
---|
204 | newelem->prev = 0; |
---|
205 | newelem->next = first; |
---|
206 | first = newelem; |
---|
207 | } |
---|
208 | newelem->node = elem; |
---|
209 | list_len++; |
---|
210 | return; |
---|
211 | } |
---|
212 | |
---|
213 | void *AP_LIST::pop() { |
---|
214 | AP_list_elem * pntr = first; |
---|
215 | if (!first) return 0; |
---|
216 | void * node = first->node; |
---|
217 | list_len --; |
---|
218 | if (0 == list_len) { |
---|
219 | first = last = 0; |
---|
220 | delete pntr; |
---|
221 | return node; |
---|
222 | } |
---|
223 | else { |
---|
224 | first = first->next; |
---|
225 | first->prev = 0; |
---|
226 | } |
---|
227 | delete pntr; |
---|
228 | return node; |
---|
229 | } |
---|
230 | |
---|
231 | |
---|
232 | void AP_LIST::clear() { |
---|
233 | AP_list_elem* npntr; |
---|
234 | AP_list_elem* pntr = first; |
---|
235 | while (pntr != 0) { |
---|
236 | npntr = pntr->next; |
---|
237 | delete pntr; |
---|
238 | pntr = npntr; |
---|
239 | } |
---|
240 | first = last = 0; |
---|
241 | akt = 0; |
---|
242 | list_len = 0; |
---|
243 | } |
---|
244 | |
---|
245 | void AP_tree_buffer::print() { |
---|
246 | cout << "AP_tree_buffer " << this; |
---|
247 | cout << "\nfather " << father; |
---|
248 | cout << "\nlefts " << leftson; |
---|
249 | cout << "\nrights " << rightson << "\n sequence " << sequence << "\n"; |
---|
250 | } |
---|
251 | |
---|
252 | void AP_main_stack::print() { |
---|
253 | unsigned long i = this->size(); |
---|
254 | cout << "AP_main_stack " << this << " Size " << i << "\n"; |
---|
255 | get_init(); |
---|
256 | for (; i > 0; i--) { |
---|
257 | AP_tree *elem = get(); |
---|
258 | cout << i << " - AP_tree *: " << elem << " \n"; |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | |
---|
263 | void AP_tree_stack::print() { |
---|
264 | unsigned long i = this->size(); |
---|
265 | cout << "AP_tree_stack : Size " << i << "\n"; |
---|
266 | get_init(); |
---|
267 | for (; i > 0; i--) { |
---|
268 | AP_tree_buffer *elem = get(); |
---|
269 | elem->print(); |
---|
270 | } |
---|
271 | } |
---|