-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy path9-count.js
68 lines (63 loc) · 1.65 KB
/
9-count.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
61
62
63
64
65
66
67
68
'use strict';
const transKey = key => key.toString() + '(' + typeof(key) + ')';
const createKey = keys => keys.map(key => transKey(key)).join('|');
const memoizeCount = (
// Memoize the results of the fucntion
fn, //Function to memoize
length = 0 //Max record in the cache, if not specify records are unlimited
//Returns: memoized function with max records count and removing least used
) => {
const cache = new Map();
return (...args) => {
const key = createKey(args);
if (cache.has(key)) {
const record = cache.get(key);
console.log(
`From cache:, ${fn.name}(${(args.join(', '))}) ${record.res}`
);
record.count += 1;
return record.res;
}
console.log(`Calculate: ${fn.name}(${args.join(', ')})`);
const res = fn(...args);
if (length && cache.size >= length) {
let minUsage = length;
let minKey;
for (const [key, value] of cache.entries()) {
if (value.count < minUsage) {
minKey = key;
minUsage = value.count;
}
}
console.log(`Delete element: ${minKey}: ${cache.get(minKey).count}`);
cache.delete(minKey);
}
cache.set(key, { res, count: 0 });
return res;
};
};
//Test
const fib = n => (n <= 2 ? 1 : fib(n - 1) + fib(n - 2));
const memoCounted = memoizeCount(fib, 7);
memoCounted(2);
memoCounted(5);
memoCounted(5);
memoCounted(6);
memoCounted(7);
memoCounted(7);
memoCounted(2);
memoCounted(9);
memoCounted(6);
memoCounted(2);
memoCounted(9);
memoCounted(2);
memoCounted(15);
memoCounted(3);
memoCounted(15);
memoCounted(3);
memoCounted(10);
memoCounted(10);
memoCounted(19);
memoCounted(18);
memoCounted(19);
memoCounted(10);