Merge pull request #237 from kberg/closure
[dygraphs.git] / jsdoc-toolkit / app / frame / Hash.js
1 /**
2 @constructor
3 @example
4 var _index = new Hash();
5 _index.set("a", "apple");
6 _index.set("b", "blue");
7 _index.set("c", "coffee");
8
9 for (var p = _index.first(); p; p = _index.next()) {
10 print(p.key+" is for "+p.value);
11 }
12
13 */
14 var Hash = function() {
15 this._map = {};
16 this._keys = [];
17 this._vals = [];
18 this.reset();
19 }
20
21 Hash.prototype.set = function(k, v) {
22 if (k != "") {
23 this._keys.push(k);
24 this._map["="+k] = this._vals.length;
25 this._vals.push(v);
26 }
27 }
28
29 Hash.prototype.replace = function(k, k2, v) {
30 if (k == k2) return;
31
32 var offset = this._map["="+k];
33 this._keys[offset] = k2;
34 if (typeof v != "undefined") this._vals[offset] = v;
35 this._map["="+k2] = offset;
36 delete(this._map["="+k]);
37 }
38
39 Hash.prototype.drop = function(k) {
40 if (k != "") {
41 var offset = this._map["="+k];
42 this._keys.splice(offset, 1);
43 this._vals.splice(offset, 1);
44 delete(this._map["="+k]);
45 for (var p in this._map) {
46 if (this._map[p] >= offset) this._map[p]--;
47 }
48 if (this._cursor >= offset && this._cursor > 0) this._cursor--;
49 }
50 }
51
52 Hash.prototype.get = function(k) {
53 if (k != "") {
54 return this._vals[this._map["="+k]];
55 }
56 }
57
58 Hash.prototype.keys = function() {
59 return this._keys;
60 }
61
62 Hash.prototype.hasKey = function(k) {
63 if (k != "") {
64 return (typeof this._map["="+k] != "undefined");
65 }
66 }
67
68 Hash.prototype.values = function() {
69 return this._vals;
70 }
71
72 Hash.prototype.reset = function() {
73 this._cursor = 0;
74 }
75
76 Hash.prototype.first = function() {
77 this.reset();
78 return this.next();
79 }
80
81 Hash.prototype.next = function() {
82 if (this._cursor++ < this._keys.length)
83 return {key: this._keys[this._cursor-1], value: this._vals[this._cursor-1]};
84 }