forked from spectreconsole/spectre.console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRatio.cs
More file actions
148 lines (123 loc) · 4.7 KB
/
Copy pathRatio.cs
File metadata and controls
148 lines (123 loc) · 4.7 KB
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
// Ported from Rich by Will McGugan, licensed under MIT.
// https://github.com/willmcgugan/rich/blob/527475837ebbfc427530b3ee0d4d0741d2d0fc6d/rich/_ratio.py
namespace Spectre.Console;
internal static class Ratio
{
public static List<int> Resolve(int total, IEnumerable<IRatioResolvable> edges)
{
static (int Div, float Mod) DivMod(float x, float y)
{
var (div, mod) = ((int)(x / y), x % y);
// If remainder is withing .0001 of 1 then we round up
if (!(mod > 0.9999))
{
return (div, mod);
}
div++;
mod = 0;
return (div, mod);
}
static int? GetEdgeWidth(IRatioResolvable edge)
{
if (edge.Size != null && edge.Size < edge.MinimumSize)
{
return edge.MinimumSize;
}
return edge.Size;
}
var sizes = edges.Select(GetEdgeWidth).ToArray();
while (sizes.Any(s => s == null))
{
// Get all edges and map them back to their index.
// Ignore edges which have a explicit size.
var flexibleEdges = sizes.Zip(edges, (a, b) => (Size: a, Edge: b))
.Enumerate()
.Select(x => (x.Index, x.Item.Size, x.Item.Edge))
.Where(x => x.Size == null)
.ToList();
// Get the remaining space
var remaining = total - sizes.Select(size => size ?? 0).Sum();
if (remaining <= 0)
{
// No more room for flexible edges.
return sizes
.Zip(edges, (size, edge) => (Size: size, Edge: edge))
.Select(zip => zip.Size ?? zip.Edge.MinimumSize)
.Select(size => size > 0 ? size : 1)
.ToList();
}
var portion = (float)remaining / flexibleEdges.Sum(x => Math.Max(1, x.Edge.Ratio));
var invalidate = false;
foreach (var (index, size, edge) in flexibleEdges)
{
if (portion * edge.Ratio <= edge.MinimumSize)
{
sizes[index] = edge.MinimumSize;
// New fixed size will invalidate calculations,
// so we need to repeat the process
invalidate = true;
break;
}
}
if (!invalidate)
{
var remainder = 0f;
foreach (var flexibleEdge in flexibleEdges)
{
var (div, mod) = DivMod((portion * flexibleEdge.Edge.Ratio) + remainder, 1);
remainder = mod;
sizes[flexibleEdge.Index] = div;
}
}
}
return sizes.Select(x => x ?? 1).ToList();
}
public static List<int> Reduce(int total, List<int> ratios, List<int> maximums, List<int> values)
{
ratios = ratios.Zip(maximums, (a, b) => (ratio: a, max: b)).Select(a => a.max > 0 ? a.ratio : 0).ToList();
var totalRatio = ratios.Sum();
if (totalRatio <= 0)
{
return values;
}
var totalRemaining = total;
var result = new List<int>();
foreach (var (ratio, maximum, value) in ratios.ZipThree(maximums, values))
{
if (ratio != 0 && totalRatio > 0)
{
var distributed = (int)Math.Min(maximum, Math.Round((double)(ratio * totalRemaining / totalRatio)));
result.Add(value - distributed);
totalRemaining -= distributed;
totalRatio -= ratio;
}
else
{
result.Add(value);
}
}
return result;
}
public static List<int> Distribute(int total, IList<int> ratios, IList<int>? minimums = null)
{
if (minimums != null)
{
ratios = ratios.Zip(minimums, (a, b) => (ratio: a, min: b)).Select(a => a.min > 0 ? a.ratio : 0).ToList();
}
var totalRatio = ratios.Sum();
Debug.Assert(totalRatio > 0, "Sum or ratios must be > 0");
var totalRemaining = total;
var distributedTotal = new List<int>();
minimums ??= ratios.Select(_ => 0).ToList();
foreach (var (ratio, minimum) in ratios.Zip(minimums, (a, b) => (a, b)))
{
var distributed = (totalRatio > 0)
? Math.Max(minimum, (int)Math.Ceiling(ratio * totalRemaining / (double)totalRatio))
: totalRemaining;
distributedTotal.Add(distributed);
totalRatio -= ratio;
totalRemaining -= distributed;
}
return distributedTotal;
}
}