Advertisement
Kouksi44

GenericList

Jan 19th, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.84 KB | None | 0 0
  1. local LuaTypes = {
  2.     ["function"] = true;
  3.     ["string"] = true;
  4.     ["table"] = true;
  5.     ["number"] = true;
  6.     ["thread"] = true;
  7.     ["boolean"] = true;
  8. }
  9.  
  10. local function resolveType(Type,Init)
  11.     if Init then
  12.         return Type
  13.     end
  14.    
  15.     if type(Type) == "table" then
  16.         if Type.__type then
  17.             return Type.__type
  18.         end
  19.     end
  20.     if LuaTypes[type(Type)] then
  21.         return type(Type)
  22.     end
  23. end
  24.  
  25. local function checkValues(ValTable,LType)
  26.     for k,v in pairs(ValTable) do
  27.         if resolveType(v) ~= LType then
  28.             return false
  29.         end
  30.     end
  31.     return true
  32. end
  33.  
  34.  
  35. local CList = {}
  36.  
  37. function CList:add(Value,Pos)
  38.     if Pos then
  39.         self.entries[Pos] = Value
  40.     else
  41.         table.insert(self.entries,Value)
  42.     end
  43. end
  44.  
  45. function CList:getType()
  46.     return self.__ListType
  47. end
  48.  
  49. function CList:remove(Key)
  50.     table.remove(self.entries,Key)
  51. end
  52.  
  53. function CList:length()
  54.     return self.__ListLength
  55. end
  56.  
  57. function CList:sort(CompFunc)
  58.     local rawentries = self.__ListEntries
  59.     table.sort(rawentries,CompFunc or nil)
  60. end
  61.  
  62. function CList:concat(Sep,RStart,RStop)
  63.     local rawentries = self.__ListEntries
  64.     return table.concat(rawentries,Sep,RStart,RStop)
  65. end
  66.    
  67.  
  68. local function createList(GenType)
  69.     local obj = {
  70.         initialized = false;
  71.         type = resolveType(GenType,true);
  72.         entries = {};
  73.     }
  74.    
  75.     setmetatable(obj,{__index = CList})
  76.     local objProxy = {
  77.         __type = "List"
  78.     }
  79.  
  80.     setmetatable(objProxy,{
  81.        
  82.         __index = function(_,k)
  83.  
  84.             if k == "__ListType" then
  85.                 return obj.type
  86.             end
  87.             if k == "__ListLength" then
  88.                 return #obj.entries
  89.             end
  90.             if k == "__ListEntries" then
  91.                 return obj.entries
  92.             end
  93.             if not obj.initialized then
  94.                 obj.initialized = true
  95.             end
  96.            
  97.             return obj.entries[k] or CList[k]
  98.         end;
  99.        
  100.         __newindex = function(_,Key,Value)
  101.             if resolveType(Value) == obj.type then
  102.                 obj:add(Value,Key)
  103.             else
  104.                 error("Type of new value("..type(Value)..") does not match with list type("..obj.type..")")
  105.             end
  106.         end;
  107.        
  108.         __call = function(_,_,InitValues)
  109.             if obj.initialized then
  110.                 error("List has already been initialized",2)
  111.             end
  112.             if checkValues(InitValues,obj.type) then
  113.                 obj.entries = InitValues
  114.             else
  115.                 error("Inital values do not match List type",2)
  116.             end
  117.             return objProxy
  118.         end;
  119.        
  120.         __add = function(Left,Right)
  121.             if Left.__type == "List" then
  122.                 if resolveType(Right) == obj.type then
  123.                     obj:add(Right,nil)
  124.                 else
  125.                     error("Type of new value("..type(Right)..") does not match with list type("..obj.type..")")
  126.                 end
  127.             else
  128.                 if resolveType(Left) == obj.type then
  129.                     obj:add(Left,nil)
  130.                 else
  131.                     error("Type of new value("..type(Left)..") does not match with list type("..obj.type..")")
  132.                 end
  133.             end
  134.             return objProxy
  135.         end;
  136.     })
  137.     return objProxy
  138. end
  139.    
  140. ListParser = setmetatable({},{
  141.     __index = function(_,GenType)
  142.         return createList(GenType)
  143.     end;
  144. })
  145.  
  146. List = ListParser
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement