Class: Plutonium::Lib::BitFlags

Inherits:
Object
  • Object
show all
Defined in:
lib/plutonium/lib/bit_flags.rb

Overview

The BitFlags class provides a convenient way to work with bit flags. It allows setting, checking, and extracting flags using a more readable interface. It supports both symbols and strings as flags.

Examples:

Usage

flags = BitFlags.new(:read, 'write', :execute)
value = flags[:read, 'write']  # => 3
flags.set?(value, :read)       # => true
flags.set?(value, 'execute')   # => false
flags.extract(value)           # => [:read, :write]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*flags) ⇒ BitFlags

Initializes a new BitFlags object with the given flags.

Examples:

BitFlags.new(:read, 'write', :execute)

Parameters:

  • flags (Array<Symbol, String>)

    The flags to be used in this BitFlags object.



26
27
28
29
30
31
# File 'lib/plutonium/lib/bit_flags.rb', line 26

def initialize(*flags)
  @flags = normalize_flags(flags).uniq.freeze
  @indices = @flags.each_index.map { |index| 1 << index }.freeze
  @map = @flags.zip(@indices).to_h.freeze
  @all_bits = calculate_all_bits
end

Instance Attribute Details

#flagsArray<Symbol> (readonly)

Returns An array of all defined flags.

Returns:

  • (Array<Symbol>)

    An array of all defined flags.



16
17
18
# File 'lib/plutonium/lib/bit_flags.rb', line 16

def flags
  @flags
end

#indicesArray<Integer> (readonly)

Returns An array of all defined bit values.

Returns:

  • (Array<Integer>)

    An array of all defined bit values.



19
20
21
# File 'lib/plutonium/lib/bit_flags.rb', line 19

def indices
  @indices
end

Instance Method Details

#[](*flags) ⇒ Integer

Returns the bit value for the given flags.

Examples:

flags[:read, 'write']  # => 3

Parameters:

  • flags (Array<Symbol, String>)

    The flags to get the bit value for.

Returns:

  • (Integer)

    The combined bit value of the given flags.



77
78
79
# File 'lib/plutonium/lib/bit_flags.rb', line 77

def [](*flags)
  bits(*flags)
end

#bits(*flags) ⇒ Integer

Calculates the combined bit value for the given flags.

Examples:

flags.bits(:read, 'write')  # => 3

Parameters:

  • flags (Array<Symbol, String>)

    The flags to calculate the bit value for.

Returns:

  • (Integer)

    The combined bit value of the given flags.



87
88
89
90
# File 'lib/plutonium/lib/bit_flags.rb', line 87

def bits(*flags)
  normalized_flags = normalize_flags(flags)
  normalized_flags.sum { |flag| @map[flag] || 0 }
end

#extract(value) ⇒ Array<Symbol>

Extracts the flags that are set in the given value.

Examples:

flags.extract(3)  # => [:read, :write]

Parameters:

  • value (Integer)

    The value to extract flags from.

Returns:

  • (Array<Symbol>)

    An array of flags that are set in the value.



66
67
68
69
# File 'lib/plutonium/lib/bit_flags.rb', line 66

def extract(value)
  value &= @all_bits
  @map.select { |_, bit| value & bit != 0 }.keys
end

#set!(value, *flags) ⇒ Integer

Sets the specified flags in the given value.

Examples:

flags.set!(2, :read, :execute)  # => 6

Parameters:

  • value (Integer)

    The original value to modify.

  • flags (Array<Symbol, String>)

    The flags to set.

Returns:

  • (Integer)

    A new value with the specified flags set.



54
55
56
57
58
# File 'lib/plutonium/lib/bit_flags.rb', line 54

def set!(value, *flags)
  normalized_flags = normalize_flags(flags)
  bits_to_set = bits(*normalized_flags)
  value | bits_to_set
end

#set?(value, *flags) ⇒ Boolean

Checks if the given value has all the specified flags set.

Examples:

flags.set?(3, :read, 'write')  # => true

Parameters:

  • value (Integer)

    The value to check against.

  • flags (Array<Symbol, String>)

    The flags to check for.

Returns:

  • (Boolean)

    True if all specified flags are set and valid, false otherwise.



40
41
42
43
44
45
# File 'lib/plutonium/lib/bit_flags.rb', line 40

def set?(value, *flags)
  normalized_flags = normalize_flags(flags)
  return false if normalized_flags.any? { |flag| !@map.key?(flag) }
  check = bits(*normalized_flags)
  value & check == check
end

#sumInteger

Calculates the sum of all bit values.

Returns:

  • (Integer)

    The sum of all bit values.



95
96
97
# File 'lib/plutonium/lib/bit_flags.rb', line 95

def sum
  @all_bits
end