Sha256: 764cb6eada8d92a2b30f3540247a667ee17f97cc4b180e2492e66d9fe7088512

Contents?: true

Size: 1.45 KB

Versions: 396

Compression:

Stored size: 1.45 KB

Contents

local Set = {}
Set.__index = Set

local function create(...)
  local o = { _contents = {} }
  for _, v in ipairs(table.pack(...)) do
    o._contents[v] = true
  end
  return setmetatable(o, Set)
end

function Set:equals(other)
  return self:is_subset(other) and other:is_subset(self)
end

function Set:add(element)
  self._contents[element] = true
end

function Set:remove(element)
  self._contents[element] = nil
end

function Set:is_empty()
  return self:equals(create())
end

function Set:contains(element)
  return self._contents[element] ~= nil
end

function Set:is_subset(other)
  for k in pairs(self._contents) do
    if not other:contains(k) then return false end
  end
  return true
end

function Set:is_disjoint(other)
  for k in pairs(self._contents) do
    if other:contains(k) then return false end
  end
  for k in pairs(other._contents) do
    if self:contains(k) then return false end
  end
  return true
end

function Set:intersection(other)
  local intersection = create()
  for k in pairs(self._contents) do
    if other:contains(k) then intersection:add(k) end
  end
  return intersection
end

function Set:union(other)
  local union = create()
  for k in pairs(self._contents) do
    union:add(k)
  end
  for k in pairs(other._contents) do
    union:add(k)
  end
  return union
end

function Set:difference(other)
  local difference = self:union(create())
  for k in pairs(other._contents) do
    difference:remove(k)
  end
  return difference
end

return create

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.119 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.118 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.117 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.116 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.115 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.114 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.113 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.111 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.110 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.109 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.108 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.107 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.106 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.105 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.104 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.103 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.102 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.101 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.100 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.99 tracks/lua/exercises/custom-set/example.lua