-
Notifications
You must be signed in to change notification settings - Fork 0
/
lfu.ts
165 lines (128 loc) · 3.85 KB
/
lfu.ts
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.
import { assertNonNegativeNumber, EmplaceableMap } from "./deps.ts";
import { Msg } from "./constants.ts";
import { BaseSet } from "./utils.ts";
import type { MapLike } from "./types.ts";
const INIT = 1;
class Container<T> {
value: T;
count = INIT;
constructor(value: T) {
this.value = value;
}
inc(): number {
return ++this.count;
}
}
/** `Map` with an upper limit, objects like. When the upper limit is reached, replaces the entry with LFU algorithm.
* @example
* ```ts
* import { LFUMap } from "https://deno.land/x/cache_mapset/lfu.ts";
*
* declare const maxNumOfEntries: number;
* const map = new LFUMap(maxNumOfEntries);
* ```
*/
export class LFUMap<K, V> implements MapLike<K, V> {
#values: Map<K, Container<V>> = new Map();
#frequency: EmplaceableMap<number, Set<K>>;
#minFrequency = INIT;
#capacity: number;
/**
* @throws {RangeError} If {@link maxNumOfEntries} is invalid range.
*/
constructor(
maxNumOfEntries: number,
entries?: Readonly<Iterable<readonly [K, V]>>,
) {
assertNonNegativeNumber(maxNumOfEntries, Msg.InvalidCapacity, RangeError);
const capacity = Math.floor(maxNumOfEntries);
this.#frequency = new EmplaceableMap();
this.#capacity = capacity;
if (entries) for (const [key, value] of entries) this.set(key, value);
}
has(key: K): boolean {
return this.#values.has(key);
}
get(key: K): V | undefined {
if (!this.#values.has(key)) return;
this.#updateFrequency(key);
return this.#values.get(key)!.value;
}
set(key: K, value: V): this {
if (!this.#capacity) return this;
if (this.#values.has(key)) {
const container = this.#values.get(key)!;
container.value = value;
this.#updateFrequency(key);
return this;
}
if (this.#values.size >= this.#capacity) {
this.#evict();
}
this.#values.set(key, new Container(value));
this.#minFrequency = INIT;
this.#frequency.emplace(this.#minFrequency, {
insert: () => new Set<K>([key]),
update: (existing) => existing.add(key),
});
return this;
}
delete(key: K): boolean {
if (!this.#values.has(key)) return false;
const container = this.#values.get(key)!;
this.#values.delete(key);
this.#frequency.get(container.count)!.delete(key);
return true;
}
clear(): void {
this.#values.clear();
this.#frequency.clear();
this.#minFrequency = INIT;
}
#evict(): void {
const keys = this.#frequency.get(this.#minFrequency)!;
const key = keys.keys().next().value;
keys.delete(key);
this.#values.delete(key);
}
#updateFrequency(key: K): void {
const container = this.#values.get(key)!;
const count = container.count;
const set = this.#frequency.get(count)!;
container.inc();
set.delete(key);
if (!set.size) this.#frequency.delete(count);
if (count === this.#minFrequency && !set.size) {
this.#minFrequency++;
}
this.#frequency.emplace(count + 1, {
insert: () => new Set<K>([key]),
update: (existing) => existing.add(key),
});
}
get size(): number {
return this.#values.size;
}
}
/** `Set` with an upper limit, objects like. When the upper limit is reached, replaces the value with LFU algorithm.
* @example
* ```ts
* import { LFUSet } from "https://deno.land/x/cache_mapset/lfu.ts";
*
* declare const maxNumOfValues: number;
* const set = new LFUSet(maxNumOfValues);
* ```
*/
export class LFUSet<T> extends BaseSet<T> {
protected cache: LFUMap<T, void>;
/**
* @throws {RangeError} If {@link maxNumOfValues} is invalid range.
*/
constructor(maxNumOfValues: number, values?: Readonly<Iterable<T>>) {
super();
this.cache = new LFUMap(maxNumOfValues);
if (values) for (const el of values) this.add(el);
}
}