containertables.inc.lua

changeset 0
cc66ad6b0d75
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/containertables.inc.lua	Sat Mar 27 17:43:08 2010 +0000
@@ -0,0 +1,52 @@
+
+--[[ I created this class for an easy way of managing the objects inside a container in the MOO. ]]
+
+containertable = { };
+
+function containertable:add(object)
+	local subt = self[(object and object._properties.name) or ""];
+	if not subt then subt = { };
+		self[(object and object._properties.name) or ""] = subt; 
+		-- Also, all object's aliases need to point to subt
+		if object._tags then
+			for i, tag in ipairs(object._tags) do
+				self[tag] = subt;
+			end
+		end
+	end
+	table.insert(subt, object);
+	return object;
+end
+
+function containertable:remove(object)
+	local subt = self[(object._properties and object._properties.name) or ""];
+	if not subt then return false; end
+	for n, stored_object in pairs(subt) do
+		if object == stored_object then table.remove(subt, n); return object; end
+	end
+	return false;
+end
+
+function containertable:addalias(name, tag)
+	self[tag] = self[name];
+end
+
+function containertable:findobject(name)
+	local subt = self[name or ""];
+	if not subt then subt = self[""]; if not subt then return; end end
+	return subt[1];
+end
+
+function containertable:getallobjects()
+	all = {};
+	for tag, list in pairs(self) do
+		for n, object in ipairs(list) do
+			all[object] = true;
+		end
+	end
+	return all; -- We return a table, where the objects are the keys, and the values are true
+end
+
+function createcontainertable(t)
+	return setmetatable(t or {}, { __index = containertable });
+end
\ No newline at end of file

mercurial