Class: GorgonAMQ::BitSet

Inherits:
Object
  • Object
show all
Defined in:
lib/gorgon_amq-protocol/lib/gorgon_amq/bit_set.rb

Overview

Very minimalistic, pure Ruby implementation of bit set. Inspired by java.util.BitSet, although significantly smaller in scope.

Originally part of amqp gem. Extracted to make it possible for Bunny to use it.

Constant Summary collapse

ADDRESS_BITS_PER_WORD =

API

6
BITS_PER_WORD =
(1 << ADDRESS_BITS_PER_WORD)
WORD_MASK =
0xffffffffffffffff

Instance Method Summary collapse

Constructor Details

#initialize(nbits) ⇒ BitSet

Returns a new instance of BitSet.

Parameters:

  • Number (Integer)

    of bits in the set



20
21
22
23
24
# File 'lib/gorgon_amq-protocol/lib/gorgon_amq/bit_set.rb', line 20

def initialize(nbits)
  @nbits = nbits

  self.init_words(nbits)
end

Instance Method Details

#clearObject

Clears all bits in the set



60
61
62
# File 'lib/gorgon_amq-protocol/lib/gorgon_amq/bit_set.rb', line 60

def clear
  self.init_words(@nbits)
end

#get(i) ⇒ Boolean Also known as: []

Fetches flag value for given bit.

Parameters:

  • A (Integer)

    bit to fetch

Returns:

  • (Boolean)

    true if given bit is set, false otherwise



40
41
42
43
44
# File 'lib/gorgon_amq-protocol/lib/gorgon_amq/bit_set.rb', line 40

def get(i)
  w = self.word_index(i)

  (@words[w] & (1 << i)) != 0
end

#set(i) ⇒ Object

Sets (flags) given bit. This method allows bits to be set more than once in a row, no exception will be raised.

Parameters:

  • A (Integer)

    bit to set



30
31
32
33
# File 'lib/gorgon_amq-protocol/lib/gorgon_amq/bit_set.rb', line 30

def set(i)
  w = self.word_index(i)
  @words[w] |= (1 << i)
end

#unset(i) ⇒ Object

Unsets (unflags) given bit. This method allows bits to be unset more than once in a row, no exception will be raised.

Parameters:

  • A (Integer)

    bit to unset



51
52
53
54
55
56
# File 'lib/gorgon_amq-protocol/lib/gorgon_amq/bit_set.rb', line 51

def unset(i)
  w = self.word_index(i)
  return if w.nil?

  @words[w] &= ~(1 << i)
end