csvorbis.CodeBook.make_words C# (CSharp) Method

make_words() static private method

static private make_words ( int l, int n ) : int[]
l int
n int
return int[]
        internal static int[] make_words(int[] l, int n)
        {
            int[] marker=new int[33];
            int[] r=new int[n];
            //memset(marker,0,sizeof(marker));

            for(int i=0;i<n;i++)
            {
                int length=l[i];
                if(length>0)
                {
                    int entry=marker[length];

                    // when we claim a node for an entry, we also claim the nodes
                    // below it (pruning off the imagined tree that may have dangled
                    // from it) as well as blocking the use of any nodes directly
                    // above for leaves

                    // update ourself
                    if(length<32 && ((uint)entry>>length)!=0)
                    {
                        // error condition; the lengths must specify an overpopulated tree
                        //free(r);
                        return(null);
                    }
                    r[i]=entry;

                    // Look to see if the next shorter marker points to the node
                    // above. if so, update it and repeat.
                {
                    for(int j=length;j>0;j--)
                    {
                        if((marker[j]&1)!=0)
                        {
                            // have to jump branches
                            if(j==1)marker[1]++;
                            else marker[j]=marker[j-1]<<1;
                            break; // invariant says next upper marker would already
                            // have been moved if it was on the same path
                        }
                        marker[j]++;
                    }
                }

                    // prune the tree; the implicit invariant says all the longer
                    // markers were dangling from our just-taken node.  Dangle them
                    // from our *new* node.
                    for(int j=length+1;j<33;j++)
                    {
                        if(((uint)marker[j]>>1) == entry)
                        {
                            entry=marker[j];
                            marker[j]=marker[j-1]<<1;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }

            // bitreverse the words because our bitwise packer/unpacker is LSb
            // endian
            for(int i=0;i<n;i++)
            {
                int temp=0;
                for(int j=0;j<l[i];j++)
                {
                    temp<<=1;
                    temp = (int)((uint)temp | ((uint)r[i]>>j)&1);
                }
                r[i]=temp;
            }

            return(r);
        }