fix: Don't assume ladder row-data is sorted by price
Bugs (fixed): * Ladder assert-crashes when rendering volume-at-price Cause: The render routine expected items to be sorted by price, and asserted as much (indirectly). In hindsight, this expectation placed an unnecessary burden on the caller.
This commit is contained in:
1 parent
4195207a71
commit
562706b968
1 file changed
+15
-27
@@ -368,44 +368,32 @@ namespace mbts.spark {
|
||||
|
||||
public delegate void RenderCell<T>(DrawingContext dc, int row, T data_item);
|
||||
|
||||
static void _render_cells<T>(DrawingVisual dv, double row_height, IEnumerable<(double price, T value)> data, LadderColumnSettings settings, IList<double> prices, RegionalTickFactors spec, RenderCell<T> render_cell) {
|
||||
static void _render_cells<T>(DrawingVisual dv, double row_height, IEnumerable<(double price, T value)> cells, LadderColumnSettings settings, IList<double> prices, RegionalTickFactors spec, RenderCell<T> render_cell) {
|
||||
using (var dc = dv.RenderOpen())
|
||||
using (_draw_with_clip(dc, settings, row_height, prices.Count)) {
|
||||
if (prices.is_null_or_empty() || data.is_null_or_empty()) {
|
||||
if (prices.is_null_or_empty() || cells.is_null_or_empty()) {
|
||||
return;
|
||||
}
|
||||
var min_price = prices[prices.Count - 1];
|
||||
var max_price = prices[0];
|
||||
Log.assert(min_price.is_real() && max_price.is_real() && min_price <= max_price);
|
||||
data = data.Where(item => item.price.is_real() && item.price.is_within_range(min_price, max_price));
|
||||
int curr_index = 0;
|
||||
double curr_price = max_price;
|
||||
foreach (var (price, value) in data) {
|
||||
// prices are in descending order, so get diff from curr_price - price.
|
||||
var tick_diff = spec.tickdiff(curr_price, price);
|
||||
if (!tick_diff.HasValue) {
|
||||
Log.Ui.warn(
|
||||
$"Unable to compute tick-diff between prices ${curr_price} and ${price}");
|
||||
break;
|
||||
}
|
||||
if (tick_diff.Value < 0) {
|
||||
Log.Ui.error(
|
||||
"Expected positive tick-diff (prices should be in descending order, and we " +
|
||||
"compute diff between prices at idx 'i-1' and 'i')");
|
||||
}
|
||||
Log.assert(tick_diff.Value >= 0);
|
||||
if (tick_diff.Value >= prices.Count) {
|
||||
break;
|
||||
}
|
||||
curr_index += (int)tick_diff.Value;
|
||||
curr_price = price;
|
||||
if (curr_index >= 0 && curr_index < prices.Count) {
|
||||
render_cell(dc, curr_index, value);
|
||||
} else break;
|
||||
var indexed_cells = cells
|
||||
.Where(item => item.price.is_real() && item.price.is_within_range(min_price, max_price))
|
||||
.Select(row => {
|
||||
checked {
|
||||
return (row.price, row.value, (int)spec.tickdiff(max_price, row.price));
|
||||
}
|
||||
});
|
||||
foreach (var (price, value, idx) in indexed_cells) {
|
||||
var check_price = spec.tickdown(max_price, idx);
|
||||
Log.assert(spec.tick_equals(price, check_price));
|
||||
Log.assert(idx >= 0 && idx < prices.Count);
|
||||
render_cell(dc, idx, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
internal static void render_last(DrawingVisual dv, GlyphContext glyph_context, NativePriceSize last, int index, double row_height, LadderColumnSettings settings, Func<uint, string> fmt_value) {
|
||||
if (fmt_value is null)
|
||||
fmt_value = FormattingDelegates.default_string<uint>();
|
||||
|
||||
Reference in new issue
Block a user