cwrs.c 6.25 KB
Newer Older
1
/* (C) 2007 Timothy B. Terriberry */
2
3
4
5
/*
   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:
6

7
8
   - Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
9

10
11
12
   - Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
13

14
15
16
   - Neither the name of the Xiph.org Foundation nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.
17

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include "cwrs.h"

/*Returns the numer of ways of choosing _m elements from a set of size _n with
   replacement when a sign bit is needed for each unique element.*/
#if 0
36
37
static celt_uint32_t ncwrs(int _n,int _m){
  static celt_uint32_t c[32][32];
38
39
40
41
42
43
44
45
  if(_n<0||_m<0)return 0;
  if(!c[_n][_m]){
    if(_m<=0)c[_n][_m]=1;
    else if(_n>0)c[_n][_m]=ncwrs(_n-1,_m)+ncwrs(_n,_m-1)+ncwrs(_n-1,_m-1);
  }
  return c[_n][_m];
}
#else
46
47
48
49
celt_uint32_t ncwrs(int _n,int _m){
  celt_uint32_t ret;
  celt_uint32_t f;
  celt_uint32_t d;
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  int      i;
  if(_n<0||_m<0)return 0;
  if(_m==0)return 1;
  if(_n==0)return 0;
  ret=0;
  f=_n;
  d=1;
  for(i=1;i<=_m;i++){
    ret+=f*d<<i;
    f=(f*(_n-i))/(i+1);
    d=(d*(_m-i))/i;
  }
  return ret;
}
#endif

66
celt_uint64_t ncwrs64(int _n,int _m){
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  celt_uint64_t ret;
  celt_uint64_t f;
  celt_uint64_t d;
  int           i;
  if(_n<0||_m<0)return 0;
  if(_m==0)return 1;
  if(_n==0)return 0;
  ret=0;
  f=_n;
  d=1;
  for(i=1;i<=_m;i++){
    ret+=f*d<<i;
    f=(f*(_n-i))/(i+1);
    d=(d*(_m-i))/i;
  }
  return ret;
83
84
}

85
86
87
88
/*Returns the _i'th combination of _m elements chosen from a set of size _n
   with associated sign bits.
  _x:      Returns the combination with elements sorted in ascending order.
  _s:      Returns the associated sign bits.*/
89
void cwrsi(int _n,int _m,celt_uint32_t _i,int *_x,int *_s){
90
91
  int j;
  int k;
92
  for(k=j=0;k<_m;k++){
93
94
95
    celt_uint32_t pn;
    celt_uint32_t p;
    celt_uint32_t t;
96
97
98
    p=ncwrs(_n-j,_m-k-1);
    pn=ncwrs(_n-j-1,_m-k-1);
    p+=pn;
99
100
101
102
103
    if(k>0){
      t=p>>1;
      if(t<=_i||_s[k-1])_i+=t;
    }
    while(p<=_i){
104
      _i-=p;
105
      j++;
106
      p=pn;
107
108
109
      pn=ncwrs(_n-j-1,_m-k-1);
      p+=pn;
    }
110
111
    t=p>>1;
    _s[k]=_i>=t;
112
113
114
115
116
117
118
119
120
    _x[k]=j;
    if(_s[k])_i-=t;
  }
}

/*Returns the index of the given combination of _m elements chosen from a set
   of size _n with associated sign bits.
  _x:      The combination with elements sorted in ascending order.
  _s:      The associated sign bits.*/
121
122
celt_uint32_t icwrs(int _n,int _m,const int *_x,const int *_s){
  celt_uint32_t i;
123
124
125
126
  int      j;
  int      k;
  i=0;
  for(k=j=0;k<_m;k++){
127
128
    celt_uint32_t pn;
    celt_uint32_t p;
129
    p=ncwrs(_n-j,_m-k-1);
130
    pn=ncwrs(_n-j-1,_m-k-1);
131
132
    p+=pn;
    if(k>0)p>>=1;
133
    while(j<_x[k]){
134
      i+=p;
135
      j++;
136
      p=pn;
137
138
139
      pn=ncwrs(_n-j-1,_m-k-1);
      p+=pn;
    }
140
    if((k==0||_x[k]!=_x[k-1])&&_s[k])i+=p>>1;
141
142
143
144
  }
  return i;
}

145
146
147
148
149
/*Returns the _i'th combination of _m elements chosen from a set of size _n
   with associated sign bits.
  _x:      Returns the combination with elements sorted in ascending order.
  _s:      Returns the associated sign bits.*/
void cwrsi64(int _n,int _m,celt_uint64_t _i,int *_x,int *_s){
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  int j;
  int k;
  for(k=j=0;k<_m;k++){
    celt_uint64_t pn;
    celt_uint64_t p;
    celt_uint64_t t;
    p=ncwrs64(_n-j,_m-k-1);
    pn=ncwrs64(_n-j-1,_m-k-1);
    p+=pn;
    if(k>0){
      t=p>>1;
      if(t<=_i||_s[k-1])_i+=t;
    }
    while(p<=_i){
      _i-=p;
      j++;
      p=pn;
167
      pn=ncwrs64(_n-j-1,_m-k-1);
168
169
170
171
172
173
174
      p+=pn;
    }
    t=p>>1;
    _s[k]=_i>=t;
    _x[k]=j;
    if(_s[k])_i-=t;
  }
175
176
177
178
179
180
181
}

/*Returns the index of the given combination of _m elements chosen from a set
   of size _n with associated sign bits.
  _x:      The combination with elements sorted in ascending order.
  _s:      The associated sign bits.*/
celt_uint64_t icwrs64(int _n,int _m,const int *_x,const int *_s){
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
  celt_uint64_t i;
  int           j;
  int           k;
  i=0;
  for(k=j=0;k<_m;k++){
    celt_uint64_t pn;
    celt_uint64_t p;
    p=ncwrs64(_n-j,_m-k-1);
    pn=ncwrs64(_n-j-1,_m-k-1);
    p+=pn;
    if(k>0)p>>=1;
    while(j<_x[k]){
      i+=p;
      j++;
      p=pn;
197
      pn=ncwrs64(_n-j-1,_m-k-1);
198
199
200
201
202
      p+=pn;
    }
    if((k==0||_x[k]!=_x[k-1])&&_s[k])i+=p>>1;
  }
  return i;
203
204
}

205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*Converts a combination _x of _m unit pulses with associated sign bits _s into
   a pulse vector _y of length _n.
  _y: Returns the vector of pulses.
  _x: The combination with elements sorted in ascending order.
  _s: The associated sign bits.*/
void comb2pulse(int _n,int _m,int *_y,const int *_x,const int *_s){
  int j;
  int k;
  int n;
  for(k=j=0;k<_m;k+=n){
    for(n=1;k+n<_m&&_x[k+n]==_x[k];n++);
    while(j<_x[k])_y[j++]=0;
    _y[j++]=_s[k]?-n:n;
  }
  while(j<_n)_y[j++]=0;
}

/*Converts a pulse vector vector _y of length _n into a combination of _m unit
   pulses with associated sign bits _s.
  _x: Returns the combination with elements sorted in ascending order.
  _s: Returns the associated sign bits.
  _y: The vector of pulses, whose sum of absolute values must be _m.*/
void pulse2comb(int _n,int _m,int *_x,int *_s,const int *_y){
  int j;
  int k;
  for(k=j=0;j<_n;j++){
    if(_y[j]){
      int n;
      int s;
      n=abs(_y[j]);
      s=_y[j]<0;
      for(;n-->0;k++){
        _x[k]=j;
        _s[k]=s;
      }
    }
  }
}