1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3 * Copyright (c) 1999-2004 Carnegie Mellon University. All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * This work was supported in part by funding from the Defense Advanced
19 * Research Projects Agency and the National Science Foundation of the
20 * United States of America, and the CMU Sphinx Speech Consortium.
22 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * ====================================================================
38 * glist.h -- Module for maintaining a generic, linear linked-list structure.
40 * **********************************************
41 * CMU ARPA Speech Project
43 * Copyright (c) 1999 Carnegie Mellon University.
44 * ALL RIGHTS RESERVED.
45 * **********************************************
49 * Revision 1.8 2005/06/22 03:02:51 arthchan2003
50 * 1, Fixed doxygen documentation, 2, add keyword.
52 * Revision 1.3 2005/03/30 01:22:48 archan
53 * Fixed mistakes in last updates. Add
56 * 09-Mar-1999 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
57 * Added glist_chkdup_*().
59 * 13-Feb-1999 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
60 * Created from earlier version.
69 #include "sphinxbase/glist.h"
70 #include "sphinxbase/ckd_alloc.h"
74 glist_add_ptr(glist_t g, void *ptr)
78 gn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t));
81 return ((glist_t) gn); /* Return the new head of the list */
86 glist_add_int32(glist_t g, int32 val)
90 gn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t));
91 gn->data.i = (long)val;
93 return ((glist_t) gn); /* Return the new head of the list */
98 glist_add_uint32(glist_t g, uint32 val)
102 gn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t));
103 gn->data.ui = (unsigned long)val;
105 return ((glist_t) gn); /* Return the new head of the list */
110 glist_add_float32(glist_t g, float32 val)
114 gn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t));
115 gn->data.fl = (double)val;
117 return ((glist_t) gn); /* Return the new head of the list */
122 glist_add_float64(glist_t g, float64 val)
126 gn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t));
127 gn->data.fl = (double)val;
129 return ((glist_t) gn); /* Return the new head of the list */
133 glist_free(glist_t g)
140 ckd_free((void *) gn);
145 glist_count(glist_t g)
150 for (gn = g, n = 0; gn; gn = gn->next, n++);
156 glist_tail(glist_t g)
163 for (gn = g; gn->next; gn = gn->next);
169 glist_reverse(glist_t g)
171 gnode_t *gn, *nextgn;
175 for (gn = g; gn; gn = nextgn) {
187 glist_insert_ptr(gnode_t * gn, void *ptr)
191 newgn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t));
192 newgn->data.ptr = ptr;
193 newgn->next = gn->next;
201 glist_insert_int32(gnode_t * gn, int32 val)
205 newgn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t));
207 newgn->next = gn->next;
215 glist_insert_uint32(gnode_t * gn, uint32 val)
219 newgn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t));
220 newgn->data.ui = val;
221 newgn->next = gn->next;
230 glist_insert_float32(gnode_t * gn, float32 val)
234 newgn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t));
235 newgn->data.fl = (double)val;
236 newgn->next = gn->next;
244 glist_insert_float64(gnode_t * gn, float64 val)
248 newgn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t));
249 newgn->data.fl = (double)val;
250 newgn->next = gn->next;
257 gnode_free(gnode_t * gn, gnode_t * pred)
263 assert(pred->next == gn);
268 ckd_free((char *) gn);