forked from JacksonTian/limitablemap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimitablemap.js
More file actions
80 lines (68 loc) · 2.06 KB
/
limitablemap.js
File metadata and controls
80 lines (68 loc) · 2.06 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var LimitableMap = function(limit) {
this.limit = limit || 16;
this.map = {}; // 要求这些键,不能是js关键字、保留字
this.keys = [];
// 常驻map
this.p_map = {};
this.p_keys = [];
};
var hasOwnProperty = Object.prototype.hasOwnProperty; // func
LimitableMap.prototype.set = function(key, value) {
var map = this.map;
var keys = this.keys;
if (!hasOwnProperty.call(map, key)) {
if (keys.length === this.limit) { // 防止内存溢出
var firstKey = keys.shift();
delete map[firstKey];
}
keys.push(key); // 当前元素的下标
}
map[key] = value; // 覆盖存在的旧对象
};
LimitableMap.prototype.p_set = function(key, value) {
var map = this.p_map;
var keys = this.p_keys;
if (!hasOwnProperty.call(map, key)) {
keys.push(key); // 当前元素的下标
}
map[key] = value; // 覆盖存在的旧对象
};
LimitableMap.prototype.get = function(key) {
return this.map[key];
};
LimitableMap.prototype.p_get = function(key) {
return this.p_map[key];
};
LimitableMap.prototype.del = LimitableMap.prototype.remove = function(key) {
var map = this.map;
var keys = this.keys;
if (hasOwnProperty.call(map, key)) { // map.hasOwnProperty(key); {key: value}
// 根据key删除,找到位子删除信息
keys.filter(function(item, index, array) {
if (key === item) {
array.splice(index, 1);
}
});
delete map[key];
}
};
LimitableMap.prototype.p_del = LimitableMap.prototype.p_remove = function(key) {
var map = this.p_map;
var keys = this.p_keys;
if (hasOwnProperty.call(map, key)) { // map.hasOwnProperty(key); {key: value}
// 根据key删除,找到位子删除信息
keys.filter(function(item, index, array) {
if (key === item) {
array.splice(index, 1);
}
});
delete map[key];
}
};
LimitableMap.prototype.length = LimitableMap.prototype.size = function() {
return this.keys.length;
};
LimitableMap.prototype.p_length = LimitableMap.prototype.p_size = function() {
return this.p_keys.length;
};
exports = module.exports = LimitableMap;