Path: blob/master/libs/fluidsynth/src/utils/fluid_list.c
4396 views
/* GLIB - Library of useful routines for C programming1* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald2*3* This library is free software; you can redistribute it and/or4* modify it under the terms of the GNU Lesser General Public5* License as published by the Free Software Foundation; either6* version 2 of the License, or (at your option) any later version.7*8* This library is distributed in the hope that it will be useful,9* but WITHOUT ANY WARRANTY; without even the implied warranty of10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU11* Lesser General Public License for more details.12*13* You should have received a copy of the GNU Lesser General Public14* License along with this library; if not, write to the15* Free Software Foundation, Inc., 59 Temple Place - Suite 330,16* Boston, MA 02110-1301, USA.17*/1819/*20* Modified by the GLib Team and others 1997-1999. See the AUTHORS21* file for a list of people on the GLib Team. See the ChangeLog22* files for a list of changes. These files are distributed with23* GLib at ftp://ftp.gtk.org/pub/gtk/.24*/25262728#include "fluid_sys.h"29#include "fluid_list.h"303132fluid_list_t *33new_fluid_list(void)34{35fluid_list_t *list;36list = (fluid_list_t *) FLUID_MALLOC(sizeof(fluid_list_t));37list->data = NULL;38list->next = NULL;39return list;40}4142void43delete_fluid_list(fluid_list_t *list)44{45fluid_list_t *next;46fluid_return_if_fail(list != NULL);4748while(list)49{50next = list->next;51FLUID_FREE(list);52list = next;53}54}5556void57delete1_fluid_list(fluid_list_t *list)58{59FLUID_FREE(list);60}6162fluid_list_t *63fluid_list_append(fluid_list_t *list, void *data)64{65fluid_list_t *new_list;66fluid_list_t *last;6768new_list = new_fluid_list();69new_list->data = data;7071if(list)72{73last = fluid_list_last(list);74/* g_assert (last != NULL); */75last->next = new_list;7677return list;78}79else80{81return new_list;82}83}8485fluid_list_t *86fluid_list_prepend(fluid_list_t *list, void *data)87{88fluid_list_t *new_list;8990new_list = new_fluid_list();91new_list->data = data;92new_list->next = list;9394return new_list;95}9697fluid_list_t *98fluid_list_nth(fluid_list_t *list, int n)99{100while((n-- > 0) && list)101{102list = list->next;103}104105return list;106}107108fluid_list_t *109fluid_list_remove(fluid_list_t *list, void *data)110{111fluid_list_t *tmp;112fluid_list_t *prev;113114prev = NULL;115tmp = list;116117while(tmp)118{119if(tmp->data == data)120{121if(prev)122{123prev->next = tmp->next;124}125126if(list == tmp)127{128list = list->next;129}130131tmp->next = NULL;132delete_fluid_list(tmp);133134break;135}136137prev = tmp;138tmp = tmp->next;139}140141return list;142}143144fluid_list_t *145fluid_list_remove_link(fluid_list_t *list, fluid_list_t *link)146{147fluid_list_t *tmp;148fluid_list_t *prev;149150prev = NULL;151tmp = list;152153while(tmp)154{155if(tmp == link)156{157if(prev)158{159prev->next = tmp->next;160}161162if(list == tmp)163{164list = list->next;165}166167tmp->next = NULL;168break;169}170171prev = tmp;172tmp = tmp->next;173}174175return list;176}177178static fluid_list_t *179fluid_list_sort_merge(fluid_list_t *l1, fluid_list_t *l2, fluid_compare_func_t compare_func)180{181fluid_list_t list, *l;182183l = &list;184185while(l1 && l2)186{187if(compare_func(l1->data, l2->data) < 0)188{189l = l->next = l1;190l1 = l1->next;191}192else193{194l = l->next = l2;195l2 = l2->next;196}197}198199l->next = l1 ? l1 : l2;200201return list.next;202}203204fluid_list_t *205fluid_list_sort(fluid_list_t *list, fluid_compare_func_t compare_func)206{207fluid_list_t *l1, *l2;208209if(!list)210{211return NULL;212}213214if(!list->next)215{216return list;217}218219l1 = list;220l2 = list->next;221222while((l2 = l2->next) != NULL)223{224if((l2 = l2->next) == NULL)225{226break;227}228229l1 = l1->next;230}231232l2 = l1->next;233l1->next = NULL;234235return fluid_list_sort_merge(fluid_list_sort(list, compare_func),236fluid_list_sort(l2, compare_func),237compare_func);238}239240241fluid_list_t *242fluid_list_last(fluid_list_t *list)243{244if(list)245{246while(list->next)247{248list = list->next;249}250}251252return list;253}254255int256fluid_list_size(fluid_list_t *list)257{258int n = 0;259260while(list)261{262n++;263list = list->next;264}265266return n;267}268269fluid_list_t *fluid_list_insert_at(fluid_list_t *list, int n, void *data)270{271fluid_list_t *new_list;272fluid_list_t *cur;273fluid_list_t *prev = NULL;274275new_list = new_fluid_list();276new_list->data = data;277278cur = list;279280while((n-- > 0) && cur)281{282prev = cur;283cur = cur->next;284}285286new_list->next = cur;287288if(prev)289{290prev->next = new_list;291return list;292}293else294{295return new_list;296}297}298299/* Compare function to sort strings alphabetically,300* for use with fluid_list_sort(). */301int302fluid_list_str_compare_func(const void *a, const void *b)303{304if(a && b)305{306return FLUID_STRCMP(a, b);307}308309if(!a && !b)310{311return 0;312}313314if(a)315{316return -1;317}318319return 1;320}321322int fluid_list_idx(fluid_list_t *list, void *data)323{324int i = 0;325326while(list)327{328if (list->data == data)329{330return i;331}332list = list->next;333}334335return -1;336}337338339