| 1 |
/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
| 2 |
/* ==================================================================== |
| 3 |
* Copyright (c) 1999-2004 Carnegie Mellon University. All rights |
| 4 |
* reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 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 |
| 16 |
* distribution. |
| 17 |
* |
| 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. |
| 21 |
* |
| 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. |
| 33 |
* |
| 34 |
* ==================================================================== |
| 35 |
* |
| 36 |
*/ |
| 37 |
/* |
| 38 |
* glist.h -- Module for maintaining a generic, linear linked-list structure. |
| 39 |
* |
| 40 |
* ********************************************** |
| 41 |
* CMU ARPA Speech Project |
| 42 |
* |
| 43 |
* Copyright (c) 1999 Carnegie Mellon University. |
| 44 |
* ALL RIGHTS RESERVED. |
| 45 |
* ********************************************** |
| 46 |
* |
| 47 |
* HISTORY |
| 48 |
* $Log: glist.c,v $ |
| 49 |
* Revision 1.8 2005/06/22 03:02:51 arthchan2003 |
| 50 |
* 1, Fixed doxygen documentation, 2, add keyword. |
| 51 |
* |
| 52 |
* Revision 1.3 2005/03/30 01:22:48 archan |
| 53 |
* Fixed mistakes in last updates. Add |
| 54 |
* |
| 55 |
* |
| 56 |
* 09-Mar-1999 M K Ravishankar ([email protected]) at Carnegie Mellon University |
| 57 |
* Added glist_chkdup_*(). |
| 58 |
* |
| 59 |
* 13-Feb-1999 M K Ravishankar ([email protected]) at Carnegie Mellon University |
| 60 |
* Created from earlier version. |
| 61 |
*/ |
| 62 |
|
| 63 |
|
| 64 |
#include <stdio.h> |
| 65 |
#include <stdlib.h> |
| 66 |
#include <string.h> |
| 67 |
#include <assert.h> |
| 68 |
|
| 69 |
#include "sphinxbase/glist.h" |
| 70 |
#include "sphinxbase/ckd_alloc.h" |
| 71 |
|
| 72 |
|
| 73 |
glist_t |
| 74 |
glist_add_ptr(glist_t g, void *ptr) |
| 75 |
{ |
| 76 |
gnode_t *gn; |
| 77 |
|
| 78 |
gn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t)); |
| 79 |
gn->data.ptr = ptr; |
| 80 |
gn->next = g; |
| 81 |
return ((glist_t) gn); /* Return the new head of the list */ |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
glist_t |
| 86 |
glist_add_int32(glist_t g, int32 val) |
| 87 |
{ |
| 88 |
gnode_t *gn; |
| 89 |
|
| 90 |
gn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t)); |
| 91 |
gn->data.i = (long)val; |
| 92 |
gn->next = g; |
| 93 |
return ((glist_t) gn); /* Return the new head of the list */ |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
glist_t |
| 98 |
glist_add_uint32(glist_t g, uint32 val) |
| 99 |
{ |
| 100 |
gnode_t *gn; |
| 101 |
|
| 102 |
gn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t)); |
| 103 |
gn->data.ui = (unsigned long)val; |
| 104 |
gn->next = g; |
| 105 |
return ((glist_t) gn); /* Return the new head of the list */ |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
glist_t |
| 110 |
glist_add_float32(glist_t g, float32 val) |
| 111 |
{ |
| 112 |
gnode_t *gn; |
| 113 |
|
| 114 |
gn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t)); |
| 115 |
gn->data.fl = (double)val; |
| 116 |
gn->next = g; |
| 117 |
return ((glist_t) gn); /* Return the new head of the list */ |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
glist_t |
| 122 |
glist_add_float64(glist_t g, float64 val) |
| 123 |
{ |
| 124 |
gnode_t *gn; |
| 125 |
|
| 126 |
gn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t)); |
| 127 |
gn->data.fl = (double)val; |
| 128 |
gn->next = g; |
| 129 |
return ((glist_t) gn); /* Return the new head of the list */ |
| 130 |
} |
| 131 |
|
| 132 |
void |
| 133 |
glist_free(glist_t g) |
| 134 |
{ |
| 135 |
gnode_t *gn; |
| 136 |
|
| 137 |
while (g) { |
| 138 |
gn = g; |
| 139 |
g = gn->next; |
| 140 |
ckd_free((void *) gn); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
int32 |
| 145 |
glist_count(glist_t g) |
| 146 |
{ |
| 147 |
gnode_t *gn; |
| 148 |
int32 n; |
| 149 |
|
| 150 |
for (gn = g, n = 0; gn; gn = gn->next, n++); |
| 151 |
return n; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
gnode_t * |
| 156 |
glist_tail(glist_t g) |
| 157 |
{ |
| 158 |
gnode_t *gn; |
| 159 |
|
| 160 |
if (!g) |
| 161 |
return NULL; |
| 162 |
|
| 163 |
for (gn = g; gn->next; gn = gn->next); |
| 164 |
return gn; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
glist_t |
| 169 |
glist_reverse(glist_t g) |
| 170 |
{ |
| 171 |
gnode_t *gn, *nextgn; |
| 172 |
gnode_t *rev; |
| 173 |
|
| 174 |
rev = NULL; |
| 175 |
for (gn = g; gn; gn = nextgn) { |
| 176 |
nextgn = gn->next; |
| 177 |
|
| 178 |
gn->next = rev; |
| 179 |
rev = gn; |
| 180 |
} |
| 181 |
|
| 182 |
return rev; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
gnode_t * |
| 187 |
glist_insert_ptr(gnode_t * gn, void *ptr) |
| 188 |
{ |
| 189 |
gnode_t *newgn; |
| 190 |
|
| 191 |
newgn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t)); |
| 192 |
newgn->data.ptr = ptr; |
| 193 |
newgn->next = gn->next; |
| 194 |
gn->next = newgn; |
| 195 |
|
| 196 |
return newgn; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
gnode_t * |
| 201 |
glist_insert_int32(gnode_t * gn, int32 val) |
| 202 |
{ |
| 203 |
gnode_t *newgn; |
| 204 |
|
| 205 |
newgn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t)); |
| 206 |
newgn->data.i = val; |
| 207 |
newgn->next = gn->next; |
| 208 |
gn->next = newgn; |
| 209 |
|
| 210 |
return newgn; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
gnode_t * |
| 215 |
glist_insert_uint32(gnode_t * gn, uint32 val) |
| 216 |
{ |
| 217 |
gnode_t *newgn; |
| 218 |
|
| 219 |
newgn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t)); |
| 220 |
newgn->data.ui = val; |
| 221 |
newgn->next = gn->next; |
| 222 |
|
| 223 |
gn->next = newgn; |
| 224 |
|
| 225 |
return newgn; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
gnode_t * |
| 230 |
glist_insert_float32(gnode_t * gn, float32 val) |
| 231 |
{ |
| 232 |
gnode_t *newgn; |
| 233 |
|
| 234 |
newgn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t)); |
| 235 |
newgn->data.fl = (double)val; |
| 236 |
newgn->next = gn->next; |
| 237 |
gn->next = newgn; |
| 238 |
|
| 239 |
return newgn; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
gnode_t * |
| 244 |
glist_insert_float64(gnode_t * gn, float64 val) |
| 245 |
{ |
| 246 |
gnode_t *newgn; |
| 247 |
|
| 248 |
newgn = (gnode_t *) ckd_calloc(1, sizeof(gnode_t)); |
| 249 |
newgn->data.fl = (double)val; |
| 250 |
newgn->next = gn->next; |
| 251 |
gn->next = newgn; |
| 252 |
|
| 253 |
return newgn; |
| 254 |
} |
| 255 |
|
| 256 |
gnode_t * |
| 257 |
gnode_free(gnode_t * gn, gnode_t * pred) |
| 258 |
{ |
| 259 |
gnode_t *next; |
| 260 |
|
| 261 |
next = gn->next; |
| 262 |
if (pred) { |
| 263 |
assert(pred->next == gn); |
| 264 |
|
| 265 |
pred->next = next; |
| 266 |
} |
| 267 |
|
| 268 |
ckd_free((char *) gn); |
| 269 |
|
| 270 |
return next; |
| 271 |
} |