Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *********************************************
- * Basic Class System for GMod - by thomasfn *
- *********************************************
- class_structs.lua - Loads useful structures
- */
- if class_flag( "Structures" ) then return end
- /* STRUCTURE */
- abstract_class "Structure"
- function Structure:Structure()
- self.Values = {}
- end
- function Structure:Size()
- return #self.Values
- end
- function Structure:IsEmpty()
- return self:Size() == 0
- end
- function Structure:Clear()
- self.Values = {}
- end
- /*function Structure:Equals( obj )
- if (!ValidClass( obj, "Structure" )) then return false end
- if (#obj.Values ~= #self.Values) then return false end
- for key, val in pairs( self.Values ) do
- if (val ~= obj.Values[ key ]) then return false end
- end
- return true
- end*/
- /* STACK */
- class "Stack" ("Structure")
- function Stack:Push( item )
- self.Values[ #self.Values + 1 ] = item
- end
- function Stack:Pop()
- local item = self.Values[ #self.Values ]
- self.Values[ #self.Values ] = nil
- return item
- end
- /* QUEUE */
- class "Queue" ("Structure")
- function Queue:Queue()
- self.LowPtr = 1
- end
- function Queue:Enqueue( item )
- self.Values[ #self.Values + 1 ] = item
- end
- function Queue:Dequeue()
- local item = self.Values[ self.LowPtr ]
- self.Values[ self.LowPtr ] = nil
- if (item) then self.LowPtr = self.LowPtr + 1 end
- return item
- end
- function Queue:Start()
- return self.LowPtr
- end
- function Queue:End()
- return #self.Values
- end
- function Queue:Size()
- return (self:End() - self:Start()) + 1
- end
- /* LIST */
- class "List" ("Structure")
- function List.InitMeta( meta )
- function meta.__add( a, b )
- if (!ValidClass( a, "List" )) then return error( "Attempt to add '" .. tostring( a ) .. "' to 'class:" .. self:Type(), 2 ) end
- if (!ValidClass( b, "List" )) then
- a:Add( b )
- return a
- else
- for i=1, b:Size() do
- a:Add( b[i] )
- end
- return a
- end
- end
- function meta.__sub( a, b )
- if (!ValidClass( a, "List" )) then return error( "Attempt to subtract '" .. tostring( a ) .. "' from 'class:" .. b:Type(), 2 ) end
- if (!ValidClass( b, "List" )) then
- a:Remove( b )
- return a
- else
- for i=1, b:Size() do
- a:Remove( b[i] )
- end
- return a
- end
- end
- function meta.__mul( a, b )
- if (!ValidClass( a, "List" )) then return error( "Attempt to intersect '" .. tostring( a ) .. "' with 'class:" .. b:Type(), 2 ) end
- local tmp = new( "List" )
- for i=1, a:Size() do
- local item = a[i]
- if (b:HasItem( item )) then tmp:Add( item ) end
- end
- return tmp
- end
- end
- function List:List( ... )
- local args = { ... }
- for i=1, #args do
- self:Add( args[i] )
- end
- end
- function List:Add( item )
- table.insert( self.Values, item )
- end
- function List:Remove( item )
- for key, val in pairs( self.Values ) do
- if (val == item) then
- table.remove( self.Values, key )
- break
- end
- end
- end
- function List:HasItem( item )
- for _, obj in pairs( self.Values ) do
- if (obj == item) then return true end
- end
- return false
- end
- function List:RemoveByKey( key )
- table.remove( self.Values, key )
- end
- function List:Index( key )
- return self.Values[ key ]
- end
- function List:Sort( func )
- table.sort( self.Values, func )
- end
- /* ARRAY2D */
- class "Array2D" ("Structure")
- function Array2D:Array2D( width, height, def )
- self.Width = width
- self.Height = height
- if (def) then
- for x=1,width do
- for y=1,height do
- self:Set( x, y, def )
- end
- end
- end
- end
- function Array2D:Set( x, y, val )
- x = x - 1
- y = y - 1
- self.Values[ (x * self.Height) + y ] = val
- end
- function Array2D:Get( x, y )
- x = x - 1
- y = y - 1
- return self.Values[ (x * self.Height) + y ]
- end
- function Array2D:GetDimensions()
- return self.Width, self.Height
- end
- /* ARRAY3D */
- class "Array3D" ("Structure")
- function Array3D:Array3D( width, height, depth, def )
- self.Width = width
- self.Height = height
- self.Depth = depth
- if (def) then
- for x=1,width do
- for y=1,height do
- for z=1,depth do
- self:Set( x, y, z, def )
- end
- end
- end
- end
- end
- function Array3D:Set( x, y, z, val )
- self.Values[ (x * self.Height * self.Depth) + (y * self.Depth) + z ] = val
- end
- function Array3D:Get( x, y, z )
- return self.Values[ (x * self.Height * self.Depth) + (y * self.Depth) + z ]
- end
- function Array3D:GetDimensions()
- return self.Width, self.Height, self.Depth
- end
Advertisement
Add Comment
Please, Sign In to add comment