local c={}function c.__index(x,c)if rawget(x,"\095\095\098\097\115\101\099\108\097\115\115")then return x.__baseclass[c]end return nil end function c.__call(c,...)return c:new(...)end function c.__add(x,c)return x:addparent(c)end function c.__eq(x,c)if not c.__baseclass or c.__baseclass~=x.__baseclass then return false end for x in pairs(x)do if not c[x]then return false end end for c in pairs(c)do if not x[c]then return false end end return true end function c.__lt(x,c)if not c.__baseclass then return false end if rawget(c.__baseclass,"\095\095\105\115\112\097\114\101\110\116\116\097\098\108\101")then for c,q in pairs(c.__baseclass)do if x==q or(getmetatable(x)).__lt(x,q)then return true end end else if x==c.__baseclass or(getmetatable(x)).__t(x,c.__baseclass)then return true end end return false end function c.__le(x,c)return x<c or x==c end local x={}function x.__index(x,c)for x,q in pairs(x)do if x~="\095\095\105\115\112\097\114\101\110\116\116\097\098\108\101"and q[c]then return q[c]end end return nil end local q=setmetatable({__baseclass={}},c)function q.new(c,...)local x={}x.__baseclass=c setmetatable(x,getmetatable(c))if x.init then x:init(...)end return x end function q.convert(x,c)c.__baseclass=x setmetatable(c,getmetatable(x))return c end function q.addparent(c,...)if not rawget(c.__baseclass,"\095\095\105\115\112\097\114\101\110\116\116\097\098\108\101")then local q={__isparenttable=true;c.__baseclass;...}c.__baseclass=setmetatable(q,x)else for x,q in ipairs({...})do table.insert(c.__baseclass,q)end end return c end function q.setmetamethod(q,c,x)local m=getmetatable(q)local E=table.clone(m)E[c]=x setmetatable(q,E)end return q
-- #selene: allow(mixed_table)

--[[
Copyright (c) 2009 Bart Bes
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
]]

local class_mt = {}

function class_mt:__index(key)
	if rawget(self, "__baseclass") then
		return self.__baseclass[key]
	end
	return nil
end

function class_mt:__call(...)
	return self:new(...)
end

function class_mt:__add(parent)
	return self:addparent(parent)
end

function class_mt:__eq(other)
	if not other.__baseclass or other.__baseclass ~= self.__baseclass then
		return false
	end
	for i in pairs(self) do
		if not other[i] then
			return false
		end
	end
	for i in pairs(other) do
		if not self[i] then
			return false
		end
	end
	return true
end

function class_mt:__lt(other)
	if not other.__baseclass then
		return false
	end
	if rawget(other.__baseclass, "__isparenttable") then
		for _, v in pairs(other.__baseclass) do
			if self == v or getmetatable(self).__lt(self, v) then
				return true
			end
		end
	else
		if self == other.__baseclass or getmetatable(self).__t(self, other.__baseclass) then
			return true
		end
	end
	return false
end

function class_mt:__le(other)
	return (self < other or self == other)
end

local pt_mt = {}
function pt_mt:__index(key)
	for i, v in pairs(self) do
		if i ~= "__isparenttable" and v[key] then
			return v[key]
		end
	end
	return nil
end

local class = setmetatable({ __baseclass = {} }, class_mt)

function class:new(...)
	local c = {}
	c.__baseclass = self
	setmetatable(c, getmetatable(self))
	if c.init then
		c:init(...)
	end
	return c
end

function class:convert(t)
	t.__baseclass = self
	setmetatable(t, getmetatable(self))
	return t
end

function class:addparent(...)
	if not rawget(self.__baseclass, "__isparenttable") then
		local t = { __isparenttable = true, self.__baseclass, ... }
		self.__baseclass = setmetatable(t, pt_mt)
	else
		for _, v in ipairs({ ... }) do
			table.insert(self.__baseclass, v)
		end
	end
	return self
end

function class:setmetamethod(name, value)
	local mt = getmetatable(self)
	local newmt = table.clone(mt)
	newmt[name] = value
	setmetatable(self, newmt)
end

return class
