Class: EnumeratorConcurrent::Queued

Inherits:
Array
  • Object
show all
Defined in:
lib/enumerator_concurrent/queued.rb

Overview

Enumerator with thread count speciyfied to handle loop

Instance Method Summary collapse

Methods inherited from Array

#concurrent

Constructor Details

#initialize(arr, thread_count) ⇒ Queued

Returns a new instance of Queued.

Parameters:

  • arr (Array)
  • thread_count (Int)

    how many thread should be handle the Enumarator



7
8
9
10
11
12
# File 'lib/enumerator_concurrent/queued.rb', line 7

def initialize(arr, thread_count)
  @thread_count = 0...thread_count
  @queue = Queue.new
  @arr = arr
  super(arr)
end

Instance Method Details

#each {|x| ... } ⇒ self

Yields:

  • (x)

    element in array

Returns:

  • (self)


16
17
18
19
20
21
22
23
24
# File 'lib/enumerator_concurrent/queued.rb', line 16

def each
  @arr.each { |x| @queue.push x }
  init_workers do
    pop_queue do |x|
      yield(x)
    end
  end
  self
end

#map {|x| ... } ⇒ Array

Returns with modified values.

Yields:

  • (x)

    element in self

Returns:

  • (Array)

    with modified values



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/enumerator_concurrent/queued.rb', line 28

def map
  return_value = []
  @arr.map.with_index { |x, i| @queue.push(i => x) }
  init_workers do
    pop_queue do |x|
      key, value = x.first
      return_value[key] = yield(value)
    end
  end
  return_value
end