This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Kouksi44's profile picture

tables with limited length and types

Started by Kouksi44, 27 June 2015 - 09:12 PM
Kouksi44 #1
Posted 27 June 2015 - 11:12 PM
TypeTable


Well I was bored so I tried to make tables that only accept values of a certain type and have a limited size .

This is basically what I was able to produce :

Spoiler

local tTables={}
local validTypes={"nil","boolean", "number", "string", "function", "thread","table"}

local function UID()
  --print(#tTables)
  return #tTables

end

local function contains(table, element)
	  if type(table)~="table" then error("Expected table,element,got "..type(table)..","..type(element),0)
	  else
		for _, value in pairs(table) do
		  if value == element then
			return true
		  end
		end
		return false
	  end
end

local function isType(tp)
  if contains(validTypes,tp) then
	return true
  else
	return false
  end
end

local function getLength(tbl)
  local count=0
  for k,v in pairs(tbl) do
	if k~="__id" and k~="__types" then
	  count=count+1
	end
  end
  return count
end


function typedTable( sType, nSize )
  if type(sType)=="string" then
	if(sType and isType(sType) and (nSize and type(nSize)=="number")) then
	  tTables[#tTables]={}
	  tTables[#tTables].__id=UID()
	  tTables[#tTables].__types={sType}
	end
  elseif type(sType)=="table" then
	  tTables[#tTables]={}
	  tTables[#tTables].__id=UID()
	  tTables[#tTables].__types={}
	for _,tp in pairs(sType) do
	  if(tp and isType(tp) and (nSize and type(nSize)=="number")) then
		table.insert(tTables[#tTables].__types,tp)
	  else
		tTables[#tTables]=nil
		error("wrong type paramter")
	  end
	end
  end



	local proxy=setmetatable({},{__index=tTables[#tTables],__newindex=function(t,k,v)  if (contains(tTables[#tTables].__types,type(v)) and getLength(tTables[t.__id])+1<=nSize) then tTables[t.__id][k]=v else error("Attempt to access typed table with wrong type ("..type(v)..") or table size limit reached ("..getLength(tTables[t.__id])..")" )end end })

	return proxy
  end


function deleteTable(pTbl)
  tTables[pTbl.__id]=nil
end

function addType(tbl,sType)
  if isType(sType) then

	getmetatable(tbl).__index.__types[#getmetatable(tbl).__index.__types+1]=sType
  end
end

--local t=typedTable({"number","function"},5)

--
-- -- t.test=1
-- -- t.w=2
-- -- t.z=3
-- -- t.u=4
-- t.pp=45
-- addType(t,"string")
-- t.pp=function() print "test" end
-- t.pp="hfdsjiak"
-- t.pp="he"

The relevant functions are
typedTable( sType, nSize) 
addType(tbl,sType) 
deleteTable(pTbl) 
.

typeTable() simply returns a table that only allows values of the type given as the parameter ( the type can be a string or a table of strings ) and the specified size.

You can add new types through addType() and delete the whole table with deleteTable()


Pastebin:
 http://pastebin.com/42zgHJHh 

It´s no masterpiece and it didn´t take too long to code I was simply bored :D/>

Maybe it´s useful for someone :)/>

Kouksi44
Lion4ever #2
Posted 29 June 2015 - 07:28 PM
Nice :)/>

One useful programming trick for the future:
If you put the allowed type names in the table "validTypes" as a key and not as a value, you would not need a "contains" method.

local validTypes={ ["nil"]=true, boolean=true, string=true, ["function"]=true, thread=true, table=true }
if  sType and validTypes[sType] and type(nSize) =="number" then
end