<commit_before>local function table_IsEmpty(t) return not next(t) end

/******************************************************************************\
  Table support
\******************************************************************************/

registerType("table", "t", {},
	function(self, input)
		local ret = {}
		local c = 0
		for k,v in pairs(input) do c = c + 1 ret[k] = v end
		self.prf = self.prf + c / 3
		return ret
	end,
	nil,
	function(retval)
		if type(retval) ~= "table" then error("Return value is not a table, but a "..type(retval).."!",0) end
	end,
	function(v)
		return type(v) ~= "table"
	end
)

-- these postexecute and construct hooks handle changes to both tables and arrays.
registerCallback("postexecute", function(self)
	local vars, vclk, lookup = self.vars, self.vclk, self.data.lookup

	-- Go through all registered values of the types table and array.
	for value,varnames in pairs(lookup) do
		local clk = vclk[value]

		local still_assigned = false
		-- For each value, go through the variables they're assigned to and trigger them.
		for varname,_ in pairs(varnames) do
			if value == vars[varname] then
				-- The value is still assigned to the variable? => trigger it.
				if clk then vclk[varname] = true end
				still_assigned = true
			else
				-- The value is no longer assigned to the variable? => remove the lookup table entry.
				varnames[varname] = nil
			end
		end

		-- if the value is no longer assigned to anything, remove all references to it.
		if not still_assigned then
			lookup[value] = nil
		end
		-- If the value has no more variable names associated, remove the value's place in the lookup table.
		if table_IsEmpty(varnames) then lookup[value] = nil end
	end
end)

registerCallback("construct", function(self)
	--self.data.lookup[rhs][lhs] = true|nil
	self.data.lookup = {}
end)

/******************************************************************************/

__e2setcost(5) -- temporary

e2function table operator=(table lhs, table rhs)
	local lookup = self.data.lookup

	-- remove old lookup entry
	if lookup[rhs] then lookup[rhs][lhs] = nil end

	-- add new lookup entry
	local lookup_entry = lookup[rhs]
	if not lookup_entry then
		lookup_entry = {}
		lookup[rhs] = lookup_entry
	end
	lookup_entry[lhs] = true

	self.vars[lhs] = rhs
	self.vclk[lhs] = true
	return rhs
end

/******************************************************************************/

e2function number operator_is(table tbl)
	if table_IsEmpty(tbl) then return 0 else return 1 end
end

/******************************************************************************/

e2function table table()
	return {}
end

e2function table table:clear()
	self.prf = self.prf + table.Count(this) / 3
	table.Empty(this)
end

e2function table table:clone()
	local ret = {}
	local c = 0
	for k,v in pairs(this) do c = c + 1 ret[k] = v end
	self.prf = self.prf + c / 3
	return ret
end

e2function number table:count()
	local c = table.Count(this)<commit_msg>[E2] table.lua: Added (table) tablekv(string key, val value, ...)<commit_after>local function table_IsEmpty(t) return not next(t) end

/******************************************************************************\
  Table support
\******************************************************************************/

registerType("table", "t", {},
	function(self, input)
		local ret = {}
		local c = 0
		for k,v in pairs(input) do c = c + 1 ret[k] = v end
		self.prf = self.prf + c / 3
		return ret
	end,
	nil,
	function(retval)
		if type(retval) ~= "table" then error("Return value is not a table, but a "..type(retval).."!",0) end
	end,
	function(v)
		return type(v) ~= "table"
	end
)

-- these postexecute and construct hooks handle changes to both tables and arrays.
registerCallback("postexecute", function(self)
	local vars, vclk, lookup = self.vars, self.vclk, self.data.lookup

	-- Go through all registered values of the types table and array.
	for value,varnames in pairs(lookup) do
		local clk = vclk[value]

		local still_assigned = false
		-- For each value, go through the variables they're assigned to and trigger them.
		for varname,_ in pairs(varnames) do
			if value == vars[varname] then
				-- The value is still assigned to the variable? => trigger it.
				if clk then vclk[varname] = true end
				still_assigned = true
			else
				-- The value is no longer assigned to the variable? => remove the lookup table entry.
				varnames[varname] = nil
			end
		end

		-- if the value is no longer assigned to anything, remove all references to it.
		if not still_assigned then
			lookup[value] = nil
		end
		-- If the value has no more variable names associated, remove the value's place in the lookup table.
		if table_IsEmpty(varnames) then lookup[value] = nil end
	end
end)

registerCallback("construct", function(self)
	--self.data.lookup[rhs][lhs] = true|nil
	self.data.lookup = {}
end)

/******************************************************************************/

__e2setcost(5) -- temporary

e2function table operator=(table lhs, table rhs)
	local lookup = self.data.lookup

	-- remove old lookup entry
	if lookup[rhs] then lookup[rhs][lhs] = nil end

	-- add new lookup entry
	local lookup_entry = lookup[rhs]
	if not lookup_entry then
		lookup_entry = {}
		lookup[rhs] = lookup_entry
	end
	lookup_entry[lhs] = true

	self.vars[lhs] = rhs
	self.vclk[lhs] = true
	return rhs
end

/******************************************************************************/

e2function number operator_is(table tbl)
	if table_IsEmpty(tbl) then return 0 else return 1 end
end

/******************************************************************************/

e2function table table()
	return {}
end

e2function table tablekv(...)
	local ret = {...}
	local final = {}
	local count = #ret

	if (count % 2) == 0 then
		for i = 1, count, 2 do
			local key = ret[i]
			local value = ret[i + 1]

			if type(key) == "string" then
				final[key] = value
			end
		end
	end

	return final
end

e2function table table:clear()
	self.prf = self.prf + table.Count(this) / 3
	table.Empty(this)
end

e2function table table:clone()
	local ret = {}
	local c = 0
	for k,v in pairs(this) do c = c + 1 ret[k] = v end
	self.prf = self.prf + c / 3
	return ret
end

e2function number table:count()
	local c = table.Count(this)
{'fraction_non_alphanumeric': 0.21898854961832062, 'fraction_numerical': 0.006838422391857507, 'mean_word_length': 4.0312, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2361386', 'n_tokens_mistral': 2057, 'n_tokens_neox': 1891, 'n_words': 681}