class DragonSkeleton::Set

Mostly implements the Set class from the Ruby standard library. See ruby-doc.org/3.2.2/stdlibs/set/Set.html

Following methods are not implemented:

Public Class Methods

[](*elements) click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-c-5B-5D

# File lib/dragon_skeleton/set.rb, line 15
def self.[](*elements)
  new(elements)
end
new(elements = nil) click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-c-new

# File lib/dragon_skeleton/set.rb, line 20
def initialize(elements = nil)
  @elements = (elements || []).map { |element| [element, true] }.to_h
end

Public Instance Methods

&(other)
Alias for: intersection
+(other)
Alias for: union
-(other) click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-2D

# File lib/dragon_skeleton/set.rb, line 40
def -(other)
  self.class.new(select { |element| !other.include?(element) })
end
Also aliased as: difference
<(other)
Alias for: proper_subset?
<<(element)
Alias for: add
<=(other)
Alias for: subset?
<=>(other) click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-3C-3D-3E

# File lib/dragon_skeleton/set.rb, line 52
def <=>(other)
  return unless other.is_a?(Set)
  return 0 if self == other
  return -1 if subset?(other)
  return 1 if superset?(other)
end
==(other) click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-3D-3D

# File lib/dragon_skeleton/set.rb, line 60
def ==(other)
  other.is_a?(Set) && @elements == other.instance_variable_get(:@elements)
end
===(element)
Alias for: include?
>(other)
Alias for: proper_superset?
>=(other)
Alias for: superset?
^(other) click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-5E

# File lib/dragon_skeleton/set.rb, line 47
def ^(other)
  (self | other) - (self & other)
end
add(element) click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-add

# File lib/dragon_skeleton/set.rb, line 123
def add(element)
  @elements[element] = true
  self
end
Also aliased as: <<
add?(element) click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-add-3F

# File lib/dragon_skeleton/set.rb, line 131
def add?(element)
  add(element) unless include?(element)
end
classify() { |element| ... } click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-classify

# File lib/dragon_skeleton/set.rb, line 208
def classify
  return enum_for(:classify) { size } unless block_given?

  result = {}

  each do |element|
    key = yield(element)
    result[key] ||= self.class.new
    result[key].add(element)
  end

  result
end
clear() click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-clear

# File lib/dragon_skeleton/set.rb, line 148
def clear
  @elements.clear
end
collect!() { |element| ... } click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-collect-21

# File lib/dragon_skeleton/set.rb, line 223
def collect!
  return enum_for(:collect!) { size } unless block_given?

  @elements = map { |element| [yield(element), true] }.to_h
  self
end
Also aliased as: map!
delete(element) click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-delete

# File lib/dragon_skeleton/set.rb, line 153
def delete(element)
  @elements.delete(element)
  self
end
delete?(element) click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-delete-3F

# File lib/dragon_skeleton/set.rb, line 159
def delete?(element)
  delete(element) if include?(element)
end
delete_if() { |element| ... } click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-delete_if

# File lib/dragon_skeleton/set.rb, line 170
def delete_if
  return enum_for(:delete_if) { size } unless block_given?

  each do |element|
    delete(element) if yield(element)
  end
  self
end
difference(other)
Alias for: -
disjoint?(other) click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-disjoint-3F

# File lib/dragon_skeleton/set.rb, line 113
def disjoint?(other)
  (self & other).empty?
end
divide() { |element, key| ... } click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-divide

# File lib/dragon_skeleton/set.rb, line 233
def divide(&func)
  return enum_for(:divide) { size } unless block_given?

  case func.arity
  when 2
    set_by_representative = {}
    each do |element|
      representative = set_by_representative.keys.find { |key|
        yield(element, key)
      }
      representative ||= element
      set_by_representative[representative] ||= self.class.new
      set_by_representative[representative] << element
    end
    Set.new set_by_representative.values
  else
    set_by_value = {}
    each do |element|
      discriminator = yield(element)
      set_by_value[discriminator] ||= self.class.new
      set_by_value[discriminator] << element
    end
    Set.new set_by_value.values
  end
end
each() { |element| ... } click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-each

# File lib/dragon_skeleton/set.rb, line 292
def each
  return to_enum(:each) { size } unless block_given?

  @elements.keys.each do |element|
    yield element
  end
end
empty?() click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-empty-3F

# File lib/dragon_skeleton/set.rb, line 72
def empty?
  @elements.empty?
end
filter!(&block)
Alias for: select!
flatten() click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-flatten

# File lib/dragon_skeleton/set.rb, line 260
def flatten
  result = []
  each do |element|
    if element.is_a?(Set)
      result += element.flatten
    else
      result << element
    end
  end
  Set.new result
end
flatten!() click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-flatten-21

# File lib/dragon_skeleton/set.rb, line 273
def flatten!
  elements_before = @elements
  @elements = flatten.instance_variable_get(:@elements)
  self if elements_before != @elements
end
include?(element) click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-include-3F

# File lib/dragon_skeleton/set.rb, line 77
def include?(element)
  @elements.key?(element)
end
Also aliased as: ===, member?
inspect() click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-to_s

# File lib/dragon_skeleton/set.rb, line 280
def inspect
  "#<Set: {#{to_a.join(', ')}}>"
end
Also aliased as: to_s
intersect?(other) click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-intersect-3F

# File lib/dragon_skeleton/set.rb, line 118
def intersect?(other)
  !disjoint?(other)
end
intersection(other) click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-intersection

# File lib/dragon_skeleton/set.rb, line 33
def intersection(other)
  self.class.new(select { |element| other.include?(element) })
end
Also aliased as: &
join(separator = nil) click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-join

# File lib/dragon_skeleton/set.rb, line 287
def join(separator = nil)
  to_a.join(separator)
end
keep_if() { |element| ... } click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-keep_if

# File lib/dragon_skeleton/set.rb, line 191
def keep_if
  return enum_for(:keep_if) { size } unless block_given?

  delete_if { |element| !yield(element) }
  self
end
length()
Alias for: size
map!()
Alias for: collect!
member?(element)
Alias for: include?
merge(elements) click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-merge

# File lib/dragon_skeleton/set.rb, line 136
def merge(elements)
  elements.each { |element| add(element) }
  self
end
proper_subset?(other) click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-proper_subset-3F

# File lib/dragon_skeleton/set.rb, line 92
def proper_subset?(other)
  subset?(other) && size < other.size
end
Also aliased as: <
proper_superset?(other) click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-proper_superset-3F

# File lib/dragon_skeleton/set.rb, line 106
def proper_superset?(other)
  superset?(other) && size > other.size
end
Also aliased as: >
reject!() { |element| ... } click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-reject-21

# File lib/dragon_skeleton/set.rb, line 199
def reject!
  return enum_for(:reject!) { size } unless block_given?

  size_before = size
  delete_if { |element| yield(element) }
  self if size_before != size
end
replace(elements) click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-replace

# File lib/dragon_skeleton/set.rb, line 142
def replace(elements)
  @elements = elements.map { |element| [element, true] }.to_h
  self
end
select!(&block) click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-select-21

# File lib/dragon_skeleton/set.rb, line 180
def select!(&block)
  return enum_for(:select!) { size } unless block_given?

  size_before = size
  keep_if(&block)
  self if size_before != size
end
Also aliased as: filter!
size() click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-size

# File lib/dragon_skeleton/set.rb, line 65
def size
  @elements.size
end
Also aliased as: length
subset?(other) click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-subset-3F

# File lib/dragon_skeleton/set.rb, line 85
def subset?(other)
  all? { |element| other.include?(element) }
end
Also aliased as: <=
subtract(elements) click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-subtract

# File lib/dragon_skeleton/set.rb, line 164
def subtract(elements)
  elements.each { |element| delete(element) }
  self
end
superset?(other) click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-superset-3F

# File lib/dragon_skeleton/set.rb, line 99
def superset?(other)
  other.subset?(self)
end
Also aliased as: >=
to_s()
Alias for: inspect
union(other) click to toggle source

See ruby-doc.org/3.2.2/stdlibs/set/Set.html#method-i-union

# File lib/dragon_skeleton/set.rb, line 25
def union(other)
  self.class.new(to_a + other.to_a)
end
Also aliased as: |, +
|(other)
Alias for: union