Class: CheapBits

Inherits:
Object
  • Object
show all
Defined in:
lib/cheap_bits.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block_size_exponent, base_dir, fn, xlat_ext) ⇒ CheapBits

Returns a new instance of CheapBits.



16
17
18
19
20
21
22
23
# File 'lib/cheap_bits.rb', line 16

def initialize(block_size_exponent, base_dir, fn, xlat_ext)
  @afn = "#{base_dir}/#{fn}#{xlat_ext}"
  @block_size = 1 << block_size_exponent
  @fd_in = nil
  @current_block = ''
  @bits_total = 0
  @bit_offset = 0
end

Class Method Details

.getbit(random_block, bit_offset) ⇒ Object



9
10
11
12
13
14
# File 'lib/cheap_bits.rb', line 9

def self.getbit(random_block, bit_offset)
  byte_offset = bit_offset >> 3
  byte_bit_offset = bit_offset - (byte_offset << 3)
  byte = random_block.getbyte(byte_offset)
  1 & (byte >> (7 - byte_bit_offset))
end

.readblock(fd_in, block_size) ⇒ Object



3
4
5
6
7
# File 'lib/cheap_bits.rb', line 3

def self.readblock(fd_in, block_size)
  fd_in.readpartial block_size
rescue EOFError
  nil
end

Instance Method Details

#broken_random(n) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cheap_bits.rb', line 96

def broken_random(n)
  current = n
  acc = 0
  while current > 0
    odd = 1 == 1 & current
    current = current >> 1
    if current > 0
      acc += current if 1 == getbit
    end
    if odd
      if (1 == getbit)
        acc += 1
      else
        current += 1
      end
    end
  end
  acc - 1
end

#closeObject



38
39
40
# File 'lib/cheap_bits.rb', line 38

def close
  @fd_in.close if @fd_in
end

#get_many_random(how_many, what) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/cheap_bits.rb', line 116

def get_many_random(how_many, what)
  a = []
  what.times {a << 0}
  how_many.times do
    r = random(what)
    a[r] +=  1
  end
  a
end

#getbitObject



54
55
56
57
58
59
# File 'lib/cheap_bits.rb', line 54

def getbit
  readblock if @bit_offset == @bits_total
  bit = self.class.getbit(@current_block, @bit_offset)
  @bit_offset += 1
  bit
end

#getbits_as_number(how_many) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/cheap_bits.rb', line 61

def getbits_as_number(how_many)
  return nil unless how_many > 0
  first_one_bit_found = false
  bits = 0
  how_many.times do
    bit = getbit
    if first_one_bit_found
      bits = bits << 1
      bits += bit
    else
      if 1 == bit
        bits = 1
        first_one_bit_found = true
      end
    end
  end
  bits
end

#openObject



42
43
44
# File 'lib/cheap_bits.rb', line 42

def open
  @fd_in = File.open(@afn)
end

#random(n) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cheap_bits.rb', line 80

def random(n)
  return nil unless n > 0
  return 0 if 1 == n
  bits_needed = 0
  power_of_two = 1
  while n > power_of_two
    bits_needed += 1
    power_of_two = power_of_two << 1
  end
  bits = power_of_two
  while bits >= n
    bits = getbits_as_number bits_needed
  end
  bits
end

#readblockObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cheap_bits.rb', line 25

def readblock
  open unless @fd_in
  s = self.class.readblock @fd_in, @block_size
  if !s
    rewind
    s = self.class.readblock @fd_in, @block_size
  end
  @current_block = s
  @bits_total = @current_block.length << 3
  @bit_offset = 0
  s
end

#rewindObject



46
47
48
49
50
51
52
# File 'lib/cheap_bits.rb', line 46

def rewind
  close
  open
  @current_block = ''
  @bits_total = 0
  @bit_offset = 0
end