Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function memoizify(memoizified, persistence)
- persistence = type(persistence) == "number" and persistence or 256
- local cache = {}
- return function(...)
- local inargs = {...}
- local outargs
- local useless = {}
- for key, value in pairs(cache) do
- local difference = false
- for ix = 1, #key do
- if key[ix] ~= inargs[ix] then
- difference = true
- break
- end
- end
- if difference then
- if persistence > 0 then
- key[0] = key[0] - 1
- if key[0] == 0 then useless[#useless + 1] = key end
- end
- else
- key[0] = persistence
- outargs = value
- end
- end
- for ix = 1, #useless do
- cache[useless[ix]] = nil
- end
- if not outargs then
- outargs = {memoizified(...)}
- inargs[0] = persistence
- cache[inargs] = outargs
- end
- return unpack(outargs)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement