Class: Antlr4::Runtime::BitSet

Inherits:
Object
  • Object
show all
Defined in:
lib/antlr4/runtime/bit_set.rb

Constant Summary collapse

MAX_BITS =
128

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBitSet

Returns a new instance of BitSet.



8
9
10
# File 'lib/antlr4/runtime/bit_set.rb', line 8

def initialize
  @bits = 0
end

Instance Attribute Details

#bitsObject (readonly)

Returns the value of attribute bits.



6
7
8
# File 'lib/antlr4/runtime/bit_set.rb', line 6

def bits
  @bits
end

Instance Method Details

#cardinalityObject



31
32
33
# File 'lib/antlr4/runtime/bit_set.rb', line 31

def cardinality
  RumourHash.bit_count(@bits)
end

#clear(idx = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/antlr4/runtime/bit_set.rb', line 16

def clear(idx = nil)
  # check for zero to avoid trying to take the log2 of it, which
  # returns -Infinity
  if !idx || bits == 0
    @bits = 0
    return
  end

  @bits &= 2**Math.log2(bits).ceil - 2**idx - 1
end

#get(x) ⇒ Object



27
28
29
# File 'lib/antlr4/runtime/bit_set.rb', line 27

def get(x)
  (@bits & (1 << x)) > 0 ? true : false
end

#next_set_bit(bit) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/antlr4/runtime/bit_set.rb', line 39

def next_set_bit(bit)
  result = bit
  i = 0
  mask = (1 << bit)
  while i < MAX_BITS
    if (@bits & mask) > 0
      return result
    end
    result += 1
    mask <<= 1
    i += 1
  end
  -1
end

#or(bit_set) ⇒ Object



35
36
37
# File 'lib/antlr4/runtime/bit_set.rb', line 35

def or(bit_set)
  @bits |= bit_set.bits
end

#set(x) ⇒ Object



12
13
14
# File 'lib/antlr4/runtime/bit_set.rb', line 12

def set(x)
  @bits |= (1 << x)
end

#to_sObject



54
55
56
57
58
59
# File 'lib/antlr4/runtime/bit_set.rb', line 54

def to_s
  buf = '['
  buf << @bits.to_s(2)
  buf << ']'
  buf
end