[b7d3cc34] | 1 | /* |
---|
| 2 | * yahoo_list.c: linked list routines |
---|
| 3 | * |
---|
| 4 | * Some code copyright (C) 2002-2004, Philip S Tellis <philip.tellis AT gmx.net> |
---|
| 5 | * Other code copyright Meredydd Luff <meredydd AT everybuddy.com> |
---|
| 6 | * |
---|
| 7 | * This program is free software; you can redistribute it and/or modify |
---|
| 8 | * it under the terms of the GNU General Public License as published by |
---|
| 9 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 10 | * (at your option) any later version. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 20 | * |
---|
| 21 | * Some of this code was borrowed from elist.c in the eb-lite sources |
---|
| 22 | * |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #include <stdlib.h> |
---|
| 26 | |
---|
| 27 | #include "yahoo_list.h" |
---|
| 28 | |
---|
| 29 | YList *y_list_append(YList * list, void *data) |
---|
| 30 | { |
---|
| 31 | YList *n; |
---|
| 32 | YList *new_list = malloc(sizeof(YList)); |
---|
| 33 | YList *attach_to = NULL; |
---|
| 34 | |
---|
| 35 | new_list->next = NULL; |
---|
| 36 | new_list->data = data; |
---|
| 37 | |
---|
| 38 | for (n = list; n != NULL; n = n->next) { |
---|
| 39 | attach_to = n; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | if (attach_to == NULL) { |
---|
| 43 | new_list->prev = NULL; |
---|
| 44 | return new_list; |
---|
| 45 | } else { |
---|
| 46 | new_list->prev = attach_to; |
---|
| 47 | attach_to->next = new_list; |
---|
| 48 | return list; |
---|
| 49 | } |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | YList *y_list_prepend(YList * list, void *data) |
---|
| 53 | { |
---|
| 54 | YList *n = malloc(sizeof(YList)); |
---|
| 55 | |
---|
| 56 | n->next = list; |
---|
| 57 | n->prev = NULL; |
---|
| 58 | n->data = data; |
---|
| 59 | if (list) |
---|
| 60 | list->prev = n; |
---|
| 61 | |
---|
| 62 | return n; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | YList *y_list_concat(YList * list, YList * add) |
---|
| 66 | { |
---|
| 67 | YList *l; |
---|
| 68 | |
---|
| 69 | if(!list) |
---|
| 70 | return add; |
---|
| 71 | |
---|
| 72 | if(!add) |
---|
| 73 | return list; |
---|
| 74 | |
---|
| 75 | for (l = list; l->next; l = l->next) |
---|
| 76 | ; |
---|
| 77 | |
---|
| 78 | l->next = add; |
---|
| 79 | add->prev = l; |
---|
| 80 | |
---|
| 81 | return list; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | YList *y_list_remove(YList * list, void *data) |
---|
| 85 | { |
---|
| 86 | YList *n; |
---|
| 87 | |
---|
| 88 | for (n = list; n != NULL; n = n->next) { |
---|
| 89 | if (n->data == data) { |
---|
| 90 | list=y_list_remove_link(list, n); |
---|
| 91 | y_list_free_1(n); |
---|
| 92 | break; |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | return list; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /* Warning */ |
---|
| 100 | /* link MUST be part of list */ |
---|
| 101 | /* caller must free link using y_list_free_1 */ |
---|
| 102 | YList *y_list_remove_link(YList * list, const YList * link) |
---|
| 103 | { |
---|
| 104 | if (!link) |
---|
| 105 | return list; |
---|
| 106 | |
---|
| 107 | if (link->next) |
---|
| 108 | link->next->prev = link->prev; |
---|
| 109 | if (link->prev) |
---|
| 110 | link->prev->next = link->next; |
---|
| 111 | |
---|
| 112 | if (link == list) |
---|
| 113 | list = link->next; |
---|
| 114 | |
---|
| 115 | return list; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | int y_list_length(const YList * list) |
---|
| 119 | { |
---|
| 120 | int retval = 0; |
---|
| 121 | const YList *n = list; |
---|
| 122 | |
---|
| 123 | for (n = list; n != NULL; n = n->next) { |
---|
| 124 | retval++; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | return retval; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /* well, you could just check for list == NULL, but that would be |
---|
| 131 | * implementation dependent |
---|
| 132 | */ |
---|
| 133 | int y_list_empty(const YList * list) |
---|
| 134 | { |
---|
| 135 | if(!list) |
---|
| 136 | return 1; |
---|
| 137 | else |
---|
| 138 | return 0; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | int y_list_singleton(const YList * list) |
---|
| 142 | { |
---|
| 143 | if(!list || list->next) |
---|
| 144 | return 0; |
---|
| 145 | return 1; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | YList *y_list_copy(YList * list) |
---|
| 149 | { |
---|
| 150 | YList *n; |
---|
| 151 | YList *copy = NULL; |
---|
| 152 | |
---|
| 153 | for (n = list; n != NULL; n = n->next) { |
---|
| 154 | copy = y_list_append(copy, n->data); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | return copy; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | void y_list_free_1(YList * list) |
---|
| 161 | { |
---|
| 162 | free(list); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | void y_list_free(YList * list) |
---|
| 166 | { |
---|
| 167 | YList *n = list; |
---|
| 168 | |
---|
| 169 | while (n != NULL) { |
---|
| 170 | YList *next = n->next; |
---|
| 171 | free(n); |
---|
| 172 | n = next; |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | YList *y_list_find(YList * list, const void *data) |
---|
| 177 | { |
---|
| 178 | YList *l; |
---|
| 179 | for (l = list; l && l->data != data; l = l->next) |
---|
| 180 | ; |
---|
| 181 | |
---|
| 182 | return l; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | void y_list_foreach(YList * list, YListFunc fn, void * user_data) |
---|
| 186 | { |
---|
| 187 | for (; list; list = list->next) |
---|
| 188 | fn(list->data, user_data); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | YList *y_list_find_custom(YList * list, const void *data, YListCompFunc comp) |
---|
| 192 | { |
---|
| 193 | YList *l; |
---|
| 194 | for (l = list; l; l = l->next) |
---|
| 195 | if (comp(l->data, data) == 0) |
---|
| 196 | return l; |
---|
| 197 | |
---|
| 198 | return NULL; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | YList *y_list_nth(YList * list, int n) |
---|
| 202 | { |
---|
| 203 | int i=n; |
---|
| 204 | for ( ; list && i; list = list->next, i--) |
---|
| 205 | ; |
---|
| 206 | |
---|
| 207 | return list; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | YList *y_list_insert_sorted(YList * list, void *data, YListCompFunc comp) |
---|
| 211 | { |
---|
| 212 | YList *l, *n, *prev = NULL; |
---|
| 213 | if (!list) |
---|
| 214 | return y_list_append(list, data); |
---|
| 215 | |
---|
| 216 | n = malloc(sizeof(YList)); |
---|
| 217 | n->data = data; |
---|
| 218 | for (l = list; l && comp(l->data, n->data) <= 0; l = l->next) |
---|
| 219 | prev = l; |
---|
| 220 | |
---|
| 221 | if (l) { |
---|
| 222 | n->prev = l->prev; |
---|
| 223 | l->prev = n; |
---|
| 224 | } else |
---|
| 225 | n->prev = prev; |
---|
| 226 | |
---|
| 227 | n->next = l; |
---|
| 228 | |
---|
| 229 | if(n->prev) { |
---|
| 230 | n->prev->next = n; |
---|
| 231 | return list; |
---|
| 232 | } else { |
---|
| 233 | return n; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | } |
---|