thomasfn

class_structs.lua

Mar 30th, 2012
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.32 KB | None | 0 0
  1. /*
  2.     *********************************************
  3.     * Basic Class System for GMod - by thomasfn *
  4.     *********************************************
  5.     class_structs.lua - Loads useful structures
  6. */
  7.  
  8. if class_flag( "Structures" ) then return end
  9.  
  10. /* STRUCTURE */
  11. abstract_class "Structure"
  12. function Structure:Structure()
  13.     self.Values = {}
  14. end
  15. function Structure:Size()
  16.     return #self.Values
  17. end
  18. function Structure:IsEmpty()
  19.     return self:Size() == 0
  20. end
  21. function Structure:Clear()
  22.     self.Values = {}
  23. end
  24. /*function Structure:Equals( obj )
  25.     if (!ValidClass( obj, "Structure" )) then return false end
  26.     if (#obj.Values ~= #self.Values) then return false end
  27.     for key, val in pairs( self.Values ) do
  28.         if (val ~= obj.Values[ key ]) then return false end
  29.     end
  30.     return true
  31. end*/
  32.  
  33. /* STACK */
  34. class "Stack" ("Structure")
  35. function Stack:Push( item )
  36.     self.Values[ #self.Values + 1 ] = item
  37. end
  38. function Stack:Pop()
  39.     local item = self.Values[ #self.Values ]
  40.     self.Values[ #self.Values ] = nil
  41.     return item
  42. end
  43.  
  44. /* QUEUE */
  45. class "Queue" ("Structure")
  46. function Queue:Queue()
  47.     self.LowPtr = 1
  48. end
  49. function Queue:Enqueue( item )
  50.     self.Values[ #self.Values + 1 ] = item
  51. end
  52. function Queue:Dequeue()
  53.     local item = self.Values[ self.LowPtr ]
  54.     self.Values[ self.LowPtr ] = nil
  55.     if (item) then self.LowPtr = self.LowPtr + 1 end
  56.     return item
  57. end
  58. function Queue:Start()
  59.     return self.LowPtr
  60. end
  61. function Queue:End()
  62.     return #self.Values
  63. end
  64. function Queue:Size()
  65.     return (self:End() - self:Start()) + 1
  66. end
  67.  
  68. /* LIST */
  69. class "List" ("Structure")
  70. function List.InitMeta( meta )
  71.     function meta.__add( a, b )
  72.         if (!ValidClass( a, "List" )) then return error( "Attempt to add '" .. tostring( a ) .. "' to 'class:" .. self:Type(), 2 ) end
  73.         if (!ValidClass( b, "List" )) then
  74.             a:Add( b )
  75.             return a
  76.         else
  77.             for i=1, b:Size() do
  78.                 a:Add( b[i] )
  79.             end
  80.             return a
  81.         end
  82.     end
  83.     function meta.__sub( a, b )
  84.         if (!ValidClass( a, "List" )) then return error( "Attempt to subtract '" .. tostring( a ) .. "' from 'class:" .. b:Type(), 2 ) end
  85.         if (!ValidClass( b, "List" )) then
  86.             a:Remove( b )
  87.             return a
  88.         else
  89.             for i=1, b:Size() do
  90.                 a:Remove( b[i] )
  91.             end
  92.             return a
  93.         end
  94.     end
  95.     function meta.__mul( a, b )
  96.         if (!ValidClass( a, "List" )) then return error( "Attempt to intersect '" .. tostring( a ) .. "' with 'class:" .. b:Type(), 2 ) end
  97.         local tmp = new( "List" )
  98.         for i=1, a:Size() do
  99.             local item = a[i]
  100.             if (b:HasItem( item )) then tmp:Add( item ) end
  101.         end
  102.         return tmp
  103.     end
  104. end
  105. function List:List( ... )
  106.     local args = { ... }
  107.     for i=1, #args do
  108.         self:Add( args[i] )
  109.     end
  110. end
  111. function List:Add( item )
  112.     table.insert( self.Values, item )
  113. end
  114. function List:Remove( item )
  115.     for key, val in pairs( self.Values ) do
  116.         if (val == item) then
  117.             table.remove( self.Values, key )
  118.             break
  119.         end
  120.     end
  121. end
  122. function List:HasItem( item )
  123.     for _, obj in pairs( self.Values ) do
  124.         if (obj == item) then return true end
  125.     end
  126.     return false
  127. end
  128. function List:RemoveByKey( key )
  129.     table.remove( self.Values, key )
  130. end
  131. function List:Index( key )
  132.     return self.Values[ key ]
  133. end
  134. function List:Sort( func )
  135.     table.sort( self.Values, func )
  136. end
  137.  
  138. /* ARRAY2D */
  139. class "Array2D" ("Structure")
  140. function Array2D:Array2D( width, height, def )
  141.     self.Width = width
  142.     self.Height = height
  143.     if (def) then
  144.         for x=1,width do
  145.             for y=1,height do
  146.                 self:Set( x, y, def )
  147.             end
  148.         end
  149.     end
  150. end
  151. function Array2D:Set( x, y, val )
  152.     x = x - 1
  153.     y = y - 1
  154.     self.Values[ (x * self.Height) + y ] = val
  155. end
  156. function Array2D:Get( x, y )
  157.     x = x - 1
  158.     y = y - 1
  159.     return self.Values[ (x * self.Height) + y ]
  160. end
  161. function Array2D:GetDimensions()
  162.     return self.Width, self.Height
  163. end
  164.  
  165. /* ARRAY3D */
  166. class "Array3D" ("Structure")
  167. function Array3D:Array3D( width, height, depth, def )
  168.     self.Width = width
  169.     self.Height = height
  170.     self.Depth = depth
  171.     if (def) then
  172.         for x=1,width do
  173.             for y=1,height do
  174.                 for z=1,depth do
  175.                     self:Set( x, y, z, def )
  176.                 end
  177.             end
  178.         end
  179.     end
  180. end
  181. function Array3D:Set( x, y, z, val )
  182.     self.Values[ (x * self.Height * self.Depth) + (y * self.Depth) + z ] = val
  183. end
  184. function Array3D:Get( x, y, z )
  185.     return self.Values[ (x * self.Height * self.Depth) + (y * self.Depth) + z ]
  186. end
  187. function Array3D:GetDimensions()
  188.     return self.Width, self.Height, self.Depth
  189. end
Advertisement
Add Comment
Please, Sign In to add comment