Class: AMQP::BitSet
- Inherits:
-
Object
- Object
- AMQP::BitSet
- Defined in:
- lib/amqp/bit_set.rb
Overview
Very minimalistic, pure Ruby implementation of bit set. Inspired by java.util.BitSet, although significantly smaller in scope.
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.
18 19 20 21 22 |
# File 'lib/amqp/bit_set.rb', line 18 def initialize(nbits) @nbits = nbits self.init_words(nbits) end |
Instance Method Details
#clear ⇒ Object
Clears all bits in the set
58 59 60 |
# File 'lib/amqp/bit_set.rb', line 58 def clear self.init_words(@nbits) end |
#get(i) ⇒ Boolean Also known as: []
Fetches flag value for given bit.
38 39 40 41 42 |
# File 'lib/amqp/bit_set.rb', line 38 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.
28 29 30 31 |
# File 'lib/amqp/bit_set.rb', line 28 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.
49 50 51 52 53 54 |
# File 'lib/amqp/bit_set.rb', line 49 def unset(i) w = self.word_index(i) return if w.nil? @words[w] &= ~(1 << i) end |