@quenty/memoize 1.0.1-canary.468.ea8a226.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/LICENSE.md +21 -0
- package/README.md +23 -0
- package/default.project.json +7 -0
- package/package.json +35 -0
- package/src/Shared/MemorizeUtils.lua +101 -0
- package/src/node_modules.project.json +7 -0
- package/test/default.project.json +11 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
## 1.0.1-canary.468.ea8a226.0 (2024-05-09)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* Add memoize package ([30d4769](https://github.com/Quenty/NevermoreEngine/commit/30d476958d465763e4f97a61a3796099e0a0988f))
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2014-2024 James Onnen (Quenty)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
## Memoize
|
|
2
|
+
|
|
3
|
+
<div align="center">
|
|
4
|
+
<a href="http://quenty.github.io/NevermoreEngine/">
|
|
5
|
+
<img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/docs.yml/badge.svg" alt="Documentation status" />
|
|
6
|
+
</a>
|
|
7
|
+
<a href="https://discord.gg/mhtGUS8">
|
|
8
|
+
<img src="https://img.shields.io/discord/385151591524597761?color=5865F2&label=discord&logo=discord&logoColor=white" alt="Discord" />
|
|
9
|
+
</a>
|
|
10
|
+
<a href="https://github.com/Quenty/NevermoreEngine/actions">
|
|
11
|
+
<img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/build.yml/badge.svg" alt="Build and release status" />
|
|
12
|
+
</a>
|
|
13
|
+
</div>
|
|
14
|
+
|
|
15
|
+
Memoization library
|
|
16
|
+
|
|
17
|
+
<div align="center"><a href="https://quenty.github.io/NevermoreEngine/api/MemoizeUtils">View docs →</a></div>
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
npm install @quenty/memoize --save
|
|
23
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@quenty/memoize",
|
|
3
|
+
"version": "1.0.1-canary.468.ea8a226.0",
|
|
4
|
+
"description": "Memoization library",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"Roblox",
|
|
7
|
+
"Nevermore",
|
|
8
|
+
"Lua",
|
|
9
|
+
"memoize"
|
|
10
|
+
],
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/Quenty/NevermoreEngine/issues"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/Quenty/NevermoreEngine.git",
|
|
17
|
+
"directory": "src/memoize/"
|
|
18
|
+
},
|
|
19
|
+
"funding": {
|
|
20
|
+
"type": "patreon",
|
|
21
|
+
"url": "https://www.patreon.com/quenty"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"contributors": [
|
|
25
|
+
"Quenty"
|
|
26
|
+
],
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@quenty/loader": "10.2.1-canary.468.ea8a226.0",
|
|
29
|
+
"@quenty/lrucache": "1.0.1-canary.468.ea8a226.0"
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"gitHead": "ea8a2260a364aaf6d6c246a93632b789935da134"
|
|
35
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
--[=[
|
|
2
|
+
@class MemorizeUtils
|
|
3
|
+
]=]
|
|
4
|
+
|
|
5
|
+
local require = require(script.Parent.loader).load(script)
|
|
6
|
+
|
|
7
|
+
local LRUCache = require("LRUCache")
|
|
8
|
+
|
|
9
|
+
local MemorizeUtils = {}
|
|
10
|
+
|
|
11
|
+
--[=[
|
|
12
|
+
Memoizes a function with a max size
|
|
13
|
+
|
|
14
|
+
@param func function
|
|
15
|
+
@param cacheConfig CacheConfig
|
|
16
|
+
]=]
|
|
17
|
+
function MemorizeUtils.memoize(func, cacheConfig)
|
|
18
|
+
assert(type(func) == "function", "Bad func")
|
|
19
|
+
assert(MemorizeUtils.isCacheConfig(cacheConfig) or cacheConfig == nil, "Bad cacheConfig")
|
|
20
|
+
|
|
21
|
+
cacheConfig = cacheConfig or MemorizeUtils.createCacheConfig()
|
|
22
|
+
|
|
23
|
+
local cache = MemorizeUtils._createCacheNode(cacheConfig)
|
|
24
|
+
|
|
25
|
+
return function(...)
|
|
26
|
+
local params = table.pack(...)
|
|
27
|
+
|
|
28
|
+
local results = MemorizeUtils._cache_get(cache, params)
|
|
29
|
+
if not results then
|
|
30
|
+
results = table.pack(func(...))
|
|
31
|
+
MemorizeUtils._cache_put(cache, params, results, cacheConfig)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
return unpack(results, 1, results.n)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
--[=[
|
|
39
|
+
Returns true if a valid cache config
|
|
40
|
+
|
|
41
|
+
@param value any
|
|
42
|
+
@return boolean
|
|
43
|
+
]=]
|
|
44
|
+
function MemorizeUtils.isCacheConfig(value)
|
|
45
|
+
return type(value) == "table" and type(value.maxSize) == "number"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
--[=[
|
|
49
|
+
Creates a new cache config
|
|
50
|
+
|
|
51
|
+
@param cacheConfig table | nil
|
|
52
|
+
@return CacheConfig
|
|
53
|
+
]=]
|
|
54
|
+
function MemorizeUtils.createCacheConfig(cacheConfig)
|
|
55
|
+
assert(MemorizeUtils.isCacheConfig(cacheConfig) or cacheConfig == nil, "Bad cacheConfig")
|
|
56
|
+
|
|
57
|
+
cacheConfig = cacheConfig or {}
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
maxSize = cacheConfig.maxSize or 128;
|
|
61
|
+
}
|
|
62
|
+
end
|
|
63
|
+
|
|
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
|
+
return MemorizeUtils
|