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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/contrek/finder/concurrent/queueable.rb', line 40

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/contrek/finder/concurrent/queueable.rb', line 63

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



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

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



134
135
136
# File 'lib/contrek/finder/concurrent/queueable.rb', line 134

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



138
139
140
# File 'lib/contrek/finder/concurrent/queueable.rb', line 138

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

#map(&block) ⇒ Object



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

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

#move_from(queueable, &block) ⇒ Object



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

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

#next_of!(node) ⇒ Object



146
147
148
149
150
# File 'lib/contrek/finder/concurrent/queueable.rb', line 146

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

#pop!Object



152
153
154
# File 'lib/contrek/finder/concurrent/queueable.rb', line 152

def pop!
  rem(@tail)
end

#rem(node) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/contrek/finder/concurrent/queueable.rb', line 23

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

#remove_adjacent_pairs!Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/contrek/finder/concurrent/queueable.rb', line 156

def remove_adjacent_pairs!
  unless @tail.nil?
    pointer = @tail
    loop do
      break if pointer == @head
      if pointer.payload == pointer.prev&.payload
        later = pointer.next
        rem pointer.prev
        rem pointer
        pointer = later.nil? ? @tail : later
        redo
      end
      break unless (pointer = pointer.prev)
    end
  end
end

#replace!(queueable) ⇒ Object



58
59
60
61
# File 'lib/contrek/finder/concurrent/queueable.rb', line 58

def replace!(queueable)
  reset!
  append(queueable)
end

#reset!Object



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

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

#reverse_each(&block) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/contrek/finder/concurrent/queueable.rb', line 117

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



142
143
144
# File 'lib/contrek/finder/concurrent/queueable.rb', line 142

def rewind!
  @iterator = 0
end

#singleton!Object



13
14
15
16
17
18
19
20
21
# File 'lib/contrek/finder/concurrent/queueable.rb', line 13

def singleton!
  if @head&.next
    @head.next.prev = nil
    @head.next = nil
  end
  @tail = nil
  @size = 1
  @iterator = 0
end

#to_aObject



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

def to_a
  map(&:payload)
end