Class: HMap::HMapSaver

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#string_tableObject (readonly)

Returns the value of attribute string_table.



10
11
12
# File 'lib/hmap/hmap/hmap_saver.rb', line 10

def string_table
  @string_table
end

Class Method Details

.new_from_buckets(buckets) ⇒ Object



12
13
14
15
16
# File 'lib/hmap/hmap/hmap_saver.rb', line 12

def self.new_from_buckets(buckets)
  saver = new
  saver.add_to_buckets(buckets) unless buckets.empty?
  saver
end

Instance Method Details

#add_to_buckets(buckets) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hmap/hmap/hmap_saver.rb', line 18

def add_to_buckets(buckets)
  return if buckets.nil?

  hash_t = HashTable::HashTable.new_from_vlaue_placeholder(buckets.length, EMPTY_BUCKET, expand: true)
  ta = HashTable::StringHashTraits.new { |bs| HMapBucket.new(*bs).serialize }
  buckets.each_pair do |key, value|
    hash_t.set(key, value, ta)
  end
  @strings = ta.string_table
  @buckets = hash_t.values
  @entries = hash_t.num_entries
end

#write_to(path) ⇒ Object



31
32
33
# File 'lib/hmap/hmap/hmap_saver.rb', line 31

def write_to(path)
  MapFile.new(@strings, @buckets, @entries).write(Pathname(path))
end