-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbounded_map.v
More file actions
344 lines (310 loc) · 7.17 KB
/
bounded_map.v
File metadata and controls
344 lines (310 loc) · 7.17 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
module gui
// BoundedMap is a map with maximum size. When full, oldest entries are
// evicted (FIFO). Used to prevent unbounded memory growth in view state.
struct BoundedMap[K, V] {
mut:
data map[K]V
order []K
head int
max_size int = 100
}
const bounded_order_compact_min = 64
// set adds or updates key-value pair. Evicts oldest if at capacity.
fn (mut m BoundedMap[K, V]) set(key K, value V) {
if m.max_size < 1 {
return
}
if key in m.data {
m.data[key] = value
return
}
if m.data.len >= m.max_size && m.order.len > m.head {
for m.head < m.order.len {
oldest_key := m.order[m.head]
m.head++
if oldest_key in m.data {
m.data.delete(oldest_key)
break
}
}
}
m.order << key
m.data[key] = value
m.compact_order()
}
// get returns value for key, or none if not found.
fn (m &BoundedMap[K, V]) get(key K) ?V {
return m.data[key] or { return none }
}
// delete removes key from map.
fn (mut m BoundedMap[K, V]) delete(key K) {
if key !in m.data {
return
}
m.data.delete(key)
if m.data.len == 0 {
array_clear(mut m.order)
m.head = 0
return
}
m.compact_order()
}
// contains returns true if key exists in map.
fn (m &BoundedMap[K, V]) contains(key K) bool {
return key in m.data
}
// len returns number of entries in map.
fn (m &BoundedMap[K, V]) len() int {
return m.data.len
}
// clear removes all entries from map.
fn (mut m BoundedMap[K, V]) clear() {
m.data.clear()
array_clear(mut m.order)
m.head = 0
}
// keys returns all keys in insertion order.
fn (m &BoundedMap[K, V]) keys() []K {
if m.data.len == 0 || m.head >= m.order.len {
return []K{}
}
mut out := []K{cap: m.data.len}
for i in m.head .. m.order.len {
k := m.order[i]
if k in m.data {
out << k
}
}
return out
}
fn (mut m BoundedMap[K, V]) compact_order() {
if m.head <= 0 {
return
}
if m.head < bounded_order_compact_min && m.head * 2 < m.order.len {
return
}
mut compact := []K{cap: m.data.len}
for i in m.head .. m.order.len {
k := m.order[i]
if k in m.data {
compact << k
}
}
m.order = compact
m.head = 0
}
// BoundedTreeState is a specialized bounded map for tree state
// (string -> map[string]bool). Maps require clone() when stored,
// which generic BoundedMap can't handle.
struct BoundedTreeState {
mut:
data map[string]map[string]bool
order []string
head int
max_size int = 30
}
// set adds or updates tree state. Evicts oldest if at capacity.
fn (mut m BoundedTreeState) set(key string, value map[string]bool) {
if m.max_size < 1 {
return
}
if key in m.data {
m.data[key] = value.clone()
return
}
if m.data.len >= m.max_size && m.order.len > m.head {
for m.head < m.order.len {
oldest_key := m.order[m.head]
m.head++
if oldest_key in m.data {
m.data.delete(oldest_key)
break
}
}
}
m.order << key
m.data[key] = value.clone()
m.compact_order()
}
// get returns tree state for key, or none if not found.
fn (m &BoundedTreeState) get(key string) ?map[string]bool {
return m.data[key] or { return none }
}
// contains returns true if key exists.
fn (m &BoundedTreeState) contains(key string) bool {
return key in m.data
}
// len returns number of entries.
fn (m &BoundedTreeState) len() int {
return m.data.len
}
// clear removes all entries.
fn (mut m BoundedTreeState) clear() {
m.data.clear()
array_clear(mut m.order)
m.head = 0
}
fn (mut m BoundedTreeState) compact_order() {
if m.head <= 0 {
return
}
if m.head < bounded_order_compact_min && m.head * 2 < m.order.len {
return
}
mut compact := []string{cap: m.data.len}
for i in m.head .. m.order.len {
key := m.order[i]
if key in m.data {
compact << key
}
}
m.order = compact
m.head = 0
}
// BoundedSvgCache is an LRU cache for SVG data.
// Uses lazy LRU via access counter for O(1) operations.
// Evicts least-recently-accessed entry when at capacity.
struct BoundedSvgCache {
mut:
data map[string]&CachedSvg
access_time map[string]u64 // Last access timestamp for LRU
access_count u64 // Monotonic counter
max_size int = 100
}
// get returns cached SVG and updates access time (O(1) LRU).
fn (mut m BoundedSvgCache) get(key string) ?&CachedSvg {
if key in m.data {
// Update access time - O(1) instead of O(n) index rebuild
m.access_count++
m.access_time[key] = m.access_count
return m.data[key] or { return none }
}
return none
}
// set adds SVG to cache. Evicts LRU entry if at capacity (O(n) scan only when full).
fn (mut m BoundedSvgCache) set(key string, value &CachedSvg) {
if m.max_size < 1 {
return
}
// Update existing entry
if key in m.data {
m.access_count++
m.access_time[key] = m.access_count
unsafe {
m.data[key] = value
}
return
}
// Need to add new entry - evict LRU if at capacity
if m.data.len >= m.max_size && m.max_size > 0 {
// Find entry with oldest access time - O(n) but only when cache full
mut oldest_key := ''
mut oldest_time := m.access_count + 1
for k, t in m.access_time {
if t < oldest_time {
oldest_time = t
oldest_key = k
}
}
if oldest_key.len > 0 {
m.data.delete(oldest_key)
m.access_time.delete(oldest_key)
}
}
// Add new entry
m.access_count++
m.access_time[key] = m.access_count
unsafe {
m.data[key] = value
}
}
// delete removes key from cache (O(1)).
fn (mut m BoundedSvgCache) delete(key string) {
m.data.delete(key)
m.access_time.delete(key)
}
// contains returns true if key exists.
fn (m &BoundedSvgCache) contains(key string) bool {
return key in m.data
}
// keys returns all keys (no specific order).
fn (m &BoundedSvgCache) keys() []string {
mut result := []string{cap: m.data.len}
for k in m.data.keys() {
result << k
}
return result
}
// len returns number of entries.
fn (m &BoundedSvgCache) len() int {
return m.data.len
}
// clear removes all entries.
fn (mut m BoundedSvgCache) clear() {
m.data.clear()
m.access_time.clear()
m.access_count = 0
}
// BoundedMarkdownCache is a FIFO cache for parsed markdown blocks.
struct BoundedMarkdownCache {
mut:
data map[int][]MarkdownBlock
order []int
head int
max_size int = 50
}
// get returns cached blocks.
fn (m &BoundedMarkdownCache) get(key int) ?[]MarkdownBlock {
return m.data[key] or { return none }
}
// set adds blocks to cache. Evicts oldest if at capacity.
fn (mut m BoundedMarkdownCache) set(key int, value []MarkdownBlock) {
if m.max_size < 1 {
return
}
if key in m.data {
m.data[key] = value.clone()
return
}
if m.data.len >= m.max_size && m.order.len > m.head {
for m.head < m.order.len {
oldest_key := m.order[m.head]
m.head++
if oldest_key in m.data {
m.data.delete(oldest_key)
break
}
}
}
m.order << key
m.data[key] = value.clone()
m.compact_order()
}
// len returns number of entries.
fn (m &BoundedMarkdownCache) len() int {
return m.data.len
}
// clear removes all entries.
fn (mut m BoundedMarkdownCache) clear() {
m.data.clear()
array_clear(mut m.order)
m.head = 0
}
fn (mut m BoundedMarkdownCache) compact_order() {
if m.head <= 0 {
return
}
if m.head < bounded_order_compact_min && m.head * 2 < m.order.len {
return
}
mut compact := []int{cap: m.data.len}
for i in m.head .. m.order.len {
key := m.order[i]
if key in m.data {
compact << key
}
}
m.order = compact
m.head = 0
}