]>
jfr.im git - irc/quakenet/qwebirc.git/blob - js/qhash.js
1 QHash = function(def
) {
5 if(def
!== undefined) {
6 var h
= Object
.prototype.hasOwnProperty
;
15 QHash
.prototype.__toKey = function(key
) {
16 if(typeof(key
) !== "string")
17 throw new TypeError("Not a string: " + key
);
21 QHash
.prototype.get = function(key
) {
22 return this.__map
[this.__toKey(key
)];
25 QHash
.prototype.put = function(key
, value
) {
26 key
= this.__toKey(key
);
28 var exists
= key
in this.__map
;
29 var oldValue
= this.__map
[key
];
31 this.__map
[key
] = value
;
41 QHash
.prototype.contains = function(key
) {
42 return this.__toKey(key
) in this.__map
;
45 QHash
.prototype.remove = function(key
) {
46 key
= this.__toKey(key
);
48 var exists
= key
in this.__map
;
52 var oldValue
= this.__map
[key
];
53 delete this.__map
[key
];
59 QHash
.prototype.each = function(fn
, def
) {
60 var h
= Object
.prototype.hasOwnProperty
;
66 var break_
= fn
.call(def
, k
.substr(0, k
.length
- 1), m
[k
]) !== undefined;
72 QHash
.prototype.isEmpty = function() {
73 return this.length
== 0;
76 QHash
.prototype.map = function(fn
, def
) {
78 this.each(function(k
, v
) {
79 l
.push(fn
.call(def
, k
, v
));
84 QHash
.prototype.keys = function() {
85 return this.map(function(k
) { return k
; });
88 QHash
.prototype.values = function() {
89 return this.map(function(k
, v
) { return v
; });
92 QHash
.prototype.items = function() {
93 return this.map(function(k
, v
) { return [k
, v
]; });
96 QHash
.prototype.toString = function() {
97 var m
= this.map(function(k
, v
) {
101 return "{QHash length=" + this.length
+ " map={" + m
+ "}}";
105 this.__map
= new QHash();
108 for(var i
=0;i
<arguments
.length
;i
++)
109 this.add(arguments
[i
]);
112 QSet
.prototype.add = function(value
) {
113 var v
= this.__map
.put(value
, true);
115 this.length
= this.__map
.length
;
120 QSet
.prototype.contains = function(value
) {
121 return this.__map
.contains(value
);
124 QSet
.prototype.remove = function(value
) {
125 var v
= this.__map
.remove(value
, true);
127 this.length
= this.__map
.length
;
132 QSet
.prototype.each = function(fn
, def
) {
133 return this.__map
.each(fn
, def
);
136 QSet
.prototype.values = function() {
137 return this.__map
.keys();
140 QSet
.prototype.isEmpty = function() {
141 return this.length
== 0;
144 QSet
.prototype.map = function(fn
, def
) {
145 return this.__map
.map(fn
, def
);
148 QSet
.prototype.toString = function(value
) {
149 return "{QSet length=" + this.length
+ " set={" + this.values() + "}}";