forked from zhoutk/js-data-struct
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDictionary.js
66 lines (49 loc) · 1.42 KB
/
Dictionary.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/***************************************************************************
> File Name : Dictionary.js
> Author : zhoutk
> Mail : zhoutk@189.cn
> Create Time : 2015-09-25 19:58
***************************************************************************/
(function(){
"use strict";
function Dictionary(){
this._size = 0;
this.datastore = Object.create(null);
}
Dictionary.prototype.isEmpty = function(){
return this._size === 0;
};
Dictionary.prototype.size = function(){
return this._size;
};
Dictionary.prototype.clear = function(){
for(var key in this.datastore){
delete this.datastore[key];
}
this._size = 0;
};
Dictionary.prototype.add = function(key, value){
this.datastore[key] = value;
this._size++;
};
Dictionary.prototype.find = function(key){
return this.datastore[key];
};
Dictionary.prototype.count = function(){
var n = 0;
for(var key in this.datastore){
n++;
}
return n;
};
Dictionary.prototype.remove = function(key){
delete this.datastore[key];
this._size--;
};
Dictionary.prototype.showAll = function(){
for(var key in this.datastore){
console.log(key + "->" + this.datastore[key]);
}
};
module.exports = Dictionary;
})();