def save_to_stream(io)
header_size = 4 * 7
table_size = 4 * 2 * size
hash_table_size = next_prime((size * 4) / 3)
hash_table_size = 3 if hash_table_size <= 2
header = Header.new(
MAGIC_LITTLE_ENDIAN,
0,
size,
header_size,
header_size + table_size,
hash_table_size,
header_size + table_size * 2
)
io.write(header.to_a.pack('a4V*'))
ary = to_a
ary.sort!{|a, b| a[0] <=> b[0]}
pos = header.hash_table_size * 4 + header.hash_table_offset
orig_table_data = Array.new()
ary.each{|item, _|
orig_table_data.push(item.bytesize)
orig_table_data.push(pos)
pos += item.bytesize + 1
}
io.write(orig_table_data.pack('V*'))
trans_table_data = Array.new()
ary.each{|_, item|
trans_table_data.push(item.bytesize)
trans_table_data.push(pos)
pos += item.bytesize + 1
}
io.write(trans_table_data.pack('V*'))
hash_tab = Array.new(hash_table_size)
j = 0
ary[0...size].each {|key, _|
hash_val = hash_string(key)
idx = hash_val % hash_table_size
if hash_tab[idx] != nil
incr = 1 + (hash_val % (hash_table_size - 2))
begin
if (idx >= hash_table_size - incr)
idx -= hash_table_size - incr
else
idx += incr
end
end until (hash_tab[idx] == nil)
end
hash_tab[idx] = j + 1
j += 1
}
hash_tab.collect!{|i| i ? i : 0}
io.write(hash_tab.pack('V*'))
ary.each{|item, _| io.write(item); io.write("\0") }
ary.each{|_, item| io.write(item); io.write("\0") }
self
end