mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-21 21:54:35 +00:00
moved unique_mapping to ferris
This commit is contained in:
parent
844c9e8c8f
commit
0d88061e3b
1
init.lua
1
init.lua
@ -33,7 +33,6 @@ local _batteries = {
|
||||
--
|
||||
timer = require_relative("timer"),
|
||||
pubsub = require_relative("pubsub"),
|
||||
unique_mapping = require_relative("unique_mapping"),
|
||||
state_machine = require_relative("state_machine"),
|
||||
async = require_relative("async"),
|
||||
manual_gc = require_relative("manual_gc"),
|
||||
|
@ -93,7 +93,6 @@ These modules are probably only useful to some folks in some circumstances, or a
|
||||
- [`colour`](./colour.lua) - Colour conversion routines. Alias `color`.
|
||||
- [`manual_gc`](./manual_gc.lua) - Get GC out of your update/draw calls. Useful when trying to get accurate profiling information; moves "randomness" of GC. Requires you to think a bit about your garbage budgets though.
|
||||
- [`measure`](./measure.lua) - Benchmarking helpers - measure the time or memory taken to run some code.
|
||||
- [`unique_mapping`](./unique_mapping.lua) - Generate a unique mapping from arbitrary lua values to numeric keys - essentially making up a consistent ordering for unordered data. Niche, but can be used to optimise draw batches for example, as you can't sort on textures without it.
|
||||
- [`make_pooled`](./make_pooled.lua) - add pooling/recycling capability to a class
|
||||
|
||||
Any aliases are provided at both the `batteries` module level, and globally when exported.
|
||||
|
@ -1,62 +0,0 @@
|
||||
--[[
|
||||
unique mapping
|
||||
|
||||
generate a mapping from unique values to plain numbers
|
||||
useful for arbitrarily ordering things that don't have
|
||||
a natural ordering in lua (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
|
||||
})
|
||||
|
||||
--create a new unique mapping
|
||||
function unique_mapping:new()
|
||||
local r = setmetatable({}, self._mt)
|
||||
--set up the actual vars
|
||||
_MAP_VARS[r] = {
|
||||
current_index = 0,
|
||||
}
|
||||
return r
|
||||
end
|
||||
|
||||
--private;
|
||||
--get the next index for this mapping
|
||||
function unique_mapping:_increment()
|
||||
local vars = _MAP_VARS[self]
|
||||
vars.current_index = vars.current_index + 1
|
||||
return vars.current_index
|
||||
end
|
||||
|
||||
--get or build a mapping for a passed value
|
||||
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
|
||||
|
||||
--get a function representing an a < b comparision that can be used
|
||||
--with table.sort and friends, like `table.sort(values, mapping:compare())`
|
||||
function unique_mapping:compare()
|
||||
-- memoised so it doesn't generate garbage, but also doesn't
|
||||
-- allocate until it's actually used
|
||||
if not self._compare then
|
||||
self._compare = function(a, b)
|
||||
return self:map(a) < self:map(b)
|
||||
end
|
||||
end
|
||||
return self._compare
|
||||
end
|
||||
|
||||
return unique_mapping
|
Loading…
Reference in New Issue
Block a user