13
0
livetrax/libs/fluidsynth/src/fluid_list.c

338 lines
5.8 KiB
C
Raw Normal View History

/* GLIB - Library of useful routines for C programming
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* This library is free software; you can redistribute it and/or
2018-10-17 18:41:02 -04:00
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2018-10-17 18:41:02 -04:00
* Lesser General Public License for more details.
*
2018-10-17 18:41:02 -04:00
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02110-1301, USA.
*/
/*
* Modified by the GLib Team and others 1997-1999. See the AUTHORS
* file for a list of people on the GLib Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GLib at ftp://ftp.gtk.org/pub/gtk/.
*/
2018-10-17 18:41:02 -04:00
#include "fluid_sys.h"
#include "fluid_list.h"
2018-10-17 18:41:02 -04:00
fluid_list_t *
new_fluid_list(void)
{
2018-10-17 18:41:02 -04:00
fluid_list_t *list;
list = (fluid_list_t *) FLUID_MALLOC(sizeof(fluid_list_t));
list->data = NULL;
list->next = NULL;
return list;
}
void
delete_fluid_list(fluid_list_t *list)
{
2018-10-17 18:41:02 -04:00
fluid_list_t *next;
fluid_return_if_fail(list != NULL);
while(list)
{
next = list->next;
FLUID_FREE(list);
list = next;
}
}
void
delete1_fluid_list(fluid_list_t *list)
{
FLUID_FREE(list);
}
2018-10-17 18:41:02 -04:00
fluid_list_t *
fluid_list_append(fluid_list_t *list, void *data)
{
2018-10-17 18:41:02 -04:00
fluid_list_t *new_list;
fluid_list_t *last;
2018-10-17 18:41:02 -04:00
new_list = new_fluid_list();
new_list->data = data;
2018-10-17 18:41:02 -04:00
if(list)
{
2018-10-17 18:41:02 -04:00
last = fluid_list_last(list);
/* g_assert (last != NULL); */
last->next = new_list;
2018-10-17 18:41:02 -04:00
return list;
}
else
{
return new_list;
}
}
2018-10-17 18:41:02 -04:00
fluid_list_t *
fluid_list_prepend(fluid_list_t *list, void *data)
{
2018-10-17 18:41:02 -04:00
fluid_list_t *new_list;
2018-10-17 18:41:02 -04:00
new_list = new_fluid_list();
new_list->data = data;
new_list->next = list;
2018-10-17 18:41:02 -04:00
return new_list;
}
2018-10-17 18:41:02 -04:00
fluid_list_t *
fluid_list_nth(fluid_list_t *list, int n)
{
2018-10-17 18:41:02 -04:00
while((n-- > 0) && list)
{
list = list->next;
}
2018-10-17 18:41:02 -04:00
return list;
}
2018-10-17 18:41:02 -04:00
fluid_list_t *
fluid_list_remove(fluid_list_t *list, void *data)
{
2018-10-17 18:41:02 -04:00
fluid_list_t *tmp;
fluid_list_t *prev;
2018-10-17 18:41:02 -04:00
prev = NULL;
tmp = list;
2018-10-17 18:41:02 -04:00
while(tmp)
{
if(tmp->data == data)
{
if(prev)
{
prev->next = tmp->next;
}
if(list == tmp)
{
list = list->next;
}
tmp->next = NULL;
delete_fluid_list(tmp);
break;
}
prev = tmp;
tmp = tmp->next;
}
return list;
}
2018-10-17 18:41:02 -04:00
fluid_list_t *
fluid_list_remove_link(fluid_list_t *list, fluid_list_t *link)
{
2018-10-17 18:41:02 -04:00
fluid_list_t *tmp;
fluid_list_t *prev;
prev = NULL;
tmp = list;
2018-10-17 18:41:02 -04:00
while(tmp)
{
if(tmp == link)
{
if(prev)
{
prev->next = tmp->next;
}
if(list == tmp)
{
list = list->next;
}
tmp->next = NULL;
break;
}
prev = tmp;
tmp = tmp->next;
}
2018-10-17 18:41:02 -04:00
return list;
}
2018-10-17 18:41:02 -04:00
static fluid_list_t *
fluid_list_sort_merge(fluid_list_t *l1, fluid_list_t *l2, fluid_compare_func_t compare_func)
{
2018-10-17 18:41:02 -04:00
fluid_list_t list, *l;
2018-10-17 18:41:02 -04:00
l = &list;
2018-10-17 18:41:02 -04:00
while(l1 && l2)
{
if(compare_func(l1->data, l2->data) < 0)
{
l = l->next = l1;
l1 = l1->next;
}
else
{
l = l->next = l2;
l2 = l2->next;
}
}
2018-10-17 18:41:02 -04:00
l->next = l1 ? l1 : l2;
return list.next;
}
2018-10-17 18:41:02 -04:00
fluid_list_t *
fluid_list_sort(fluid_list_t *list, fluid_compare_func_t compare_func)
{
2018-10-17 18:41:02 -04:00
fluid_list_t *l1, *l2;
2018-10-17 18:41:02 -04:00
if(!list)
{
return NULL;
}
if(!list->next)
{
return list;
}
l1 = list;
l2 = list->next;
while((l2 = l2->next) != NULL)
{
if((l2 = l2->next) == NULL)
{
break;
}
l1 = l1->next;
}
l2 = l1->next;
l1->next = NULL;
return fluid_list_sort_merge(fluid_list_sort(list, compare_func),
fluid_list_sort(l2, compare_func),
compare_func);
}
2018-10-17 18:41:02 -04:00
fluid_list_t *
fluid_list_last(fluid_list_t *list)
{
2018-10-17 18:41:02 -04:00
if(list)
{
while(list->next)
{
list = list->next;
}
}
2018-10-17 18:41:02 -04:00
return list;
}
int
fluid_list_size(fluid_list_t *list)
{
2018-10-17 18:41:02 -04:00
int n = 0;
while(list)
{
n++;
list = list->next;
}
return n;
}
2018-10-17 18:41:02 -04:00
fluid_list_t *fluid_list_insert_at(fluid_list_t *list, int n, void *data)
{
2018-10-17 18:41:02 -04:00
fluid_list_t *new_list;
fluid_list_t *cur;
fluid_list_t *prev = NULL;
2018-10-17 18:41:02 -04:00
new_list = new_fluid_list();
new_list->data = data;
2018-10-17 18:41:02 -04:00
cur = list;
2018-10-17 18:41:02 -04:00
while((n-- > 0) && cur)
{
prev = cur;
cur = cur->next;
}
2018-10-17 18:41:02 -04:00
new_list->next = cur;
if(prev)
{
prev->next = new_list;
return list;
}
else
{
return new_list;
}
}
/* Compare function to sort strings alphabetically,
* for use with fluid_list_sort(). */
int
2018-10-17 18:41:02 -04:00
fluid_list_str_compare_func(void *a, void *b)
{
2018-10-17 18:41:02 -04:00
if(a && b)
{
return FLUID_STRCMP((char *)a, (char *)b);
}
if(!a && !b)
{
return 0;
}
if(a)
{
return -1;
}
return 1;
}
2021-04-18 15:25:20 -04:00
int fluid_list_idx(fluid_list_t *list, void *data)
{
int i = 0;
while(list)
{
if (list->data == data)
{
return i;
}
list = list->next;
}
return -1;
}