Class: GodObject::BitSet::BitSet
- Inherits:
-
Object
- Object
- GodObject::BitSet::BitSet
- 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
-
#configuration ⇒ GodObject::BitSet::Configuration
readonly
The configuration for the BitSet.
-
#integer_representation ⇒ Integer
readonly
The BitSet as binary number.
Instance Method Summary collapse
-
#+(other) ⇒ GodObject::BitSet
A new BitSet with the enabled digits of the current and other.
-
#-(other) ⇒ GodObject::BitSet
A new BitSet with the enabled digits of the current without the enabled of other.
-
#<=>(other) ⇒ -1, ...
Compares the BitSet to another to determine its relative position.
- #[](index_or_digit) ⇒ Object
-
#disabled_digits ⇒ Array<Symbol>
A list of all digits which are disabled.
-
#enabled_digits ⇒ Array<Symbol>
A list of all digits which are enabled.
-
#eql?(other) ⇒ true, false
Answers if another object is equal and of the same type family.
-
#hash ⇒ see Array#hash
Identity hash for hash table usage.
-
#initialize(*state, configuration) ⇒ BitSet
constructor
Initializes a new BitSet.
-
#inspect ⇒ String
Represents a BitSet as String for debugging.
-
#intersection(other) ⇒ GodObject::BitSet
(also: #&)
A new BitSet with only those digits enabled which are enabled in both the current and other.
-
#invert ⇒ GodObject::BitSet
A new BitSet of the same configuration with all digit states inverted.
-
#state ⇒ {Symbol => true, false}
(also: #attributes)
All digits and their current state.
-
#symmetric_difference(other) ⇒ GodObject::BitSet
(also: #^)
A new BitSet with only those enabled digits which are enabled in only one of current and other.
-
#to_i ⇒ Integer
Represents a BitSet as a binary Integer.
-
#to_s(format = :long) ⇒ String
Represents a BitSet as String.
-
#union(other) ⇒ GodObject::BitSet
(also: #|)
A new BitSet with the enabled digits of the current and other.
Constructor Details
#initialize(*state, configuration) ⇒ BitSet
Initializes a new 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
#configuration ⇒ GodObject::BitSet::Configuration (readonly)
Returns the configuration for the BitSet.
33 34 35 |
# File 'lib/god_object/bit_set/bit_set.rb', line 33 def configuration @configuration end |
#integer_representation ⇒ Integer (readonly)
Returns 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.
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.
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.
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
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_digits ⇒ Array<Symbol>
Returns 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_digits ⇒ Array<Symbol>
Returns 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.
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 |
#hash ⇒ see Array#hash
Returns 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 |
#inspect ⇒ String
Represents a BitSet as String 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.
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 |
#invert ⇒ GodObject::BitSet
Returns 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.
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.
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_i ⇒ Integer
Represents a BitSet as a binary Integer.
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.
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.
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 |