Debian Codegraph @

dojox/encoding/compression/splay.js

raw symbols SCIP
1 define([
2 "dojo/_base/lang", // dojo.extend
3 "../bits"
4 ], function(lang, bits) {
5 var compression = lang.getObject("dojox.encoding.compression", true);
6
7 compression.Splay = function(n){
8 this.up = new Array(2 * n + 1);
9 this.left = new Array(n);
10 this.right = new Array(n);
11 this.reset();
12 };
13
14 lang.extend(compression.Splay, {
15 reset: function(){
16 for(var i = 1; i < this.up.length; this.up[i] = Math.floor((i - 1) / 2), ++i);
17 for(var i = 0; i < this.left.length; this.left[i] = 2 * i + 1, this.right[i] = 2 * i + 2, ++i);
18 },
19 splay: function(i){
20 var a = i + this.left.length;
21 do{
22 var c = this.up[a];
23 if(c){ // root
24 // rotated pair
25 var d = this.up[c];
26 // swap descendants
27 var b = this.left[d];
28 if(c == b){
29 b = this.right[d];
30 this.right[d] = a;
31 } else {
32 this.left[d] = a;
33 }
34 this[a == this.left[c] ? "left" : "right"][c] = b;
35 this.up[a] = d;
36 this.up[b] = c;
37 a = d;
38 }else{
39 a = c;
40 }
41 }while(a); // root
42 },
43 encode: function(value, stream){
44 var s = [], a = value + this.left.length;
45 do{
46 s.push(this.right[this.up[a]] == a);
47 a = this.up[a];
48 }while(a); // root
49 this.splay(value);
50 var l = s.length;
51 while(s.length){ stream.putBits(s.pop() ? 1 : 0, 1); }
52 return l;
53 },
54 decode: function(stream){
55 var a = 0; // root;
56 do{
57 a = this[stream.getBits(1) ? "right" : "left"][a];
58 }while(a < this.left.length);
59 a -= this.left.length;
60 this.splay(a);
61 return a;
62 }
63 });
64
65
66 return compression.Splay;
67 });