Class: GorgonAMQ::BitSet
- Inherits:
-
Object
- Object
- GorgonAMQ::BitSet
- 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
-
#clear ⇒ Object
Clears all bits in the set.
-
#get(i) ⇒ Boolean
(also: #[])
Fetches flag value for given bit.
-
#initialize(nbits) ⇒ BitSet
constructor
A new instance of BitSet.
-
#set(i) ⇒ Object
Sets (flags) given bit.
-
#unset(i) ⇒ Object
Unsets (unflags) given bit.
Constructor Details
#initialize(nbits) ⇒ BitSet
Returns a new instance of BitSet.
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
#clear ⇒ Object
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.
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.
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.
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 |