[96fb8ad] | 1 | /* GLIB - Library of useful routines for C programming |
---|
| 2 | * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald |
---|
| 3 | * |
---|
| 4 | * This library is free software; you can redistribute it and/or |
---|
| 5 | * modify it under the terms of the GNU Library General Public |
---|
| 6 | * License as published by the Free Software Foundation; either |
---|
| 7 | * version 2 of the License, or (at your option) any later version. |
---|
| 8 | * |
---|
| 9 | * This library is distributed in the hope that it will be useful, |
---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 12 | * Library General Public License for more details. |
---|
| 13 | * |
---|
| 14 | * You should have received a copy of the GNU Library General Public |
---|
| 15 | * License along with this library; if not, write to the |
---|
| 16 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
| 17 | * Boston, MA 02111-1307, USA. |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | /* Modified by the GLib Team and others 1997-1999. See the AUTHORS |
---|
| 21 | * file for a list of people on the GLib Team. See the ChangeLog |
---|
| 22 | * files for a list of changes. These files are distributed with |
---|
| 23 | * GLib at ftp://ftp.gtk.org/pub/gtk/. |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | #include "aubio_priv.h" |
---|
| 27 | #include "list.h" |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | aubio_list_t* |
---|
| 32 | new_aubio_list(void) |
---|
| 33 | { |
---|
| 34 | aubio_list_t* list; |
---|
| 35 | list = AUBIO_NEW(aubio_list_t); |
---|
| 36 | list->data = NULL; |
---|
| 37 | list->next = NULL; |
---|
| 38 | return list; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | void |
---|
| 42 | del_aubio_list(aubio_list_t *list) |
---|
| 43 | { |
---|
| 44 | aubio_list_t *next; |
---|
| 45 | while (list) { |
---|
| 46 | next = list->next; |
---|
| 47 | AUBIO_FREE(list); |
---|
| 48 | list = next; |
---|
| 49 | } |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | void |
---|
| 53 | del_aubio_list1(aubio_list_t *list) |
---|
| 54 | { |
---|
| 55 | if (list) { |
---|
| 56 | AUBIO_FREE(list); |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | aubio_list_t* |
---|
| 61 | aubio_list_append(aubio_list_t *list, void* data) |
---|
| 62 | { |
---|
| 63 | aubio_list_t *new_list; |
---|
| 64 | aubio_list_t *last; |
---|
| 65 | |
---|
| 66 | new_list = new_aubio_list(); |
---|
| 67 | new_list->data = data; |
---|
| 68 | |
---|
| 69 | if (list) |
---|
| 70 | { |
---|
| 71 | last = aubio_list_last(list); |
---|
| 72 | /* g_assert (last != NULL); */ |
---|
| 73 | last->next = new_list; |
---|
| 74 | |
---|
| 75 | return list; |
---|
| 76 | } |
---|
| 77 | else |
---|
| 78 | return new_list; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | aubio_list_t* |
---|
| 82 | aubio_list_prepend(aubio_list_t *list, void* data) |
---|
| 83 | { |
---|
| 84 | aubio_list_t *new_list; |
---|
| 85 | |
---|
| 86 | new_list = new_aubio_list(); |
---|
| 87 | new_list->data = data; |
---|
| 88 | new_list->next = list; |
---|
| 89 | |
---|
| 90 | return new_list; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | aubio_list_t* |
---|
| 94 | aubio_list_nth(aubio_list_t *list, int n) |
---|
| 95 | { |
---|
| 96 | while ((n-- > 0) && list) { |
---|
| 97 | list = list->next; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | return list; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | aubio_list_t* |
---|
| 104 | aubio_list_remove(aubio_list_t *list, void* data) |
---|
| 105 | { |
---|
| 106 | aubio_list_t *tmp; |
---|
| 107 | aubio_list_t *prev; |
---|
| 108 | |
---|
| 109 | prev = NULL; |
---|
| 110 | tmp = list; |
---|
| 111 | |
---|
| 112 | while (tmp) { |
---|
| 113 | if (tmp->data == data) { |
---|
| 114 | if (prev) { |
---|
| 115 | prev->next = tmp->next; |
---|
| 116 | } |
---|
| 117 | if (list == tmp) { |
---|
| 118 | list = list->next; |
---|
| 119 | } |
---|
| 120 | tmp->next = NULL; |
---|
| 121 | del_aubio_list(tmp); |
---|
| 122 | |
---|
| 123 | break; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | prev = tmp; |
---|
| 127 | tmp = tmp->next; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | return list; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | aubio_list_t* |
---|
| 134 | aubio_list_remove_link(aubio_list_t *list, aubio_list_t *link) |
---|
| 135 | { |
---|
| 136 | aubio_list_t *tmp; |
---|
| 137 | aubio_list_t *prev; |
---|
| 138 | |
---|
| 139 | prev = NULL; |
---|
| 140 | tmp = list; |
---|
| 141 | |
---|
| 142 | while (tmp) { |
---|
| 143 | if (tmp == link) { |
---|
| 144 | if (prev) { |
---|
| 145 | prev->next = tmp->next; |
---|
| 146 | } |
---|
| 147 | if (list == tmp) { |
---|
| 148 | list = list->next; |
---|
| 149 | } |
---|
| 150 | tmp->next = NULL; |
---|
| 151 | break; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | prev = tmp; |
---|
| 155 | tmp = tmp->next; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | return list; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | static aubio_list_t* |
---|
| 162 | aubio_list_sort_merge(aubio_list_t *l1, aubio_list_t *l2, aubio_compare_func_t compare_func) |
---|
| 163 | { |
---|
| 164 | aubio_list_t list, *l; |
---|
| 165 | |
---|
| 166 | l = &list; |
---|
| 167 | |
---|
| 168 | while (l1 && l2) { |
---|
| 169 | if (compare_func(l1->data,l2->data) < 0) { |
---|
| 170 | l = l->next = l1; |
---|
| 171 | l1 = l1->next; |
---|
| 172 | } else { |
---|
| 173 | l = l->next = l2; |
---|
| 174 | l2 = l2->next; |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | l->next= l1 ? l1 : l2; |
---|
| 178 | |
---|
| 179 | return list.next; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | aubio_list_t* |
---|
| 183 | aubio_list_sort(aubio_list_t *list, aubio_compare_func_t compare_func) |
---|
| 184 | { |
---|
| 185 | aubio_list_t *l1, *l2; |
---|
| 186 | |
---|
| 187 | if (!list) { |
---|
| 188 | return NULL; |
---|
| 189 | } |
---|
| 190 | if (!list->next) { |
---|
| 191 | return list; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | l1 = list; |
---|
| 195 | l2 = list->next; |
---|
| 196 | |
---|
| 197 | while ((l2 = l2->next) != NULL) { |
---|
| 198 | if ((l2 = l2->next) == NULL) |
---|
| 199 | break; |
---|
| 200 | l1=l1->next; |
---|
| 201 | } |
---|
| 202 | l2 = l1->next; |
---|
| 203 | l1->next = NULL; |
---|
| 204 | |
---|
| 205 | return aubio_list_sort_merge(aubio_list_sort(list, compare_func), |
---|
| 206 | aubio_list_sort(l2, compare_func), |
---|
| 207 | compare_func); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | |
---|
| 211 | aubio_list_t* |
---|
| 212 | aubio_list_last(aubio_list_t *list) |
---|
| 213 | { |
---|
| 214 | if (list) { |
---|
| 215 | while (list->next) |
---|
| 216 | list = list->next; |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | return list; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | int |
---|
| 223 | aubio_list_size(aubio_list_t *list) |
---|
| 224 | { |
---|
| 225 | int n = 0; |
---|
| 226 | while (list) { |
---|
| 227 | n++; |
---|
| 228 | list = list->next; |
---|
| 229 | } |
---|
| 230 | return n; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | aubio_list_t* aubio_list_insert_at(aubio_list_t *list, int n, void* data) |
---|
| 234 | { |
---|
| 235 | aubio_list_t *new_list; |
---|
| 236 | aubio_list_t *cur; |
---|
| 237 | aubio_list_t *prev = NULL; |
---|
| 238 | |
---|
| 239 | new_list = new_aubio_list(); |
---|
| 240 | new_list->data = data; |
---|
| 241 | |
---|
| 242 | cur = list; |
---|
| 243 | while ((n-- > 0) && cur) { |
---|
| 244 | prev = cur; |
---|
| 245 | cur = cur->next; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | new_list->next = cur; |
---|
| 249 | |
---|
| 250 | if (prev) { |
---|
| 251 | prev->next = new_list; |
---|
| 252 | return list; |
---|
| 253 | } else { |
---|
| 254 | return new_list; |
---|
| 255 | } |
---|
| 256 | } |
---|