Class: Contrek::Concurrent::Part

Inherits:
Object
  • Object
show all
Includes:
Queueable
Defined in:
lib/contrek/finder/concurrent/part.rb

Constant Summary collapse

SEAM =
1
EXCLUSIVE =
0
ADDED =
2

Instance Attribute Summary collapse

Attributes included from Queueable

#head, #size, #tail

Instance Method Summary collapse

Methods included from Queueable

#add, #each, #forward!, #iterator, #map, #move_from, #next_of!, #pop!, #rem, #reverse_each, #rewind!, #to_a

Constructor Details

#initialize(type, polyline) ⇒ Part

Returns a new instance of Part.



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

def initialize(type, polyline)
  @type = type
  @polyline = polyline
  @next = nil
  @circular_next = nil
  @prev = nil
  @dead_end = false
  @touched = false
  @inverts = false
  @trasmuted = false
  @versus = 0
  @mirror = false
end

Instance Attribute Details

#circular_nextObject

Returns the value of attribute circular_next.



13
14
15
# File 'lib/contrek/finder/concurrent/part.rb', line 13

def circular_next
  @circular_next
end

#dead_endObject

Returns the value of attribute dead_end.



13
14
15
# File 'lib/contrek/finder/concurrent/part.rb', line 13

def dead_end
  @dead_end
end

#invertsObject

Returns the value of attribute inverts.



13
14
15
# File 'lib/contrek/finder/concurrent/part.rb', line 13

def inverts
  @inverts
end

#mirrorObject

Returns the value of attribute mirror.



13
14
15
# File 'lib/contrek/finder/concurrent/part.rb', line 13

def mirror
  @mirror
end

#nextObject

Returns the value of attribute next.



13
14
15
# File 'lib/contrek/finder/concurrent/part.rb', line 13

def next
  @next
end

#polylineObject (readonly)

Returns the value of attribute polyline.



12
13
14
# File 'lib/contrek/finder/concurrent/part.rb', line 12

def polyline
  @polyline
end

#prevObject

Returns the value of attribute prev.



13
14
15
# File 'lib/contrek/finder/concurrent/part.rb', line 13

def prev
  @prev
end

#touchedObject (readonly)

Returns the value of attribute touched.



12
13
14
# File 'lib/contrek/finder/concurrent/part.rb', line 12

def touched
  @touched
end

#trasmutedObject

Returns the value of attribute trasmuted.



13
14
15
# File 'lib/contrek/finder/concurrent/part.rb', line 13

def trasmuted
  @trasmuted
end

#typeObject

Returns the value of attribute type.



13
14
15
# File 'lib/contrek/finder/concurrent/part.rb', line 13

def type
  @type
end

#versusObject

Returns the value of attribute versus.



13
14
15
# File 'lib/contrek/finder/concurrent/part.rb', line 13

def versus
  @versus
end

Instance Method Details

#add_position(position) ⇒ Object



32
33
34
35
# File 'lib/contrek/finder/concurrent/part.rb', line 32

def add_position(position)
  hub = is?(EXCLUSIVE) ? nil : polyline.tile.cluster.hub
  add(Position.new(position: position, hub: hub))
end

#continuum_to?(other) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/contrek/finder/concurrent/part.rb', line 83

def continuum_to?(other)
  return [] if size <= 2 && inverts && other.size <= 2 && other.inverts
  return [] if other.head.nil?

  target = other.head.payload
  cursor = tail
  while cursor
    if cursor.payload == target
      s = cursor
      o = other.head
      match = true
      nodes = []
      while s && o
        if s.payload != o.payload
          match = false
          break
        end
        nodes << s.end_point
        s = s.next
        o = o.next
      end
      if match && s.nil?
        return nodes
      end
    end
    cursor = cursor.prev
  end
  []
end

#innerable?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/contrek/finder/concurrent/part.rb', line 65

def innerable?
  (@touched == false) && is?(EXCLUSIVE)
end

#inspectObject



61
62
63
# File 'lib/contrek/finder/concurrent/part.rb', line 61

def inspect
  "part #{polyline.parts.index(self)} (mir=#{@mirror} versus=#{@versus} inv=#{@inverts} trm=#{@trasmuted} touched=#{@touched} dead_end =#{@dead_end}, #{size}x) of #{polyline.named} (#{name}) (#{to_a.map { |e| "[#{e[:x]},#{e[:y]}]" }.join})"
end

#is?(type) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/contrek/finder/concurrent/part.rb', line 28

def is?(type)
  @type == type
end

#nameObject



55
56
57
58
59
# File 'lib/contrek/finder/concurrent/part.rb', line 55

def name
  {Part::EXCLUSIVE => "EXCLUSIVE",
   Part::SEAM => "SEAM",
   Part::ADDED => "ADDED"}[type]
end

#next_position(force_position = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/contrek/finder/concurrent/part.rb', line 37

def next_position(force_position = nil)
  if force_position
    move_to_this = reverse_each { |pos| break pos if pos.payload == force_position.payload }
    next_of!(move_to_this)
    force_position
  else
    return nil if iterator.nil?
    position = iterator
    @touched = true
    forward!
    position
  end
end

#orient!Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/contrek/finder/concurrent/part.rb', line 69

def orient!
  @versus = if size <= 1 || (size == 2 && @inverts)
    0
  else
    diff = tail.payload[:y] - head.payload[:y]
    if diff == 0
      @mirror = true
      0
    else
      diff.positive? ? 1 : -1
    end
  end
end

#touch!Object



51
52
53
# File 'lib/contrek/finder/concurrent/part.rb', line 51

def touch!
  @touched = true
end