aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Modules/Stubs_Handler/StubRealTimeUsage.h
blob: 7c0fee8ed8546997deff5a87bfa9d84d3ad3f2d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Tango.Core;

/// <summary>
/// Contains <see cref="IEnumerable{T}"/> extension methods.
/// </summary>
public static class IEnumerableExtensions
{
    /// <summary>
    /// Creates a new observable collection from the specified enumerable.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="enumerable">The enumerable.</param>
    /// <returns></returns>
    public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> enumerable)
    {
        return new ObservableCollection<T>(enumerable);
    }

    /// <summary>
    /// Creates a new synchronized observable collection from the specified enumerable.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="enumerable">The enumerable.</param>
    /// <returns></returns>
    public static SynchronizedObservableCollection<T> ToSynchronizedObservableCollection<T>(this IEnumerable<T> enumerable)
    {
        return new SynchronizedObservableCollection<T>(enumerable);
    }

    /// <summary>
    /// Creates a new read-only collection from the specified enumerable.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="enumerable">The enumerable.</param>
    /// <returns></returns>
    public static ReadOnlyCollection<T> ToReadOnlyCollection<T>(this IEnumerable<T> enumerable)
    {
        return new ReadOnlyCollection<T>(enumerable.ToList());
    }

    /// <summary>
    /// Replace an element in the array.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="items">The items.</param>
    /// <param name="condition">The condition.</param>
    /// <param name="replaceAction">The replace action.</param>
    /// <returns></returns>
    public static IEnumerable<T> Replace<T>(this IEnumerable<T> items, Predicate<T> condition, Func<T, T> replaceAction)
    {
        return items.Select(item => condition(item) ? replaceAction(item) : item);
    }

    /// <summary>
    /// Returns the closest double number in the array.
    /// </summary>
    /// <param name="items">The items.</param>
    /// <param name="source">The source.</param>
    /// <returns></returns>
    public static double Closest(this IEnumerable<double> items, double source)
    {
        double closest = items.Aggregate((x, y) => Math.Abs(x - source) < Math.Abs(y - source) ? x : y);
        return closest;
    }

    /// <summary>
    /// Takes the last n elements from the list..
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="source">The source.</param>
    /// <param name="N">The n.</param>
    /// <returns></returns>
    public static IEnumerable<T> TakeLast<T>(this IEnumerable<T> source, int N)
    {
        return source.Skip(Math.Max(0, source.Count() - N));
    }

    /// <summary>
    /// Joins the list items ToString outputs to a single string using the specified separator.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="source">The source.</param>
    /// <param name="separator">The separator.</param>
    /// <returns></returns>
    public static String Join<T>(this IEnumerable<T> source, String separator)
    {
        return String.Join(separator, source);
    }

    /// <summary>
    /// Returns a distinct collection by the specified property.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="source">The source.</param>
    /// <param name="property">The property.</param>
    /// <returns></returns>
    public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> source, Func<T, TKey> property)
    {
        return source.GroupBy(property).Select(g => g.First());
    }

    /// <summary>
    /// Orders the collection by natural alphanumeric string.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="source">The source.</param>
    /// <param name="selector">The selector.</param>
    /// <returns></returns>
    public static IOrderedEnumerable<T> OrderByAlphaNumeric<T>(this IEnumerable<T> source, Func<T, string> selector)
    {
        int max = source
            .SelectMany(i => Regex.Matches(selector(i), @"\d+").Cast<Match>().Select(m => (int?)m.Value.Length))
            .Max() ?? 0;

        return source.OrderBy(i => Regex.Replace(selector(i), @"\d+", m => m.Value.PadLeft(max, '0')));
    }
}