• Main Page
  • Related Pages
  • Data Structures
  • Files
  • File List
  • Globals

src/libsphinxbase/util/glist.c

00001 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
00002 /* ====================================================================
00003  * Copyright (c) 1999-2004 Carnegie Mellon University.  All rights
00004  * reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer. 
00012  *
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in
00015  *    the documentation and/or other materials provided with the
00016  *    distribution.
00017  *
00018  * This work was supported in part by funding from the Defense Advanced 
00019  * Research Projects Agency and the National Science Foundation of the 
00020  * United States of America, and the CMU Sphinx Speech Consortium.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 
00023  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
00024  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00025  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
00026  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00027  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
00028  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
00029  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
00030  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00031  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
00032  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  * ====================================================================
00035  *
00036  */
00037 /*
00038  * glist.h -- Module for maintaining a generic, linear linked-list structure.
00039  *
00040  * **********************************************
00041  * CMU ARPA Speech Project
00042  *
00043  * Copyright (c) 1999 Carnegie Mellon University.
00044  * ALL RIGHTS RESERVED.
00045  * **********************************************
00046  * 
00047  * HISTORY
00048  * $Log: glist.c,v $
00049  * Revision 1.8  2005/06/22 03:02:51  arthchan2003
00050  * 1, Fixed doxygen documentation, 2, add  keyword.
00051  *
00052  * Revision 1.3  2005/03/30 01:22:48  archan
00053  * Fixed mistakes in last updates. Add
00054  *
00055  * 
00056  * 09-Mar-1999  M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
00057  *              Added glist_chkdup_*().
00058  * 
00059  * 13-Feb-1999  M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
00060  *              Created from earlier version.
00061  */
00062 
00063 
00064 #include <stdio.h>
00065 #include <stdlib.h>
00066 #include <string.h>
00067 #include <assert.h>
00068 
00069 #include "glist.h"
00070 #include "ckd_alloc.h"
00071 
00072 
00073 glist_t
00074 glist_add_ptr(glist_t g, void *ptr)
00075 {
00076     gnode_t *gn;
00077 
00078     gn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t));
00079     gn->data.ptr = ptr;
00080     gn->next = g;
00081     return ((glist_t) gn);      /* Return the new head of the list */
00082 }
00083 
00084 
00085 glist_t
00086 glist_add_int32(glist_t g, int32 val)
00087 {
00088     gnode_t *gn;
00089 
00090     gn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t));
00091     gn->data.i = (long)val;
00092     gn->next = g;
00093     return ((glist_t) gn);      /* Return the new head of the list */
00094 }
00095 
00096 
00097 glist_t
00098 glist_add_uint32(glist_t g, uint32 val)
00099 {
00100     gnode_t *gn;
00101 
00102     gn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t));
00103     gn->data.ui = (unsigned long)val;
00104     gn->next = g;
00105     return ((glist_t) gn);      /* Return the new head of the list */
00106 }
00107 
00108 
00109 glist_t
00110 glist_add_float32(glist_t g, float32 val)
00111 {
00112     gnode_t *gn;
00113 
00114     gn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t));
00115     gn->data.fl = (double)val;
00116     gn->next = g;
00117     return ((glist_t) gn);      /* Return the new head of the list */
00118 }
00119 
00120 
00121 glist_t
00122 glist_add_float64(glist_t g, float64 val)
00123 {
00124     gnode_t *gn;
00125 
00126     gn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t));
00127     gn->data.fl = (double)val;
00128     gn->next = g;
00129     return ((glist_t) gn);      /* Return the new head of the list */
00130 }
00131 
00132 void
00133 glist_free(glist_t g)
00134 {
00135     gnode_t *gn;
00136 
00137     while (g) {
00138         gn = g;
00139         g = gn->next;
00140         ckd_free((void *) gn);
00141     }
00142 }
00143 
00144 int32
00145 glist_count(glist_t g)
00146 {
00147     gnode_t *gn;
00148     int32 n;
00149 
00150     for (gn = g, n = 0; gn; gn = gn->next, n++);
00151     return n;
00152 }
00153 
00154 
00155 gnode_t *
00156 glist_tail(glist_t g)
00157 {
00158     gnode_t *gn;
00159 
00160     if (!g)
00161         return NULL;
00162 
00163     for (gn = g; gn->next; gn = gn->next);
00164     return gn;
00165 }
00166 
00167 
00168 glist_t
00169 glist_reverse(glist_t g)
00170 {
00171     gnode_t *gn, *nextgn;
00172     gnode_t *rev;
00173 
00174     rev = NULL;
00175     for (gn = g; gn; gn = nextgn) {
00176         nextgn = gn->next;
00177 
00178         gn->next = rev;
00179         rev = gn;
00180     }
00181 
00182     return rev;
00183 }
00184 
00185 
00186 gnode_t *
00187 glist_insert_ptr(gnode_t * gn, void *ptr)
00188 {
00189     gnode_t *newgn;
00190 
00191     newgn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t));
00192     newgn->data.ptr = ptr;
00193     newgn->next = gn->next;
00194     gn->next = newgn;
00195 
00196     return newgn;
00197 }
00198 
00199 
00200 gnode_t *
00201 glist_insert_int32(gnode_t * gn, int32 val)
00202 {
00203     gnode_t *newgn;
00204 
00205     newgn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t));
00206     newgn->data.i = val;
00207     newgn->next = gn->next;
00208     gn->next = newgn;
00209 
00210     return newgn;
00211 }
00212 
00213 
00214 gnode_t *
00215 glist_insert_uint32(gnode_t * gn, uint32 val)
00216 {
00217     gnode_t *newgn;
00218 
00219     newgn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t));
00220     newgn->data.ui = val;
00221     newgn->next = gn->next;
00222 
00223     gn->next = newgn;
00224 
00225     return newgn;
00226 }
00227 
00228 
00229 gnode_t *
00230 glist_insert_float32(gnode_t * gn, float32 val)
00231 {
00232     gnode_t *newgn;
00233 
00234     newgn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t));
00235     newgn->data.fl = (double)val;
00236     newgn->next = gn->next;
00237     gn->next = newgn;
00238 
00239     return newgn;
00240 }
00241 
00242 
00243 gnode_t *
00244 glist_insert_float64(gnode_t * gn, float64 val)
00245 {
00246     gnode_t *newgn;
00247 
00248     newgn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t));
00249     newgn->data.fl = (double)val;
00250     newgn->next = gn->next;
00251     gn->next = newgn;
00252 
00253     return newgn;
00254 }
00255 
00256 gnode_t *
00257 gnode_free(gnode_t * gn, gnode_t * pred)
00258 {
00259     gnode_t *next;
00260 
00261     next = gn->next;
00262     if (pred) {
00263         assert(pred->next == gn);
00264 
00265         pred->next = next;
00266     }
00267 
00268     ckd_free((char *) gn);
00269 
00270     return next;
00271 }

Generated on Mon Aug 29 2011 for SphinxBase by  doxygen 1.7.1