class Bitfifo

Constants

VERSION

Public Class Methods

new() click to toggle source
# File lib/bitfifo.rb, line 16
def initialize
  @bits = 0
  @size = 0
  self
end
reverse(data,nb) click to toggle source
# File lib/bitfifo.rb, line 4
def self.reverse(data,nb)
  return nil if data.nil?
  data &= (1 << nb) - 1
  rtn = 0
  nb.times do |ii|
    rtn <<= 1
    rtn |= (data & 1)
    data >>= 1
  end
  rtn
end

Public Instance Methods

count() click to toggle source
# File lib/bitfifo.rb, line 77
def count
  @size
end
empty() click to toggle source
# File lib/bitfifo.rb, line 81
def empty
  rtn = @bits
  @bits = 0
  @size = 0
  rtn
end
empty?() click to toggle source
# File lib/bitfifo.rb, line 88
def empty?
  @size==0
end
get(nb=1) click to toggle source
# File lib/bitfifo.rb, line 48
def get(nb=1)
  mask = (1 << nb) - 1
  rtn = (@bits & (mask << (@size-nb))) >> (@size-nb)
  @size -= nb
  if @size < 0
    @size=0
    return nil
  end
  rtn
end
put(data, nb=nil) click to toggle source
# File lib/bitfifo.rb, line 32
def put(data, nb=nil)
  if nb.nil?
    data=1 if data==true
    data=0 if data==false
    @bits <<= 1
    @bits |= data & 1
    @size += 1
  else
    mask = (1 << nb) - 1
    data &= mask
    @bits <<= nb
    @bits |= data
    @size += nb
  end
end
rget(nb=1) click to toggle source
# File lib/bitfifo.rb, line 69
def rget(nb=1)
  self.class.reverse(get(nb),nb)
end
rput(data, nb=nil) click to toggle source
# File lib/bitfifo.rb, line 64
def rput(data, nb=nil)
  return put(data) if nb.nil?
  put(self.class.reverse(data,nb),nb)
end
size() click to toggle source
# File lib/bitfifo.rb, line 73
def size
  @size
end
to_i(reduce=true) click to toggle source
# File lib/bitfifo.rb, line 22
def to_i(reduce=true)
  if reduce
    size = @size
    rtn = get(size)
    unget(size)
    return rtn
  end
  return @bits
end
unget(nb=1) click to toggle source
# File lib/bitfifo.rb, line 59
def unget(nb=1)
  @size += nb
  self
end