-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
48 lines (45 loc) · 1.08 KB
/
index.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
"use strict";
var toPosInt = require("es5-ext/number/to-pos-integer");
var create = Object.create;
module.exports = function (limit) {
var size = 0, base = 1, queue = create(null), map = create(null), index = 0, del;
limit = toPosInt(limit);
return {
hit: function (id) {
var oldIndex = map[id], nuIndex = ++index;
queue[nuIndex] = id;
map[id] = nuIndex;
if (!oldIndex) {
++size;
if (size <= limit) return undefined;
id = queue[base];
del(id);
return id;
}
delete queue[oldIndex];
if (base !== oldIndex) return undefined;
while (!hasOwnProperty.call(queue, ++base)) continue; // jslint: ignore
return undefined;
},
delete: (del = function (id) {
var oldIndex = map[id];
if (!oldIndex) return;
delete queue[oldIndex];
delete map[id];
--size;
if (base !== oldIndex) return;
if (!size) {
index = 0;
base = 1;
return;
}
while (!hasOwnProperty.call(queue, ++base)) continue; // jslint: ignore
}),
clear: function () {
size = index = 0;
base = 1;
queue = create(null);
map = create(null);
}
};
};