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.



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

def head
  @head
end

#sizeObject (readonly)

Returns the value of attribute size.



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

def size
  @size
end

#tailObject (readonly)

Returns the value of attribute tail.



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

def tail
  @tail
end

Instance Method Details

#add(node) ⇒ Object



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

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

#append(queueable) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/contrek/finder/concurrent/queueable.rb', line 50

def append(queueable)
  return if queueable.size.zero?
  queueable.each do |node|
    node.before_rem(queueable)
    node.owner = self
  end
  if @tail
    @tail.next = queueable.head
    queueable.head.prev = @tail
  else
    @head = queueable.head
  end
  @tail = queueable.tail
  @size += queueable.size
  queueable.reset!

  each { |node| node.after_add(self) }
end

#each(&block) ⇒ Object

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



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

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



113
114
115
# File 'lib/contrek/finder/concurrent/queueable.rb', line 113

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

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



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

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

#iteratorObject



117
118
119
# File 'lib/contrek/finder/concurrent/queueable.rb', line 117

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

#map(&block) ⇒ Object



90
91
92
93
94
# File 'lib/contrek/finder/concurrent/queueable.rb', line 90

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

#next_of!(node) ⇒ Object



125
126
127
128
129
# File 'lib/contrek/finder/concurrent/queueable.rb', line 125

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

#pop!Object



131
132
133
# File 'lib/contrek/finder/concurrent/queueable.rb', line 131

def pop!
  rem(@tail)
end

#rem(node) ⇒ Object



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

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

#reset!Object



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

def reset!
  @head = nil
  @tail = nil
  @size = 0
  @iterator = 0
end

#reverse_each(&block) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/contrek/finder/concurrent/queueable.rb', line 96

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



121
122
123
# File 'lib/contrek/finder/concurrent/queueable.rb', line 121

def rewind!
  @iterator = 0
end

#to_aObject



109
110
111
# File 'lib/contrek/finder/concurrent/queueable.rb', line 109

def to_a
  map(&:payload)
end