Class: Duke::Iterator

Inherits:
RecursiveAction
  • Object
show all
Defined in:
lib/duke/iterator.rb

Overview

The iterator is a recursive action that executes in parallel on a ForkJoinPool.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(objects, threshold, block) ⇒ Duke::Iterator

Instantiate the new iterator.

Examples:

Create the iterator.

Enumerable.new([ 1, 2, 3 ], 100000)

Parameters:

  • objects (Array<Object>)

    The object array.

  • threshold (Integer)

    The single process threshold count.

Since:

  • 0.1.0



48
49
50
51
# File 'lib/duke/iterator.rb', line 48

def initialize(objects, threshold, block)
  super()
  @objects, @threshold, @block = objects, threshold, block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



13
14
15
# File 'lib/duke/iterator.rb', line 13

def block
  @block
end

#block The block to execute on each object.(Theblocktoexecuteoneachobject.) ⇒ Object (readonly)



13
# File 'lib/duke/iterator.rb', line 13

attr_reader :block, :objects, :threshold

#objectsObject (readonly)

Returns the value of attribute objects.



13
14
15
# File 'lib/duke/iterator.rb', line 13

def objects
  @objects
end

#objects The array to process.(Thearraytoprocess.) ⇒ Object (readonly)



13
# File 'lib/duke/iterator.rb', line 13

attr_reader :block, :objects, :threshold

#thresholdObject (readonly)

Returns the value of attribute threshold.



13
14
15
# File 'lib/duke/iterator.rb', line 13

def threshold
  @threshold
end

#threshold The single process threshold count.(Thesingleprocessthresholdcount.) ⇒ Object (readonly)



13
# File 'lib/duke/iterator.rb', line 13

attr_reader :block, :objects, :threshold

Instance Method Details

#computenil

Splits the work into the appropriate chunks and executes in parallel.

Examples:

Execute in parallel.

iterator.compute

Returns:

  • (nil)

    nil.

Since:

  • 0.1.0



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/duke/iterator.rb', line 23

def compute
  if objects.size <= threshold
    objects.each do |object|
      block.call(object)
    end
  else
    midpoint = objects.size / 2
    invoke_all(
      Iterator.new(objects[0, midpoint], threshold, block),
      Iterator.new(objects[midpoint, objects.size], threshold, block)
    )
  end
end

#invoke_all(*args) ⇒ nil

Convenience method for invoking against the class.

Examples:

Invoke the tasks.

iterator.invoke_all(tasks)

Parameters:

  • args (Array<RecursiveAction>)

    The tasks to execute.

Returns:

  • (nil)

    nil.

Since:

  • 0.1.0



63
64
65
# File 'lib/duke/iterator.rb', line 63

def invoke_all(*args)
  self.class.invoke_all(*args)
end