Class: TextAlignment::CultivationMap

Inherits:
Object
  • Object
show all
Defined in:
lib/text_alignment/cultivation_map.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCultivationMap

Returns a new instance of CultivationMap.



6
7
8
# File 'lib/text_alignment/cultivation_map.rb', line 6

def initialize
  @map = []
end

Instance Attribute Details

#mapObject (readonly)

Returns the value of attribute map.



4
5
6
# File 'lib/text_alignment/cultivation_map.rb', line 4

def map
  @map
end

Instance Method Details

#cultivate(regions) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/text_alignment/cultivation_map.rb', line 10

def cultivate(regions)
  @map += regions
  @map.sort!{|a, b| a[0] <=> b[0]}
  new_map = []
  @map.each do |region|
    if new_map.empty?
      new_map << region
    elsif new_map.last[1] > region[0]
      raise "Overlapping regions: #{new_map.last} : #{region}"
    elsif new_map.last[1] == region[0]
      new_map.last[1] == region[1]
    else
      new_map << region
    end
  end
  @map = new_map
end

#in_regions(region) ⇒ Object



48
49
50
# File 'lib/text_alignment/cultivation_map.rb', line 48

def in_regions(region)
  @map.select{|r| (r[1] > region[0] && r[1] <= region[1]) || (r[0] < region[1] && r[0] >= region[0])}
end

#index(target, string, position = 0) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/text_alignment/cultivation_map.rb', line 73

def index(target, string, position = 0)
  length = target.length
  loop do
    _begin = string.index(target, position)
    break if _begin.nil?
    position = search_again_position(_begin)
    next unless position.nil?
    break _begin if region_state([_begin, _begin + length])[0] == :open
    position = _begin + 1
  end
end

#last_cultivated_position(position) ⇒ Object



38
39
40
41
# File 'lib/text_alignment/cultivation_map.rb', line 38

def last_cultivated_position(position)
  ridx = @map.rindex{|r| r[1] <= position}
  ridx.nil? ? nil : @map[ridx][1]
end

#next_cultivated_position(position) ⇒ Object



43
44
45
46
# File 'lib/text_alignment/cultivation_map.rb', line 43

def next_cultivated_position(position)
  region = @map.bsearch{|r| position <= r[0]}
  region.nil? ? nil : region[0]
end

#region_state(region) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/text_alignment/cultivation_map.rb', line 52

def region_state(region)
  closed_parts = in_regions(region)
  if closed_parts.empty?
    [:open, region]
  else
    if front_open?(region, closed_parts)
      if rear_open?(region, closed_parts)
        [:middle_closed, [closed_parts.first[0], closed_parts.last[1]]]
      else
        [:front_open, [region[0], closed_parts.first[0]]]
      end
    else
      if rear_open?(region, closed_parts)
        [:rear_open, [closed_parts.last[1], region[1]]]
      else
        [:closed, nil]
      end
    end
  end
end

#search_again_position(position, end_position = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/text_alignment/cultivation_map.rb', line 28

def search_again_position(position, end_position = nil)
  end_position ||= position
  region = @map.bsearch{|r| end_position < r[1]}
  if region.nil? || region[0] > position
    nil
  else
    region[1]
  end
end