Class: TrickBag::Numeric::Bitmap

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/trick_bag/numeric/bitmap.rb

Overview

Instances of this class can be created that will hold on to bitmap data and be used to test bits and convert to other formats.

Where an array is used to represent bits, the first element (#0) will be the high bit and the last element will be the low (1’s column) bit.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number) ⇒ Bitmap

Returns a new instance of Bitmap.



91
92
93
94
# File 'lib/trick_bag/numeric/bitmap.rb', line 91

def initialize(number)
  BitMapping.assert_non_negative(number)
  @number = number
end

Instance Attribute Details

#numberObject

This is the internal representation of the bitmap value:



18
19
20
# File 'lib/trick_bag/numeric/bitmap.rb', line 18

def number
  @number
end

Class Method Details

.from_binary_string(string) ⇒ Object

Creates an instance from a binary string (e.g. “x0C”).



50
51
52
# File 'lib/trick_bag/numeric/bitmap.rb', line 50

def self.from_binary_string(string)
  new(BitMapping.binary_string_to_number(string))
end

.from_bit_array(array) ⇒ Object

Creates an instance from a bit array (e.g. [1, 0, 0, 1])



60
61
62
# File 'lib/trick_bag/numeric/bitmap.rb', line 60

def self.from_bit_array(array)
  new(BitMapping.bit_array_to_number(array))
end

.from_number(number) ⇒ Object

Creates an instance from a nonnegative number.



45
46
47
# File 'lib/trick_bag/numeric/bitmap.rb', line 45

def self.from_number(number)
  new(number)
end

.from_place_value_array(array) ⇒ Object

Creates an instance from a value array (e.g. [8, 0, 0, 1])



55
56
57
# File 'lib/trick_bag/numeric/bitmap.rb', line 55

def self.from_place_value_array(array)
  new(BitMapping.place_value_array_to_number(array))
end

.from_set_bit_position_array(array) ⇒ Object

Creates an instance from an array of positions for the bits that are set (e.g. [0, 3])



65
66
67
# File 'lib/trick_bag/numeric/bitmap.rb', line 65

def self.from_set_bit_position_array(array)
  new(BitMapping.set_bit_position_array_to_number(array))
end

Instance Method Details

#==(other) ⇒ Object



96
97
98
# File 'lib/trick_bag/numeric/bitmap.rb', line 96

def ==(other)
  other.is_a?(self.class) && other.number == self.number
end

#to_binary_string(min_length = 0) ⇒ Object

Returns the instance’s value as a binary string (e.g. “x0C”)



72
73
74
# File 'lib/trick_bag/numeric/bitmap.rb', line 72

def to_binary_string(min_length = 0)
  BitMapping.number_to_binary_string(number, min_length)
end

#to_bit_arrayObject

Returns the instance’s value as an array of bit column place values (e.g. [8, 0, 0, 1])



82
83
84
# File 'lib/trick_bag/numeric/bitmap.rb', line 82

def to_bit_array
  BitMapping.number_to_bit_array(number)
end

#to_place_value_arrayObject

Returns the instance’s value as an array of bit column values (e.g. [8, 0, 0, 1])



77
78
79
# File 'lib/trick_bag/numeric/bitmap.rb', line 77

def to_place_value_array
  BitMapping.number_to_place_value_array(number)
end

#to_set_bit_position_arrayObject

Returns the instance’s value as an array of positions for the bits that are set (e.g. [0, 3])



87
88
89
# File 'lib/trick_bag/numeric/bitmap.rb', line 87

def to_set_bit_position_array
  BitMapping.number_to_set_bit_positions_array(number)
end