From 1ce9466289eaa1843e503b951078fc4432ce96c4 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 22 Apr 2009 17:46:17 +0100 Subject: util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name --- util/array.lua | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'util') diff --git a/util/array.lua b/util/array.lua index 9e99a3ec..072b24a7 100644 --- a/util/array.lua +++ b/util/array.lua @@ -52,4 +52,17 @@ function array:reverse() end end -_G.array = array +function array.collect(f, s, var) + local t, var = {}; + while true do + var = f(s, var); + if var == nil then break; end + table.insert(t, var); + end + return setmetatable(t, array_mt); +end + +_G.array = array; +module("array"); + +return array; -- cgit v1.2.3 From 34a4b98cf6dbb4a97dda590d16832ffcd196bd3a Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 22 Apr 2009 18:00:45 +0100 Subject: util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators --- util/set.lua | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 4 deletions(-) (limited to 'util') diff --git a/util/set.lua b/util/set.lua index f550a721..892f1c9d 100644 --- a/util/set.lua +++ b/util/set.lua @@ -1,10 +1,60 @@ -local ipairs, pairs = - ipairs, pairs; +local ipairs, pairs, setmetatable, next, tostring = + ipairs, pairs, setmetatable, next, tostring; +local t_concat = table.concat; module "set" +local set_mt = {}; +function set_mt.__call(set, _, k) + return next(set._items, k); +end +function set_mt.__add(set1, set2) + return _M.union(set1, set2); +end +function set_mt.__sub(set1, set2) + return _M.difference(set1, set2); +end +function set_mt.__div(set, func) + local new_set, new_items = _M.new(); + local items, new_items = set._items, new_set._items; + for item in pairs(items) do + if func(item) then + new_items[item] = true; + end + end + return new_set; +end +function set_mt.__eq(set1, set2) + local set1, set2 = set1._items, set2._items; + for item in pairs(set1) do + if not set2[item] then + return false; + end + end + + for item in pairs(set2) do + if not set1[item] then + return false; + end + end + + return true; +end +function set_mt.__tostring(set) + local s, items = { }, set._items; + for item in pairs(items) do + s[#s+1] = tostring(item); + end + return t_concat(s, ", "); +end + +local items_mt = {}; +function items_mt.__call(items, _, k) + return next(items, k); +end + function new(list) - local items = {}; + local items = setmetatable({}, items_mt); local set = { _items = items }; function set:add(item) @@ -45,7 +95,7 @@ function new(list) set:add_list(list); end - return set; + return setmetatable(set, set_mt); end function union(set1, set2) -- cgit v1.2.3 From 901af1535f708d391d10e7c116d4a9538b89dac2 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 22 Apr 2009 18:03:02 +0100 Subject: util.set: Add set:empty() to discover if the set is the empty set --- util/set.lua | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'util') diff --git a/util/set.lua b/util/set.lua index 892f1c9d..bb318adf 100644 --- a/util/set.lua +++ b/util/set.lua @@ -91,6 +91,10 @@ function new(list) end end + function set:empty() + return not next(items); + end + if list then set:add_list(list); end -- cgit v1.2.3 From 0d98ee54b5a332e5d6a42aebf85026da2d432d2f Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 22 Apr 2009 18:03:42 +0100 Subject: util.set: Add set.xor() to get a set consisting of items not in both sets --- util/set.lua | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'util') diff --git a/util/set.lua b/util/set.lua index bb318adf..84bfe8c4 100644 --- a/util/set.lua +++ b/util/set.lua @@ -141,4 +141,8 @@ function intersection(set1, set2) return set; end +function xor(set1, set2) + return union(set1, set2) - intersection(set1, set2); +end + return _M; -- cgit v1.2.3