Сравнение множеств

Массивы и коллекции / Множества

-- Every item of a is in b
local function subset_of(a, b)
  for item in pairs(a) do
    if not b[item] then
      return false
    end
  end
  return true
end

-- Some item of a is in b
local function intersects(a, b)
  for item in pairs(a) do
    if b[item] then
      return true
    end
  end
  return false
end

local first = {A = true, B = true}
local second = {B = true, A = true}
local third = {A = true, B = true, C = true}

-- Equal: each set is a subset of the other
local is_equal = subset_of(first, second)
                 and subset_of(second, first)
-- is_equal = true

local is_intersects = intersects(first, third)
-- is_intersects = true

local is_third_subset = subset_of(third, first)
-- is_third_subset = false

local is_subset = subset_of(first, third)
-- is_subset = true

-- Proper subset: a subset that is not equal
local is_proper_subset = subset_of(first, second)
                         and not is_equal
-- is_proper_subset = false

print("is_equal = " .. tostring(is_equal))
print("is_intersects = "
      .. tostring(is_intersects))
print("is_third_subset = "
      .. tostring(is_third_subset))
print("is_subset = " .. tostring(is_subset))
print("is_proper_subset = "
      .. tostring(is_proper_subset))