source: Dev/branches/rest-dojo-ui/client/dojox/encoding/compression/splay.js @ 275

Last change on this file since 275 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

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