Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libvcodex/Vchuff/vchbits.c
1810 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 2003-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Phong Vo <[email protected]> *
18
* *
19
***********************************************************************/
20
#include "vchhdr.h"
21
22
/* Construct the code bits given their lengths.
23
**
24
** Written by Kiem-Phong Vo
25
*/
26
27
/* sort by code lengths */
28
#if __STD_C
29
static int sizecmp(Void_t* one, Void_t* two, Void_t* disc)
30
#else
31
static int sizecmp(one,two,disc)
32
Void_t* one;
33
Void_t* two;
34
Void_t* disc;
35
#endif
36
{
37
int d;
38
ssize_t *o = *((ssize_t**)one), *t = *((ssize_t**)two);
39
40
return (d = *o - *t) != 0 ? d : o < t ? -1 : o > t ? 1 : 0;
41
}
42
43
#if __STD_C
44
ssize_t vchbits(ssize_t nsym, ssize_t* size, Vcbit_t* bits)
45
#else
46
ssize_t vchbits(nsym, size, bits)
47
ssize_t nsym; /* alphabet size or #symbols */
48
ssize_t* size; /* encoding lengths of bytes */
49
Vcbit_t* bits; /* encoding bits to be computed */
50
#endif
51
{
52
int i, notz, k, s;
53
ssize_t **sort;
54
Vcbit_t b = 0;
55
56
if(!(sort = (ssize_t**)malloc(nsym*sizeof(ssize_t*))) )
57
return -1;
58
59
for(notz = 0, i = 0; i < nsym; ++i)
60
{ if(size[i] == 0)
61
continue;
62
sort[notz++] = size+i;
63
}
64
vcqsort(sort, notz, sizeof(ssize_t*), sizecmp, 0);
65
66
for(i = 0; i < notz; i = k)
67
{ s = *sort[i];
68
for(k = i; k < notz && *sort[k] == s; ++k)
69
bits[sort[k]-size] = (b++) << (VC_BITSIZE - s);
70
if(k < notz)
71
b <<= (*sort[k] - s);
72
}
73
74
/* return the maximum size of any code */
75
s = notz <= 0 ? 0 : *sort[notz-1];
76
77
free(sort);
78
return s;
79
}
80
81