/***********************************************************************1* *2* This software is part of the ast package *3* Copyright (c) 2003-2011 AT&T Intellectual Property *4* and is licensed under the *5* Eclipse Public License, Version 1.0 *6* by AT&T Intellectual Property *7* *8* A copy of the License is available at *9* http://www.eclipse.org/org/documents/epl-v10.html *10* (with md5 checksum b35adb5213ca9657e911e9befb180842) *11* *12* Information and Software Systems Research *13* AT&T Research *14* Florham Park NJ *15* *16* Phong Vo <[email protected]> *17* *18***********************************************************************/19#include "vchhdr.h"2021/* Construct the code bits given their lengths.22**23** Written by Kiem-Phong Vo24*/2526/* sort by code lengths */27#if __STD_C28static int sizecmp(Void_t* one, Void_t* two, Void_t* disc)29#else30static int sizecmp(one,two,disc)31Void_t* one;32Void_t* two;33Void_t* disc;34#endif35{36int d;37ssize_t *o = *((ssize_t**)one), *t = *((ssize_t**)two);3839return (d = *o - *t) != 0 ? d : o < t ? -1 : o > t ? 1 : 0;40}4142#if __STD_C43ssize_t vchbits(ssize_t nsym, ssize_t* size, Vcbit_t* bits)44#else45ssize_t vchbits(nsym, size, bits)46ssize_t nsym; /* alphabet size or #symbols */47ssize_t* size; /* encoding lengths of bytes */48Vcbit_t* bits; /* encoding bits to be computed */49#endif50{51int i, notz, k, s;52ssize_t **sort;53Vcbit_t b = 0;5455if(!(sort = (ssize_t**)malloc(nsym*sizeof(ssize_t*))) )56return -1;5758for(notz = 0, i = 0; i < nsym; ++i)59{ if(size[i] == 0)60continue;61sort[notz++] = size+i;62}63vcqsort(sort, notz, sizeof(ssize_t*), sizecmp, 0);6465for(i = 0; i < notz; i = k)66{ s = *sort[i];67for(k = i; k < notz && *sort[k] == s; ++k)68bits[sort[k]-size] = (b++) << (VC_BITSIZE - s);69if(k < notz)70b <<= (*sort[k] - s);71}7273/* return the maximum size of any code */74s = notz <= 0 ? 0 : *sort[notz-1];7576free(sort);77return s;78}798081