Skip to content

Commit

Permalink
query compression algo works
Browse files Browse the repository at this point in the history
  • Loading branch information
dschwen committed Feb 21, 2012
1 parent 234a808 commit 59bc6db
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions compressreq.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
'.#........',
'.#........',
'.#######..',
'.#........',
'.#######..',
'..........'
];

Expand All @@ -49,7 +49,7 @@
shape[2] = makeShape(map[2]);

function compress(s) {
var i,j,a=null,b=[];
var i,j,a=null,b=[],q=[];
// horizontal
for(i=0; i<s.length;++i) {
for(j=0; j<=s[i].length;++j) {
Expand All @@ -66,23 +66,29 @@
}
}
}

// sort to optimize compression
b.sort(function(a,b){return a.i-b.i});
b.sort(function(a,b){return (a.j+'').localeCompare(b.j)});

// add dummy
b.push( { i:null, j:null } );

// vertical compression
if(b.length>1) {
a=0;
for(i=1; i<b.length;++i) {
if( (b[i].j !== b[i-1].j) || ( (b[i].i-1) !== b[i-1].i ) ) {
if( (b[i].i-1) > b[a].i ) {
console.log( b[a].i+'-'+(b[i].i-1)+','+b[a].j );
if( b[i-1].i > b[a].i ) {
q.push( b[a].i+'-'+b[i-1].i+','+b[a].j );
} else {
console.log( b[a].i+','+b[a].j );
q.push( b[a].i+','+b[a].j );
}
a=i;
}
}
}
return b;
return q.join('|');
}

compress(shape[0]);
Expand Down

0 comments on commit 59bc6db

Please sign in to comment.