\doxysection{bitvec.\+c}
\label{bitvec_8c_source}\index{src/libsphinxbase/util/bitvec.c@{src/libsphinxbase/util/bitvec.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}{\ *\ bitvec.c\ -\/-\/\ Bit\ vector\ type.}}
\DoxyCodeLine{00039\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00040\ \textcolor{comment}{\ *\ **********************************************}}
\DoxyCodeLine{00041\ \textcolor{comment}{\ *\ CMU\ ARPA\ Speech\ Project}}
\DoxyCodeLine{00042\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00043\ \textcolor{comment}{\ *\ Copyright\ (c)\ 1999\ Carnegie\ Mellon\ University.}}
\DoxyCodeLine{00044\ \textcolor{comment}{\ *\ ALL\ RIGHTS\ RESERVED.}}
\DoxyCodeLine{00045\ \textcolor{comment}{\ *\ **********************************************}}
\DoxyCodeLine{00046\ \textcolor{comment}{\ *\ }}
\DoxyCodeLine{00047\ \textcolor{comment}{\ *\ HISTORY}}
\DoxyCodeLine{00048\ \textcolor{comment}{\ *\ \$Log:\ bitvec.c,v\ \$}}
\DoxyCodeLine{00049\ \textcolor{comment}{\ *\ Revision\ 1.4\ \ 2005/06/22\ 02:58:22\ \ arthchan2003}}
\DoxyCodeLine{00050\ \textcolor{comment}{\ *\ Added\ \ keyword}}
\DoxyCodeLine{00051\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00052\ \textcolor{comment}{\ *\ Revision\ 1.3\ \ 2005/03/30\ 01:22:48\ \ archan}}
\DoxyCodeLine{00053\ \textcolor{comment}{\ *\ Fixed\ mistakes\ in\ last\ updates.\ Add}}
\DoxyCodeLine{00054\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00055\ \textcolor{comment}{\ *\ }}
\DoxyCodeLine{00056\ \textcolor{comment}{\ *\ 05-\/Mar-\/1999\ \ M\ K\ Ravishankar\ (rkm@cs.cmu.edu)\ at\ Carnegie\ Mellon}}
\DoxyCodeLine{00057\ \textcolor{comment}{\ *\ \ \ \ \ \ \ \ \ \ \ \ \ \ Started.}}
\DoxyCodeLine{00058\ \textcolor{comment}{\ */}}
\DoxyCodeLine{00059\ }
\DoxyCodeLine{00060\ }
\DoxyCodeLine{00061\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/bitvec.h"{}}}
\DoxyCodeLine{00062\ }
\DoxyCodeLine{00063\ bitvec\_t\ *}
\DoxyCodeLine{00064\ bitvec\_realloc(bitvec\_t\ *vec,}
\DoxyCodeLine{00065\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{size\_t}\ old\_len,}
\DoxyCodeLine{00066\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{size\_t}\ new\_len)}
\DoxyCodeLine{00067\ \{}
\DoxyCodeLine{00068\ \ \ \ \ bitvec\_t\ *new\_vec;}
\DoxyCodeLine{00069\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ old\_size\ =\ bitvec\_size(old\_len);}
\DoxyCodeLine{00070\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ new\_size\ =\ bitvec\_size(new\_len);}
\DoxyCodeLine{00071\ \ \ \ \ }
\DoxyCodeLine{00072\ \ \ \ \ new\_vec\ =\ ckd\_realloc(vec,\ new\_size\ *\ \textcolor{keyword}{sizeof}(bitvec\_t));}
\DoxyCodeLine{00073\ \ \ \ \ \textcolor{keywordflow}{if}\ (new\_size\ >\ old\_size)}
\DoxyCodeLine{00074\ \ \ \ \ \ \ \ \ memset(new\_vec\ +\ old\_size,\ 0,\ (new\_size\ -\/\ old\_size)\ *\ \textcolor{keyword}{sizeof}(bitvec\_t));}
\DoxyCodeLine{00075\ }
\DoxyCodeLine{00076\ \ \ \ \ \textcolor{keywordflow}{return}\ new\_vec;}
\DoxyCodeLine{00077\ \}}
\DoxyCodeLine{00078\ }
\DoxyCodeLine{00079\ \textcolor{keywordtype}{size\_t}}
\DoxyCodeLine{00080\ bitvec\_count\_set(bitvec\_t\ *vec,\ \textcolor{keywordtype}{size\_t}\ len)}
\DoxyCodeLine{00081\ \{}
\DoxyCodeLine{00082\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ words,\ bits,\ w,\ b,\ n;}
\DoxyCodeLine{00083\ \ \ \ \ bitvec\_t\ *v;}
\DoxyCodeLine{00084\ }
\DoxyCodeLine{00085\ \ \ \ \ words\ =\ len\ /\ BITVEC\_BITS;}
\DoxyCodeLine{00086\ \ \ \ \ bits\ =\ len\ \%\ BITVEC\_BITS;}
\DoxyCodeLine{00087\ \ \ \ \ v\ =\ vec;}
\DoxyCodeLine{00088\ \ \ \ \ n\ =\ 0;}
\DoxyCodeLine{00089\ \ \ \ \ \textcolor{keywordflow}{for}\ (w\ =\ 0;\ w\ <\ words;\ ++w,\ ++v)\ \{}
\DoxyCodeLine{00090\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (*v\ ==\ 0)}
\DoxyCodeLine{00091\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{continue};}
\DoxyCodeLine{00092\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (b\ =\ 0;\ b\ <\ BITVEC\_BITS;\ ++b)}
\DoxyCodeLine{00093\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (*v\ \&\ (1<<b))}
\DoxyCodeLine{00094\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ++n;}
\DoxyCodeLine{00095\ \ \ \ \ \}}
\DoxyCodeLine{00096\ \ \ \ \ \textcolor{keywordflow}{for}\ (b\ =\ 0;\ b\ <\ bits;\ ++b)}
\DoxyCodeLine{00097\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (*v\ \&\ (1<<b))}
\DoxyCodeLine{00098\ \ \ \ \ \ \ \ \ \ \ \ \ ++n;}
\DoxyCodeLine{00099\ }
\DoxyCodeLine{00100\ \ \ \ \ \textcolor{keywordflow}{return}\ n;}
\DoxyCodeLine{00101\ \}}

\end{DoxyCode}
