Class: GodObject::BitSet::BitSet

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable
Defined in:
lib/god_object/bit_set/bit_set.rb

Overview

A bit set with named digits and numeric internal value

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*state, configuration) ⇒ BitSet

Initializes a new BitSet

Parameters:

  • state (Integer, Array<Symbol>)

    either the octal state of the BitSet or a list of enabled digits

  • configuration (GodObject::BitSet::Configuration)

    the configuration which defines the digits of the BitSet


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/god_object/bit_set/bit_set.rb', line 48

def initialize(*state, configuration)
  @configuration = Configuration.build(configuration)

  create_attribute_readers

  if state.size == 1 && state.first.respond_to?(:to_int)
    @integer_representation = state.first.to_int
  else
    state = state.first if state.size == 1 && state.first.is_a?(Enumerable)

    state, invalid_tokens = state.flatten.partition do |token|
      digits.include?(token)
    end

    if invalid_tokens.any?
      string = invalid_tokens.map(&:inspect).join(', ')
      raise ArgumentError, "Invalid digit(s): #{string}"
    end

    @integer_representation = 0

    state.each do |digit|
      @integer_representation |= binary_position(digit)
    end
  end
end

Instance Attribute Details

#configurationGodObject::BitSet::Configuration (readonly)

Returns the configuration for the BitSet.

Returns:


33
34
35
# File 'lib/god_object/bit_set/bit_set.rb', line 33

def configuration
  @configuration
end

#integer_representationInteger (readonly)

Returns the BitSet as binary number.

Returns:

  • (Integer)

    the BitSet as binary number


36
37
38
# File 'lib/god_object/bit_set/bit_set.rb', line 36

def integer_representation
  @integer_representation
end

Instance Method Details

#+(other) ⇒ GodObject::BitSet

Returns a new BitSet with the enabled digits of the current and other.

Parameters:

Returns:

  • (GodObject::BitSet)

    a new BitSet with the enabled digits of the current and other


135
136
137
138
139
# File 'lib/god_object/bit_set/bit_set.rb', line 135

def +(other)
  other = other.enabled_digits if other.respond_to?(:enabled_digits)

  @configuration.new(enabled_digits + other)
end

#-(other) ⇒ GodObject::BitSet

Returns a new BitSet with the enabled digits of the current without the enabled of other.

Parameters:

Returns:

  • (GodObject::BitSet)

    a new BitSet with the enabled digits of the current without the enabled of other


145
146
147
148
149
# File 'lib/god_object/bit_set/bit_set.rb', line 145

def -(other)
  other = other.enabled_digits if other.respond_to?(:enabled_digits)

  @configuration.new(enabled_digits - other)
end

#<=>(other) ⇒ -1, ...

Compares the BitSet to another to determine its relative position.

BitSets are only comparable if their configuration is equal. Relative position is then defined by comparing the Integer representation.

Parameters:

Returns:

  • (-1, 0, 1, nil)

    -1 if other is greater, 0 if other is equal and 1 if other is lesser than self, nil if comparison is impossible


192
193
194
195
196
197
198
199
200
# File 'lib/god_object/bit_set/bit_set.rb', line 192

def <=>(other)
  if @configuration == other.configuration
    @integer_representation <=> other.integer_representation
  else
    nil
  end
rescue NoMethodError
  nil
end

#[](index) ⇒ true, false #[](digit) ⇒ true, false

Overloads:

  • #[](index) ⇒ true, false

    Returns the state of a digit selected by index.

    Parameters:

    • index (Integer)

      a digit index

    Returns:

    • (true, false)

      the digit's current state

  • #[](digit) ⇒ true, false

    Returns the state of a digit selected by name.

    Parameters:

    • digit (Symbol)

      a digit name

    Returns:

    • (true, false)

      the digit's current state


97
98
99
100
101
102
103
104
105
# File 'lib/god_object/bit_set/bit_set.rb', line 97

def [](index_or_digit)
  digit = find_digit(index_or_digit)

  case (@integer_representation & binary_position(digit)) >> digits.reverse.index(digit)
  when 1 then true
  else
    false
  end
end

#disabled_digitsArray<Symbol>

Returns a list of all digits which are disabled.

Returns:

  • (Array<Symbol>)

    a list of all digits which are disabled


117
118
119
120
121
122
123
# File 'lib/god_object/bit_set/bit_set.rb', line 117

def disabled_digits
  set = Set[]

  digits.each {|digit| set << digit unless self[digit] }

  set
end

#enabled_digitsArray<Symbol>

Returns a list of all digits which are enabled.

Returns:

  • (Array<Symbol>)

    a list of all digits which are enabled


108
109
110
111
112
113
114
# File 'lib/god_object/bit_set/bit_set.rb', line 108

def enabled_digits
  set = Set[]

  digits.each {|digit| set << digit if self[digit] }

  set
end

#eql?(other) ⇒ true, false

Answers if another object is equal and of the same type family.

Parameters:

  • other (Object)

    an object to be checked for equality

Returns:

  • (true, false)

    true if the object is considered equal and of the same type familiy, false otherwise

See Also:

  • GodObject::BitSet#<=>

208
209
210
# File 'lib/god_object/bit_set/bit_set.rb', line 208

def eql?(other)
  self == other && other.kind_of?(self.class)
end

#hashsee Array#hash

Returns identity hash for hash table usage.

Returns:

  • (see Array#hash)

    identity hash for hash table usage


213
214
215
# File 'lib/god_object/bit_set/bit_set.rb', line 213

def hash
  [@configuration, @integer_representation].hash
end

#inspectString

Represents a BitSet as String for debugging.

Returns:

  • (String)

    a String representation for debugging


220
221
222
# File 'lib/god_object/bit_set/bit_set.rb', line 220

def inspect
  "#<#{self.class}: #{self.to_s.inspect}>"
end

#intersection(other) ⇒ GodObject::BitSet Also known as: &

Returns a new BitSet with only those digits enabled which are enabled in both the current and other.

Parameters:

Returns:

  • (GodObject::BitSet)

    a new BitSet with only those digits enabled which are enabled in both the current and other


165
166
167
168
169
# File 'lib/god_object/bit_set/bit_set.rb', line 165

def intersection(other)
  other = other.to_i if other.respond_to?(:to_i)

  @configuration.new(@integer_representation & other)
end

#invertGodObject::BitSet

Returns a new BitSet of the same configuration with all digit states inverted.

Returns:

  • (GodObject::BitSet)

    a new BitSet of the same configuration with all digit states inverted


127
128
129
# File 'lib/god_object/bit_set/bit_set.rb', line 127

def invert
  @configuration.new(@configuration.valid_range.max - @integer_representation)
end

#state{Symbol => true, false} Also known as: attributes

Returns all digits and their current state.

Returns:

  • ({Symbol => true, false})

    all digits and their current state


76
77
78
79
80
81
82
83
84
# File 'lib/god_object/bit_set/bit_set.rb', line 76

def state
  state = {}

  digits.each do |digit|
    state[digit] = self[digit]
  end

  state
end

#symmetric_difference(other) ⇒ GodObject::BitSet Also known as: ^

Returns a new BitSet with only those enabled digits which are enabled in only one of current and other.

Parameters:

Returns:

  • (GodObject::BitSet)

    a new BitSet with only those enabled digits which are enabled in only one of current and other


176
177
178
179
180
# File 'lib/god_object/bit_set/bit_set.rb', line 176

def symmetric_difference(other)
  other = other.to_i if other.respond_to?(:to_i)

  @configuration.new(@integer_representation ^ other)
end

#to_iInteger

Represents a BitSet as a binary Integer.

Returns:

  • (Integer)

    an Integer representation


227
228
229
# File 'lib/god_object/bit_set/bit_set.rb', line 227

def to_i
  @integer_representation
end

#to_s(format = :long) ⇒ String

Represents a BitSet as String.

Parameters:

  • format (:long, :short) (defaults to: :long)

    the String format

Returns:

  • (String)

    a String representation


235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/god_object/bit_set/bit_set.rb', line 235

def to_s(format = :long)
  unless STRING_FORMAT.include?(format)
    raise ArgumentError, "Invalid format: #{format.inspect}"
  end

  if format == :short && !@configuration.unique_characters?
    raise ArgumentError, 'Short format only available for configurations with unique characters for each digit'
  end

  output = ''

  attributes.each do |digit, value|
    case value
    when true
      output << enabled_character(digit)
    else
      output << disabled_character(digit) if format == :long
    end
  end

  if @integer_representation == 0 && format == :short
    output << '-'
  end

  output
end

#union(other) ⇒ GodObject::BitSet Also known as: |

Returns a new BitSet with the enabled digits of the current and other.

Parameters:

Returns:

  • (GodObject::BitSet)

    a new BitSet with the enabled digits of the current and other


154
155
156
157
158
# File 'lib/god_object/bit_set/bit_set.rb', line 154

def union(other)
  other = other.to_i if other.respond_to?(:to_i)

  @configuration.new(@integer_representation | other)
end