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

#each(&block) ⇒ Object

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



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

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



95
96
97
# File 'lib/contrek/finder/concurrent/queueable.rb', line 95

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



99
100
101
# File 'lib/contrek/finder/concurrent/queueable.rb', line 99

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

#map(&block) ⇒ Object



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

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

#move_from(queueable, &block) ⇒ Object



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

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

#next_of!(node) ⇒ Object



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

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

#pop!Object



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

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

#reverse_each(&block) ⇒ Object



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

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



103
104
105
# File 'lib/contrek/finder/concurrent/queueable.rb', line 103

def rewind!
  @iterator = 0
end

#to_aObject



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

def to_a
  map(&:payload)
end