Class: Ant::BitVector

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
lib/ant/bitvector.rb

Overview

Ant::BitVector – a convenience class for manipulating and comparing bit vectors.

This is a slightly-modified version of the same utility in StaticCling.

Synopsis

require 'ant/bitvector'

vector = Ant::BitVector.new

vector.on( 4 )     # => 16
vector.on( 12 )    # => 4112
vector.toggle( 4 ) # => 4096
vector.on?( 4 )    # => false
vector.size        # => 13
vector.to_hex      # => 0x1000

vector2 = Ant::BitVector.new( 5 )
vector > vector2   # => true

vector2.each_with_index do |bit, i|

puts “Bit %d is %s” % [ i + 1, bit.zero? ? ‘off’ : ‘on’ ] end

Bit 1 is on Bit 2 is off Bit 3 is on

Version

$Id$

Author

License

Copyright © 2000-2013, Mahlon E. Smith <[email protected]>

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

* Neither the name of the author, nor the names of contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(init = 0) ⇒ BitVector

Create a new bit vector object, optionally from a pre-existing init number (Any number that ruby supports natively should be fine – 0b, 0x, or decimal.)



83
84
85
86
87
88
89
# File 'lib/ant/bitvector.rb', line 83

def initialize( init=0 )
  unless init.respond_to?( :to_i )
    raise ArgumentError, "I don't know what to do with a %s object." % [ init.class.name ]
  end

  @bv = init.to_i
end

Instance Attribute Details

#bvObject (readonly)

Allow external access to the underlying Fixnum/Bignum



109
110
111
# File 'lib/ant/bitvector.rb', line 109

def bv
  @bv
end

Instance Method Details

#<=>(cmp) ⇒ Object

Comparision operator for Comparable mixin, fallthrough to Fixnum/Bignum. Compares current state against cmp.



192
193
194
# File 'lib/ant/bitvector.rb', line 192

def <=>( cmp )
  @bv <=> cmp.bv
end

#[]=(bit, bool) ⇒ Object

Set a bit to bool – either true (on) or false (off). Any value other than nil or false is treated as true. This form also accepts ranges of bits, a la: vector[ 1..4 ] = true



173
174
175
176
177
178
179
# File 'lib/ant/bitvector.rb', line 173

def []=( bit, bool )
  if bit.respond_to?( :each )
    bit.each { |b| bool ? self.on( b ) : self.off( b ) }
  else
    bool ? self.on( bit ) : self.off( bit )
  end
end

#eachObject

Yield each binary position, least significant bit first.



183
184
185
186
187
# File 'lib/ant/bitvector.rb', line 183

def each
  @bv.to_s(2).reverse.each_byte do |bit|
    yield bit.chr.to_i
  end
end

#off(bit) ⇒ Object

Switch a bit off.



152
153
154
# File 'lib/ant/bitvector.rb', line 152

def off( bit )
  @bv = @bv & ~( 1 << bit )
end

#off?(bit) ⇒ Boolean

Return boolean true if given bit is currently off.

Returns:

  • (Boolean)


158
159
160
# File 'lib/ant/bitvector.rb', line 158

def off?( bit )
  return ! self.on?( bit )
end

#on(bit) ⇒ Object

Switch a bit on.



139
140
141
# File 'lib/ant/bitvector.rb', line 139

def on( bit )
  @bv = @bv | ( 1 << bit )
end

#on?(bit) ⇒ Boolean Also known as: []

Return boolean true if given bit is currently on.

Returns:

  • (Boolean)


145
146
147
# File 'lib/ant/bitvector.rb', line 145

def on?( bit )
  return @bv[ bit ].zero? ? false : true
end

#sizeObject

Return the length of the vector in bytes.



133
134
135
# File 'lib/ant/bitvector.rb', line 133

def size
  return @bv.to_s(2).length
end

#to_binObject

Return the bit vector as a binary string.



121
122
123
# File 'lib/ant/bitvector.rb', line 121

def to_bin
  return "0b%s" % @bv.to_s(2)
end

#to_hexObject

Return the bit vector as a hexidecimal string.



127
128
129
# File 'lib/ant/bitvector.rb', line 127

def to_hex
  return "0x%04x" % @bv
end

#to_iObject Also known as: to_int, to_dec

Return the bit vector as a decimal.



113
114
115
# File 'lib/ant/bitvector.rb', line 113

def to_i
  return @bv
end

#toggle(bit) ⇒ Object Also known as: flip

Swap the current state of the given bit.



164
165
166
# File 'lib/ant/bitvector.rb', line 164

def toggle( bit )
  @bv = @bv ^ ( 1 << bit )
end