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 * heap.c -- Generic heap structure for inserting in any and popping in sorted
41 * **********************************************
42 * CMU ARPA Speech Project
44 * Copyright (c) 1999 Carnegie Mellon University.
45 * ALL RIGHTS RESERVED.
46 * **********************************************
50 * Revision 1.4 2005/06/22 03:05:49 arthchan2003
51 * 1, Fixed doxygen documentation, 2, Add keyword.
53 * Revision 1.3 2005/03/30 01:22:48 archan
54 * Fixed mistakes in last updates. Add
57 * 05-Mar-99 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
58 * Fixed bug in heap_destroy() (in while loop exit condition).
60 * 23-Dec-96 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
70 #include "sphinxbase/heap.h"
71 #include "sphinxbase/err.h"
72 #include "sphinxbase/ckd_alloc.h"
75 * One node on the heap
77 typedef struct heapnode_s {
78 void *data; /**< Application data at this node */
79 int32 val; /**< Associated with above application data; according to which
80 heap is sorted (in ascending order) */
81 int32 nl, nr; /**< #left/right descendants of this node (for balancing heap) */
82 struct heapnode_s *l; /**< Root of left descendant heap */
83 struct heapnode_s *r; /**< Root of right descendant heap */
87 * Internal heap data structure.
96 heap_dump(heapnode_t * top, int32 level)
103 for (i = 0; i < level; i++)
106 heap_dump(top->l, level + 1);
107 heap_dump(top->r, level + 1);
115 heap_t *h = ckd_calloc(1, sizeof(*h));
121 subheap_insert(heapnode_t * root, void *data, int32 val)
128 h = (heapnode_t *) ckd_calloc(1, sizeof(heapnode_t));
136 /* Root already exists; if new value is less, replace root node */
137 if (root->val > val) {
138 tmpdata = root->data;
146 /* Insert new or old (replaced) node in right or left subtree; keep them balanced */
147 if (root->nl > root->nr) {
148 root->r = subheap_insert(root->r, data, val);
152 root->l = subheap_insert(root->l, data, val);
161 heap_insert(heap_t *heap, void *data, int32 val)
163 heap->top = subheap_insert(heap->top, data, val);
169 subheap_pop(heapnode_t * root)
173 /* Propagate best value from below into root, if any */
179 ckd_free((char *) root);
183 root->data = r->data;
185 root->r = subheap_pop(r);
190 if ((!r) || (l->val < r->val)) {
191 root->data = l->data;
193 root->l = subheap_pop(l);
197 root->data = r->data;
199 root->r = subheap_pop(r);
209 heap_pop(heap_t *heap, void **data, int32 * val)
211 if (heap->top == NULL)
213 *data = heap->top->data;
214 *val = heap->top->val;
215 heap->top = subheap_pop(heap->top);
221 heap_top(heap_t *heap, void **data, int32 * val)
223 if (heap->top == NULL)
225 *data = heap->top->data;
226 *val = heap->top->val;
231 heap_remove_one(heap_t *heap, heapnode_t *top, void *data)
235 else if (top->data == data) {
236 assert(top == heap->top);
237 heap->top = subheap_pop(heap->top);
241 if (top->l->data == data) {
242 top->l = subheap_pop(top->l);
246 else if (heap_remove_one(heap, top->l, data) == 0) {
252 if (top->r->data == data) {
253 top->r = subheap_pop(top->r);
257 else if (heap_remove_one(heap, top->r, data) == 0) {
266 heap_remove(heap_t *heap, void *data)
268 return heap_remove_one(heap, heap->top, data);
273 heap_size(heap_t *heap)
275 if (heap->top == NULL)
277 return heap->top->nl + heap->top->nr + 1;
281 heap_destroy(heap_t *heap)
286 /* Empty the heap and free it */
287 while (heap_pop(heap, &data, &val) > 0)