Class: BitDatabase

Inherits:
Object
  • Object
show all
Defined in:
lib/chem/utils/bitdb.rb

Constant Summary collapse

ARCH =
32

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, bit_length) ⇒ BitDatabase

Returns a new instance of BitDatabase.



19
20
21
22
23
24
25
26
# File 'lib/chem/utils/bitdb.rb', line 19

def initialize(filename, bit_length)
  @out = File.open(filename + ".dat", "w")
  @idx = File.open(filename + ".inf", "w")
  @bit_length = bit_length
  @n_bytes    = (bit_length - 1) / ARCH + 1
  @idx.write [@bit_length, @n_bytes].pack("l*")
  @current    = 0
end

Instance Attribute Details

#bit_lengthObject (readonly)

Returns the value of attribute bit_length.



17
18
19
# File 'lib/chem/utils/bitdb.rb', line 17

def bit_length
  @bit_length
end

Class Method Details

.open(filename, bit_length) {|db| ... } ⇒ Object

Yields:

  • (db)


43
44
45
46
47
# File 'lib/chem/utils/bitdb.rb', line 43

def self.open(filename, bit_length)
  db = new(filename, bit_length)
  yield db
  db.close
end

Instance Method Details

#closeObject



37
38
39
40
41
# File 'lib/chem/utils/bitdb.rb', line 37

def close
  @idx.write [@current * 1000].pack("l*")
  @idx.close
  @out.close
end

#push(ary) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/chem/utils/bitdb.rb', line 28

def push(ary)
  @current += 1
  @out.write ary.inject(Array.new(@n_bytes, 0)){|ret, num|
    raise Exception if num > @bit_length
    ret[num / ARCH] += (1 << (num % ARCH))
    ret
  }.pack('l*')
end