| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TimeSpanDecomposer.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Linq; |
| | | 10 | | using MyNet.Primitives; |
| | | 11 | | using MyNet.Primitives.Temporal; |
| | | 12 | | |
| | | 13 | | namespace MyNet.Temporal.Decomposition; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Decomposes a <see cref="TimeSpan"/> into ordered unit values using strict arithmetic. |
| | | 17 | | /// </summary> |
| | | 18 | | public sealed class TimeSpanDecomposer : ITimeSpanDecomposer |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Gets the default decomposer instance. |
| | | 22 | | /// </summary> |
| | 6 | 23 | | public static TimeSpanDecomposer Default { get; } = new(); |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | | 26 | | public IReadOnlyList<TimeUnitValue> Decompose(TimeSpan timeSpan, TimeSpanDecompositionOptions options) |
| | | 27 | | { |
| | 342 | 28 | | ArgumentNullException.ThrowIfNull(options); |
| | 342 | 29 | | ArgumentNullException.ThrowIfNull(options.RuleEngine); |
| | | 30 | | |
| | 342 | 31 | | var ticks = GetAbsoluteTicks(timeSpan); |
| | | 32 | | |
| | 342 | 33 | | var units = options.RuleEngine |
| | 342 | 34 | | .Select(timeSpan, options.MinUnit, options.MaxUnit) |
| | 342 | 35 | | .Distinct() |
| | 342 | 36 | | .OrderByDescending(GetUnitTicks) |
| | 342 | 37 | | .ToList(); |
| | | 38 | | |
| | 342 | 39 | | if (units.Count == 0) |
| | 0 | 40 | | return [new(0, options.MinUnit)]; |
| | | 41 | | |
| | 342 | 42 | | var result = options.Mode switch |
| | 342 | 43 | | { |
| | 321 | 44 | | TimeSpanDecompositionMode.Hierarchical => DecomposeHierarchical(ticks, units), |
| | 3 | 45 | | TimeSpanDecompositionMode.Flat => DecomposeFlat(ticks, units), |
| | 12 | 46 | | TimeSpanDecompositionMode.LargestUnitOnly => DecomposeLargestOnly(ticks, units), |
| | 6 | 47 | | TimeSpanDecompositionMode.SmallestUnitOnly => DecomposeSmallestOnly(ticks, units), |
| | 0 | 48 | | _ => DecomposeHierarchical(ticks, units) |
| | 342 | 49 | | }; |
| | | 50 | | |
| | 342 | 51 | | result = ApplyPostProcessing(result, options); |
| | | 52 | | |
| | 342 | 53 | | return result.Count == 0 ? [new(0, options.MinUnit)] : result; |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | // -------------------------------------------------- |
| | | 57 | | // Core decomposition strategies |
| | | 58 | | // -------------------------------------------------- |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Decomposes the given ticks into unit values for all specified units using a hierarchical subtraction approach. E |
| | | 62 | | /// </summary> |
| | | 63 | | /// <param name="ticks">The number of ticks to decompose.</param> |
| | | 64 | | /// <param name="units">The list of time units to consider.</param> |
| | | 65 | | /// <returns>A list of TimeUnitValue representing the timespan in each specified unit.</returns> |
| | | 66 | | private static List<TimeUnitValue> DecomposeHierarchical(long ticks, List<TimeUnit> units) |
| | | 67 | | { |
| | 321 | 68 | | var remaining = ticks; |
| | 321 | 69 | | var result = new List<TimeUnitValue>(units.Count); |
| | | 70 | | |
| | 1716 | 71 | | foreach (var unit in units) |
| | | 72 | | { |
| | 693 | 73 | | var unitTicks = GetUnitTicks(unit); |
| | 693 | 74 | | if (unitTicks <= 0) |
| | | 75 | | continue; |
| | | 76 | | |
| | 693 | 77 | | var value = (int)(remaining / unitTicks); |
| | 693 | 78 | | result.Add(new(value, unit)); |
| | | 79 | | |
| | 693 | 80 | | remaining -= value * unitTicks; |
| | 693 | 81 | | if (remaining <= 0) |
| | 312 | 82 | | break; |
| | | 83 | | } |
| | | 84 | | |
| | 321 | 85 | | return result; |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Decomposes the given ticks into unit values for all specified units without performing hierarchical subtraction. |
| | | 90 | | /// </summary> |
| | | 91 | | /// <param name="ticks">The number of ticks to decompose.</param> |
| | | 92 | | /// <param name="units">The list of time units to consider.</param> |
| | | 93 | | /// <returns>A list of TimeUnitValue representing the timespan in each specified unit.</returns> |
| | | 94 | | private static List<TimeUnitValue> DecomposeFlat(long ticks, List<TimeUnit> units) |
| | | 95 | | { |
| | 3 | 96 | | var result = new List<TimeUnitValue>(units.Count); |
| | 3 | 97 | | result.AddRange(from unit in units let unitTicks = GetUnitTicks(unit) where unitTicks > 0 let value = (int)(tick |
| | | 98 | | |
| | 3 | 99 | | return result; |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Decomposes the given ticks into a single unit value based on the largest unit in the provided list. This strateg |
| | | 104 | | /// </summary> |
| | | 105 | | /// <param name="ticks">The number of ticks to decompose.</param> |
| | | 106 | | /// <param name="units">The list of time units to consider.</param> |
| | | 107 | | /// <returns>A list containing a single TimeUnitValue representing the entire timespan in the largest unit.</returns |
| | | 108 | | private static List<TimeUnitValue> DecomposeLargestOnly(long ticks, List<TimeUnit> units) |
| | | 109 | | { |
| | 12 | 110 | | var unit = units[0]; |
| | 12 | 111 | | var value = (int)(ticks / GetUnitTicks(unit)); |
| | | 112 | | |
| | 12 | 113 | | return [new(value, unit)]; |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Decomposes the given ticks into a single unit value based on the smallest unit in the provided list. This strate |
| | | 118 | | /// </summary> |
| | | 119 | | /// <param name="ticks">The number of ticks to decompose.</param> |
| | | 120 | | /// <param name="units">The list of time units to consider.</param> |
| | | 121 | | /// <returns>A list containing a single TimeUnitValue representing the entire timespan in the smallest unit.</return |
| | | 122 | | private static List<TimeUnitValue> DecomposeSmallestOnly(long ticks, List<TimeUnit> units) |
| | | 123 | | { |
| | 6 | 124 | | var unit = units[^1]; |
| | 6 | 125 | | var value = (int)(ticks / GetUnitTicks(unit)); |
| | | 126 | | |
| | 6 | 127 | | return [new(value, unit)]; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Applies post-processing steps such as quantization, zero-unit filtering, and component limiting to the decompose |
| | | 132 | | /// </summary> |
| | | 133 | | /// <param name="input">The list of decomposed time unit values.</param> |
| | | 134 | | /// <param name="options">The decomposition options to apply.</param> |
| | | 135 | | /// <returns>The post-processed list of time unit values.</returns> |
| | | 136 | | private static List<TimeUnitValue> ApplyPostProcessing(List<TimeUnitValue> input, TimeSpanDecompositionOptions optio |
| | | 137 | | { |
| | 342 | 138 | | var result = input; |
| | | 139 | | |
| | 342 | 140 | | if (options.Quantizer is not null) |
| | 9 | 141 | | result = [.. options.Quantizer.Quantize(result)]; |
| | | 142 | | |
| | 342 | 143 | | if (!options.IncludeZeroUnits) |
| | 333 | 144 | | result = [.. result.Where(x => x.Value != 0)]; |
| | | 145 | | |
| | 342 | 146 | | if (options.MaxComponents is > 0) |
| | 318 | 147 | | result = [.. result.Take(options.MaxComponents.Value)]; |
| | | 148 | | |
| | 342 | 149 | | return result; |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | // -------------------------------------------------- |
| | | 153 | | // Helpers |
| | | 154 | | // -------------------------------------------------- |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Gets the absolute value of ticks, treating long.MinValue as long.MaxValue to avoid overflow issues. |
| | | 158 | | /// </summary> |
| | | 159 | | /// <param name="timeSpan">The TimeSpan to get the absolute ticks from.</param> |
| | | 160 | | /// <returns>The absolute value of ticks.</returns> |
| | 342 | 161 | | private static long GetAbsoluteTicks(TimeSpan timeSpan) => timeSpan.Ticks == long.MinValue ? long.MaxValue : Math.Ab |
| | | 162 | | |
| | | 163 | | /// <summary> |
| | | 164 | | /// Gets the number of ticks corresponding to a given time unit, using fixed average values for variable-length unit |
| | | 165 | | /// </summary> |
| | | 166 | | /// <param name="unit">The time unit.</param> |
| | | 167 | | /// <returns>The number of ticks for the specified time unit.</returns> |
| | | 168 | | private static long GetUnitTicks(TimeUnit unit) |
| | 1743 | 169 | | => unit switch |
| | 1743 | 170 | | { |
| | 36 | 171 | | TimeUnit.Year => (long)(DateTimeHelper.DaysPerYear * TimeSpan.TicksPerDay), |
| | 153 | 172 | | TimeUnit.Month => (long)(DateTimeHelper.DaysPerMonth * TimeSpan.TicksPerDay), |
| | 72 | 173 | | TimeUnit.Week => TimeSpan.TicksPerDay * DateTimeHelper.DaysPerWeek, |
| | 273 | 174 | | TimeUnit.Day => TimeSpan.TicksPerDay, |
| | 354 | 175 | | TimeUnit.Hour => TimeSpan.TicksPerHour, |
| | 450 | 176 | | TimeUnit.Minute => TimeSpan.TicksPerMinute, |
| | 390 | 177 | | TimeUnit.Second => TimeSpan.TicksPerSecond, |
| | 15 | 178 | | TimeUnit.Millisecond => TimeSpan.TicksPerMillisecond, |
| | 0 | 179 | | _ => 1L |
| | 1743 | 180 | | }; |
| | | 181 | | } |
| | | 182 | | |