| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="SimplificationExtensions.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 MyNet.Primitives.Conversion; |
| | | 10 | | |
| | | 11 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 12 | | namespace MyNet.Primitives; |
| | | 13 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 14 | | |
| | | 15 | | public static class SimplificationExtensions |
| | | 16 | | { |
| | | 17 | | extension<TUnit>(Quantity<TUnit> quantity) |
| | | 18 | | where TUnit : struct, Enum |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Simplifies a quantity by converting it to the most appropriate unit within a specified range. The method ite |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="min">Optional minimum unit for simplification.</param> |
| | | 24 | | /// <param name="max">Optional maximum unit for simplification.</param> |
| | | 25 | | /// <returns>The simplified quantity.</returns> |
| | | 26 | | public Quantity<TUnit> Simplify(TUnit? min = null, TUnit? max = null) |
| | | 27 | | { |
| | 9 | 28 | | var (value, unit) = quantity.Value.Simplify(quantity.Unit, min, max); |
| | | 29 | | |
| | 9 | 30 | | return Quantity.Of(value, unit); |
| | | 31 | | } |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | extension(double value) |
| | | 35 | | { |
| | | 36 | | /// <summary> |
| | | 37 | | /// Simplifies a value by converting it to the most appropriate unit within a specified range. The method iterat |
| | | 38 | | /// </summary> |
| | | 39 | | /// <param name="unit">Current unit of the value.</param> |
| | | 40 | | /// <param name="min">Optional minimum unit for simplification.</param> |
| | | 41 | | /// <param name="max">Optional maximum unit for simplification.</param> |
| | | 42 | | /// <typeparam name="TUnit">Type of the unit.</typeparam> |
| | | 43 | | /// <returns>A tuple containing the simplified value and its corresponding unit.</returns> |
| | | 44 | | public (double NewValue, TUnit NewUnit) Simplify<TUnit>(TUnit unit, TUnit? min = null, TUnit? max = null) |
| | | 45 | | where TUnit : struct, Enum |
| | 21 | 46 | | => value.Simplify(typeof(TUnit), unit, min, max); |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Simplifies a value by converting it to the most appropriate unit within a specified range. The method iterat |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="type">Type of unit.</param> |
| | | 52 | | /// <param name="unit">Current unit of the value.</param> |
| | | 53 | | /// <param name="min">Optional minimum unit for simplification.</param> |
| | | 54 | | /// <param name="max">Optional maximum unit for simplification.</param> |
| | | 55 | | /// <typeparam name="TUnit">Type of the unit.</typeparam> |
| | | 56 | | /// <returns>A tuple containing the simplified value and its corresponding unit.</returns> |
| | | 57 | | public (double NewValue, TUnit NewUnit) Simplify<TUnit>(Type type, TUnit unit, TUnit? min = null, TUnit? max = n |
| | | 58 | | where TUnit : struct, Enum |
| | | 59 | | { |
| | 21 | 60 | | var values = (TUnit[])Enum.GetValues(type); |
| | | 61 | | |
| | 21 | 62 | | var bestValue = value; |
| | 21 | 63 | | var bestUnit = unit; |
| | 21 | 64 | | var hasBestCandidate = false; |
| | 21 | 65 | | var bestUnderOneValue = double.MinValue; |
| | 21 | 66 | | var bestUnderOneUnit = unit; |
| | 21 | 67 | | var hasUnderOneCandidate = false; |
| | | 68 | | |
| | 348 | 69 | | foreach (var u in values) |
| | | 70 | | { |
| | 153 | 71 | | if (min.HasValue && Comparer<TUnit>.Default.Compare(u, min.Value) < 0) |
| | | 72 | | continue; |
| | | 73 | | |
| | 129 | 74 | | if (max.HasValue && Comparer<TUnit>.Default.Compare(u, max.Value) > 0) |
| | | 75 | | continue; |
| | | 76 | | |
| | 117 | 77 | | var converted = UnitConverterRegistry.Get<TUnit>().Convert(value, unit, u); |
| | | 78 | | |
| | 117 | 79 | | switch (converted) |
| | | 80 | | { |
| | 96 | 81 | | case >= 1 when !hasBestCandidate || converted < bestValue: |
| | 51 | 82 | | bestValue = converted; |
| | 51 | 83 | | bestUnit = u; |
| | 51 | 84 | | hasBestCandidate = true; |
| | 51 | 85 | | break; |
| | 21 | 86 | | case < 1 when !hasUnderOneCandidate || converted > bestUnderOneValue: |
| | 9 | 87 | | bestUnderOneValue = converted; |
| | 9 | 88 | | bestUnderOneUnit = u; |
| | 9 | 89 | | hasUnderOneCandidate = true; |
| | | 90 | | break; |
| | | 91 | | } |
| | | 92 | | } |
| | | 93 | | |
| | 21 | 94 | | return hasBestCandidate |
| | 21 | 95 | | ? (bestValue, bestUnit) |
| | 21 | 96 | | : hasUnderOneCandidate |
| | 21 | 97 | | ? (bestUnderOneValue, bestUnderOneUnit) |
| | 21 | 98 | | : (bestValue, bestUnit); |
| | | 99 | | } |
| | | 100 | | } |
| | | 101 | | } |
| | | 102 | | |