4
4
5
5
class Range
6
6
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
9
9
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
+
10
24
alias_method :& , :intersection
25
+ alias_method :+ , :shift_right
26
+ alias_method :- , :shift_left
27
+
11
28
end
12
29
13
30
@@ -23,6 +40,22 @@ def bubble_sort!(&closure)
23
40
end
24
41
end
25
42
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 )
26
59
27
60
end
28
61
end
@@ -31,7 +64,7 @@ class Recreator
31
64
32
65
def initialize ( type )
33
66
@type = type
34
- @Debug = true & false ;
67
+ @Debug = $DEBUG_project ;
35
68
@sc = SpaceConf . new ( type )
36
69
end
37
70
@@ -46,7 +79,7 @@ def get_string_from_meta(meta)
46
79
meta . value . each do |token |
47
80
if token . class == MetaExpression then
48
81
min_spaces = @sc . get_min ( prev_min , token . get_first_token )
49
- if @Debug then
82
+ if @Debug > 0 then
50
83
p "from " , prev_min . type
51
84
p "to" , token . get_first_token . type
52
85
p min_spaces
@@ -57,7 +90,7 @@ def get_string_from_meta(meta)
57
90
prev_min = token . get_last_token ;
58
91
else
59
92
min_spaces = @sc . get_min ( prev_min , token )
60
- if @Debug then
93
+ if @Debug > 0 then
61
94
p "debug:"
62
95
p "from " , prev_min
63
96
p "to" , token
@@ -92,7 +125,7 @@ def multiline_reconstruction(meta_array, chains)
92
125
begin
93
126
if t_token . class == Token then
94
127
min_spaces = @sc . get_min ( @prev_tokens [ line_index ] , t_token )
95
- if @Debug then
128
+ if @Debug > 0 then
96
129
p "from " , @prev_tokens [ line_index ] . type
97
130
p "to" , t_token . type
98
131
p min_spaces
@@ -102,7 +135,7 @@ def multiline_reconstruction(meta_array, chains)
102
135
@prev_tokens [ line_index ] = t_token ;
103
136
else
104
137
min_spaces = @sc . get_min ( @prev_tokens [ line_index ] , t_token . get_first_token )
105
- if @Debug then
138
+ if @Debug > 0 then
106
139
p "from " , @prev_tokens [ line_index ] . type
107
140
p "to" , t_token . get_first_token . type
108
141
p min_spaces
@@ -113,7 +146,7 @@ def multiline_reconstruction(meta_array, chains)
113
146
end
114
147
rescue Exception => e
115
148
p e
116
- p e . backtrace
149
+ e . backtrace . each { | x | p x }
117
150
p "Exceprion: "
118
151
p "line_index: " + line_index . to_s
119
152
p "token_index: " + indexes [ line_index ] . to_s
@@ -127,6 +160,10 @@ def multiline_reconstruction(meta_array, chains)
127
160
end
128
161
# chain processing
129
162
163
+ if @Debug > 0
164
+ puts "chains"
165
+ chains . each { |x | p x }
166
+ end
130
167
chains . each do |chain |
131
168
132
169
begin_line = chain [ 0 ] ;
@@ -176,7 +213,7 @@ def multiline_reconstruction(meta_array, chains)
176
213
params . push ( t2 . str_index ) ;
177
214
limit = @sc . get_max ( t1 , t2 , params ) ;
178
215
accept = delta [ i ] < limit ;
179
- if @Debug & false then
216
+ if @Debug > 1 then
180
217
p "prev: " + t1 . type . to_s ;
181
218
p "next: " + t2 . type . to_s ;
182
219
p "delta:" + delta [ i ] . to_s ;
@@ -218,8 +255,13 @@ def multiline_reconstruction(meta_array, chains)
218
255
219
256
# input [ [ [index, index], [i, i], ... ], ...]
220
257
# output [ [line_id, [[token-id, token-id], [t-id, t-id], ...]], .... ]
221
-
258
+ # line_id - is first line of chain
222
259
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
223
265
n = pairs_array . size ( ) ;
224
266
used_indexes = n . times . map { { } } ;
225
267
curr_indexes = [ 0 ] * n ;
@@ -273,28 +315,56 @@ def generate_chains(pairs_array)
273
315
end
274
316
275
317
# sort
276
- chains . bubble_sort! do |x , y |
318
+
319
+ chains . sort! do |x , y |
320
+
277
321
# 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 )
280
324
281
325
282
- str_inter = x_range & y_range ;
326
+ str_inter = x_range & y_range ; # intersection of ranges
283
327
284
328
x_min = 0 ;
285
329
y_min = 0 ;
286
330
287
331
x_by_lines = x [ 1 ] . map { |i | i [ 0 ] } + [ x [ 1 ] . last [ 1 ] ]
288
332
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
+
289
336
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
293
353
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
298
368
end
299
369
300
370
return chains ;
0 commit comments