<commit_before>require ("lib.lclass")

class "EntityManager"

function EntityManager:EntityManager ()
	-- last assigned id
	self.idCounter = 0
	-- tags -> list of ids
	self.tags = {}
	-- id -> list of assigned data
	self.data = {}
end

function EntityManager:nextId ()
	self.idCounter = self.idCounter + 1

	return self.idCounter
end

function EntityManager:createEntity (tags)
	-- fetch newly created id (no need for reuse lua has bignum (whoohoo :D))
	local eid = self:nextId ()
	-- create empty data list for entity
	self.data[eid] = {}
	-- add tags if any
	if tags then
		for _, tag in pairs (tags) do
			self:addTag (eid, tag)
		end
	end

	return eid
end

function EntityManager:addTag (eid, tag)
	if not self.tags[tag] then
		self.tags[tag] = {}
	end

	table.insert (self.tags[tag], eid)
end

function EntityManager:addData (eid, data)
	if self:hasData (eid, data) then
		error ("Entity " .. eid .. " already has data of type " .. data.__type)
	end

	table.insert (self.data[eid], data)
end

function EntityManager:removeData (eid, datatype)
	if not self:hasData (eid, data) then
		error ("Entity " .. eid .. " has no data of type " .. data.__type)
	end
end

function EntityManager:hasData (eid, datatype)
	if not self.data[eid] then
		error ("There is no entity with id " .. eid)
	end

	local has = false
	for _, data in pairs (self.data[eid]) do
		has = data.__type == datatype.__type
		if has then
			break
		end
	end

	return has
end

function EntityManager:getData (eid, datatype)
	if not self.data[eid] then
		error ("There is no entity with id " .. eid)
	end

	return self.data[eid]
end<commit_msg>entity manager now exclusivly works with strings<commit_after>require ("lib.lclass")

class "EntityManager"

function EntityManager:EntityManager ()
	-- last assigned id
	self.idCounter = 0
	-- tags -> list of ids
	self.tags = {}
	-- id -> list of assigned data
	self.dataMap = {}
	-- datatypename -> list of entities holding this type of data
	self.typeMap = {}
end

function EntityManager:nextId ()
	self.idCounter = self.idCounter + 1

	return self.idCounter
end

function EntityManager:createEntity (tags)
	-- fetch newly created id (no need for reuse lua has bignum (whoohoo :D))
	local eid = self:nextId ()
	-- create empty data list for entity
	self.dataMap[eid] = {}
	-- add tags if any
	if tags then
		for _, tag in pairs (tags) do
			self:addTag (eid, tag)
		end
	end

	return eid
end

function EntityManager:addTag (eid, tag)
	if not self.tags[tag] then
		self.tags[tag] = {}
	end

	table.insert (self.tags[tag], eid)

	return tag, eid
end

function EntityManager:addData (eid, data)
	local typename = data:getClass ()

	if self:hasData (eid, typename) then
		error ("Entity " .. eid .. " already has data of type " .. typename)
	end

	-- store data in data map
	self.dataMap[eid][typename] = data
	-- mark id in type map
	if not self.typeMap[typename] then
		self.typeMap[typename] = {}
	end
	self.typeMap[typename][eid] = true

	return data, eid
end

function EntityManager:removeData (eid, typename)
	if not self:hasData (eid, typename) then
		error ("Entity " .. eid .. " has no data of type " .. typename)
	end

	self.dataMap[eid][typename] = nil
	self.typeMap[typename][eid] = nil

	return typename, eid
end

local inspect = require ("lib.inspect")
function EntityManager:hasData (eid, typename)
	if not self.dataMap[eid] then
		error ("There is no entity with id " .. eid)
	end
	print ("Checking if " .. eid .. " has " .. typename)
	local has = false
	if self.typeMap[typename] then
		return true == self.typeMap[typename][eid]
	end
	print ("Result: " .. tostring (has))
	print ("TypeMap: " .. inspect (self.typeMap))
	return has
end

function EntityManager:getData (eid, typename)
	local data = self.dataMap[eid][typename]
	if not data then
		error ("Entity " .. eid .. " doesnt hold data of type " .. typename)
	end

	return data, eid
end

function EntityManager:findEntitiesByData (datalist)

end

function EntityManager:findEntitiesByTag (taglist)
end
{'fraction_non_alphanumeric': 0.1144578313253012, 'fraction_numerical': 0.001004016064257028, 'mean_word_length': 3.5752009184845006, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '97696', 'n_tokens_mistral': 1414, 'n_tokens_neox': 1356, 'n_words': 459}