@quenty/memoize 1.2.0-canary.478.211e09e.0 → 1.2.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 +1 -1
- package/package.json +5 -4
- package/src/Shared/MemorizeUtils.lua +12 -44
package/CHANGELOG.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
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.2.0
|
|
6
|
+
# [1.2.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/memoize@1.1.0...@quenty/memoize@1.2.0) (2024-09-12)
|
|
7
7
|
|
|
8
8
|
**Note:** Version bump only for package @quenty/memoize
|
|
9
9
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quenty/memoize",
|
|
3
|
-
"version": "1.2.0
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Memoization library",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Roblox",
|
|
@@ -25,11 +25,12 @@
|
|
|
25
25
|
"Quenty"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@quenty/loader": "10.4.0
|
|
29
|
-
"@quenty/lrucache": "1.2.0
|
|
28
|
+
"@quenty/loader": "^10.4.0",
|
|
29
|
+
"@quenty/lrucache": "^1.2.0",
|
|
30
|
+
"@quenty/tuple": "^1.1.0"
|
|
30
31
|
},
|
|
31
32
|
"publishConfig": {
|
|
32
33
|
"access": "public"
|
|
33
34
|
},
|
|
34
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "fb172906f3ee725269ec1e5f4daf9dca227e729d"
|
|
35
36
|
}
|
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
local require = require(script.Parent.loader).load(script)
|
|
6
6
|
|
|
7
7
|
local LRUCache = require("LRUCache")
|
|
8
|
+
local Tuple = require("Tuple")
|
|
9
|
+
local TupleLookup = require("TupleLookup")
|
|
8
10
|
|
|
9
11
|
local MemorizeUtils = {}
|
|
10
12
|
|
|
@@ -20,18 +22,21 @@ function MemorizeUtils.memoize(func, cacheConfig)
|
|
|
20
22
|
|
|
21
23
|
cacheConfig = cacheConfig or MemorizeUtils.createCacheConfig()
|
|
22
24
|
|
|
23
|
-
local
|
|
25
|
+
local tupleLookup = TupleLookup.new()
|
|
26
|
+
local cache = LRUCache.new(cacheConfig.maxSize)
|
|
24
27
|
|
|
25
28
|
return function(...)
|
|
26
|
-
local params =
|
|
29
|
+
local params = tupleLookup:ToTuple(...)
|
|
27
30
|
|
|
28
|
-
local
|
|
29
|
-
if
|
|
30
|
-
|
|
31
|
-
MemorizeUtils._cache_put(cache, params, results, cacheConfig)
|
|
31
|
+
local found = cache:get(params)
|
|
32
|
+
if found then
|
|
33
|
+
return found:Unpack()
|
|
32
34
|
end
|
|
33
35
|
|
|
34
|
-
|
|
36
|
+
local result = Tuple.new(func(...))
|
|
37
|
+
cache:set(params, result)
|
|
38
|
+
|
|
39
|
+
return result:Unpack()
|
|
35
40
|
end
|
|
36
41
|
end
|
|
37
42
|
|
|
@@ -61,41 +66,4 @@ function MemorizeUtils.createCacheConfig(cacheConfig)
|
|
|
61
66
|
}
|
|
62
67
|
end
|
|
63
68
|
|
|
64
|
-
function MemorizeUtils._createCacheNode(cacheConfig)
|
|
65
|
-
return {
|
|
66
|
-
childrenLRUCache = LRUCache.new(cacheConfig.maxSize);
|
|
67
|
-
}
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
function MemorizeUtils._cache_get(cache, params)
|
|
71
|
-
local node = cache
|
|
72
|
-
for i=1, #params do
|
|
73
|
-
node = node.childrenLRUCache:get(params[i])
|
|
74
|
-
if not node then
|
|
75
|
-
return nil
|
|
76
|
-
end
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
return node.results
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
function MemorizeUtils._cache_put(cache, params, results, cacheConfig)
|
|
83
|
-
local node = cache
|
|
84
|
-
|
|
85
|
-
for i=1, params.n do
|
|
86
|
-
local param = params[i]
|
|
87
|
-
|
|
88
|
-
local paramNode = node.childrenLRUCache:get(param)
|
|
89
|
-
if paramNode then
|
|
90
|
-
node = paramNode
|
|
91
|
-
else
|
|
92
|
-
paramNode = MemorizeUtils._createCacheNode(cacheConfig)
|
|
93
|
-
node.childrenLRUCache:set(param, paramNode)
|
|
94
|
-
node = paramNode
|
|
95
|
-
end
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
node.results = results
|
|
99
|
-
end
|
|
100
|
-
|
|
101
69
|
return MemorizeUtils
|