-
Notifications
You must be signed in to change notification settings - Fork 2
/
recreator.rb
372 lines (304 loc) · 8.41 KB
/
recreator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
69
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
require "./staff.rb";
require "./expression.rb"
require "./space_configurator.rb"
class Range
def intersection(other)
return (1..0) if (self.last < other.begin or other.last < self.begin)
[self.begin, other.begin].last..[self.last, other.last].min
end
def shift_left(indx)
if indx.class == Fixnum then
return (self.begin - indx)..(self.last - indx)
end
end
def shift_right(indx)
if indx.class == Fixnum then
return (self.begin + indx)..(self.last + indx)
end
end
alias_method :&, :intersection
alias_method :+, :shift_right
alias_method :-, :shift_left
end
class Array
def bubble_sort!(&closure)
closure ||= lambda{ |x,y| x <=> y}
for i in 0..size-1 do
for j in 0..size-2 do
cmp = closure.call(self[j], self[j + 1]);
if cmp == 1 then
self[j + 1], self[j] = self[j], self[j + 1]
end
end
end
end
end
class Pairs
def initialize(arr)
@arr = arr
end
def print(sp = 0)
@arr.each do |pair|
end
end
end
class Chain
def initialize(arr)
end
end
class Recreator
def initialize(type)
@type = type
@Debug = $DEBUG_project;
@sc = SpaceConf.new(type)
end
def set_debug(d)
@Debug = d;
end
def get_string_from_meta(meta)
res_str = meta.first == nil ? "" : meta.first.value;
prev_min = meta.first;
meta.value.each do |token|
if token.class == MetaExpression then
min_spaces = @sc.get_min(prev_min, token.get_first_token)
if @Debug > 0 then
p "from ", prev_min.type
p "to", token.get_first_token.type
p min_spaces
end
res_str += " " * min_spaces;
res_str += get_string_from_meta(token);
prev_min = token.get_last_token;
else
min_spaces = @sc.get_min(prev_min, token)
if @Debug > 0 then
p "debug:"
p "from ", prev_min
p "to", token
p min_spaces
end
res_str += " " * min_spaces;
res_str += token.value;
prev_min = token;
end
end
return res_str;
end
# input : [Meta, ...], [chain, ...], [prev, ...]
# output: [string, ...]
def multiline_reconstruction(meta_array, chains)
n = meta_array.size;
indexes = [0]*n;
@prev_tokens ||= [];
res_strings = meta_array.map{|meta| meta.first == nil ? "" : meta.first.value;}
meta_array.each_with_index{|meta, i| @prev_tokens[i] ||= meta.first;}
reconstruct_line_to = lambda do |line_index, end_index|
while indexes[line_index] != end_index do
t_token = meta_array[line_index].value[indexes[line_index]]
begin
if t_token.class == Token then
min_spaces = @sc.get_min(@prev_tokens[line_index], t_token)
if @Debug > 0 then
p "from ", @prev_tokens[line_index].type
p "to", t_token.type
p min_spaces
end
res_strings[line_index] += " "*min_spaces;
res_strings[line_index] += t_token.value;
@prev_tokens[line_index] = t_token;
else
min_spaces = @sc.get_min(@prev_tokens[line_index], t_token.get_first_token)
if @Debug > 0 then
p "from ", @prev_tokens[line_index].type
p "to", t_token.get_first_token.type
p min_spaces
end
res_strings[line_index] += " "*min_spaces;
res_strings[line_index] += get_string_from_meta(t_token);
@prev_tokens[line_index] = t_token.get_last_token;
end
rescue Exception => e
p e
e.backtrace.each{|x| p x}
p "Exceprion: "
p "line_index: " + line_index.to_s
p "token_index: " + indexes[line_index].to_s
p "end_index: " + end_index.to_s
p "metas: " + meta_array[line_index].value.to_s
exit();
end
indexes[line_index] += 1;
end
end
# chain processing
if @Debug > 0
puts "chains"
chains.each {|x| p x}
end
chains.each do |chain|
begin_line = chain[0];
end_line = begin_line + chain[1].size
line_index = chain[0];
#byebug
chain[1].each do |pair|
reconstruct_line_to.call(line_index, pair[0])
line_index += 1;
reconstruct_line_to.call(line_index, pair[1])
end
#debugger
# TODO some fix here
if chain.size <= 2
for i in begin_line..end_line do
min_spaces = @sc.get_min(@prev_tokens[i], meta_array[i].value[indexes[i]])
res_strings[i] += " "*min_spaces
end
end
# delta calculation here
max_size = 0;
for i in begin_line..end_line do
max_size = [max_size, res_strings[i].size].max;
end
delta = {}
for i in begin_line..end_line do
delta[i] = max_size - res_strings[i].size
end
# limitations here
for i in begin_line..end_line do
accept = true;
if delta[i] > 0 then
t1 = @prev_tokens[i]
t2 = meta_array[i].value[indexes[i]];
if t2.class == MetaExpression
t2 = t2.get_first_token
end
params = [];
params.push(t2.str_index);
limit = @sc.get_max(t1, t2, params);
accept = delta[i] < limit;
if @Debug > 1 then
p "prev: " + t1.type .to_s;
p "next: " + t2.type .to_s;
p "delta:" + delta[i].to_s;
p "max: " + limit .to_s;
end
end
res_strings[i] += " "*delta[i] if accept;
end
if chain.size > 2
# recursivity
metas = n.times.map{MetaExpression.new};
for i in begin_line..end_line do
metas[i] = meta_array[i].value[indexes[i]]
end
strings = multiline_reconstruction(metas, chain[2])
for i in begin_line..end_line do
res_strings[i] += strings[i];
indexes[i] += 1;
end
else
for i in begin_line..end_line do
res_strings[i] += meta_array[i].value[indexes[i]].value
@prev_tokens[i] = meta_array[i].value[indexes[i]]
indexes[i] += 1;
end
end
end
for i in 0..n-1 do
reconstruct_line_to.call(i, meta_array[i].value.size)
end
return res_strings
end
# input [ [ [index, index], [i, i], ... ], ...]
# output [ [line_id, [[token-id, token-id], [t-id, t-id], ...]], .... ]
# line_id - is first line of chain
def generate_chains(pairs_array)
if @Debug > 0
puts "generate_chains(pairs_array): "
pairs_array.each{|x| p x}
end
n = pairs_array.size();
used_indexes = n.times.map{{}};
curr_indexes = [ 0 ] * n;
chains = [];
for i in 0..n-1 do
while curr_indexes[i] < pairs_array[i].size do
if used_indexes[i][curr_indexes[i]] != nil
curr_indexes[i] += 1;
next;
end
used_indexes[i][curr_indexes[i]] = 1;
# start chain creation
child_pairs = n.times.map{[]};
rec = pairs_array[i][curr_indexes[i]][2] != nil;
child_pairs[i] = (pairs_array[i][curr_indexes[i]][2]) if pairs_array[i][curr_indexes[i]][2] != nil
chain =
[i,
[
[
pairs_array[i][curr_indexes[i]][0],
pairs_array[i][curr_indexes[i]][1]
]
]
];
k = i + 1;
tid = pairs_array[i][curr_indexes[i]][1];
while k != n do
nexus = pairs_array[k].drop(curr_indexes[k]).index{|x| x[0] == tid}
if (nexus == nil) then
break;
else
nexus += curr_indexes[k];
break if used_indexes[k][nexus] != nil
used_indexes[k][nexus] = 1;
child_pairs[k] = (pairs_array[k][nexus][2]) if pairs_array[k][nexus][2] != nil
chain[1].push([pairs_array[k][nexus][0],pairs_array[k][nexus][1]]);
tid = pairs_array[k][nexus][1];
k += 1;
end
end
chain.push(generate_chains(child_pairs)) if rec
chains.push(chain);
curr_indexes[i] += 1;
end
end
# sort
chains.sort! do |x,y|
# TODO add intersection here! Add convertation to line!
x_range = x[0]..(x[0] + x[1].size )
y_range = y[0]..(y[0] + y[1].size )
str_inter = x_range & y_range; # intersection of ranges
x_min = 0;
y_min = 0;
x_by_lines = x[1].map{|i| i[0]} + [x[1].last[1]]
y_by_lines = y[1].map{|i| i[0]} + [y[1].last[1]]
res = [x_by_lines[str_inter - x[0]].min, -x[0]] <=> [y_by_lines[str_inter - y[0]].min, -y[0]]
if @Debug > 1
p "#{x} < #{y}" if res == -1
p "#{x} > #{y}" if res == 1
p "#{x} = #{y}" if res == 0
p "#{x} ??? #{y}"if res == nil
if res == nil
p [x_by_lines[str_inter - x[0]].min, x[0]]
p [y_by_lines[str_inter - y[0]].min, y[0]]
p str_inter
p "x_by_lines: #{x_by_lines}"
p "y_by_lines: #{y_by_lines}"
p "x_range: #{x_range}"
p "y_range: #{y_range}"
end
end
#p "str_inter: ", str_inter
#p x_by_lines[str_inter]
#p y_by_lines[str_inter]
#if str_inter != nil
# x_range = str_inter;
# y_range = str_inter;
#end
#x_range.each{ |i| x_min = [x_min, x_by_lines[i - x[0]]].max }
#y_range.each{ |i| y_min = [y_min, y_by_lines[i - y[0]]].max }
#x_min <=> y_min;
res
end
return chains;
end
end