local function empty(container)
	for i,j in pairs(container) do
		return false
	end	
	return true
end

class "object_collection"
function object_collection:__init()
	self.m_free						= {}
	self.m_given					= {}
	self.m_count					= 0
	self.m_last_id					= 0
end
function object_collection:get_id()	
	printf("get_id called [%s]", self.m_count)

	self.m_count					= self.m_count + 1

	if (empty(self.m_free) == true) then
		self.m_last_id				= self.m_last_id + 1
		self.m_given[self.m_last_id] = true
		return self.m_last_id
	end

	for i,j in pairs(self.m_free) do
		self.m_free[i]				= nil
		self.m_given[i] 			= true
		return i
	end
end
function object_collection:remove(id)
	printf("remove called [%s]", tostring(id))
	if self.m_given[id] == true then
		self.m_given[id] = nil
		self.m_free[id] = 1
		self.m_count = self.m_count - 1
	end
end
function object_collection:size()
	return self.m_count
end
function object_collection:save(packet)
	set_save_marker(packet, "save", false, "object_collection")

	printf("SAVE OBJECT COLLECTION")
	printf("------- m_count %s  m_last_id %s", self.m_count, self.m_last_id)
	printf("--------  m_free")
	print_table(self.m_free)
	printf("--------  m_given")
	print_table(self.m_given)
	printf("--------")
	
	
	packet:w_u16(self.m_count)	
	packet:w_u16(self.m_last_id)

	local n = 0
	for k,v in pairs(self.m_free) do
		n = n + 1		
	end		
	packet:w_u16(n)
	for k,v in pairs(self.m_free) do
		packet:w_u16(k)
	end

	n = 0
	for k,v in pairs(self.m_given) do
		n = n + 1		
	end		
	packet:w_u16(n)
	for k,v in pairs(self.m_given) do
		packet:w_u16(k)
	end
	
	set_save_marker(packet, "save", true, "object_collection")
end
function object_collection:load(packet)
	set_save_marker(packet, "load", false, "object_collection")

	printf("LOAD OBJECT COLLECTION")
	callstack()

	self.m_count = packet:r_u16()	
	self.m_last_id = packet:r_u16()
	
	local n = packet:r_u16()
	self.m_free = {}
	for i = 1,n do
		local id = packet:r_u16()
		self.m_free[id] = 1
	end	

	n = packet:r_u16()
	self.m_given = {}
	for i = 1,n do
		local id = packet:r_u16()
		self.m_given[id] = true
	end	
	
	printf("------- m_count %s  m_last_id %s", self.m_count, self.m_last_id)
	printf("--------  m_free")
	print_table(self.m_free)
	printf("--------  m_given")
	print_table(self.m_given)
	printf("--------")
	
	set_save_marker(packet, "load", true, "object_collection")
end

