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:
-
compare_by_identity
-
compare_by_identity?
-
reset
-
to_set
-
+Enumerable#to_set+
Public Class Methods
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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