-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLFU.js
123 lines (89 loc) · 2.87 KB
/
LFU.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const DoubleListHash = require('./structure/DoubleListHash')
class Cache_LFU {
constructor(cap = 50) {
this.cap = cap
this.kvfMap = new Map()
this.fkMpList = new Map()//new DoubleListHash()
this.minFrq = 1 //记录最小使用次数
}
get(key) {
if (this.kvfMap.has(key)) {
let val = this.kvfMap.get(key)
this._kufkMpList(val.frq, ++val.frq, key)
// val.frq < this.minFrq ? this.minFrq = val.frq : ""
return val.val
}
}
put(key, val = null) {
if (this.kvfMap.size >= this.cap) {
this._removeMinFrqVal() //这个只有在put的时候 之后 minFrq 就为1 ,minFrq 不用管
}
if (this.kvfMap.has(key)) {
//只要更新 val frq fkMpList
let val = this.kvfMap.get(key)
val.val = val
this._kufkMpList(val.frq, ++val.frq, key)
// val.frq < this.minFrq ? this.minFrq = val.frq : ""
return
}
this.kvfMap.set(key, { frq: 1, val: val })
let hashList = this._createfkMpList(1)
hashList.push(key)
this.minFrq = 1
}
get keys() {
return this.kvfMap.keys()
}
_testDebug() {
console.log('kvfMap', this.kvfMap.size)
console.log('fkMpList', this.fkMpList.size)
console.log('fkMpList', this.fkMpList.keys())
for (let k of this.fkMpList.keys()) {
console.log(`fkMpList-${k}`, this.fkMpList.get(k).size)
}
// console.log('kvfMap', this.kvfMap.values())
}
//删除次数最少的
_removeMinFrqVal() {
if (!this.fkMpList.has(this.minFrq)) {
console.error('its impossible no minFrq hashList')
return
}
let hashList = this.fkMpList.get(this.minFrq)
let x = hashList.shift()
// console.log(this.minFrq, x)
this.kvfMap.delete(x.key)
this._deleteNullfkMpList(this.minFrq)
}
//key 晋升
_kufkMpList(pre, now, key) {
let hashListNow = this._createfkMpList(now)
let hashListPre = this._createfkMpList(pre)
hashListPre.remove(key)
let c = this._deleteNullfkMpList(pre)
if (c && pre == this.minFrq) this.minFrq = now
hashListNow.push(key)
}
//创建 f - hashList (空链表)
_createfkMpList(f) {
if (this.fkMpList.has(f)) {
return this.fkMpList.get(f)
}
let hashList = new DoubleListHash()
this.fkMpList.set(f, hashList)
return hashList
}
//删除 f - hashList (空链表)
_deleteNullfkMpList(f) {
if (!this.fkMpList.has(f)) {
return
}
let hashList = this.fkMpList.get(f)
if (hashList.size != 0) {
return
}
this.fkMpList.delete(f)
return true
}
}
module.exports = Cache_LFU