1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NokitaKaze\OrthogonalArrays; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Arrays |
7
|
|
|
* @package NokitaKaze\OrthogonalArrays |
8
|
|
|
*/ |
9
|
|
|
abstract class Arrays { |
10
|
|
|
const MAXIMUM_VARIANT_PER_ITERATION = 200; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param integer[] $input |
14
|
|
|
* @param integer $iteration_size_limit |
15
|
|
|
* |
16
|
|
|
* @return integer[][] |
17
|
|
|
* @throws OrthogonalArraysException |
18
|
|
|
* @throws \InvalidArgumentException |
19
|
|
|
*/ |
20
|
105 |
|
public static function generateN2(array $input, $iteration_size_limit = self::MAXIMUM_VARIANT_PER_ITERATION) { |
21
|
105 |
|
foreach ($input as $value) { |
22
|
105 |
|
if (!is_int($value)) { |
23
|
63 |
|
throw new \InvalidArgumentException("Input array must be int[]"); |
24
|
|
|
} |
25
|
42 |
|
} |
26
|
|
|
{ |
27
|
105 |
|
$input2 = $input; |
28
|
105 |
|
arsort($input2); |
29
|
|
|
|
30
|
105 |
|
$keys = array_keys($input2); |
31
|
105 |
|
$geometry = array_values($input2); |
32
|
105 |
|
unset($input2); |
33
|
|
|
} |
34
|
105 |
|
$additional_keys = []; |
35
|
105 |
|
while (!empty($geometry)) { |
36
|
105 |
|
$last_count = $geometry[count($geometry) - 1]; |
37
|
105 |
|
if ($last_count == 1) { |
38
|
40 |
|
$additional_keys[] = $keys[count($geometry) - 1]; |
39
|
40 |
|
unset($geometry[count($geometry) - 1]); |
40
|
16 |
|
} else { |
41
|
90 |
|
break; |
42
|
|
|
} |
43
|
16 |
|
} |
44
|
105 |
|
$additional_keys = array_reverse($additional_keys); |
45
|
105 |
|
$db_value = []; |
46
|
105 |
|
if (count($geometry) == 1) { |
47
|
20 |
|
$db_value = []; |
48
|
20 |
|
for ($i = 0; $i < $geometry[0]; $i++) { |
49
|
20 |
|
$db_value[] = [$i]; |
50
|
8 |
|
} |
51
|
20 |
|
unset($a, $i); |
52
|
93 |
|
} elseif (count($geometry) == 2) { |
53
|
25 |
|
$db_value = []; |
54
|
25 |
|
for ($i = 0; $i < $geometry[0]; $i++) { |
55
|
25 |
|
for ($j = 0; $j < $geometry[1]; $j++) { |
56
|
25 |
|
$db_value[] = [$i, $j]; |
57
|
10 |
|
} |
58
|
10 |
|
} |
59
|
25 |
|
unset($a, $i); |
60
|
70 |
|
} elseif (!empty($geometry)) { |
61
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
62
|
45 |
|
$db_value = ArraysDB::get_array($geometry); |
63
|
45 |
|
if (is_null($db_value)) { |
64
|
|
|
$db_value = self::direct_generateN2($geometry, $iteration_size_limit); |
65
|
|
|
} |
66
|
18 |
|
} |
67
|
105 |
|
$additional_keys_count = count($additional_keys); |
68
|
105 |
|
for ($i = 0; $i < $additional_keys_count; $i++) { |
69
|
40 |
|
if (empty($db_value)) { |
70
|
15 |
|
$db_value = [[0]]; |
71
|
6 |
|
} else { |
72
|
40 |
|
foreach ($db_value as &$value) { |
73
|
40 |
|
$value[] = 0; |
74
|
16 |
|
} |
75
|
40 |
|
unset($value); |
76
|
|
|
} |
77
|
16 |
|
} |
78
|
105 |
|
unset($additional_keys_count); |
79
|
|
|
|
80
|
105 |
|
$real_values = null; |
81
|
105 |
|
foreach ($keys as $i => $o) { |
82
|
105 |
|
if ($i !== $o) { |
83
|
15 |
|
$real_values = array_map(function ($line) use ($keys) { |
84
|
53 |
|
$new_line = []; |
85
|
53 |
|
foreach ($keys as $old_value => $new_value) { |
86
|
53 |
|
$new_line[$new_value] = $line[$old_value]; |
87
|
38 |
|
} |
88
|
53 |
|
ksort($new_line); |
89
|
|
|
|
90
|
53 |
|
return $new_line; |
91
|
53 |
|
}, $db_value); |
92
|
101 |
|
break; |
93
|
|
|
} |
94
|
42 |
|
} |
95
|
|
|
|
96
|
105 |
|
return !is_null($real_values) ? $real_values : $db_value; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param integer[] $geometry |
101
|
|
|
* @param integer $iteration_size_limit |
102
|
|
|
* |
103
|
|
|
* @return integer[][] |
104
|
|
|
* @throws OrthogonalArraysException |
105
|
|
|
* @doc https://habrahabr.ru/post/187882/ |
106
|
|
|
* |
107
|
|
|
* Ведёт себя неоптимально на размере в 2, там оптимально просто перебрать |
108
|
|
|
*/ |
109
|
80 |
|
protected static function direct_generateN2(array $geometry, $iteration_size_limit) { |
110
|
80 |
|
$full_mutation_left = self::generate_all_permutations_iteration([], $geometry); |
111
|
|
|
|
112
|
80 |
|
$output = []; |
113
|
80 |
|
while (!empty($full_mutation_left)) { |
114
|
80 |
|
self::direct_generateN2_iteration($full_mutation_left, $output, $iteration_size_limit); |
115
|
32 |
|
} |
116
|
|
|
|
117
|
48 |
|
usort($output, function ($a, $b) { |
118
|
70 |
|
$a_count = count($a); |
119
|
70 |
|
for ($i = 0; $i < $a_count; $i++) { |
120
|
70 |
|
if ($a[$i] < $b[$i]) { |
121
|
64 |
|
return -1; |
122
|
67 |
|
} elseif ($a[$i] > $b[$i]) { |
123
|
61 |
|
return 1; |
124
|
|
|
} |
125
|
26 |
|
} |
126
|
|
|
|
127
|
|
|
return 0; |
128
|
80 |
|
}); |
129
|
|
|
|
130
|
80 |
|
return $output; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param integer $full_mutation_left_count |
135
|
|
|
* @param integer $iteration_size_limit |
136
|
|
|
* |
137
|
|
|
* @return integer |
138
|
|
|
*/ |
139
|
80 |
|
protected static function get_max_line_select($full_mutation_left_count, $iteration_size_limit) { |
140
|
80 |
|
$mutation_count = 0; |
141
|
80 |
|
$mutation_count_i = 1; |
142
|
80 |
|
$max_line_select = 1; |
143
|
80 |
|
for ($i = 1; $i < $full_mutation_left_count; $i++) { |
144
|
70 |
|
$mutation_count_i = $mutation_count_i * ($full_mutation_left_count + 1 - $i) / $i; |
145
|
70 |
|
$mutation_count += $mutation_count_i; |
146
|
70 |
|
if ($mutation_count > $iteration_size_limit) { |
147
|
45 |
|
break; |
148
|
|
|
} else { |
149
|
70 |
|
$max_line_select = $i; |
150
|
|
|
} |
151
|
28 |
|
} |
152
|
|
|
|
153
|
80 |
|
return $max_line_select; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param integer[][] $full_mutation_left |
158
|
|
|
* @param integer[][] $output |
159
|
|
|
* @param integer $iteration_size_limit |
160
|
|
|
* |
161
|
|
|
* @throws OrthogonalArraysException |
162
|
|
|
*/ |
163
|
80 |
|
protected static function direct_generateN2_iteration( |
164
|
|
|
array &$full_mutation_left, |
165
|
|
|
array &$output, |
166
|
|
|
$iteration_size_limit) { |
167
|
|
|
/** |
168
|
|
|
* @var integer $max_line_select Количество линий, которые добавятся к массиву |
169
|
|
|
*/ |
170
|
80 |
|
$max_line_select = self::get_max_line_select(count($full_mutation_left), $iteration_size_limit); |
171
|
80 |
|
$max_line_select = max(min($max_line_select, count($full_mutation_left) - 1), 1); |
172
|
|
|
|
173
|
80 |
|
$sets = []; |
174
|
|
|
|
175
|
80 |
|
for ($select_line_count = 1; $select_line_count <= $max_line_select; $select_line_count++) { |
176
|
80 |
|
$all_lines = self::select_all_lines_permutations($full_mutation_left, $select_line_count); |
177
|
80 |
|
$best_set = null; |
178
|
80 |
|
$best_left = null; |
179
|
|
|
|
180
|
80 |
|
foreach ($all_lines as $single_lines_set) { |
181
|
80 |
|
$temporary = array_merge($output, $single_lines_set); |
182
|
80 |
|
$full_mutation_left_this = self::remove_useless_lines($temporary, $full_mutation_left); |
183
|
80 |
|
if (is_null($best_left)) { |
184
|
80 |
|
$best_set = $single_lines_set; |
185
|
80 |
|
$best_left = $full_mutation_left_this; |
186
|
74 |
|
} elseif (count($best_left) + count($best_set) > |
187
|
70 |
|
count($full_mutation_left_this) + count($single_lines_set)) { |
188
|
40 |
|
$best_set = $single_lines_set; |
189
|
64 |
|
$best_left = $full_mutation_left_this; |
190
|
16 |
|
} |
191
|
32 |
|
} |
192
|
|
|
|
193
|
80 |
|
$sets[$select_line_count] = [$best_set, $best_left]; |
194
|
32 |
|
} |
195
|
80 |
|
unset($best_left, $best_set, $select_line_count, $all_lines, |
196
|
48 |
|
$max_line_select, $temporary, $single_lines_set); |
197
|
80 |
|
usort($sets, function ($set1, $set2) { |
198
|
55 |
|
$v1 = count($set1[0]) + count($set1[1]); |
199
|
55 |
|
$v2 = count($set2[0]) + count($set2[1]); |
200
|
55 |
|
if ($v1 < $v2) { |
201
|
37 |
|
return -1; |
202
|
55 |
|
} elseif ($v1 > $v2) { |
203
|
40 |
|
return 1; |
204
|
|
|
} else { |
205
|
30 |
|
return (count($set1[0]) > count($set1[1])) ? -1 : 1; |
206
|
|
|
} |
207
|
80 |
|
}); |
208
|
80 |
|
list($best_set, $best_left) = $sets[array_keys($sets)[0]]; |
209
|
|
|
|
210
|
80 |
|
if (is_null($best_set)) { |
211
|
|
|
// @codeCoverageIgnoreStart |
212
|
|
|
throw new OrthogonalArraysException("Code flow Exception"); |
213
|
|
|
// @codeCoverageIgnoreEnd |
214
|
|
|
} |
215
|
80 |
|
foreach ($best_set as $line) { |
216
|
80 |
|
$output[] = $line; |
217
|
32 |
|
} |
218
|
80 |
|
$full_mutation_left = $best_left; |
219
|
80 |
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param integer[][] $existed |
223
|
|
|
* @param integer[] $geometry |
224
|
|
|
* |
225
|
|
|
* @return integer[][] |
226
|
|
|
*/ |
227
|
80 |
|
protected static function generate_all_permutations_iteration( |
228
|
|
|
array $existed, |
229
|
|
|
array $geometry |
230
|
|
|
) { |
231
|
80 |
|
if (empty($geometry)) { |
232
|
80 |
|
return $existed; |
233
|
|
|
} |
234
|
80 |
|
$index = array_shift($geometry); |
235
|
80 |
|
if (empty($existed)) { |
236
|
80 |
|
for ($i = 0; $i < $index; $i++) { |
237
|
80 |
|
$existed[] = [$i]; |
238
|
32 |
|
} |
239
|
32 |
|
} else { |
240
|
80 |
|
$a = []; |
241
|
80 |
|
for ($i = 0; $i < $index; $i++) { |
242
|
80 |
|
foreach ($existed as $exist) { |
243
|
80 |
|
$exist[] = $i; |
244
|
80 |
|
$a[] = $exist; |
245
|
32 |
|
} |
246
|
32 |
|
} |
247
|
80 |
|
$existed = $a; |
248
|
|
|
} |
249
|
|
|
|
250
|
80 |
|
return self::generate_all_permutations_iteration($existed, $geometry); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @param array $lines |
255
|
|
|
* @param integer $select_line_count |
256
|
|
|
* |
257
|
|
|
* @return array[][] |
258
|
|
|
*/ |
259
|
80 |
|
protected static function select_all_lines_permutations( |
260
|
|
|
$lines, |
261
|
|
|
$select_line_count |
262
|
|
|
) { |
263
|
80 |
|
$full_exist = []; |
264
|
80 |
|
self::select_all_lines_permutations_iteration($lines, $select_line_count, 0, [], $full_exist); |
265
|
|
|
|
266
|
80 |
|
return $full_exist; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @param array $lines |
271
|
|
|
* @param integer $select_line_count |
272
|
|
|
* @param integer $min_index |
273
|
|
|
* @param array $exist |
274
|
|
|
* @param array $full_exist |
275
|
|
|
*/ |
276
|
80 |
|
protected static function select_all_lines_permutations_iteration( |
277
|
|
|
$lines, |
278
|
|
|
$select_line_count, |
279
|
|
|
$min_index = 0, |
280
|
|
|
array $exist = [], |
281
|
|
|
array &$full_exist = [] |
282
|
|
|
) { |
283
|
80 |
|
if ($select_line_count == 0) { |
284
|
80 |
|
$full_exist[] = $exist; |
285
|
|
|
|
286
|
80 |
|
return; |
287
|
|
|
} |
288
|
80 |
|
$lines_count = count($lines); |
289
|
80 |
|
for ($i = $min_index; $i < $lines_count; $i++) { |
290
|
80 |
|
$this_exist = $exist; |
291
|
80 |
|
$this_exist[] = $lines[$i]; |
292
|
|
|
|
293
|
80 |
|
self::select_all_lines_permutations_iteration($lines, $select_line_count - 1, |
294
|
80 |
|
$i + 1, $this_exist, $full_exist); |
295
|
32 |
|
} |
296
|
80 |
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @param array[] $set |
300
|
|
|
* @param array[] $origin_array |
301
|
|
|
* |
302
|
|
|
* @return array[] |
303
|
|
|
*/ |
304
|
80 |
|
protected static function remove_useless_lines(array $set, array $origin_array) { |
305
|
80 |
|
if (empty($set)) { |
306
|
|
|
// @codeCoverageIgnoreStart |
307
|
|
|
return $origin_array; |
308
|
|
|
// @codeCoverageIgnoreEnd |
309
|
|
|
} |
310
|
80 |
|
$row_count = count($set[0]); |
311
|
80 |
|
$indexes = []; |
312
|
|
|
// @todo если я захочу делать бесконечную степень свободы, я должен копать отсюда |
313
|
80 |
|
for ($i = 0; $i < $row_count - 1; $i++) { |
314
|
80 |
|
for ($j = $i + 1; $j < $row_count; $j++) { |
315
|
80 |
|
$indexes[] = [$i, $j]; |
316
|
32 |
|
} |
317
|
32 |
|
} |
318
|
|
|
|
319
|
80 |
|
$existed_pairs = []; |
320
|
80 |
|
foreach ($indexes as $index_id => $index) { |
321
|
80 |
|
$this_set = []; |
322
|
80 |
|
foreach ($set as $value) { |
323
|
|
|
// @todo бесконечные степени свободы |
324
|
80 |
|
$this_set[] = [$value[$index[0]], $value[$index[1]]]; |
325
|
32 |
|
} |
326
|
80 |
|
$filtered = []; |
327
|
|
|
|
328
|
80 |
|
$this_set_count = count($this_set); |
329
|
80 |
|
for ($i = 0; $i < $this_set_count; $i++) { |
330
|
80 |
|
$u = true; |
331
|
80 |
|
for ($j = 0; $j < $i; $j++) { |
332
|
70 |
|
if (self::compare_chunk($this_set[$i], $this_set[$j])) { |
333
|
60 |
|
$u = false; |
334
|
60 |
|
break; |
335
|
|
|
} |
336
|
28 |
|
} |
337
|
|
|
|
338
|
80 |
|
if ($u) { |
339
|
80 |
|
$filtered[] = $this_set[$i]; |
340
|
32 |
|
} |
341
|
32 |
|
} |
342
|
|
|
|
343
|
80 |
|
$existed_pairs[] = $filtered; |
344
|
32 |
|
} |
345
|
|
|
|
346
|
80 |
|
$left = []; |
347
|
80 |
|
foreach ($origin_array as $origin_line) { |
348
|
80 |
|
$u = false; |
349
|
80 |
|
foreach ($indexes as $index_id => $index) { |
350
|
80 |
|
$this_set = [$origin_line[$index[0]], $origin_line[$index[1]]]; |
351
|
80 |
|
$u1 = false; |
352
|
80 |
|
foreach ($existed_pairs[$index_id] as $in_index_chunk) { |
353
|
80 |
|
if (self::compare_chunk($in_index_chunk, $this_set)) { |
354
|
80 |
|
$u1 = true; |
355
|
80 |
|
break; |
356
|
|
|
} |
357
|
32 |
|
} |
358
|
|
|
|
359
|
80 |
|
if (!$u1) { |
360
|
70 |
|
$u = true; |
361
|
76 |
|
break; |
362
|
|
|
} |
363
|
32 |
|
} |
364
|
|
|
|
365
|
80 |
|
if ($u) { |
366
|
76 |
|
$left[] = $origin_line; |
367
|
28 |
|
} |
368
|
32 |
|
} |
369
|
|
|
|
370
|
80 |
|
return $left; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* @param array $a |
375
|
|
|
* @param array $b |
376
|
|
|
* |
377
|
|
|
* @return boolean |
378
|
|
|
*/ |
379
|
80 |
|
protected static function compare_chunk(array $a, array $b) { |
380
|
80 |
|
foreach ($a as $num => $value) { |
381
|
80 |
|
if (!self::compare_value($value, $b[$num])) { |
382
|
76 |
|
return false; |
383
|
|
|
} |
384
|
32 |
|
} |
385
|
|
|
|
386
|
80 |
|
return true; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* @param mixed $a |
391
|
|
|
* @param mixed $b |
392
|
|
|
* |
393
|
|
|
* @return boolean |
394
|
|
|
*/ |
395
|
130 |
|
protected static function compare_value($a, $b) { |
396
|
130 |
|
if (is_null($a)) { |
397
|
20 |
|
return is_null($b); |
398
|
130 |
|
} elseif (is_null($b)) { |
399
|
5 |
|
return is_null($a); |
400
|
|
|
} else { |
401
|
130 |
|
return ($a === $b); |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* @param array[] $values |
407
|
|
|
* @param integer $iteration_size_limit |
408
|
|
|
* |
409
|
|
|
* @return array[] |
410
|
|
|
* @throws OrthogonalArraysException |
411
|
|
|
* @throws \InvalidArgumentException |
412
|
|
|
*/ |
413
|
20 |
|
public static function generateN2_values(array $values, $iteration_size_limit = self::MAXIMUM_VARIANT_PER_ITERATION) { |
414
|
20 |
|
$geometry = []; |
415
|
20 |
|
foreach ($values as $value) { |
416
|
20 |
|
$geometry[] = count($value); |
417
|
8 |
|
} |
418
|
|
|
|
419
|
20 |
|
$output_raw = self::generateN2($geometry, $iteration_size_limit); |
420
|
20 |
|
$output = []; |
421
|
20 |
|
foreach ($output_raw as $line) { |
422
|
20 |
|
$output_line = []; |
423
|
20 |
|
foreach ($line as $i => $index) { |
424
|
20 |
|
$output_line[] = $values[$i][$index]; |
425
|
8 |
|
} |
426
|
|
|
|
427
|
20 |
|
$output[] = $output_line; |
428
|
8 |
|
} |
429
|
|
|
|
430
|
20 |
|
return $output; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* @param array[] $values |
435
|
|
|
* @param integer $iteration_size_limit |
436
|
|
|
* |
437
|
|
|
* @return array[] |
438
|
|
|
* @throws OrthogonalArraysException |
439
|
|
|
* @throws \InvalidArgumentException |
440
|
|
|
*/ |
441
|
20 |
|
public static function squeeze(array $values, $iteration_size_limit = self::MAXIMUM_VARIANT_PER_ITERATION) { |
442
|
20 |
|
$unique_values = self::get_unique_values($values); |
443
|
|
|
|
444
|
20 |
|
return self::generateN2_values($unique_values, $iteration_size_limit); |
445
|
|
|
} |
446
|
|
|
|
447
|
50 |
|
protected static function get_unique_values_item(array $input) { |
448
|
50 |
|
$filtered = []; |
449
|
50 |
|
foreach ($input as $value) { |
450
|
50 |
|
$u = false; |
451
|
50 |
|
foreach ($filtered as $filtered_value) { |
452
|
50 |
|
if (self::compare_value($value, $filtered_value)) { |
453
|
40 |
|
$u = true; |
454
|
46 |
|
break; |
455
|
|
|
} |
456
|
20 |
|
} |
457
|
|
|
|
458
|
50 |
|
if (!$u) { |
459
|
50 |
|
$filtered[] = $value; |
460
|
20 |
|
} |
461
|
20 |
|
} |
462
|
|
|
|
463
|
50 |
|
return $filtered; |
464
|
|
|
} |
465
|
|
|
|
466
|
20 |
|
protected static function get_unique_values(array $values) { |
467
|
20 |
|
$unique_values = []; |
468
|
20 |
|
$line_width = count($values[0]); |
469
|
20 |
|
for ($i = 0; $i < $line_width; $i++) { |
470
|
20 |
|
$unique_values[] = []; |
471
|
8 |
|
} |
472
|
20 |
|
foreach ($values as $value) { |
473
|
20 |
|
foreach ($value as $index => $sub_value) { |
474
|
20 |
|
$unique_values[$index][] = $sub_value; |
475
|
8 |
|
} |
476
|
8 |
|
} |
477
|
20 |
|
foreach ($unique_values as &$unique_sub_values) { |
478
|
20 |
|
$unique_sub_values = self::get_unique_values_item($unique_sub_values); |
479
|
8 |
|
} |
480
|
|
|
|
481
|
20 |
|
return $unique_values; |
482
|
|
|
} |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
?> |