Files
StarterNG/StarterNG/Domain/ConsistRuns.cs
maj00r ecf3067d85 feat(consists): the consist lists show the vehicles' minis, alike ones under one caption
The scenery's consists and the warehouse's are listed as a strip of minis at
their own 30 px, vehicles alike next to each other taken together under one
caption ("412W ×20"). A consist wider than the list scrolls sideways in its
row, with a lane for the bar under the captions. A warehouse consist's strip
follows an edit as soon as it is saved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 22:47:39 +01:00

43 lines
1.3 KiB
C#

using System;
using System.Collections.Generic;
using StarterNG.Classes;
namespace StarterNG.Domain;
/// <summary>A stretch of a consist made of vehicles alike, shown as one picture with a count.</summary>
public sealed record ConsistRun(Dynamic First, int Count);
public static class ConsistRuns
{
/// <summary>
/// The consist's vehicles in order, those next to each other that look alike taken together.
/// </summary>
/// <param name="keyOf">
/// What makes two vehicles alike, given the vehicle and its place in the consist.
/// No key keeps the vehicle on its own.
/// </param>
public static List<ConsistRun> Of(IReadOnlyList<Dynamic> vehicles, Func<Dynamic, int, string?> keyOf)
{
var runs = new List<ConsistRun>();
string? prevKey = null;
for (int i = 0; i < vehicles.Count; i++)
{
string? key = keyOf(vehicles[i], i);
if (key is not null && prevKey is not null && runs.Count > 0 &&
string.Equals(key, prevKey, StringComparison.OrdinalIgnoreCase))
{
var last = runs[^1];
runs[^1] = last with { Count = last.Count + 1 };
}
else
{
runs.Add(new ConsistRun(vehicles[i], 1));
}
prevKey = key;
}
return runs;
}
}