44
55class Range
66 def intersection ( other )
7- return nil if ( self . max < other . begin or other . max < self . begin )
8- [ self . begin , other . begin ] . max ..[ self . max , other . max ] . min
7+ return ( 1 .. 0 ) if ( self . last < other . begin or other . last < self . begin )
8+ [ self . begin , other . begin ] . last ..[ self . last , other . last ] . min
99 end
10+
11+ def shift_left ( indx )
12+ if indx . class == Fixnum then
13+ return ( self . begin - indx ) ..( self . last - indx )
14+ end
15+ end
16+
17+ def shift_right ( indx )
18+ if indx . class == Fixnum then
19+ return ( self . begin + indx ) ..( self . last + indx )
20+ end
21+ end
22+
23+
1024 alias_method :& , :intersection
25+ alias_method :+ , :shift_right
26+ alias_method :- , :shift_left
27+
1128end
1229
1330
@@ -23,6 +40,22 @@ def bubble_sort!(&closure)
2340 end
2441 end
2542 end
43+ end
44+ end
45+
46+ class Pairs
47+ def initialize ( arr )
48+ @arr = arr
49+ end
50+
51+ def print ( sp = 0 )
52+ @arr . each do |pair |
53+ end
54+ end
55+ end
56+
57+ class Chain
58+ def initialize ( arr )
2659
2760 end
2861end
@@ -31,7 +64,7 @@ class Recreator
3164
3265 def initialize ( type )
3366 @type = type
34- @Debug = true & false ;
67+ @Debug = $DEBUG_project ;
3568 @sc = SpaceConf . new ( type )
3669 end
3770
@@ -46,7 +79,7 @@ def get_string_from_meta(meta)
4679 meta . value . each do |token |
4780 if token . class == MetaExpression then
4881 min_spaces = @sc . get_min ( prev_min , token . get_first_token )
49- if @Debug then
82+ if @Debug > 0 then
5083 p "from " , prev_min . type
5184 p "to" , token . get_first_token . type
5285 p min_spaces
@@ -57,7 +90,7 @@ def get_string_from_meta(meta)
5790 prev_min = token . get_last_token ;
5891 else
5992 min_spaces = @sc . get_min ( prev_min , token )
60- if @Debug then
93+ if @Debug > 0 then
6194 p "debug:"
6295 p "from " , prev_min
6396 p "to" , token
@@ -92,7 +125,7 @@ def multiline_reconstruction(meta_array, chains)
92125 begin
93126 if t_token . class == Token then
94127 min_spaces = @sc . get_min ( @prev_tokens [ line_index ] , t_token )
95- if @Debug then
128+ if @Debug > 0 then
96129 p "from " , @prev_tokens [ line_index ] . type
97130 p "to" , t_token . type
98131 p min_spaces
@@ -102,7 +135,7 @@ def multiline_reconstruction(meta_array, chains)
102135 @prev_tokens [ line_index ] = t_token ;
103136 else
104137 min_spaces = @sc . get_min ( @prev_tokens [ line_index ] , t_token . get_first_token )
105- if @Debug then
138+ if @Debug > 0 then
106139 p "from " , @prev_tokens [ line_index ] . type
107140 p "to" , t_token . get_first_token . type
108141 p min_spaces
@@ -113,7 +146,7 @@ def multiline_reconstruction(meta_array, chains)
113146 end
114147 rescue Exception => e
115148 p e
116- p e . backtrace
149+ e . backtrace . each { | x | p x }
117150 p "Exceprion: "
118151 p "line_index: " + line_index . to_s
119152 p "token_index: " + indexes [ line_index ] . to_s
@@ -127,6 +160,10 @@ def multiline_reconstruction(meta_array, chains)
127160 end
128161 # chain processing
129162
163+ if @Debug > 0
164+ puts "chains"
165+ chains . each { |x | p x }
166+ end
130167 chains . each do |chain |
131168
132169 begin_line = chain [ 0 ] ;
@@ -176,7 +213,7 @@ def multiline_reconstruction(meta_array, chains)
176213 params . push ( t2 . str_index ) ;
177214 limit = @sc . get_max ( t1 , t2 , params ) ;
178215 accept = delta [ i ] < limit ;
179- if @Debug & false then
216+ if @Debug > 1 then
180217 p "prev: " + t1 . type . to_s ;
181218 p "next: " + t2 . type . to_s ;
182219 p "delta:" + delta [ i ] . to_s ;
@@ -218,8 +255,13 @@ def multiline_reconstruction(meta_array, chains)
218255
219256 # input [ [ [index, index], [i, i], ... ], ...]
220257 # output [ [line_id, [[token-id, token-id], [t-id, t-id], ...]], .... ]
221-
258+ # line_id - is first line of chain
222259 def generate_chains ( pairs_array )
260+
261+ if @Debug > 0
262+ puts "generate_chains(pairs_array): "
263+ pairs_array . each { |x | p x }
264+ end
223265 n = pairs_array . size ( ) ;
224266 used_indexes = n . times . map { { } } ;
225267 curr_indexes = [ 0 ] * n ;
@@ -273,28 +315,56 @@ def generate_chains(pairs_array)
273315 end
274316
275317 # sort
276- chains . bubble_sort! do |x , y |
318+
319+ chains . sort! do |x , y |
320+
277321 # TODO add intersection here! Add convertation to line!
278- x_range = x [ 0 ] ..( x [ 0 ] + x [ 1 ] . size )
279- y_range = y [ 0 ] ..( y [ 0 ] + y [ 1 ] . size )
322+ x_range = x [ 0 ] ..( x [ 0 ] + x [ 1 ] . size )
323+ y_range = y [ 0 ] ..( y [ 0 ] + y [ 1 ] . size )
280324
281325
282- str_inter = x_range & y_range ;
326+ str_inter = x_range & y_range ; # intersection of ranges
283327
284328 x_min = 0 ;
285329 y_min = 0 ;
286330
287331 x_by_lines = x [ 1 ] . map { |i | i [ 0 ] } + [ x [ 1 ] . last [ 1 ] ]
288332 y_by_lines = y [ 1 ] . map { |i | i [ 0 ] } + [ y [ 1 ] . last [ 1 ] ]
333+
334+ res = [ x_by_lines [ str_inter - x [ 0 ] ] . min , -x [ 0 ] ] <=> [ y_by_lines [ str_inter - y [ 0 ] ] . min , -y [ 0 ] ]
335+
289336
290- if str_inter != nil
291- x_range = str_inter ;
292- y_range = str_inter ;
337+ if @Debug > 1
338+ p "#{ x } < #{ y } " if res == -1
339+ p "#{ x } > #{ y } " if res == 1
340+ p "#{ x } = #{ y } " if res == 0
341+ p "#{ x } ??? #{ y } " if res == nil
342+
343+ if res == nil
344+ p [ x_by_lines [ str_inter - x [ 0 ] ] . min , x [ 0 ] ]
345+ p [ y_by_lines [ str_inter - y [ 0 ] ] . min , y [ 0 ] ]
346+ p str_inter
347+ p "x_by_lines: #{ x_by_lines } "
348+ p "y_by_lines: #{ y_by_lines } "
349+ p "x_range: #{ x_range } "
350+ p "y_range: #{ y_range } "
351+
352+ end
293353 end
294-
295- x_range . each { |i | x_min = [ x_min , x_by_lines [ i - x [ 0 ] ] ] . max }
296- y_range . each { |i | y_min = [ y_min , y_by_lines [ i - y [ 0 ] ] ] . max }
297- x_min <=> y_min ;
354+
355+
356+ #p "str_inter: ", str_inter
357+
358+ #p x_by_lines[str_inter]
359+ #p y_by_lines[str_inter]
360+ #if str_inter != nil
361+ # x_range = str_inter;
362+ # y_range = str_inter;
363+ #end
364+ #x_range.each{ |i| x_min = [x_min, x_by_lines[i - x[0]]].max }
365+ #y_range.each{ |i| y_min = [y_min, y_by_lines[i - y[0]]].max }
366+ #x_min <=> y_min;
367+ res
298368 end
299369
300370 return chains ;
0 commit comments