source: ext/midi/list.c @ ac4f38e

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since ac4f38e was b60dd4ae, checked in by Paul Brossier <piem@altern.org>, 20 years ago

moved midi functions to ext/

  • Property mode set to 100644
File size: 4.6 KB
Line 
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
31aubio_list_t*
32new_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
41void
42del_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
52void
53del_aubio_list1(aubio_list_t *list)
54{
55  if (list) {
56    AUBIO_FREE(list);
57  }
58}
59
60aubio_list_t*
61aubio_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
81aubio_list_t*
82aubio_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
93aubio_list_t*
94aubio_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
103aubio_list_t*
104aubio_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
133aubio_list_t*
134aubio_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
161static aubio_list_t* 
162aubio_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
182aubio_list_t* 
183aubio_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
211aubio_list_t*
212aubio_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
222int 
223aubio_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
233aubio_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}
Note: See TracBrowser for help on using the repository browser.