Module: Contrek::Concurrent::Queueable

Included in:
Part, Sequence
Defined in:
lib/contrek/finder/concurrent/queueable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#headObject (readonly)

Returns the value of attribute head.



4
5
6
# File 'lib/contrek/finder/concurrent/queueable.rb', line 4

def head
  @head
end

#sizeObject (readonly)

Returns the value of attribute size.



4
5
6
# File 'lib/contrek/finder/concurrent/queueable.rb', line 4

def size
  @size
end

#tailObject (readonly)

Returns the value of attribute tail.



4
5
6
# File 'lib/contrek/finder/concurrent/queueable.rb', line 4

def tail
  @tail
end

Instance Method Details

#add(node) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/contrek/finder/concurrent/queueable.rb', line 30

def add(node)
  node.owner&.rem(node)  # verifies owner presence

  if @tail
    @tail.next = node
    node.prev = @tail
  else
    @head = node
    node.prev = nil
  end
  @tail = node
  node.next = nil
  node.owner = self
  @size += 1

  node.after_add(self)
end

#each(&block) ⇒ Object

from yield: false => stop, true => continue



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/contrek/finder/concurrent/queueable.rb', line 57

def each(&block)
  last = nil
  unless @head.nil?
    pointer = @head
    loop do
      break unless yield(pointer)
      last = pointer
      break unless (pointer = pointer.next)
    end
  end
  last
end

#forward!Object



93
94
95
# File 'lib/contrek/finder/concurrent/queueable.rb', line 93

def forward!
  @iterator = iterator.next unless iterator.nil?
end

#initialize(*args, **kwargs, &block) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/contrek/finder/concurrent/queueable.rb', line 5

def initialize(*args, **kwargs, &block)
  super
  @head = nil
  @tail = nil
  @iterator = 0
  @size = 0
end

#iteratorObject



97
98
99
# File 'lib/contrek/finder/concurrent/queueable.rb', line 97

def iterator
  (@iterator == 0) ? @head : @iterator
end

#map(&block) ⇒ Object



70
71
72
73
74
# File 'lib/contrek/finder/concurrent/queueable.rb', line 70

def map(&block)
  ret_array = []
  each { |node| ret_array << yield(node) }
  ret_array
end

#move_from(queueable, &block) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/contrek/finder/concurrent/queueable.rb', line 48

def move_from(queueable, &block)
  queueable.rewind!
  while (node = queueable.iterator)
    queueable.forward!
    add(node) if yield(node)
  end
end

#next_of!(node) ⇒ Object



105
106
107
108
109
# File 'lib/contrek/finder/concurrent/queueable.rb', line 105

def next_of!(node)
  raise "nil node" if node.nil?
  raise "wrong node" if node.owner != self
  @iterator = node.next
end

#pop!Object



111
112
113
# File 'lib/contrek/finder/concurrent/queueable.rb', line 111

def pop!
  rem(@tail)
end

#rem(node) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/contrek/finder/concurrent/queueable.rb', line 13

def rem(node)
  Raise "Not my node" if node.owner != self

  node.before_rem(self)

  node.prev&.next = node.next
  node.next&.prev = node.prev

  @head = node.next if node == @head
  @tail = node.prev if node == @tail

  node.next = node.prev = node.owner = nil
  @size -= 1

  node
end

#reverse_each(&block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/contrek/finder/concurrent/queueable.rb', line 76

def reverse_each(&block)
  last = nil
  unless @tail.nil?
    pointer = @tail
    loop do
      yield(pointer)
      last = pointer
      break unless (pointer = pointer.prev)
    end
  end
  last
end

#rewind!Object



101
102
103
# File 'lib/contrek/finder/concurrent/queueable.rb', line 101

def rewind!
  @iterator = 0
end

#to_aObject



89
90
91
# File 'lib/contrek/finder/concurrent/queueable.rb', line 89

def to_a
  map(&:payload)
end