Debian Codegraph @

memoizee/normalizers/get.js

raw
1 /* eslint max-statements: 0 */
2
3 "use strict";
4
5 var indexOf = require("es5-ext/array/#/e-index-of");
6
7 var create = Object.create;
8
9 module.exports = function () {
10 var lastId = 0, map = [], cache = create(null);
11 return {
12 get: function (args) {
13 var index = 0, set = map, i, length = args.length;
14 if (length === 0) return set[length] || null;
15 if ((set = set[length])) {
16 while (index < length - 1) {
17 i = indexOf.call(set[0], args[index]);
18 if (i === -1) return null;
19 set = set[1][i];
20 ++index;
21 }
22 i = indexOf.call(set[0], args[index]);
23 if (i === -1) return null;
24 return set[1][i] || null;
25 }
26 return null;
27 },
28 set: function (args) {
29 var index = 0, set = map, i, length = args.length;
30 if (length === 0) {
31 set[length] = ++lastId;
32 } else {
33 if (!set[length]) {
34 set[length] = [[], []];
35 }
36 set = set[length];
37 while (index < length - 1) {
38 i = indexOf.call(set[0], args[index]);
39 if (i === -1) {
40 i = set[0].push(args[index]) - 1;
41 set[1].push([[], []]);
42 }
43 set = set[1][i];
44 ++index;
45 }
46 i = indexOf.call(set[0], args[index]);
47 if (i === -1) {
48 i = set[0].push(args[index]) - 1;
49 }
50 set[1][i] = ++lastId;
51 }
52 cache[lastId] = args;
53 return lastId;
54 },
55 delete: function (id) {
56 var index = 0, set = map, i, args = cache[id], length = args.length, path = [];
57 if (length === 0) {
58 delete set[length];
59 } else if ((set = set[length])) {
60 while (index < length - 1) {
61 i = indexOf.call(set[0], args[index]);
62 if (i === -1) {
63 return;
64 }
65 path.push(set, i);
66 set = set[1][i];
67 ++index;
68 }
69 i = indexOf.call(set[0], args[index]);
70 if (i === -1) {
71 return;
72 }
73 id = set[1][i];
74 set[0].splice(i, 1);
75 set[1].splice(i, 1);
76 while (!set[0].length && path.length) {
77 i = path.pop();
78 set = path.pop();
79 set[0].splice(i, 1);
80 set[1].splice(i, 1);
81 }
82 }
83 delete cache[id];
84 },
85 clear: function () {
86 map = [];
87 cache = create(null);
88 },
89 };
90 };