Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 1 | /* (C) 2007 Timothy B. Terriberry */ |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 2 | /* |
| 3 | Redistribution and use in source and binary forms, with or without |
| 4 | modification, are permitted provided that the following conditions |
| 5 | are met: |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 6 | |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 7 | - Redistributions of source code must retain the above copyright |
| 8 | notice, this list of conditions and the following disclaimer. |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 9 | |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 10 | - Redistributions in binary form must reproduce the above copyright |
| 11 | notice, this list of conditions and the following disclaimer in the |
| 12 | documentation and/or other materials provided with the distribution. |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 13 | |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 14 | - Neither the name of the Xiph.org Foundation nor the names of its |
| 15 | contributors may be used to endorse or promote products derived from |
| 16 | this software without specific prior written permission. |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 17 | |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 22 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 23 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 25 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 26 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 27 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 30 | #include <stdlib.h> |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 31 | #include "cwrs.h" |
| 32 | |
| 33 | /*Returns the numer of ways of choosing _m elements from a set of size _n with |
| 34 | replacement when a sign bit is needed for each unique element.*/ |
| 35 | #if 0 |
| 36 | static unsigned ncwrs(int _n,int _m){ |
| 37 | static unsigned c[32][32]; |
| 38 | if(_n<0||_m<0)return 0; |
| 39 | if(!c[_n][_m]){ |
| 40 | if(_m<=0)c[_n][_m]=1; |
| 41 | else if(_n>0)c[_n][_m]=ncwrs(_n-1,_m)+ncwrs(_n,_m-1)+ncwrs(_n-1,_m-1); |
| 42 | } |
| 43 | return c[_n][_m]; |
| 44 | } |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 45 | #else |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 46 | unsigned ncwrs(int _n,int _m){ |
| 47 | unsigned ret; |
| 48 | unsigned f; |
| 49 | unsigned d; |
| 50 | int i; |
| 51 | if(_n<0||_m<0)return 0; |
| 52 | if(_m==0)return 1; |
| 53 | if(_n==0)return 0; |
| 54 | ret=0; |
| 55 | f=_n; |
| 56 | d=1; |
| 57 | for(i=1;i<=_m;i++){ |
| 58 | ret+=f*d<<i; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 59 | f=(f*(_n-i))/(i+1); |
| 60 | d=(d*(_m-i))/i; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 61 | } |
| 62 | return ret; |
| 63 | } |
| 64 | #endif |
| 65 | |
Jean-Marc Valin | f8db800 | 2007-12-11 14:52:56 +1100 | [diff] [blame] | 66 | celt_uint64_t ncwrs64(int _n,int _m){ |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 67 | celt_uint64_t ret; |
| 68 | celt_uint64_t f; |
| 69 | celt_uint64_t d; |
| 70 | int i; |
| 71 | if(_n<0||_m<0)return 0; |
| 72 | if(_m==0)return 1; |
| 73 | if(_n==0)return 0; |
| 74 | ret=0; |
| 75 | f=_n; |
| 76 | d=1; |
| 77 | for(i=1;i<=_m;i++){ |
| 78 | ret+=f*d<<i; |
| 79 | f=(f*(_n-i))/(i+1); |
| 80 | d=(d*(_m-i))/i; |
| 81 | } |
| 82 | return ret; |
Jean-Marc Valin | f8db800 | 2007-12-11 14:52:56 +1100 | [diff] [blame] | 83 | } |
| 84 | |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 85 | /*Returns the _i'th combination of _m elements chosen from a set of size _n |
| 86 | with associated sign bits. |
| 87 | _x: Returns the combination with elements sorted in ascending order. |
| 88 | _s: Returns the associated sign bits.*/ |
| 89 | void cwrsi(int _n,int _m,unsigned _i,int *_x,int *_s){ |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 90 | int j; |
| 91 | int k; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 92 | for(k=j=0;k<_m;k++){ |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 93 | unsigned pn; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 94 | unsigned p; |
| 95 | unsigned t; |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 96 | p=ncwrs(_n-j,_m-k-1); |
| 97 | pn=ncwrs(_n-j-1,_m-k-1); |
| 98 | p+=pn; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 99 | if(k>0){ |
| 100 | t=p>>1; |
| 101 | if(t<=_i||_s[k-1])_i+=t; |
| 102 | } |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 103 | while(p<=_i){ |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 104 | _i-=p; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 105 | j++; |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 106 | p=pn; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 107 | pn=ncwrs(_n-j-1,_m-k-1); |
| 108 | p+=pn; |
| 109 | } |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 110 | t=p>>1; |
| 111 | _s[k]=_i>=t; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 112 | _x[k]=j; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 113 | if(_s[k])_i-=t; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /*Returns the index of the given combination of _m elements chosen from a set |
| 118 | of size _n with associated sign bits. |
| 119 | _x: The combination with elements sorted in ascending order. |
| 120 | _s: The associated sign bits.*/ |
| 121 | unsigned icwrs(int _n,int _m,const int *_x,const int *_s){ |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 122 | unsigned i; |
| 123 | int j; |
| 124 | int k; |
| 125 | i=0; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 126 | for(k=j=0;k<_m;k++){ |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 127 | unsigned pn; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 128 | unsigned p; |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 129 | p=ncwrs(_n-j,_m-k-1); |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 130 | pn=ncwrs(_n-j-1,_m-k-1); |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 131 | p+=pn; |
| 132 | if(k>0)p>>=1; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 133 | while(j<_x[k]){ |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 134 | i+=p; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 135 | j++; |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 136 | p=pn; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 137 | pn=ncwrs(_n-j-1,_m-k-1); |
| 138 | p+=pn; |
| 139 | } |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 140 | if((k==0||_x[k]!=_x[k-1])&&_s[k])i+=p>>1; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 141 | } |
| 142 | return i; |
| 143 | } |
| 144 | |
Jean-Marc Valin | f8db800 | 2007-12-11 14:52:56 +1100 | [diff] [blame] | 145 | /*Returns the _i'th combination of _m elements chosen from a set of size _n |
| 146 | with associated sign bits. |
| 147 | _x: Returns the combination with elements sorted in ascending order. |
| 148 | _s: Returns the associated sign bits.*/ |
| 149 | void cwrsi64(int _n,int _m,celt_uint64_t _i,int *_x,int *_s){ |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 150 | int j; |
| 151 | int k; |
| 152 | for(k=j=0;k<_m;k++){ |
| 153 | celt_uint64_t pn; |
| 154 | celt_uint64_t p; |
| 155 | celt_uint64_t t; |
| 156 | p=ncwrs64(_n-j,_m-k-1); |
| 157 | pn=ncwrs64(_n-j-1,_m-k-1); |
| 158 | p+=pn; |
| 159 | if(k>0){ |
| 160 | t=p>>1; |
| 161 | if(t<=_i||_s[k-1])_i+=t; |
| 162 | } |
| 163 | while(p<=_i){ |
| 164 | _i-=p; |
| 165 | j++; |
| 166 | p=pn; |
Jean-Marc Valin | f8db800 | 2007-12-11 14:52:56 +1100 | [diff] [blame] | 167 | pn=ncwrs64(_n-j-1,_m-k-1); |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 168 | p+=pn; |
| 169 | } |
| 170 | t=p>>1; |
| 171 | _s[k]=_i>=t; |
| 172 | _x[k]=j; |
| 173 | if(_s[k])_i-=t; |
| 174 | } |
Jean-Marc Valin | f8db800 | 2007-12-11 14:52:56 +1100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /*Returns the index of the given combination of _m elements chosen from a set |
| 178 | of size _n with associated sign bits. |
| 179 | _x: The combination with elements sorted in ascending order. |
| 180 | _s: The associated sign bits.*/ |
| 181 | celt_uint64_t icwrs64(int _n,int _m,const int *_x,const int *_s){ |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 182 | celt_uint64_t i; |
| 183 | int j; |
| 184 | int k; |
| 185 | i=0; |
| 186 | for(k=j=0;k<_m;k++){ |
| 187 | celt_uint64_t pn; |
| 188 | celt_uint64_t p; |
| 189 | p=ncwrs64(_n-j,_m-k-1); |
| 190 | pn=ncwrs64(_n-j-1,_m-k-1); |
| 191 | p+=pn; |
| 192 | if(k>0)p>>=1; |
| 193 | while(j<_x[k]){ |
| 194 | i+=p; |
| 195 | j++; |
| 196 | p=pn; |
Jean-Marc Valin | f8db800 | 2007-12-11 14:52:56 +1100 | [diff] [blame] | 197 | pn=ncwrs64(_n-j-1,_m-k-1); |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 198 | p+=pn; |
| 199 | } |
| 200 | if((k==0||_x[k]!=_x[k-1])&&_s[k])i+=p>>1; |
| 201 | } |
| 202 | return i; |
Jean-Marc Valin | f8db800 | 2007-12-11 14:52:56 +1100 | [diff] [blame] | 203 | } |
| 204 | |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 205 | /*Converts a combination _x of _m unit pulses with associated sign bits _s into |
| 206 | a pulse vector _y of length _n. |
| 207 | _y: Returns the vector of pulses. |
| 208 | _x: The combination with elements sorted in ascending order. |
| 209 | _s: The associated sign bits.*/ |
| 210 | void comb2pulse(int _n,int _m,int *_y,const int *_x,const int *_s){ |
| 211 | int j; |
| 212 | int k; |
| 213 | int n; |
| 214 | for(k=j=0;k<_m;k+=n){ |
| 215 | for(n=1;k+n<_m&&_x[k+n]==_x[k];n++); |
| 216 | while(j<_x[k])_y[j++]=0; |
| 217 | _y[j++]=_s[k]?-n:n; |
| 218 | } |
| 219 | while(j<_n)_y[j++]=0; |
| 220 | } |
| 221 | |
| 222 | /*Converts a pulse vector vector _y of length _n into a combination of _m unit |
| 223 | pulses with associated sign bits _s. |
| 224 | _x: Returns the combination with elements sorted in ascending order. |
| 225 | _s: Returns the associated sign bits. |
| 226 | _y: The vector of pulses, whose sum of absolute values must be _m.*/ |
| 227 | void pulse2comb(int _n,int _m,int *_x,int *_s,const int *_y){ |
| 228 | int j; |
| 229 | int k; |
| 230 | for(k=j=0;j<_n;j++){ |
| 231 | if(_y[j]){ |
| 232 | int n; |
| 233 | int s; |
| 234 | n=abs(_y[j]); |
| 235 | s=_y[j]<0; |
| 236 | for(;n-->0;k++){ |
| 237 | _x[k]=j; |
| 238 | _s[k]=s; |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 244 | #if 0 |
| 245 | #include <stdio.h> |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 246 | #define NMAX (10) |
| 247 | #define MMAX (9) |
| 248 | |
| 249 | int main(int _argc,char **_argv){ |
| 250 | int n; |
| 251 | for(n=0;n<=NMAX;n++){ |
| 252 | int m; |
| 253 | for(m=0;m<=MMAX;m++){ |
| 254 | unsigned nc; |
| 255 | unsigned i; |
| 256 | nc=ncwrs(n,m); |
| 257 | for(i=0;i<nc;i++){ |
| 258 | int x[MMAX]; |
| 259 | int s[MMAX]; |
| 260 | int x2[MMAX]; |
| 261 | int s2[MMAX]; |
| 262 | int y[NMAX]; |
| 263 | int j; |
| 264 | int k; |
| 265 | cwrsi(n,m,i,x,s); |
| 266 | printf("%6u of %u:",i,nc); |
| 267 | for(k=0;k<m;k++){ |
| 268 | printf(" %c%i",k>0&&x[k]==x[k-1]?' ':s[k]?'-':'+',x[k]); |
| 269 | } |
| 270 | printf(" ->"); |
| 271 | if(icwrs(n,m,x,s)!=i){ |
| 272 | fprintf(stderr,"Combination-index mismatch.\n"); |
| 273 | } |
| 274 | comb2pulse(n,m,y,x,s); |
| 275 | for(j=0;j<n;j++)printf(" %c%i",y[j]?y[j]<0?'-':'+':' ',abs(y[j])); |
| 276 | printf("\n"); |
| 277 | pulse2comb(n,m,x2,s2,y); |
| 278 | for(k=0;k<m;k++)if(x[k]!=x2[k]||s[k]!=s2[k]){ |
| 279 | fprintf(stderr,"Pulse-combination mismatch.\n"); |
| 280 | break; |
| 281 | } |
| 282 | } |
| 283 | printf("\n"); |
| 284 | } |
| 285 | } |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 286 | return -1; |
Timothy B. Terriberry | c4541ae | 2007-12-03 11:51:29 +1100 | [diff] [blame] | 287 | } |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 288 | #endif |
Jean-Marc Valin | f8db800 | 2007-12-11 14:52:56 +1100 | [diff] [blame] | 289 | |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 290 | #if 0 |
Jean-Marc Valin | f8db800 | 2007-12-11 14:52:56 +1100 | [diff] [blame] | 291 | #include <stdio.h> |
| 292 | #define NMAX (32) |
| 293 | #define MMAX (16) |
| 294 | |
| 295 | int main(int _argc,char **_argv){ |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 296 | int n; |
| 297 | for(n=0;n<=NMAX;n+=3){ |
| 298 | int m; |
| 299 | for(m=0;m<=MMAX;m++){ |
| 300 | celt_uint64_t nc; |
| 301 | celt_uint64_t i; |
| 302 | nc=ncwrs64(n,m); |
| 303 | printf("%d/%d: %llu",n,m, nc); |
| 304 | for(i=0;i<nc;i+=100000){ |
| 305 | int x[MMAX]; |
| 306 | int s[MMAX]; |
| 307 | int x2[MMAX]; |
| 308 | int s2[MMAX]; |
| 309 | int y[NMAX]; |
| 310 | int j; |
| 311 | int k; |
| 312 | cwrsi64(n,m,i,x,s); |
| 313 | /*printf("%llu of %llu:",i,nc); |
| 314 | for(k=0;k<m;k++){ |
| 315 | printf(" %c%i",k>0&&x[k]==x[k-1]?' ':s[k]?'-':'+',x[k]); |
| 316 | } |
| 317 | printf(" ->");*/ |
| 318 | if(icwrs64(n,m,x,s)!=i){ |
| 319 | fprintf(stderr,"Combination-index mismatch.\n"); |
| 320 | } |
| 321 | comb2pulse(n,m,y,x,s); |
| 322 | /*for(j=0;j<n;j++)printf(" %c%i",y[j]?y[j]<0?'-':'+':' ',abs(y[j])); |
| 323 | printf("\n");*/ |
| 324 | pulse2comb(n,m,x2,s2,y); |
| 325 | for(k=0;k<m;k++)if(x[k]!=x2[k]||s[k]!=s2[k]){ |
| 326 | fprintf(stderr,"Pulse-combination mismatch.\n"); |
| 327 | break; |
| 328 | } |
Jean-Marc Valin | f8db800 | 2007-12-11 14:52:56 +1100 | [diff] [blame] | 329 | } |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 330 | printf("\n"); |
| 331 | } |
| 332 | } |
| 333 | return 0; |
Jean-Marc Valin | f8db800 | 2007-12-11 14:52:56 +1100 | [diff] [blame] | 334 | } |
Timothy B. Terriberry | 71c9bbf | 2008-01-01 07:18:06 +1100 | [diff] [blame] | 335 | #endif |