|
1 | 1 | package org.utbot.python.typing
|
2 | 2 |
|
3 |
| -import org.utbot.fuzzer.CartesianProduct |
4 |
| -import org.utbot.fuzzer.Combinations |
5 |
| -import org.utbot.fuzzer.PseudoShuffledIntProgression |
6 |
| -import kotlin.math.min |
7 |
| -import kotlin.random.Random |
| 3 | +import java.lang.Integer.min |
8 | 4 |
|
9 | 5 | fun main() {
|
10 |
| - val x = PriorityCartesianProduct(listOf(listOf(1, 2, 3), listOf(4, 5), listOf(6, 7, 8, 9, 10))) |
| 6 | + val x = PriorityCartesianProduct(listOf(listOf(1, 2, 3), listOf(4, 5), listOf(6, 7, 8, 9, 10))).getSequence() |
11 | 7 | x.forEach {
|
12 | 8 | println("-- $it")
|
13 | 9 | }
|
14 | 10 | }
|
15 | 11 |
|
16 |
| -class PriorityCartesianProduct<T>( |
17 |
| - private val lists: List<List<T>>, |
18 |
| -): Iterable<List<T>> { |
| 12 | +class PriorityCartesianProduct<T>(private val lists: List<List<T>>){ |
19 | 13 |
|
20 |
| - fun asSequence(): Sequence<List<T>> = iterator().asSequence() |
| 14 | + private fun generateFixedSumRepresentation( |
| 15 | + sum: Int, |
| 16 | + index: Int = 0, |
| 17 | + curRepr: List<Int> = emptyList() |
| 18 | + ): Sequence<List<Int>> { |
| 19 | + val itemNumber = lists.size |
| 20 | + var result = emptySequence<List<Int>>() |
| 21 | + if (index == itemNumber && sum == 0) { |
| 22 | + return sequenceOf(curRepr) |
| 23 | + } else if (index < itemNumber && sum >= 0) { |
| 24 | + for (i in 0..min(sum, lists[index].size - 1)) { |
| 25 | + result += generateFixedSumRepresentation( |
| 26 | + sum - i, |
| 27 | + index + 1, |
| 28 | + curRepr + listOf(i) |
| 29 | + ) |
| 30 | + } |
| 31 | + } |
| 32 | + return result |
| 33 | + } |
21 | 34 |
|
22 |
| - override fun iterator(): Iterator<List<T>> { |
23 |
| - val sizes = lists.map {it.size} |
24 |
| - val combinations = Combinations(*sizes.toIntArray()) |
25 |
| - val groupedCombinations = combinations.groupBy { combination -> |
26 |
| - combination.sum() |
| 35 | + fun getSequence(): Sequence<List<T>> { |
| 36 | + var curSum = 0 |
| 37 | + val maxSum = lists.fold(0) { acc, elem -> acc + elem.size } |
| 38 | + val combinations = generateSequence { |
| 39 | + if (curSum > maxSum) |
| 40 | + null |
| 41 | + else |
| 42 | + generateFixedSumRepresentation(curSum++) |
27 | 43 | }
|
28 |
| - val sortedCombinations = (0..sizes.sumOf { it - 1 }).mapNotNull { |
29 |
| - groupedCombinations[it] |
| 44 | + return combinations.flatten().map { combination: List<Int> -> |
| 45 | + combination.mapIndexed { element, value -> lists[element][value] } |
30 | 46 | }
|
31 |
| -// val groupedCombinations = combinations.groupBy { combination -> |
32 |
| -// combination.maxOf {it} |
33 |
| -// } |
34 |
| -// val sortedCombinations = (0..sizes.maxOf { it - 1 }).mapNotNull { |
35 |
| -// groupedCombinations[it] |
36 |
| -// } |
37 |
| - val sequence = sortedCombinations.flatten().asSequence() |
38 |
| - return sequence.map { combination -> |
39 |
| - combination.mapIndexedTo(mutableListOf()) { element, value -> lists[element][value] } |
40 |
| - }.iterator() |
41 | 47 | }
|
42 | 48 | }
|
0 commit comments