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.180 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.179 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.178 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.177 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.176 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.175 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.174 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.173 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.172 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.171 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.170 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.169 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.167 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.166 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.165 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.164 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.163 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.162 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.161 tracks/lua/exercises/custom-set/example.lua
trackler-2.2.1.160 tracks/lua/exercises/custom-set/example.lua