-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb2list.c
416 lines (313 loc) · 15.6 KB
/
db2list.c
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*****************************************************************************
* *
* ----------------------------- db2list.c -------------------------------- *
* *
*****************************************************************************/
#include <stdlib.h>
#include <string.h>
#include "db2list.h"
#include "db2topasutil.h"
/*****************************************************************************
* *
* ---------------------------- Private Functions ------------------------- *
* *
*****************************************************************************/
void swap (DB2ListElmt *element1, DB2ListElmt *element2) ;
/*****************************************************************************
* *
* ---------------------------- db2list_init ------------------------------ *
* *
*****************************************************************************/
void db2list_init(DB2List *list, int (*match)(const void *key1, const void *key2),
void (*destroy)(void *data)
, SnapReqType snap_req_type
)
{
/*****************************************************************************
* *
* Initialize the list. *
* *
*****************************************************************************/
list->snap_req_type = snap_req_type;
list->size = 0;
list->match = match;
list->destroy = destroy;
list->head = NULL;
list->tail = NULL;
list->highlighted_element = NULL;
return;
}
/*****************************************************************************
* *
* -------------------------- db2list_destroy ----------------------------- *
* *
*****************************************************************************/
void db2list_destroy(DB2List *list)
{
void *data;
/*****************************************************************************
* *
* Remove each element. *
* *
*****************************************************************************/
while (db2list_size(list) > 0) {
if (db2list_remove(list, db2list_tail(list), (void **) & data) == 0 && list->
destroy != NULL) {
/***********************************************************************
* *
* Call a user-defined function to free dynamically allocated data. *
* *
***********************************************************************/
list->destroy(data);
}
}
memset(list, 0, sizeof(DB2List));
return;
}
/*****************************************************************************
* *
* -------------------------- db2list_cleanup ----------------------------- *
* *
*****************************************************************************/
void db2list_cleanup(DB2List *list)
{
void *data;
DB2ListElmt * cur_element;
DB2ListElmt * old_element;
for (cur_element = db2list_head(list); cur_element != NULL; ) {
if (cur_element->delete == TRUE) {
old_element = cur_element;
cur_element = db2list_next(cur_element);
if (db2list_remove(list, old_element, (void **) & data) == 0 && list->
destroy != NULL)
list->destroy(data);
} else
cur_element = db2list_next(cur_element);
}
return;
}
/*****************************************************************************
* *
*---------------------------- db2list_ins_next ---------------------------- *
* *
*****************************************************************************/
int db2list_ins_next(DB2List *list, DB2ListElmt *element, const void *data)
{
DB2ListElmt * new_element;
/*****************************************************************************
* *
* Do not allow a NULL element unless the list is empty. *
* *
*****************************************************************************/
if (element == NULL && db2list_size(list) != 0)
return - 1;
/*****************************************************************************
* *
* Allocate storage for the element. *
* *
*****************************************************************************/
if ((new_element = (DB2ListElmt * )malloc(sizeof(DB2ListElmt))) == NULL)
return - 1;
/*****************************************************************************
* *
* Insert the new element into the list. *
* *
*****************************************************************************/
new_element->data = (void *)data;
new_element->delete = FALSE;
new_element->highlight = FALSE;
if (db2list_size(list) == 0) {
/**************************************************************************
* *
* Handle insertion when the list is empty. *
* *
**************************************************************************/
list->head = new_element;
list->head->prev = NULL;
list->head->next = NULL;
list->tail = new_element;
}
else {
/**************************************************************************
* *
* Handle insertion when the list is not empty. *
* *
**************************************************************************/
new_element->next = element->next;
new_element->prev = element;
if (element->next == NULL)
list->tail = new_element;
else
element->next->prev = new_element;
element->next = new_element;
}
/*****************************************************************************
* *
* Adjust the size of the list to account for the inserted element. *
* *
*****************************************************************************/
list->size++;
return 0;
}
/*****************************************************************************
* *
* ----------------------------- db2list_remove ----------------------------- *
* *
*****************************************************************************/
int db2list_remove(DB2List *list, DB2ListElmt *element, void **data)
{
/*****************************************************************************
* *
* Do not allow a NULL element or removal from an empty list. *
* *
*****************************************************************************/
if (element == NULL || db2list_size(list) == 0)
return - 1;
/*****************************************************************************
* *
* Remove the element from the list. *
* *
*****************************************************************************/
*data = element->data;
if (element->highlight == TRUE)
list->highlighted_element = NULL;
if (element == list->head) {
/**************************************************************************
* *
* Handle removal from the head of the list. *
* *
**************************************************************************/
list->head = element->next;
if (list->head == NULL)
list->tail = NULL;
else
element->next->prev = NULL;
}
else {
/**************************************************************************
* *
* Handle removal from other than the head of the list. *
* *
**************************************************************************/
element->prev->next = element->next;
if (element->next == NULL)
list->tail = element->prev;
else
element->next->prev = element->prev;
}
/*****************************************************************************
* *
* Free the storage allocated by the abstract data type. *
* *
*****************************************************************************/
free(element);
/*****************************************************************************
* *
* Adjust the size of the list to account for the removed element. *
* *
*****************************************************************************/
list->size--;
return 0;
}
/*****************************************************************************
* *
* --------------------------- db2list_lookup ----------------------------- *
* *
*****************************************************************************/
DB2ListElmt *db2list_lookup(const DB2List *list, void *data)
{
DB2ListElmt * element;
for (element = db2list_head(list); element != NULL; element =
db2list_next(element)) {
if (db2list_data(element) != NULL
&& list->match(db2list_data(element), data) == 0) {
element->delete = FALSE;
return element;
}
}
return NULL;
} /* db2list_lookup */
/*****************************************************************************
* *
* --------------------- db2list_check_highlight -------------------------- *
* *
*****************************************************************************/
void db2list_check_highlight(Header *header, DB2List *list
, int (*compare) (const void *, const void *)
)
{
DB2ListElmt * element;
if (db2list_size(list) == 0 || list->highlighted_element != NULL)
return;
/**************************************************************************/
/* possibly highlight one of the list elements */
/**************************************************************************/
if (compare != NULL) {
for (element = db2list_head(list); element != NULL;
element = db2list_next(element)) {
if ( compare(header, db2list_data(element)) == 0) {
element->highlight = TRUE;
list->highlighted_element = element;
return;
}
} /*for*/
}
(db2list_head(list))->highlight = TRUE;
list->highlighted_element = db2list_head(list);
return ;
}
/*****************************************************************************
* *
* --------------------- db2list_sort ------------------------------------- *
* *
*****************************************************************************/
void db2list_sort(DB2List *list, int (*compare) (const void *, const void *))
{
DB2ListElmt *left, *right, *key;
if (list == NULL )
return;
else if (db2list_size(list) < 2 )
return;
/***********************************************************
* db2list_sort uses insertion sort
* -first for loop moves down (right) the list using a key
* -a second for loop places the key
* in the sorted pile left of the list
***********************************************************/
for (right = (db2list_head(list))->next; right != NULL; right = db2list_next(right) ) {
key = right;
for(left = db2list_prev(right);
left != NULL && compare(db2list_data(left),db2list_data(key)) > 0;
left = db2list_prev(left) ) {
swap(left, key);
/* update pointer to the highlighted element */
if (left->highlight == TRUE)
list->highlighted_element = left;
else if (key->highlight == TRUE)
list->highlighted_element = key;
/* after swap smaller/bigger value moved from key to left
adjust key so it follows the smaller/bigger value */
key = left;
}
}
return;
}
/*****************************************************************************
* *
* --------------------- swap --------------------------------------------- *
* *
*****************************************************************************/
void swap (DB2ListElmt *element1, DB2ListElmt *element2)
{
void *data1 = db2list_data( element1);
void *data2 = db2list_data( element2);
Boolean highlight1 = ( element1)->highlight;
Boolean highlight2 = ( element2)->highlight;
/* swap the data */
db2list_data( element1) = data2;
db2list_data( element2) = data1;
/* swap highlight indicator */
( element1)->highlight = highlight2;
( element2)->highlight = highlight1;
return;
}