mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-10 02:31:48 +00:00
[modified] split sequence and unique_mapping to their own files
[added] async to init
This commit is contained in:
parent
8a582f8c66
commit
6e7cabe9e0
117
functional.lua
117
functional.lua
@ -273,120 +273,3 @@ function table.find_match(t, f)
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
-----------------------------------------------------------
|
||||
--sequence - functional wrapper for ordered tables
|
||||
-----------------------------------------------------------
|
||||
sequence = {}
|
||||
|
||||
sequence.mt = {__index = sequence}
|
||||
|
||||
--proxy missing table fns to table
|
||||
sequence._mt = {__index = table}
|
||||
setmetatable(sequence, sequence._mt)
|
||||
|
||||
--upgrade a table into a functional sequence
|
||||
function sequence:new(t)
|
||||
return setmetatable(t or {}, sequence.mt)
|
||||
end
|
||||
|
||||
--import table functions to sequence as-is
|
||||
sequence.join = table.concat --alias
|
||||
|
||||
--sorting
|
||||
sequence.sort = table.stable_sort --(default stable)
|
||||
sequence.stable_sort = table.stable_sort
|
||||
sequence.unstable_sort = table.sort
|
||||
|
||||
--import functional interface to sequence in a sequence preserving way
|
||||
function sequence:keys()
|
||||
return sequence:new(table.keys(self))
|
||||
end
|
||||
|
||||
function sequence:values()
|
||||
return sequence:new(table.values(self))
|
||||
end
|
||||
|
||||
function sequence:foreach(f)
|
||||
return table.foreach(self, f)
|
||||
end
|
||||
|
||||
function sequence:reduce(f, o)
|
||||
return table.foreach(self, f, o)
|
||||
end
|
||||
|
||||
function sequence:map(f)
|
||||
return sequence:new(table.map(self, f))
|
||||
end
|
||||
|
||||
function sequence:filter(f)
|
||||
return sequence:new(table.filter(self, f))
|
||||
end
|
||||
|
||||
function sequence:partition(f)
|
||||
local a, b = table.partition(self, f)
|
||||
return sequence:new(a), sequence:new(b)
|
||||
end
|
||||
|
||||
function sequence:zip(other, f)
|
||||
return sequence:new(table.zip(self, other, f))
|
||||
end
|
||||
|
||||
function sequence:dedupe()
|
||||
return table.dedupe(self)
|
||||
end
|
||||
|
||||
function sequence:append_inplace(other)
|
||||
return table.append_inplace(self, other)
|
||||
end
|
||||
|
||||
function sequence:append(other)
|
||||
return sequence:new():append_inplace(self):append_inplace(other)
|
||||
end
|
||||
|
||||
function sequence:copy(deep)
|
||||
return sequence:new(table.copy(self, deep))
|
||||
end
|
||||
|
||||
--generate a mapping from unique values to plain numbers
|
||||
--useful for arbitrarily ordering things that don't have
|
||||
--a natural ordering implied (eg textures for batching)
|
||||
|
||||
unique_mapping = {}
|
||||
unique_mapping.mt = {
|
||||
__index = unique_mapping,
|
||||
__mode = "kv", --weak refs
|
||||
}
|
||||
|
||||
--(used as storage for non-weak data)
|
||||
local _MAP_VARS = setmetatable({}, {
|
||||
__mode = "k" --only keys are weak
|
||||
})
|
||||
|
||||
function unique_mapping:new()
|
||||
local r = setmetatable({}, unique_mapping.mt)
|
||||
--set up the actual vars
|
||||
_MAP_VARS[r] = {
|
||||
current_index = 0,
|
||||
}
|
||||
return r
|
||||
end
|
||||
|
||||
function unique_mapping:_increment()
|
||||
local vars = _MAP_VARS[self]
|
||||
vars.current_index = vars.current_index + 1
|
||||
return vars.current_index
|
||||
end
|
||||
|
||||
function unique_mapping:map(value)
|
||||
local val = self[value]
|
||||
if val then
|
||||
return val
|
||||
end
|
||||
local i = self:_increment()
|
||||
self[value] = i
|
||||
return i
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
4
init.lua
4
init.lua
@ -21,8 +21,12 @@ require(relative_file("table"))
|
||||
require(relative_file("stable_sort"))
|
||||
|
||||
require(relative_file("functional"))
|
||||
sequence = require(relative_file("sequence"))
|
||||
unique_mapping = require(relative_file("unique_mapping"))
|
||||
|
||||
vec2 = require(relative_file("vec2"))
|
||||
intersect = require(relative_file("intersect"))
|
||||
|
||||
state_machine = require(relative_file("state_machine"))
|
||||
|
||||
async = require(relative_file("async"))
|
||||
|
79
sequence.lua
Normal file
79
sequence.lua
Normal file
@ -0,0 +1,79 @@
|
||||
--[[
|
||||
sequence - functional + oo wrapper for ordered tables
|
||||
|
||||
sort of depends on functional.lua but can be used without it, will just
|
||||
crash if you call the functional interface
|
||||
|
||||
in that case, you can still use the table methods that accept a table
|
||||
first as method calls.
|
||||
]]
|
||||
local sequence = {}
|
||||
|
||||
sequence.mt = {__index = sequence}
|
||||
|
||||
--proxy missing table fns to table
|
||||
sequence._mt = {__index = table}
|
||||
setmetatable(sequence, sequence._mt)
|
||||
|
||||
--upgrade a table into a functional sequence
|
||||
function sequence:new(t)
|
||||
return setmetatable(t or {}, sequence.mt)
|
||||
end
|
||||
|
||||
--import table functions to sequence as-is
|
||||
sequence.join = table.concat --alias
|
||||
|
||||
--sorting default to stable if present
|
||||
sequence.sort = table.stable_sort or table.sort
|
||||
|
||||
--import functional interface to sequence in a sequence preserving way
|
||||
function sequence:keys()
|
||||
return sequence:new(table.keys(self))
|
||||
end
|
||||
|
||||
function sequence:values()
|
||||
return sequence:new(table.values(self))
|
||||
end
|
||||
|
||||
function sequence:foreach(f)
|
||||
return table.foreach(self, f)
|
||||
end
|
||||
|
||||
function sequence:reduce(f, o)
|
||||
return table.foreach(self, f, o)
|
||||
end
|
||||
|
||||
function sequence:map(f)
|
||||
return sequence:new(table.map(self, f))
|
||||
end
|
||||
|
||||
function sequence:filter(f)
|
||||
return sequence:new(table.filter(self, f))
|
||||
end
|
||||
|
||||
function sequence:partition(f)
|
||||
local a, b = table.partition(self, f)
|
||||
return sequence:new(a), sequence:new(b)
|
||||
end
|
||||
|
||||
function sequence:zip(other, f)
|
||||
return sequence:new(table.zip(self, other, f))
|
||||
end
|
||||
|
||||
function sequence:dedupe()
|
||||
return table.dedupe(self)
|
||||
end
|
||||
|
||||
function sequence:append_inplace(other)
|
||||
return table.append_inplace(self, other)
|
||||
end
|
||||
|
||||
function sequence:append(other)
|
||||
return sequence:new():append_inplace(self):append_inplace(other)
|
||||
end
|
||||
|
||||
function sequence:copy(deep)
|
||||
return sequence:new(table.copy(self, deep))
|
||||
end
|
||||
|
||||
return sequence
|
41
unique_mapping.lua
Normal file
41
unique_mapping.lua
Normal file
@ -0,0 +1,41 @@
|
||||
--generate a mapping from unique values to plain numbers
|
||||
--useful for arbitrarily ordering things that don't have
|
||||
--a natural ordering implied (eg textures for batching)
|
||||
|
||||
local unique_mapping = {}
|
||||
unique_mapping.mt = {
|
||||
__index = unique_mapping,
|
||||
__mode = "kv", --weak refs
|
||||
}
|
||||
|
||||
--(used as storage for non-weak data)
|
||||
local _MAP_VARS = setmetatable({}, {
|
||||
__mode = "k" --only keys are weak
|
||||
})
|
||||
|
||||
function unique_mapping:new()
|
||||
local r = setmetatable({}, unique_mapping.mt)
|
||||
--set up the actual vars
|
||||
_MAP_VARS[r] = {
|
||||
current_index = 0,
|
||||
}
|
||||
return r
|
||||
end
|
||||
|
||||
function unique_mapping:_increment()
|
||||
local vars = _MAP_VARS[self]
|
||||
vars.current_index = vars.current_index + 1
|
||||
return vars.current_index
|
||||
end
|
||||
|
||||
function unique_mapping:map(value)
|
||||
local val = self[value]
|
||||
if val then
|
||||
return val
|
||||
end
|
||||
local i = self:_increment()
|
||||
self[value] = i
|
||||
return i
|
||||
end
|
||||
|
||||
return unique_mapping
|
Loading…
Reference in New Issue
Block a user