Skip to content

Commit 43986dd

Browse files
committed
new PriorityCartesianProduct
1 parent dab4564 commit 43986dd

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

utbot-python/src/main/kotlin/org/utbot/python/typing/MypyAnnotations.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ object MypyAnnotations {
3737
if (candidates.any { it.isEmpty() })
3838
return@sequence
3939

40-
val annotations = PriorityCartesianProduct(candidates).asSequence()
40+
val annotations = PriorityCartesianProduct(candidates).getSequence()
4141

4242
annotations.forEach {
4343
val annotationMap = it.toMap()
Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,48 @@
11
package org.utbot.python.typing
22

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
84

95
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()
117
x.forEach {
128
println("-- $it")
139
}
1410
}
1511

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>>){
1913

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+
}
2134

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++)
2743
}
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] }
3046
}
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()
4147
}
4248
}

0 commit comments

Comments
 (0)