Class: AMQ::BitSet
- Inherits:
-
Object
- Object
- AMQ::BitSet
- Defined in:
- lib/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 Attribute Summary collapse
-
#words_in_use ⇒ Object
readonly
Returns the value of attribute words_in_use.
Class Method Summary collapse
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.
-
#next_clear_bit ⇒ Object
clear.
-
#set(i) ⇒ Object
Sets (flags) given bit.
-
#to_s ⇒ Object
next_clear_bit.
-
#unset(i) ⇒ Object
Unsets (unflags) given bit.
- #word_index(i) ⇒ Object
Constructor Details
#initialize(nbits) ⇒ BitSet
Returns a new instance of BitSet.
22 23 24 25 26 |
# File 'lib/amq/bit_set.rb', line 22 def initialize(nbits) @nbits = nbits self.init_words(nbits) end |
Instance Attribute Details
#words_in_use ⇒ Object (readonly)
Returns the value of attribute words_in_use.
11 12 13 |
# File 'lib/amq/bit_set.rb', line 11 def words_in_use @words_in_use end |
Class Method Details
.number_of_trailing_ones(num) ⇒ Object
94 95 96 97 98 99 |
# File 'lib/amq/bit_set.rb', line 94 def self.number_of_trailing_ones(num) 0.upto(BITS_PER_WORD) do |bit| return bit if num[bit] == 0 end BITS_PER_WORD end |
Instance Method Details
#clear ⇒ Object
Clears all bits in the set
67 68 69 |
# File 'lib/amq/bit_set.rb', line 67 def clear self.init_words(@nbits) end |
#get(i) ⇒ Boolean Also known as: []
Fetches flag value for given bit.
44 45 46 47 48 49 |
# File 'lib/amq/bit_set.rb', line 44 def get(i) check_range(i) w = self.word_index(i) (@words[w] & (1 << i % BITS_PER_WORD)) != 0 end |
#next_clear_bit ⇒ Object
clear
71 72 73 74 75 76 77 78 79 |
# File 'lib/amq/bit_set.rb', line 71 def next_clear_bit() @words.each_with_index do |word, i| if word == WORD_MASK next end return i * BITS_PER_WORD + BitSet.number_of_trailing_ones(word) end -1 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.
32 33 34 35 36 37 |
# File 'lib/amq/bit_set.rb', line 32 def set(i) check_range(i) w = self.word_index(i) result = @words[w] |= (1 << (i % BITS_PER_WORD)) result end |
#to_s ⇒ Object
next_clear_bit
81 82 83 84 85 86 87 |
# File 'lib/amq/bit_set.rb', line 81 def to_s result = "" @words.each do |w| result += w.to_s(2).rjust(BITS_PER_WORD,'0') + ":" end result 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.
56 57 58 59 60 61 62 63 |
# File 'lib/amq/bit_set.rb', line 56 def unset(i) check_range(i) w = self.word_index(i) return if w.nil? result = @words[w] &= ~(1 << i % BITS_PER_WORD) result end |
#word_index(i) ⇒ Object
102 103 104 |
# File 'lib/amq/bit_set.rb', line 102 def word_index(i) i >> ADDRESS_BITS_PER_WORD end |