1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
/*
Lightweight C Container Library
Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
Original code was written by Chris Schlaeger <cs@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "ccont.h"
#include <stdio.h>
#include <stdlib.h>
struct container_info {
INDEX count;
CONTAINER currentNode;
};
typedef struct container_info T_CONTAINER_INFO;
typedef struct container_info* CONTAINER_INFO;
#define rpterr(x) fprintf(stderr, "%s\n", x)
CONTAINER new_ctnr(void)
{
CONTAINER_INFO info;
CONTAINER rootNode;
rootNode = (CONTAINER)malloc(sizeof(T_CONTAINER));
info = (CONTAINER_INFO)malloc(sizeof(T_CONTAINER_INFO));
info->count = 0;
info->currentNode = rootNode;
rootNode->next = rootNode;
rootNode->prev = rootNode;
rootNode->data = info;
return rootNode;
}
void zero_destr_ctnr(CONTAINER rootNode, DESTR_FUNC destr_func)
{
INDEX counter;
if (rootNode == NIL || destr_func == NIL) {
rpterr("destr_ctnr: NIL argument");
return;
}
for (counter = level_ctnr(rootNode); counter > -1; --counter)
destr_func(pop_ctnr(rootNode));
if (rootNode->data)
free(rootNode->data);
free(rootNode);
rootNode = 0;
return;
}
INDEX level_ctnr(CONTAINER rootNode)
{
if (rootNode == NIL) {
rpterr("level_ctnr: NIL argument");
return -1;
}
return ((CONTAINER_INFO)rootNode->data)->count;
}
void insert_ctnr(CONTAINER rootNode, void* object, INDEX pos)
{
CONTAINER it;
INDEX counter = 0;
if (rootNode == NIL || object == NIL) {
rpterr("insert_ctnr: NIL argument");
return;
}
for (it = rootNode->next; it != rootNode; it = it->next) {
if (counter == pos) {
CONTAINER newNode = (CONTAINER)malloc(sizeof(T_CONTAINER));
newNode->prev = it;
newNode->next = it->next;
it->next->prev = newNode;
it->next = newNode;
newNode->data = object;
((CONTAINER_INFO)rootNode->data)->count++;
return;
}
counter++;
}
}
void push_ctnr(CONTAINER rootNode, void* object)
{
CONTAINER newNode;
if (rootNode == NIL || object == NIL) {
rpterr("push_ctnr: NIL argument");
return;
}
newNode = (CONTAINER)malloc(sizeof(T_CONTAINER));
newNode->next = rootNode;
newNode->prev = rootNode->prev;
rootNode->prev->next = newNode;
rootNode->prev = newNode;
newNode->data = object;
((CONTAINER_INFO)rootNode->data)->count++;
}
void* remove_at_ctnr(CONTAINER rootNode, INDEX pos)
{
CONTAINER it;
INDEX counter = 0;
void* retval;
if (rootNode == NIL) {
rpterr("remove_ctnr: NIL argument");
return NIL;
}
for (it = rootNode->next; it != rootNode; it = it->next) {
if (counter == pos) {
retval = it->data;
it->prev->next = it->next;
it->next->prev = it->prev;
free(it);
((CONTAINER_INFO)rootNode->data)->count--;
return retval;
}
counter++;
}
return NIL;
}
void* pop_ctnr(CONTAINER rootNode)
{
CONTAINER ptr;
void* retval;
if (rootNode == NIL) {
rpterr("pop_ctnr: NIL argument");
return NIL;
}
if (rootNode->next == rootNode)
return NIL;
ptr = rootNode->next;
retval = ptr->data;
rootNode->next = ptr->next;
ptr->next->prev = rootNode;
((CONTAINER_INFO)rootNode->data)->count--;
free(ptr);
return retval;
}
void* get_ctnr(CONTAINER rootNode, INDEX pos)
{
CONTAINER it;
INDEX counter = 0;
if (rootNode == NIL) {
rpterr("get_ctnr: NIL argument");
return NIL;
}
for (it = rootNode->next; it != rootNode; it = it->next) {
if (counter == pos)
return it->data;
counter++;
}
return NIL;
}
INDEX search_ctnr(CONTAINER rootNode, COMPARE_FUNC compare_func, void* pattern)
{
CONTAINER it;
INDEX counter = 0;
if (rootNode == NIL || compare_func == NIL || pattern == NIL) {
rpterr("search_ctnr: NIL argument");
return -1;
}
for (it = rootNode->next; it != rootNode; it = it->next) {
if (compare_func(pattern, it->data) == 0)
return counter;
counter++;
}
return -1;
}
void swop_ctnr(CONTAINER rootNode, INDEX pos1, INDEX pos2)
{
CONTAINER it, node1, node2;
INDEX counter = 0;
int found = 0;
void* tmpData;
if (rootNode == NIL) {
rpterr("swop_ctnr: NIL argument");
return;
}
if (pos1 == pos2)
return;
/**
* it is a bit hackish because of the 'goto' but it's fast
* since we have to run through the list only once
*/
for (it = rootNode->next; it != rootNode; it = it->next) {
if (counter == pos1) {
node1 = it;
if (found)
goto swapIt;
else
found = 1;
}
if (counter == pos2) {
node2 = it;
if (found)
goto swapIt;
else
found = 1;
}
counter++;
}
return;
swapIt:
tmpData = node1->data;
node1->data = node2->data;
node2->data = tmpData;
return;
}
void bsort_ctnr(CONTAINER rootNode, COMPARE_FUNC compare_func)
{
INDEX right, i, level, last;
if (rootNode == NIL || compare_func == NIL) {
rpterr("destr_ctnr: NIL argument");
return;
}
last = level = level_ctnr(rootNode);
do
{
right = last;
last = 0;
for (i = 1; i < right; i++)
if (compare_func(get_ctnr(rootNode, i - 1), get_ctnr(rootNode, i)) > 0)
swop_ctnr(rootNode, i - 1, last = i);
} while (last > 0);
}
void* first_ctnr(CONTAINER rootNode)
{
if (rootNode == NIL) {
rpterr("first_ctnr: NIL argument");
return NIL;
}
if (rootNode->next == rootNode)
return NIL;
((CONTAINER_INFO)rootNode->data)->currentNode = rootNode->next;
return rootNode->next->data;
}
void* next_ctnr(CONTAINER rootNode)
{
CONTAINER_INFO info;
if (rootNode == NIL) {
rpterr("next_ctnr: NIL argument");
return NIL;
}
info = (CONTAINER_INFO)rootNode->data;
if (info->currentNode->next == rootNode)
return NIL;
info->currentNode = info->currentNode->next;
return info->currentNode->data;
}
void* remove_ctnr(CONTAINER rootNode)
{
CONTAINER currentNode, tmp;
CONTAINER_INFO info;
void* retval;
if (rootNode == NIL) {
rpterr("remove_curr_ctnr: NIL argument");
return NIL;
}
info = (CONTAINER_INFO)rootNode->data;
currentNode = info->currentNode;
if (currentNode == rootNode) { /* should never happen */
rpterr("remove_curr_ctnr: delete root node");
return NIL;
}
retval = currentNode->data;
tmp = currentNode->prev;
currentNode->prev->next = currentNode->next;
currentNode->next->prev = currentNode->prev;
free(currentNode);
info->count--;
info->currentNode = tmp;
return retval;
}
|