[Snippet] Meta Tables (almost)
stableDescription
Another one for the rabbit hole, creating meta table like objects without the table commands.
Use this framework to handle the recording of information in your scripts
HaasScript
--- Define the create table function
create = function()
array = {{},_index=HNC()} -- not really using this yet
maxRows = 1
maxColumns = 1
index = HNC() -- as above, not yet
for row=1,maxRows do
for col=1,maxColumns do
local idx = row*maxColumns +col
array._index = ArrayUnshift(array._index, idx)
array[idx] = ''
end
end
return array
end
-- Define the Get Row function
getrow = function(row, data)
local out = {}
for col=1,maxColumns do
out[col] = data[row*maxColumns +col]
end
return out
end
-- Define the Set Row function
setrow = function(value, row, data)
for col=1,maxColumns do
data[row*maxColumns +col] = value
end
return data
end
-- Define the Delete Row function
delrow = function(row, data)
for col=1,maxColumns do
data[row*maxColumns +col] = nil
end
return data
end
-- Define the Delete Col function
delcol = function(col, data)
for row=1, maxRows do
data[row*maxColumns +col] = nil
end
return data
end
-- Define the Get Col function
getcol = function(col, data)
local out = {}
for row=1, maxRows do
out[row] = data[row*maxColumns +col]
end
return out
end
-- Define the Set Col function
setcol = function(value, col, data)
for row=1, maxRows do
data[row*maxColumns +col] = value
end
return data
end
-- Define the Set Value function
setvalue = function(value, row, col, data)
data[row*maxColumns +col] = value
return data
end
-- Define the Get Value function
getvalue = function(row, col, data)
return data[row*maxColumns +col]
end
-- Create the the table data instance
db = Load('db', create())
--- Define the table class meta methods
table = {
_private = db,
_create = create,
_getrow = getrow,
_setrow = setrow,
_getcol = getcol,
_setcol = setcol,
_delrow = delrow,
_delcol = delcol,
_setvalue = setvalue,
_getvalue = getvalue,
}
--- Define the Add Keys Function
function table:addkeys(keys)
if Count(IfNull(self._private._keys , {})) == 0 then
self._private._keys = IfNull(self._private._keys , {})
end
if GetType(keys) == TextDataType then
local v = keys
local idx = #self._private._keys+1
if ArrayAny(self._private._keys, v) == false then
self._private._keys[idx] = v
self._private._keys[v] = idx
end
else
for k,v in pairs(keys) do
local idx = #self._private._keys+1
if ArrayAny(self._private._keys, v) == false then
self._private._keys[idx] = v
self._private._keys[v] = idx
end
end
end
end
--- Define the Get Multiple Keys Function
function table:getkeys()
local ret = {}
for index, key in pairs(self._private._keys) do
ret[#ret+1] = key
end
return ret
end
--- Define the Get Single Key Function
function table:getkey(key)
if Count(IfNull(self._private._keys , {})) == 0 then
self._private._keys = IfNull(self._private._keys , {})
end
if ArrayAny(self._private._keys, key) then
return self._private._keys[key]
else
self:addkeys(key)
return self._private._keys[key]
end
end
--- Define the Clear Data Function
function table:clear()
self._private = self._create()
end
--- Define the Get Row Function
function table:getrow(row)
return self._getrow(row, self._private)
end
--- Define the Get Col Function
function table:getcol(col)
if GetType(col) == NumberDataType then
return self._getcol(col, self._private)
else
return self._getcol(self:getkey(col), self._private)
end
end
--- Define the Set Row Function
function table:setrow(row, value)
value = IfNull(value, "")
self._private = self._setrow(value, row, self._private)
end
--- Define the Set Col Function
function table:setcol(col, value)
value = IfNull(value, {})
if GetType(col) == NumberDataType then
self._private = self._setcol(value, col, self._private)
else
self._private = self._setcol(value, self:getkey(col), self._private)
end
end
--- Define the Delete Row Function
function table:delrow(row)
self._private = self._delrow(row, self._private)
end
--- Define the Delete Column Function
function table:delcol(col)
if GetType(col) == NumberDataType then
self._private = self._delcol(col, self._private)
else
self._private = self._delcol(self:getkey(col), self._private)
end
end
--- Define the Set Value Function
function table:setvalue(value, row, col)
if GetType(col) == NumberDataType then
self._private = self._setvalue(value, row, col, self._private)
else
self._private = self._setvalue(value, row, self:getkey(col), self._private)
end
end
--- Define the Get Value Function
function table:getvalue(row, col)
if GetType(col) == NumberDataType then
return self._getvalue(row, col, self._private)
else
return self._getvalue(row, self:getkey(col), self._private)
end
end
--- Define the Add Multiple Columns Function
function table:addcols(cols)
for _, col in pairs(cols) do
self:setcol(self:getkey(col))
end
end
--- Examples
--- Fill an entry and add it into the table
table:setvalue({oid = NewGuid(), pid = NewGuid(), time = CreateTimestamp(), dir = 1, active = 1, price = CurrentPrice().close}, 2, 'long')
table:setvalue({oid = NewGuid(), pid = NewGuid(), time = CreateTimestamp(), dir = 1, active = 1, price = CurrentPrice().close}, 5, 'short')
table:setvalue({oid = NewGuid(), pid = NewGuid(), time = CreateTimestamp(), dir = 1, active = 1, price = CurrentPrice().close}, 7, 'long')
--- Add some columns to the table
table:addcols({'a','b'}, 22)
--- Get the data we just added
Log(table:getvalue(2, 'long'))
Log(table:getvalue(7, 'long'))
Log(table:getvalue(5, 'short'))
Log(table)
--- iterate though the table directly
for k,item in pairs(table:getrow(2)) do
Log(item.oid)
Log(item.pid)
Log(item.time)
Log(item.dir)
Log(item.active)
Log(item.price)
end
---Save the Data to memory
Save('db', db)
---- moonwalks backwards out of here like mj.... before the well... you know....
0 Comments
Sign in to leave a comment.
No comments yet. Be the first!