Module: Contrek::Concurrent::Partitionable

Included in:
Polyline
Defined in:
lib/contrek/finder/concurrent/partitionable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#partsObject (readonly)

Returns the value of attribute parts.



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

def parts
  @parts
end

Instance Method Details

#add_part(new_part) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/contrek/finder/concurrent/partitionable.rb', line 11

def add_part(new_part)
  last = @parts.last
  @parts << new_part
  last.next = last.circular_next = new_part if last
  new_part.circular_next = @parts.first
  new_part.prev = last

  new_part.orient! if new_part.is?(Part::SEAM)
end

#find_first_part_by_position(position, versus) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/contrek/finder/concurrent/partitionable.rb', line 30

def find_first_part_by_position(position, versus)
  @parts.find do |part|
    part.is?(Part::SEAM) &&
      part.versus == -versus &&
      position.end_point.queues.include?(part)
  end
end

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



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

def initialize(*args, **kwargs, &block)
  super
  @parts = []
end

#insert_after(part, new_part) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/contrek/finder/concurrent/partitionable.rb', line 21

def insert_after(part, new_part)
  part_index = @parts.index(part)
  @parts.insert(part_index + 1, new_part)
  new_part.prev = part
  new_part.next = new_part.circular_next = part.next
  part.next.prev = new_part if part.next
  part.next = part.circular_next = new_part
end

#inspect_partsObject



38
39
40
# File 'lib/contrek/finder/concurrent/partitionable.rb', line 38

def inspect_parts
  [" "] + ["#{self.class} parts=#{@parts.size}"] + @parts.map { |p| p.inspect } + [" "]
end

#partition!Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/contrek/finder/concurrent/partitionable.rb', line 42

def partition!
  current_part = nil
  @parts = []

  @raw.each_with_index do |position, n|
    if @tile.tg_border?(position)
      if current_part.nil?
        current_part = Part.new(Part::SEAM, self)
      elsif !current_part.is?(Part::SEAM)
        add_part(current_part)
        current_part = Part.new(Part::SEAM, self)
      end
    elsif current_part.nil?
      current_part = Part.new(Part::EXCLUSIVE, self)
    elsif !current_part.is?(Part::EXCLUSIVE)
      add_part(current_part)
      current_part = Part.new(Part::EXCLUSIVE, self)
    end
    if n > 0 && @raw[n - 1] == position
      current_part.inverts = true
    end
    current_part.add_position(position)
  end
  add_part(current_part)

  trasmute_parts!
end

#sew!(intersection, other) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/contrek/finder/concurrent/partitionable.rb', line 70

def sew!(intersection, other)
  matching_part_indexes, other_matching_part_indexes = intersection.transpose.map(&:sort)
  # other_matching_part_indexes and matching_part_indexes always must contain at least one element
  before_parts = other.parts[other_matching_part_indexes.last + 1..]
  after_parts = other_matching_part_indexes.first.zero? ? [] : other.parts[0..other_matching_part_indexes.first - 1]
  part_start = parts[matching_part_indexes.first]
  part_end = parts[matching_part_indexes.last]

  # left and right side reduces will be combined and later converted into orphan inners sequences
  returning_data = [[matching_part_indexes, parts], [other_matching_part_indexes, other.parts]].map do |matching_part_indexes, parts|
    lastn = 0
    result = []
    (matching_part_indexes.first + 1).upto(matching_part_indexes.last - 1) do |n|
      if matching_part_indexes.index(n).nil?
        part = parts[n]
        if part.is?(Part::SEAM) && part.size > 0 && !part.delayed # fallback, delays the shape
          part.delayed = true
          return nil
        end
        if (lastn == (n - 1)) && result.any?
          result.last.concat part.to_a
        else
          result << part.to_a
        end
        lastn = n
      end
    end
    result
  end

  if part_start != part_end
    (matching_part_indexes.last - 1).downto(matching_part_indexes.first + 1) do |n|
      delete_part = parts[n]
      delete_part.prev.next = delete_part.next if delete_part.prev
      delete_part.next.prev = delete_part.prev if delete_part.next
      parts.delete_at(n)
    end
  end

  all_parts = before_parts + after_parts
  will_be_last = all_parts.last
  all_parts.reverse_each do |part|
    insert_after(part_start, part)
    other.parts.delete(part)
    part.set_polyline(self)
  end

  part_start.type = Part::EXCLUSIVE
  new_end_part = Part.new(Part::EXCLUSIVE, self)
  new_end_part.add(part_end.tail)
  part_start.singleton! # reduce part to its head only

  if part_start != part_end
    part_end.prev.next = part_end.next if part_end.prev
    part_end.next.prev = part_end.prev if part_end.next
    parts.delete(part_end)
  end
  insert_after(will_be_last, new_end_part)

  reset_tracked_endpoints!

  returning_data
end