@quenty/lrucache 1.6.3 → 1.6.4-canary.11a5dcf.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/package.json +3 -3
- package/src/Shared/LRUCache.lua +146 -148
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [1.6.4-canary.11a5dcf.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/lrucache@1.6.3...@quenty/lrucache@1.6.4-canary.11a5dcf.0) (2025-05-10)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* Additional type checking updates ([05ba29a](https://github.com/Quenty/NevermoreEngine/commit/05ba29a03efc9f3feed74b34f1d9dfb237496214))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
6
17
|
## [1.6.3](https://github.com/Quenty/NevermoreEngine/compare/@quenty/lrucache@1.6.2...@quenty/lrucache@1.6.3) (2025-04-10)
|
|
7
18
|
|
|
8
19
|
**Note:** Version bump only for package @quenty/lrucache
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quenty/lrucache",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.4-canary.11a5dcf.0",
|
|
4
4
|
"description": "LRUCache library",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Roblox",
|
|
@@ -25,10 +25,10 @@
|
|
|
25
25
|
"Quenty"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@quenty/loader": "
|
|
28
|
+
"@quenty/loader": "10.8.4-canary.11a5dcf.0"
|
|
29
29
|
},
|
|
30
30
|
"publishConfig": {
|
|
31
31
|
"access": "public"
|
|
32
32
|
},
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "11a5dcf7d4c7a0bfbf3337e97d30e8346ea09d3f"
|
|
34
34
|
}
|
package/src/Shared/LRUCache.lua
CHANGED
|
@@ -29,153 +29,151 @@ local LRUCache = {}
|
|
|
29
29
|
LRUCache.ClassName = "LRUCache"
|
|
30
30
|
|
|
31
31
|
function LRUCache.new(maxSize: number, maxBytes: number?)
|
|
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
|
-
return setmetatable({}, mt)
|
|
32
|
+
assert(maxSize >= 1, "maxSize must be >= 1")
|
|
33
|
+
assert(not maxBytes or maxBytes >= 1, "maxBytes must be >= 1")
|
|
34
|
+
|
|
35
|
+
-- current size
|
|
36
|
+
local size = 0
|
|
37
|
+
local bytesUsed = 0
|
|
38
|
+
|
|
39
|
+
-- map is a hash map from keys to tuples
|
|
40
|
+
-- tuple: value, prev, next, key
|
|
41
|
+
-- prev and next are pointers to tuples
|
|
42
|
+
local map = {}
|
|
43
|
+
|
|
44
|
+
-- indices of tuple
|
|
45
|
+
local VALUE = 1
|
|
46
|
+
local PREV = 2
|
|
47
|
+
local NEXT = 3
|
|
48
|
+
local KEY = 4
|
|
49
|
+
local BYTES = 5
|
|
50
|
+
|
|
51
|
+
-- newest and oldest are ends of double-linked list
|
|
52
|
+
local newest = nil -- first
|
|
53
|
+
local oldest = nil -- last
|
|
54
|
+
|
|
55
|
+
local removed_tuple -- created in del(), removed in set()
|
|
56
|
+
|
|
57
|
+
-- remove a tuple from linked list
|
|
58
|
+
local function cut(tuple)
|
|
59
|
+
local tuple_prev = tuple[PREV]
|
|
60
|
+
local tuple_next = tuple[NEXT]
|
|
61
|
+
tuple[PREV] = nil
|
|
62
|
+
tuple[NEXT] = nil
|
|
63
|
+
if tuple_prev and tuple_next then
|
|
64
|
+
tuple_prev[NEXT] = tuple_next
|
|
65
|
+
tuple_next[PREV] = tuple_prev
|
|
66
|
+
elseif tuple_prev then
|
|
67
|
+
-- tuple is the oldest element
|
|
68
|
+
tuple_prev[NEXT] = nil
|
|
69
|
+
oldest = tuple_prev
|
|
70
|
+
elseif tuple_next then
|
|
71
|
+
-- tuple is the newest element
|
|
72
|
+
tuple_next[PREV] = nil
|
|
73
|
+
newest = tuple_next
|
|
74
|
+
else
|
|
75
|
+
-- tuple is the only element
|
|
76
|
+
newest = nil
|
|
77
|
+
oldest = nil
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
-- insert a tuple to the newest end
|
|
82
|
+
local function setNewest(tuple)
|
|
83
|
+
if not newest then
|
|
84
|
+
newest = tuple
|
|
85
|
+
oldest = tuple
|
|
86
|
+
else
|
|
87
|
+
tuple[NEXT] = newest
|
|
88
|
+
newest[PREV] = tuple
|
|
89
|
+
newest = tuple
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
local function del(key, tuple)
|
|
94
|
+
map[key] = nil
|
|
95
|
+
cut(tuple)
|
|
96
|
+
size = size - 1
|
|
97
|
+
bytesUsed = bytesUsed - (tuple[BYTES] or 0)
|
|
98
|
+
removed_tuple = tuple
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
-- removes elemenets to provide enough memory
|
|
102
|
+
-- returns last removed element or nil
|
|
103
|
+
local function makeFreeSpace(bytes)
|
|
104
|
+
while size + 1 > maxSize or (maxBytes and bytesUsed + bytes > maxBytes) do
|
|
105
|
+
assert(oldest, "not enough storage for cache")
|
|
106
|
+
del(oldest[KEY], oldest)
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
local function get(_, key)
|
|
111
|
+
local tuple = map[key]
|
|
112
|
+
if not tuple then
|
|
113
|
+
return nil
|
|
114
|
+
end
|
|
115
|
+
cut(tuple)
|
|
116
|
+
setNewest(tuple)
|
|
117
|
+
return tuple[VALUE]
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
local function set(_, key, value, bytes)
|
|
121
|
+
local tuple = map[key]
|
|
122
|
+
if tuple then
|
|
123
|
+
del(key, tuple)
|
|
124
|
+
end
|
|
125
|
+
if value ~= nil then
|
|
126
|
+
-- the value is not removed
|
|
127
|
+
bytes = maxBytes and (bytes or #value) or 0
|
|
128
|
+
makeFreeSpace(bytes)
|
|
129
|
+
local tuple1 = removed_tuple or {}
|
|
130
|
+
map[key] = tuple1
|
|
131
|
+
tuple1[VALUE] = value
|
|
132
|
+
tuple1[KEY] = key
|
|
133
|
+
tuple1[BYTES] = maxBytes and bytes
|
|
134
|
+
size = size + 1
|
|
135
|
+
bytesUsed = bytesUsed + bytes
|
|
136
|
+
setNewest(tuple1)
|
|
137
|
+
else
|
|
138
|
+
assert(key ~= nil, "Key may not be nil")
|
|
139
|
+
end
|
|
140
|
+
removed_tuple = nil
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
local function delete(_, key)
|
|
144
|
+
return set(nil, key, nil)
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
local function mynext(_, prev_key)
|
|
148
|
+
local tuple
|
|
149
|
+
if prev_key then
|
|
150
|
+
tuple = map[prev_key][NEXT]
|
|
151
|
+
else
|
|
152
|
+
tuple = newest
|
|
153
|
+
end
|
|
154
|
+
if tuple then
|
|
155
|
+
return tuple[KEY], tuple[VALUE]
|
|
156
|
+
else
|
|
157
|
+
return nil
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
-- returns iterator for keys and values
|
|
162
|
+
local function lru_pairs()
|
|
163
|
+
return mynext, nil, nil
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
local mt = {
|
|
167
|
+
__index = {
|
|
168
|
+
get = get,
|
|
169
|
+
set = set,
|
|
170
|
+
delete = delete,
|
|
171
|
+
pairs = lru_pairs,
|
|
172
|
+
},
|
|
173
|
+
__pairs = lru_pairs,
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return setmetatable({}, mt)
|
|
179
177
|
end
|
|
180
178
|
|
|
181
|
-
return LRUCache
|
|
179
|
+
return LRUCache
|