root / trunk / web / dojo / dojox / encoding / compression / splay.js @ 13
History | View | Annotate | Download (1.37 KB)
| 1 |
/*
|
|---|---|
| 2 |
Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
| 3 |
Available via Academic Free License >= 2.1 OR the modified BSD license.
|
| 4 |
see: http://dojotoolkit.org/license for details
|
| 5 |
*/
|
| 6 |
|
| 7 |
|
| 8 |
if(!dojo._hasResource["dojox.encoding.compression.splay"]){ |
| 9 |
dojo._hasResource["dojox.encoding.compression.splay"]=true; |
| 10 |
dojo.provide("dojox.encoding.compression.splay");
|
| 11 |
dojo.require("dojox.encoding.bits");
|
| 12 |
dojox.encoding.compression.Splay=function(n){ |
| 13 |
this.up=new Array(2*n+1); |
| 14 |
this.left=new Array(n); |
| 15 |
this.right=new Array(n); |
| 16 |
this.reset();
|
| 17 |
}; |
| 18 |
dojo.extend(dojox.encoding.compression.Splay,{reset:function(){
|
| 19 |
for(var i=1;i<this.up.length;this.up[i]=Math.floor((i-1)/2),++i){ |
| 20 |
} |
| 21 |
for(var i=0;i<this.left.length;this.left[i]=2*i+1,this.right[i]=2*i+2,++i){ |
| 22 |
} |
| 23 |
},splay:function(i){ |
| 24 |
var a=i+this.left.length; |
| 25 |
do{
|
| 26 |
var c=this.up[a]; |
| 27 |
if(c){
|
| 28 |
var d=this.up[c]; |
| 29 |
var b=this.left[d]; |
| 30 |
if(c==b){
|
| 31 |
b=this.right[d];
|
| 32 |
this.right[d]=a;
|
| 33 |
}else{
|
| 34 |
this.left[d]=a;
|
| 35 |
} |
| 36 |
this[a==this.left[c]?"left":"right"][c]=b; |
| 37 |
this.up[a]=d;
|
| 38 |
this.up[b]=c;
|
| 39 |
a=d; |
| 40 |
}else{
|
| 41 |
a=c; |
| 42 |
} |
| 43 |
}while(a);
|
| 44 |
},encode:function(_1,_2){ |
| 45 |
var s=[],a=_1+this.left.length; |
| 46 |
do{
|
| 47 |
s.push(this.right[this.up[a]]==a); |
| 48 |
a=this.up[a];
|
| 49 |
}while(a);
|
| 50 |
this.splay(_1);
|
| 51 |
var l=s.length;
|
| 52 |
while(s.length){
|
| 53 |
_2.putBits(s.pop()?1:0,1); |
| 54 |
} |
| 55 |
return l;
|
| 56 |
},decode:function(_3){ |
| 57 |
var a=0; |
| 58 |
do{
|
| 59 |
a=this[_3.getBits(1)?"right":"left"][a]; |
| 60 |
}while(a<this.left.length); |
| 61 |
a-=this.left.length;
|
| 62 |
this.splay(a);
|
| 63 |
return a;
|
| 64 |
}}); |
| 65 |
} |