Class: Parasort::Compound
- Inherits:
-
Object
- Object
- Parasort::Compound
- Includes:
- Enumerable
- Defined in:
- lib/parasort.rb
Constant Summary collapse
- GRANULES_COUNT =
128
Instance Attribute Summary collapse
-
#tempdir ⇒ Object
readonly
Returns the value of attribute tempdir.
Instance Method Summary collapse
- #add(level, path) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(tempdir) ⇒ Compound
constructor
A new instance of Compound.
- #pack! ⇒ Object
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
#tempdir ⇒ Object (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 |