Class: Stupidedi::Sets::RelativeComplement

Inherits:
AbstractSet show all
Defined in:
lib/stupidedi/sets/relative_complement.rb

Overview

This data type is an infinite and non-enumerable set of values that encodes the complement of a RelativeSet

Set Operations collapse

Set Ordering collapse

Pretty Printing collapse

Instance Method Summary collapse

Methods inherited from AbstractSet

#&, #+, #-, #<, #<=, #>, #>=, #^, #disjoint?, #exclude?, #infinite?, #subset?, #superset?, #|, #~

Constructor Details

#initialize(complement) ⇒ RelativeComplement

Returns a new instance of RelativeComplement.



12
13
14
# File 'lib/stupidedi/sets/relative_complement.rb', line 12

def initialize(complement)
  @complement = complement
end

Instance Method Details

#==(other) ⇒ Object



119
120
121
122
# File 'lib/stupidedi/sets/relative_complement.rb', line 119

def ==(other)
  eql?(other) or
   (other.is_a?(RelativeComplement) and complement == other.complement)
end

#complementObject



48
49
50
51
# File 'lib/stupidedi/sets/relative_complement.rb', line 48

def complement
  # ¬(¬A) = A
  @complement
end

#difference(other) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/stupidedi/sets/relative_complement.rb', line 95

def difference(other)
  if other.is_a?(RelativeComplement)
    # ¬A ∖ ¬B = ¬A ∩ B = B ∖ A
    other.complement.difference(complement)
  else
    # ¬A ∖ B = ¬A ∩ ¬B = ¬(A ∪ B)
    complement.union(Sets.build(other)).complement
  end
end

#empty?Boolean

Returns false.

Returns:

  • (Boolean)

    false



35
36
37
# File 'lib/stupidedi/sets/relative_complement.rb', line 35

def empty?
  false
end

#finite?Boolean

Returns false.

Returns:

  • (Boolean)

    false



29
30
31
# File 'lib/stupidedi/sets/relative_complement.rb', line 29

def finite?
  false
end

#include?(object) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/stupidedi/sets/relative_complement.rb', line 17

def include?(object)
  not @complement.include?(object)
end

#inspectString

Returns:



128
129
130
# File 'lib/stupidedi/sets/relative_complement.rb', line 128

def inspect
  "RelativeComplement(#{@complement.inspect})"
end

#intersection(other) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/stupidedi/sets/relative_complement.rb', line 54

def intersection(other)
  if other.is_a?(RelativeComplement)
    # ¬A ∩ ¬B = ¬(A ∪ B)
    complement.union(other.complement).complement
  else
    # ¬A ∩ B = B ∖ A
    Sets.build(other).difference(complement)
  end
end

#proper_subset?(other) ⇒ Boolean

True if this set is a subset of the ‘other` set and there exists at least one element in the `other` set that doesn’t belong to this set

Returns:

  • (Boolean)

See Also:



109
110
111
# File 'lib/stupidedi/sets/relative_complement.rb', line 109

def proper_subset?(other)
  other.is_a?(RelativeComplement) and intersection(other) == self
end

#proper_superset?(other) ⇒ Boolean

True if this set is a superset of the ‘other` set and there exists at least one element in this set that doesn’t belong to the ‘other` set

Returns:

  • (Boolean)

See Also:



114
115
116
# File 'lib/stupidedi/sets/relative_complement.rb', line 114

def proper_superset?(other)
  other.is_a?(RelativeComplement) and intersection(other) == other
end

#replace(other) ⇒ AbstractSet

Returns the ‘other` set, converting it to an AbstractSet if it isn’t already.

Returns:



40
41
42
# File 'lib/stupidedi/sets/relative_complement.rb', line 40

def replace(other)
  Sets.build(other)
end

#sizeNumeric

Returns the number of elements in the set

Returns:

  • (Numeric)
  • Infinity



23
24
25
# File 'lib/stupidedi/sets/relative_complement.rb', line 23

def size
  1.0 / 0.0
end

#symmetric_difference(other) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/stupidedi/sets/relative_complement.rb', line 76

def symmetric_difference(other)
  if other.is_a?(RelativeComplement)
    # ¬A ⊖ ¬B = (¬A ∖ ¬B) ∪ (¬B ∖ ¬A)
    #         =  (B ∖ A)  ∪ (A ∖ B)
    #         = A ⊖ B
    complement.symmetric_difference(other.complement)
  else
    # ¬A ⊖ B = (¬A ∖ B)  ∪ (B ∖ ¬A)
    #        = (¬A ∩ ¬B) ∪ (B ∩ A)
    #        = (¬B ∖ A)  ∪ (A ∖ ¬B)
    #        = A ⊖ ¬B
    other = Sets.build(other)

    intersection(other.complement).
      union(other.intersection(complement))
  end
end

#union(other) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/stupidedi/sets/relative_complement.rb', line 65

def union(other)
  if other.is_a?(RelativeComplement)
    # ¬A ∪ ¬B = ¬(A ∩ B)
    complement.intersection(other.complement).complement
  else
    # ¬A ∪ B = ¬(A ∖ B)
    complement.difference(Sets.build(other)).complement
  end
end