Class: AMQ::BitSet

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nbits) ⇒ BitSet

Returns a new instance of BitSet.

Parameters:

  • of bits in the set

API:

  • public



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_useObject (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

#clearObject

Clears all bits in the set

API:

  • public



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.

Parameters:

  • bit to fetch

Returns:

  • true if given bit is set, false otherwise

API:

  • public



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_bitObject

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.

Parameters:

  • bit to set

API:

  • public



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_sObject

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.

Parameters:

  • bit to unset

API:

  • public



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