]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/qhash.js
normalise line endings
[irc/quakenet/qwebirc.git] / js / qhash.js
1 QHash = function(def) {
2 this.__map = {};
3 this.length = 0;
4
5 if(def !== undefined) {
6 var h = Object.prototype.hasOwnProperty;
7 for (var k in def) {
8 if(!h.call(def, k))
9 continue;
10 this.put(k, def[k]);
11 }
12 }
13 };
14
15 QHash.prototype.__toKey = function(key) {
16 if(typeof(key) !== "string")
17 throw new TypeError("Not a string: " + key);
18 return key + "$";
19 };
20
21 QHash.prototype.get = function(key) {
22 return this.__map[this.__toKey(key)];
23 };
24
25 QHash.prototype.put = function(key, value) {
26 key = this.__toKey(key);
27
28 var exists = key in this.__map;
29 var oldValue = this.__map[key];
30
31 this.__map[key] = value;
32
33 if(!exists) {
34 this.length++;
35 return null;
36 }
37
38 return oldValue;
39 };
40
41 QHash.prototype.contains = function(key) {
42 return this.__toKey(key) in this.__map;
43 };
44
45 QHash.prototype.remove = function(key) {
46 key = this.__toKey(key);
47
48 var exists = key in this.__map;
49 if(!exists)
50 return null;
51
52 var oldValue = this.__map[key];
53 delete this.__map[key];
54 this.length--;
55
56 return oldValue;
57 };
58
59 QHash.prototype.each = function(fn, def) {
60 var h = Object.prototype.hasOwnProperty;
61 var m = this.__map;
62 for(var k in m) {
63 if(!h.call(m, k))
64 continue;
65
66 var break_ = fn.call(def, k.substr(0, k.length - 1), m[k]) !== undefined;
67 if(break_)
68 return break_;
69 }
70 };
71
72 QHash.prototype.isEmpty = function() {
73 return this.length == 0;
74 };
75
76 QHash.prototype.map = function(fn, def) {
77 var l = [];
78 this.each(function(k, v) {
79 l.push(fn.call(def, k, v));
80 });
81 return l;
82 };
83
84 QHash.prototype.keys = function() {
85 return this.map(function(k) { return k; });
86 };
87
88 QHash.prototype.values = function() {
89 return this.map(function(k, v) { return v; });
90 };
91
92 QHash.prototype.items = function() {
93 return this.map(function(k, v) { return [k, v]; });
94 };
95
96 QHash.prototype.toString = function() {
97 var m = this.map(function(k, v) {
98 return k + "=" + v;
99 }, this);
100
101 return "{QHash length=" + this.length + " map={" + m + "}}";
102 };
103
104 QSet = function() {
105 this.__map = new QHash();
106
107 this.length = 0;
108 for(var i=0;i<arguments.length;i++)
109 this.add(arguments[i]);
110 };
111
112 QSet.prototype.add = function(value) {
113 var v = this.__map.put(value, true);
114 if(v !== true)
115 this.length = this.__map.length;
116
117 return v;
118 };
119
120 QSet.prototype.contains = function(value) {
121 return this.__map.contains(value);
122 };
123
124 QSet.prototype.remove = function(value) {
125 var v = this.__map.remove(value, true);
126 if(v)
127 this.length = this.__map.length;
128
129 return v;
130 };
131
132 QSet.prototype.each = function(fn, def) {
133 return this.__map.each(fn, def);
134 };
135
136 QSet.prototype.values = function() {
137 return this.__map.keys();
138 }
139
140 QSet.prototype.isEmpty = function() {
141 return this.length == 0;
142 };
143
144 QSet.prototype.map = function(fn, def) {
145 return this.__map.map(fn, def);
146 };
147
148 QSet.prototype.toString = function(value) {
149 return "{QSet length=" + this.length + " set={" + this.values() + "}}";
150 };