1
by brian
clean slate |
1 |
/* Copyright (C) 2000 MySQL AB
|
2 |
||
3 |
This program is free software; you can redistribute it and/or modify
|
|
4 |
it under the terms of the GNU General Public License as published by
|
|
5 |
the Free Software Foundation; version 2 of the License.
|
|
6 |
||
7 |
This program is distributed in the hope that it will be useful,
|
|
8 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 |
GNU General Public License for more details.
|
|
11 |
||
12 |
You should have received a copy of the GNU General Public License
|
|
13 |
along with this program; if not, write to the Free Software
|
|
14 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
15 |
||
16 |
/*
|
|
17 |
Code for handling dubble-linked lists in C
|
|
18 |
*/
|
|
19 |
||
20 |
#include "mysys_priv.h" |
|
21 |
#include <my_list.h> |
|
22 |
||
23 |
||
24 |
||
25 |
/* Add a element to start of list */
|
|
26 |
||
27 |
LIST *list_add(LIST *root, LIST *element) |
|
28 |
{
|
|
29 |
if (root) |
|
30 |
{
|
|
31 |
if (root->prev) /* If add in mid of list */ |
|
32 |
root->prev->next= element; |
|
33 |
element->prev=root->prev; |
|
34 |
root->prev=element; |
|
35 |
}
|
|
36 |
else
|
|
37 |
element->prev=0; |
|
38 |
element->next=root; |
|
51.3.22
by Jay Pipes
Final round of removal of DBUG in mysys/, including Makefile |
39 |
return(element); /* New root */ |
1
by brian
clean slate |
40 |
}
|
41 |
||
42 |
||
43 |
LIST *list_delete(LIST *root, LIST *element) |
|
44 |
{
|
|
45 |
if (element->prev) |
|
46 |
element->prev->next=element->next; |
|
47 |
else
|
|
48 |
root=element->next; |
|
49 |
if (element->next) |
|
50 |
element->next->prev=element->prev; |
|
51 |
return root; |
|
52 |
}
|
|
53 |
||
54 |
||
55 |
void list_free(LIST *root, uint free_data) |
|
56 |
{
|
|
57 |
LIST *next; |
|
58 |
while (root) |
|
59 |
{
|
|
60 |
next=root->next; |
|
61 |
if (free_data) |
|
62 |
my_free((uchar*) root->data,MYF(0)); |
|
63 |
my_free((uchar*) root,MYF(0)); |
|
64 |
root=next; |
|
65 |
}
|
|
66 |
}
|
|
67 |
||
68 |
||
69 |
LIST *list_cons(void *data, LIST *list) |
|
70 |
{
|
|
71 |
LIST *new_charset=(LIST*) my_malloc(sizeof(LIST),MYF(MY_FAE)); |
|
72 |
if (!new_charset) |
|
73 |
return 0; |
|
74 |
new_charset->data=data; |
|
75 |
return list_add(list,new_charset); |
|
76 |
}
|
|
77 |
||
78 |
||
79 |
LIST *list_reverse(LIST *root) |
|
80 |
{
|
|
81 |
LIST *last; |
|
82 |
||
83 |
last=root; |
|
84 |
while (root) |
|
85 |
{
|
|
86 |
last=root; |
|
87 |
root=root->next; |
|
88 |
last->next=last->prev; |
|
89 |
last->prev=root; |
|
90 |
}
|
|
91 |
return last; |
|
92 |
}
|
|
93 |
||
94 |
uint list_length(LIST *list) |
|
95 |
{
|
|
96 |
uint count; |
|
97 |
for (count=0 ; list ; list=list->next, count++) ; |
|
98 |
return count; |
|
99 |
}
|
|
100 |
||
101 |
||
102 |
int list_walk(LIST *list, list_walk_action action, uchar* argument) |
|
103 |
{
|
|
104 |
int error=0; |
|
105 |
while (list) |
|
106 |
{
|
|
107 |
if ((error = (*action)(list->data,argument))) |
|
108 |
return error; |
|
109 |
list=list_rest(list); |
|
110 |
}
|
|
111 |
return 0; |
|
112 |
}
|