\doxysection{heap.\+c}
\label{heap_8c_source}\index{src/libsphinxbase/util/heap.c@{src/libsphinxbase/util/heap.c}}

\begin{DoxyCode}{0}
\DoxyCodeLine{00001\ \textcolor{comment}{/*\ -\/*-\/\ c-\/basic-\/offset:\ 4;\ indent-\/tabs-\/mode:\ nil\ -\/*-\/\ */}}
\DoxyCodeLine{00002\ \textcolor{comment}{/*\ ====================================================================}}
\DoxyCodeLine{00003\ \textcolor{comment}{\ *\ Copyright\ (c)\ 1999-\/2004\ Carnegie\ Mellon\ University.\ \ All\ rights}}
\DoxyCodeLine{00004\ \textcolor{comment}{\ *\ reserved.}}
\DoxyCodeLine{00005\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00006\ \textcolor{comment}{\ *\ Redistribution\ and\ use\ in\ source\ and\ binary\ forms,\ with\ or\ without}}
\DoxyCodeLine{00007\ \textcolor{comment}{\ *\ modification,\ are\ permitted\ provided\ that\ the\ following\ conditions}}
\DoxyCodeLine{00008\ \textcolor{comment}{\ *\ are\ met:}}
\DoxyCodeLine{00009\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00010\ \textcolor{comment}{\ *\ 1.\ Redistributions\ of\ source\ code\ must\ retain\ the\ above\ copyright}}
\DoxyCodeLine{00011\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer.\ }}
\DoxyCodeLine{00012\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00013\ \textcolor{comment}{\ *\ 2.\ Redistributions\ in\ binary\ form\ must\ reproduce\ the\ above\ copyright}}
\DoxyCodeLine{00014\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer\ in}}
\DoxyCodeLine{00015\ \textcolor{comment}{\ *\ \ \ \ the\ documentation\ and/or\ other\ materials\ provided\ with\ the}}
\DoxyCodeLine{00016\ \textcolor{comment}{\ *\ \ \ \ distribution.}}
\DoxyCodeLine{00017\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00018\ \textcolor{comment}{\ *\ This\ work\ was\ supported\ in\ part\ by\ funding\ from\ the\ Defense\ Advanced\ }}
\DoxyCodeLine{00019\ \textcolor{comment}{\ *\ Research\ Projects\ Agency\ and\ the\ National\ Science\ Foundation\ of\ the\ }}
\DoxyCodeLine{00020\ \textcolor{comment}{\ *\ United\ States\ of\ America,\ and\ the\ CMU\ Sphinx\ Speech\ Consortium.}}
\DoxyCodeLine{00021\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00022\ \textcolor{comment}{\ *\ THIS\ SOFTWARE\ IS\ PROVIDED\ BY\ CARNEGIE\ MELLON\ UNIVERSITY\ \`{}\`{}AS\ IS''\ AND\ }}
\DoxyCodeLine{00023\ \textcolor{comment}{\ *\ ANY\ EXPRESSED\ OR\ IMPLIED\ WARRANTIES,\ INCLUDING,\ BUT\ NOT\ LIMITED\ TO,\ }}
\DoxyCodeLine{00024\ \textcolor{comment}{\ *\ THE\ IMPLIED\ WARRANTIES\ OF\ MERCHANTABILITY\ AND\ FITNESS\ FOR\ A\ PARTICULAR}}
\DoxyCodeLine{00025\ \textcolor{comment}{\ *\ PURPOSE\ ARE\ DISCLAIMED.\ \ IN\ NO\ EVENT\ SHALL\ CARNEGIE\ MELLON\ UNIVERSITY}}
\DoxyCodeLine{00026\ \textcolor{comment}{\ *\ NOR\ ITS\ EMPLOYEES\ BE\ LIABLE\ FOR\ ANY\ DIRECT,\ INDIRECT,\ INCIDENTAL,}}
\DoxyCodeLine{00027\ \textcolor{comment}{\ *\ SPECIAL,\ EXEMPLARY,\ OR\ CONSEQUENTIAL\ DAMAGES\ (INCLUDING,\ BUT\ NOT\ }}
\DoxyCodeLine{00028\ \textcolor{comment}{\ *\ LIMITED\ TO,\ PROCUREMENT\ OF\ SUBSTITUTE\ GOODS\ OR\ SERVICES;\ LOSS\ OF\ USE,\ }}
\DoxyCodeLine{00029\ \textcolor{comment}{\ *\ DATA,\ OR\ PROFITS;\ OR\ BUSINESS\ INTERRUPTION)\ HOWEVER\ CAUSED\ AND\ ON\ ANY\ }}
\DoxyCodeLine{00030\ \textcolor{comment}{\ *\ THEORY\ OF\ LIABILITY,\ WHETHER\ IN\ CONTRACT,\ STRICT\ LIABILITY,\ OR\ TORT\ }}
\DoxyCodeLine{00031\ \textcolor{comment}{\ *\ (INCLUDING\ NEGLIGENCE\ OR\ OTHERWISE)\ ARISING\ IN\ ANY\ WAY\ OUT\ OF\ THE\ USE\ }}
\DoxyCodeLine{00032\ \textcolor{comment}{\ *\ OF\ THIS\ SOFTWARE,\ EVEN\ IF\ ADVISED\ OF\ THE\ POSSIBILITY\ OF\ SUCH\ DAMAGE.}}
\DoxyCodeLine{00033\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00034\ \textcolor{comment}{\ *\ ====================================================================}}
\DoxyCodeLine{00035\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00036\ \textcolor{comment}{\ */}}
\DoxyCodeLine{00037\ \textcolor{comment}{/*}}
\DoxyCodeLine{00038\ \textcolor{comment}{\ *\ heap.c\ -\/-\/\ Generic\ heap\ structure\ for\ inserting\ in\ any\ and\ popping\ in\ sorted}}
\DoxyCodeLine{00039\ \textcolor{comment}{\ *\ \ \ \ \ \ \ \ \ \ \ \ \ \ order.}}
\DoxyCodeLine{00040\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00041\ \textcolor{comment}{\ *\ **********************************************}}
\DoxyCodeLine{00042\ \textcolor{comment}{\ *\ CMU\ ARPA\ Speech\ Project}}
\DoxyCodeLine{00043\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00044\ \textcolor{comment}{\ *\ Copyright\ (c)\ 1999\ Carnegie\ Mellon\ University.}}
\DoxyCodeLine{00045\ \textcolor{comment}{\ *\ ALL\ RIGHTS\ RESERVED.}}
\DoxyCodeLine{00046\ \textcolor{comment}{\ *\ **********************************************}}
\DoxyCodeLine{00047\ \textcolor{comment}{\ *\ }}
\DoxyCodeLine{00048\ \textcolor{comment}{\ *\ HISTORY}}
\DoxyCodeLine{00049\ \textcolor{comment}{\ *\ \$Log:\ heap.c,v\ \$}}
\DoxyCodeLine{00050\ \textcolor{comment}{\ *\ Revision\ 1.4\ \ 2005/06/22\ 03:05:49\ \ arthchan2003}}
\DoxyCodeLine{00051\ \textcolor{comment}{\ *\ 1,\ Fixed\ doxygen\ documentation,\ 2,\ Add\ \ keyword.}}
\DoxyCodeLine{00052\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00053\ \textcolor{comment}{\ *\ Revision\ 1.3\ \ 2005/03/30\ 01:22:48\ \ archan}}
\DoxyCodeLine{00054\ \textcolor{comment}{\ *\ Fixed\ mistakes\ in\ last\ updates.\ Add}}
\DoxyCodeLine{00055\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00056\ \textcolor{comment}{\ *\ }}
\DoxyCodeLine{00057\ \textcolor{comment}{\ *\ 05-\/Mar-\/99\ \ \ \ M\ K\ Ravishankar\ (rkm@cs.cmu.edu)\ at\ Carnegie\ Mellon\ University}}
\DoxyCodeLine{00058\ \textcolor{comment}{\ *\ \ \ \ \ \ \ \ \ \ \ \ \ \ Fixed\ bug\ in\ heap\_destroy()\ (in\ while\ loop\ exit\ condition).}}
\DoxyCodeLine{00059\ \textcolor{comment}{\ *\ }}
\DoxyCodeLine{00060\ \textcolor{comment}{\ *\ 23-\/Dec-\/96\ \ \ \ M\ K\ Ravishankar\ (rkm@cs.cmu.edu)\ at\ Carnegie\ Mellon\ University}}
\DoxyCodeLine{00061\ \textcolor{comment}{\ *\ \ \ \ \ \ \ \ \ \ \ \ \ \ Started.}}
\DoxyCodeLine{00062\ \textcolor{comment}{\ */}}
\DoxyCodeLine{00063\ }
\DoxyCodeLine{00064\ }
\DoxyCodeLine{00065\ \textcolor{preprocessor}{\#include\ <stdio.h>}}
\DoxyCodeLine{00066\ \textcolor{preprocessor}{\#include\ <stdlib.h>}}
\DoxyCodeLine{00067\ \textcolor{preprocessor}{\#include\ <string.h>}}
\DoxyCodeLine{00068\ \textcolor{preprocessor}{\#include\ <assert.h>}}
\DoxyCodeLine{00069\ }
\DoxyCodeLine{00070\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/heap.h"{}}}
\DoxyCodeLine{00071\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/err.h"{}}}
\DoxyCodeLine{00072\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/ckd\_alloc.h"{}}}
\DoxyCodeLine{00073\ }
\DoxyCodeLine{00077\ \textcolor{keyword}{typedef}\ \textcolor{keyword}{struct\ }heapnode\_s\ \{}
\DoxyCodeLine{00078\ \ \ \ \ \textcolor{keywordtype}{void}\ *data;\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ }
\DoxyCodeLine{00079\ \ \ \ \ int32\ val;\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ }
\DoxyCodeLine{00081\ \ \ \ \ int32\ nl,\ nr;\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ }
\DoxyCodeLine{00082\ \ \ \ \ \textcolor{keyword}{struct\ }heapnode\_s\ *l;\ \ \ \ \ \ \ }
\DoxyCodeLine{00083\ \ \ \ \ \textcolor{keyword}{struct\ }heapnode\_s\ *r;\ \ \ \ \ \ \ }
\DoxyCodeLine{00084\ \}\ heapnode\_t;}
\DoxyCodeLine{00085\ }
\DoxyCodeLine{00089\ \textcolor{keyword}{struct\ }heap\_s\ \{}
\DoxyCodeLine{00090\ \ \ \ \ heapnode\_t\ *top;}
\DoxyCodeLine{00091\ \};}
\DoxyCodeLine{00092\ }
\DoxyCodeLine{00093\ }
\DoxyCodeLine{00094\ \textcolor{preprocessor}{\#if\ 0}}
\DoxyCodeLine{00095\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00096\ heap\_dump(heapnode\_t\ *\ top,\ int32\ level)}
\DoxyCodeLine{00097\ \{}
\DoxyCodeLine{00098\ \ \ \ \ int32\ i;}
\DoxyCodeLine{00099\ }
\DoxyCodeLine{00100\ \ \ \ \ \textcolor{keywordflow}{if}\ (!top)}
\DoxyCodeLine{00101\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return};}
\DoxyCodeLine{00102\ }
\DoxyCodeLine{00103\ \ \ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ level;\ i++)}
\DoxyCodeLine{00104\ \ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}\ \ "{}});}
\DoxyCodeLine{00105\ \ \ \ \ \textcolor{comment}{/*\ print\ top\ info\ */}}
\DoxyCodeLine{00106\ \ \ \ \ heap\_dump(top-\/>l,\ level\ +\ 1);}
\DoxyCodeLine{00107\ \ \ \ \ heap\_dump(top-\/>r,\ level\ +\ 1);}
\DoxyCodeLine{00108\ \}}
\DoxyCodeLine{00109\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00110\ }
\DoxyCodeLine{00111\ }
\DoxyCodeLine{00112\ heap\_t\ *}
\DoxyCodeLine{00113\ heap\_new(\textcolor{keywordtype}{void})}
\DoxyCodeLine{00114\ \{}
\DoxyCodeLine{00115\ \ \ \ \ heap\_t\ *h\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*h));}
\DoxyCodeLine{00116\ \ \ \ \ \textcolor{keywordflow}{return}\ h;}
\DoxyCodeLine{00117\ \}}
\DoxyCodeLine{00118\ }
\DoxyCodeLine{00119\ }
\DoxyCodeLine{00120\ \textcolor{keyword}{static}\ heapnode\_t\ *}
\DoxyCodeLine{00121\ subheap\_insert(heapnode\_t\ *\ root,\ \textcolor{keywordtype}{void}\ *data,\ int32\ val)}
\DoxyCodeLine{00122\ \{}
\DoxyCodeLine{00123\ \ \ \ \ heapnode\_t\ *h;}
\DoxyCodeLine{00124\ \ \ \ \ \textcolor{keywordtype}{void}\ *tmpdata;}
\DoxyCodeLine{00125\ \ \ \ \ int32\ tmpval;}
\DoxyCodeLine{00126\ }
\DoxyCodeLine{00127\ \ \ \ \ \textcolor{keywordflow}{if}\ (!root)\ \{}
\DoxyCodeLine{00128\ \ \ \ \ \ \ \ \ h\ =\ (heapnode\_t\ *)\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(heapnode\_t));}
\DoxyCodeLine{00129\ \ \ \ \ \ \ \ \ h-\/>data\ =\ data;}
\DoxyCodeLine{00130\ \ \ \ \ \ \ \ \ h-\/>val\ =\ val;}
\DoxyCodeLine{00131\ \ \ \ \ \ \ \ \ h-\/>l\ =\ h-\/>r\ =\ NULL;}
\DoxyCodeLine{00132\ \ \ \ \ \ \ \ \ h-\/>nl\ =\ h-\/>nr\ =\ 0;}
\DoxyCodeLine{00133\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ h;}
\DoxyCodeLine{00134\ \ \ \ \ \}}
\DoxyCodeLine{00135\ }
\DoxyCodeLine{00136\ \ \ \ \ \textcolor{comment}{/*\ Root\ already\ exists;\ if\ new\ value\ is\ less,\ replace\ root\ node\ */}}
\DoxyCodeLine{00137\ \ \ \ \ \textcolor{keywordflow}{if}\ (root-\/>val\ >\ val)\ \{}
\DoxyCodeLine{00138\ \ \ \ \ \ \ \ \ tmpdata\ =\ root-\/>data;}
\DoxyCodeLine{00139\ \ \ \ \ \ \ \ \ tmpval\ =\ root-\/>val;}
\DoxyCodeLine{00140\ \ \ \ \ \ \ \ \ root-\/>data\ =\ data;}
\DoxyCodeLine{00141\ \ \ \ \ \ \ \ \ root-\/>val\ =\ val;}
\DoxyCodeLine{00142\ \ \ \ \ \ \ \ \ data\ =\ tmpdata;}
\DoxyCodeLine{00143\ \ \ \ \ \ \ \ \ val\ =\ tmpval;}
\DoxyCodeLine{00144\ \ \ \ \ \}}
\DoxyCodeLine{00145\ }
\DoxyCodeLine{00146\ \ \ \ \ \textcolor{comment}{/*\ Insert\ new\ or\ old\ (replaced)\ node\ in\ right\ or\ left\ subtree;\ keep\ them\ balanced\ */}}
\DoxyCodeLine{00147\ \ \ \ \ \textcolor{keywordflow}{if}\ (root-\/>nl\ >\ root-\/>nr)\ \{}
\DoxyCodeLine{00148\ \ \ \ \ \ \ \ \ root-\/>r\ =\ subheap\_insert(root-\/>r,\ data,\ val);}
\DoxyCodeLine{00149\ \ \ \ \ \ \ \ \ root-\/>nr++;}
\DoxyCodeLine{00150\ \ \ \ \ \}}
\DoxyCodeLine{00151\ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00152\ \ \ \ \ \ \ \ \ root-\/>l\ =\ subheap\_insert(root-\/>l,\ data,\ val);}
\DoxyCodeLine{00153\ \ \ \ \ \ \ \ \ root-\/>nl++;}
\DoxyCodeLine{00154\ \ \ \ \ \}}
\DoxyCodeLine{00155\ }
\DoxyCodeLine{00156\ \ \ \ \ \textcolor{keywordflow}{return}\ root;}
\DoxyCodeLine{00157\ \}}
\DoxyCodeLine{00158\ }
\DoxyCodeLine{00159\ }
\DoxyCodeLine{00160\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00161\ heap\_insert(heap\_t\ *heap,\ \textcolor{keywordtype}{void}\ *data,\ int32\ val)}
\DoxyCodeLine{00162\ \{}
\DoxyCodeLine{00163\ \ \ \ \ heap-\/>top\ =\ subheap\_insert(heap-\/>top,\ data,\ val);}
\DoxyCodeLine{00164\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00165\ \}}
\DoxyCodeLine{00166\ }
\DoxyCodeLine{00167\ }
\DoxyCodeLine{00168\ \textcolor{keyword}{static}\ heapnode\_t\ *}
\DoxyCodeLine{00169\ subheap\_pop(heapnode\_t\ *\ root)}
\DoxyCodeLine{00170\ \{}
\DoxyCodeLine{00171\ \ \ \ \ heapnode\_t\ *l,\ *r;}
\DoxyCodeLine{00172\ }
\DoxyCodeLine{00173\ \ \ \ \ \textcolor{comment}{/*\ Propagate\ best\ value\ from\ below\ into\ root,\ if\ any\ */}}
\DoxyCodeLine{00174\ \ \ \ \ l\ =\ root-\/>l;}
\DoxyCodeLine{00175\ \ \ \ \ r\ =\ root-\/>r;}
\DoxyCodeLine{00176\ }
\DoxyCodeLine{00177\ \ \ \ \ \textcolor{keywordflow}{if}\ (!l)\ \{}
\DoxyCodeLine{00178\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (!r)\ \{}
\DoxyCodeLine{00179\ \ \ \ \ \ \ \ \ \ \ \ \ ckd\_free((\textcolor{keywordtype}{char}\ *)\ root);}
\DoxyCodeLine{00180\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00181\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00182\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00183\ \ \ \ \ \ \ \ \ \ \ \ \ root-\/>data\ =\ r-\/>data;}
\DoxyCodeLine{00184\ \ \ \ \ \ \ \ \ \ \ \ \ root-\/>val\ =\ r-\/>val;}
\DoxyCodeLine{00185\ \ \ \ \ \ \ \ \ \ \ \ \ root-\/>r\ =\ subheap\_pop(r);}
\DoxyCodeLine{00186\ \ \ \ \ \ \ \ \ \ \ \ \ root-\/>nr-\/-\/;}
\DoxyCodeLine{00187\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00188\ \ \ \ \ \}}
\DoxyCodeLine{00189\ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00190\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((!r)\ ||\ (l-\/>val\ <\ r-\/>val))\ \{}
\DoxyCodeLine{00191\ \ \ \ \ \ \ \ \ \ \ \ \ root-\/>data\ =\ l-\/>data;}
\DoxyCodeLine{00192\ \ \ \ \ \ \ \ \ \ \ \ \ root-\/>val\ =\ l-\/>val;}
\DoxyCodeLine{00193\ \ \ \ \ \ \ \ \ \ \ \ \ root-\/>l\ =\ subheap\_pop(l);}
\DoxyCodeLine{00194\ \ \ \ \ \ \ \ \ \ \ \ \ root-\/>nl-\/-\/;}
\DoxyCodeLine{00195\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00196\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00197\ \ \ \ \ \ \ \ \ \ \ \ \ root-\/>data\ =\ r-\/>data;}
\DoxyCodeLine{00198\ \ \ \ \ \ \ \ \ \ \ \ \ root-\/>val\ =\ r-\/>val;}
\DoxyCodeLine{00199\ \ \ \ \ \ \ \ \ \ \ \ \ root-\/>r\ =\ subheap\_pop(r);}
\DoxyCodeLine{00200\ \ \ \ \ \ \ \ \ \ \ \ \ root-\/>nr-\/-\/;}
\DoxyCodeLine{00201\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00202\ \ \ \ \ \}}
\DoxyCodeLine{00203\ }
\DoxyCodeLine{00204\ \ \ \ \ \textcolor{keywordflow}{return}\ root;}
\DoxyCodeLine{00205\ \}}
\DoxyCodeLine{00206\ }
\DoxyCodeLine{00207\ }
\DoxyCodeLine{00208\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00209\ heap\_pop(heap\_t\ *heap,\ \textcolor{keywordtype}{void}\ **data,\ int32\ *\ val)}
\DoxyCodeLine{00210\ \{}
\DoxyCodeLine{00211\ \ \ \ \ \textcolor{keywordflow}{if}\ (heap-\/>top\ ==\ NULL)}
\DoxyCodeLine{00212\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00213\ \ \ \ \ *data\ =\ heap-\/>top-\/>data;}
\DoxyCodeLine{00214\ \ \ \ \ *val\ =\ heap-\/>top-\/>val;}
\DoxyCodeLine{00215\ \ \ \ \ heap-\/>top\ =\ subheap\_pop(heap-\/>top);}
\DoxyCodeLine{00216\ \ \ \ \ \textcolor{keywordflow}{return}\ 1;}
\DoxyCodeLine{00217\ \}}
\DoxyCodeLine{00218\ }
\DoxyCodeLine{00219\ }
\DoxyCodeLine{00220\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00221\ heap\_top(heap\_t\ *heap,\ \textcolor{keywordtype}{void}\ **data,\ int32\ *\ val)}
\DoxyCodeLine{00222\ \{}
\DoxyCodeLine{00223\ \ \ \ \ \textcolor{keywordflow}{if}\ (heap-\/>top\ ==\ NULL)}
\DoxyCodeLine{00224\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00225\ \ \ \ \ *data\ =\ heap-\/>top-\/>data;}
\DoxyCodeLine{00226\ \ \ \ \ *val\ =\ heap-\/>top-\/>val;}
\DoxyCodeLine{00227\ \ \ \ \ \textcolor{keywordflow}{return}\ 1;}
\DoxyCodeLine{00228\ \}}
\DoxyCodeLine{00229\ }
\DoxyCodeLine{00230\ \textcolor{keyword}{static}\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00231\ heap\_remove\_one(heap\_t\ *heap,\ heapnode\_t\ *top,\ \textcolor{keywordtype}{void}\ *data)}
\DoxyCodeLine{00232\ \{}
\DoxyCodeLine{00233\ \ \ \ \ \textcolor{keywordflow}{if}\ (top\ ==\ NULL)}
\DoxyCodeLine{00234\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00235\ \ \ \ \ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}\ (top-\/>data\ ==\ data)\ \{}
\DoxyCodeLine{00236\ \ \ \ \ \ \ \ \ assert(top\ ==\ heap-\/>top);}
\DoxyCodeLine{00237\ \ \ \ \ \ \ \ \ heap-\/>top\ =\ subheap\_pop(heap-\/>top);}
\DoxyCodeLine{00238\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00239\ \ \ \ \ \}}
\DoxyCodeLine{00240\ \ \ \ \ \textcolor{keywordflow}{if}\ (top-\/>l)\ \{}
\DoxyCodeLine{00241\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (top-\/>l-\/>data\ ==\ data)\ \{}
\DoxyCodeLine{00242\ \ \ \ \ \ \ \ \ \ \ \ \ top-\/>l\ =\ subheap\_pop(top-\/>l);}
\DoxyCodeLine{00243\ \ \ \ \ \ \ \ \ \ \ \ \ -\/-\/top-\/>nl;}
\DoxyCodeLine{00244\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00245\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00246\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}\ (heap\_remove\_one(heap,\ top-\/>l,\ data)\ ==\ 0)\ \{}
\DoxyCodeLine{00247\ \ \ \ \ \ \ \ \ \ \ \ \ -\/-\/top-\/>nl;}
\DoxyCodeLine{00248\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00249\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00250\ \ \ \ \ \}}
\DoxyCodeLine{00251\ \ \ \ \ \textcolor{keywordflow}{if}\ (top-\/>r)\ \{}
\DoxyCodeLine{00252\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (top-\/>r-\/>data\ ==\ data)\ \{}
\DoxyCodeLine{00253\ \ \ \ \ \ \ \ \ \ \ \ \ top-\/>r\ =\ subheap\_pop(top-\/>r);}
\DoxyCodeLine{00254\ \ \ \ \ \ \ \ \ \ \ \ \ -\/-\/top-\/>nr;}
\DoxyCodeLine{00255\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00256\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00257\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}\ (heap\_remove\_one(heap,\ top-\/>r,\ data)\ ==\ 0)\ \{}
\DoxyCodeLine{00258\ \ \ \ \ \ \ \ \ \ \ \ \ -\/-\/top-\/>nr;}
\DoxyCodeLine{00259\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00260\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00261\ \ \ \ \ \}}
\DoxyCodeLine{00262\ \ \ \ \ \textcolor{keywordflow}{return}\ -\/1;}
\DoxyCodeLine{00263\ \}}
\DoxyCodeLine{00264\ }
\DoxyCodeLine{00265\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00266\ heap\_remove(heap\_t\ *heap,\ \textcolor{keywordtype}{void}\ *data)}
\DoxyCodeLine{00267\ \{}
\DoxyCodeLine{00268\ \ \ \ \ \textcolor{keywordflow}{return}\ heap\_remove\_one(heap,\ heap-\/>top,\ data);}
\DoxyCodeLine{00269\ \}}
\DoxyCodeLine{00270\ }
\DoxyCodeLine{00271\ }
\DoxyCodeLine{00272\ \textcolor{keywordtype}{size\_t}}
\DoxyCodeLine{00273\ heap\_size(heap\_t\ *heap)}
\DoxyCodeLine{00274\ \{}
\DoxyCodeLine{00275\ \ \ \ \ \textcolor{keywordflow}{if}\ (heap-\/>top\ ==\ NULL)}
\DoxyCodeLine{00276\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00277\ \ \ \ \ \textcolor{keywordflow}{return}\ heap-\/>top-\/>nl\ +\ heap-\/>top-\/>nr\ +\ 1;}
\DoxyCodeLine{00278\ \}}
\DoxyCodeLine{00279\ }
\DoxyCodeLine{00280\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{00281\ heap\_destroy(heap\_t\ *heap)}
\DoxyCodeLine{00282\ \{}
\DoxyCodeLine{00283\ \ \ \ \ \textcolor{keywordtype}{void}\ *data;}
\DoxyCodeLine{00284\ \ \ \ \ int32\ val;}
\DoxyCodeLine{00285\ }
\DoxyCodeLine{00286\ \ \ \ \ \textcolor{comment}{/*\ Empty\ the\ heap\ and\ free\ it\ */}}
\DoxyCodeLine{00287\ \ \ \ \ \textcolor{keywordflow}{while}\ (heap\_pop(heap,\ \&data,\ \&val)\ >\ 0)}
\DoxyCodeLine{00288\ \ \ \ \ \ \ \ \ ;}
\DoxyCodeLine{00289\ \ \ \ \ ckd\_free(heap);}
\DoxyCodeLine{00290\ }
\DoxyCodeLine{00291\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00292\ \}}

\end{DoxyCode}
