Class: Parasort::Compound

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/parasort.rb

Constant Summary collapse

GRANULES_COUNT =
128

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tempdir) ⇒ Compound

Returns a new instance of Compound.



34
35
36
37
38
39
# File 'lib/parasort.rb', line 34

def initialize(tempdir)
  @tempdir = tempdir
  @compound = Hash.new{ |h, k| h[k] = [] }
  pid = Process.pid
  at_exit{ FileUtils.rm_rf(tempdir) if pid == Process.pid }
end

Instance Attribute Details

#tempdirObject (readonly)

Returns the value of attribute tempdir.



30
31
32
# File 'lib/parasort.rb', line 30

def tempdir
  @tempdir
end

Instance Method Details

#add(level, path) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/parasort.rb', line 41

def add(level, path)
  @compound[level] << path
  loop do
    level, granules = @compound.detect{ |lvl, grans| grans.count >= GRANULES_COUNT }
    break unless level

    @compound[level + 1] << Molecule.new(tempdir, granules.dup)
    @compound[level].clear
  end
  path
end

#each(&block) ⇒ Object



65
66
67
# File 'lib/parasort.rb', line 65

def each(&block)
  [].merge_sort(*@compound.flat_map{ |lvl, grans| grans.map(&:each) }).each(&block)
end

#pack!Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/parasort.rb', line 53

def pack!
  loop do
    break if @compound.each_value.map(&:count).sum <= GRANULES_COUNT

    level, granules = @compound.detect{ |lvl, grans| !grans.empty? }
    break unless level

    @compound[level + 1] << Molecule.new(tempdir, granules.dup)
    @compound[level].clear
  end
end