js/image/node/jpeg-js/jsgui_decoder.js
// Doubtful that the heaped version is faster.
Doubtful that the heaped version is faster.
Doubtful that the heaped version is faster .
function buildHuffmanTable(codeLengths, values) {
        console.log('buildHuffmanTable');
        var k = 0, code = [], i, j, length = 16;
        while (length > 0 && !codeLengths[length - 1])
            length--;
        code.push({children: [], index: 0});
        var p = code[0], q, codeLengths_i_length;
        for (i = 0; i < length; i++) {
            for (j = 0, codeLengths_i_length = codeLengths[i]; j < codeLengths_i_length; j++) {
                p = code.pop();
                p.children[p.index] = values[k];
                while (p.index > 0) {
                    p = code.pop();
                }
                p.index++;
                code.push(p);
                while (code.length <= i) {
                    code.push(q = {children: [], index: 0});
                    p.children[p.index] = q.children;
                    p = q;
                }
                k++;
            }
            if (i + 1 < length) {
                code.push(q = {children: [], index: 0});
                p.children[p.index] = q.children;
                p = q;
            }
        }
        return code[0].children;
    }
function buildHuffmanTable ( codeLengths , values ) { console . log ( 'buildHuffmanTable' ) ; var k = 0 , code = [ ] , i , j , length = 16 ; while ( length > 0 && ! codeLengths [ length - 1 ] ) length -- ; code . push ( { children : [ ] , index : 0 } ) ; var p = code [ 0 ] , q , codeLengths_i_length ; for ( i = 0 ; i < length ; i ++ ) { for ( j = 0 , codeLengths_i_length = codeLengths [ i ] ; j < codeLengths_i_length ; j ++ ) { p = code . pop ( ) ; p . children [ p . index ] = values [ k ] ; while ( p . index > 0 ) { p = code . pop ( ) ; } p . index ++ ; code . push ( p ) ; while ( code . length <= i ) { code . push ( q = { children : [ ] , index : 0 } ) ; p . children [ p . index ] = q . children ; p = q ; } k ++ ; } if ( i + 1 < length ) { code . push ( q = { children : [ ] , index : 0 } ) ; p . children [ p . index ] = q . children ; p = q ; } } return code [ 0 ] . children ; }
Doubtful that the heaped version is faster.
Doubtful that the heaped version is faster .
// Could make j, i, j, length refer to values in a heap. //  p as well? // i = vars_heap[0] // j = vars_heap[1] // k = vars_heap[2] // length = vars_heap[3]; // codeLengths_i_length = vars_heap[4]; //var code = [], length = 16; //var code = []; //length = 16; //var p = code[0], q; //k = 0; //console.log('p', p); //console.log('k', k); // p here points to last code //var res = code[0].children; //console.log('res', res); //console.log('res', JSON.stringify(res)); //return res;