-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathLazyCartesianProduct.cs
75 lines (62 loc) · 2.44 KB
/
LazyCartesianProduct.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PalCalc.Solver
{
public interface ILazyCartesianProduct<T>
{
long Count { get; }
IEnumerable<IEnumerable<(T, T)>> Chunks(int chunkSize);
}
public class LazyCartesianProduct<T>(List<T> listA, List<T> listB) : ILazyCartesianProduct<T>
{
public long Count { get; } = ((long)listA.Count) * ((long)listB.Count);
private IEnumerable<(T, T)> ChunkAt(long chunkStart, long chunkEnd)
{
int aStartIndex = (int)(chunkStart / listB.Count);
int bStartIndex = (int)(chunkStart % listB.Count);
int aEndIndex = (int)(chunkEnd / listB.Count);
int bEndIndex = (int)(chunkEnd % listB.Count);
if (aStartIndex == aEndIndex)
{
var elemA = listA[aStartIndex];
for (int i = bStartIndex; i <= bEndIndex; i++)
yield return (elemA, listB[i]);
}
else
{
for (int ia = aStartIndex; ia <= aEndIndex; ia++)
{
var elemA = listA[ia];
int ibStart = ia == aStartIndex ? bStartIndex : 0;
int ibEnd = ia == aEndIndex ? bEndIndex : (listB.Count - 1);
for (int ib = ibStart; ib <= ibEnd; ib++)
yield return (elemA, listB[ib]);
}
}
}
public IEnumerable<IEnumerable<(T, T)>> Chunks(int chunkSize)
{
long curChunkStart = 0;
while (curChunkStart < Count)
{
var curChunkEnd = Math.Min(Count - 1, curChunkStart + chunkSize);
yield return ChunkAt(curChunkStart, curChunkEnd);
curChunkStart = curChunkEnd + 1;
}
}
}
public class ConcatenatedLazyCartesianProduct<T>(IEnumerable<(List<T>, List<T>)> setPairs) : ILazyCartesianProduct<T>
{
private List<LazyCartesianProduct<T>> innerProducts = setPairs.Select(p => new LazyCartesianProduct<T>(p.Item1, p.Item2)).ToList();
public long Count => innerProducts.Sum(p => p.Count);
public IEnumerable<IEnumerable<(T, T)>> Chunks(int chunkSize)
{
foreach (var p in innerProducts)
foreach (var chunk in p.Chunks(chunkSize))
yield return chunk;
}
}
}