From 33dbfba3a66a622b3dcce7d8738ed4ef48069979 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 10 Dec 2022 23:44:13 +0100 Subject: [PATCH] Add initial IFixedCountList abstraction --- .../Buffers/FixedCountList{T}.cs | 106 ++++++++++++++++++ .../Buffers/IFixedCountList{T}.cs | 17 +++ 2 files changed, 123 insertions(+) create mode 100644 src/ComputeSharp.D2D1.Uwp/Buffers/FixedCountList{T}.cs create mode 100644 src/ComputeSharp.D2D1.Uwp/Buffers/IFixedCountList{T}.cs diff --git a/src/ComputeSharp.D2D1.Uwp/Buffers/FixedCountList{T}.cs b/src/ComputeSharp.D2D1.Uwp/Buffers/FixedCountList{T}.cs new file mode 100644 index 00000000..44204a5f --- /dev/null +++ b/src/ComputeSharp.D2D1.Uwp/Buffers/FixedCountList{T}.cs @@ -0,0 +1,106 @@ +using System; +using System.Collections.Generic; +using ComputeSharp.D2D1.Helpers; + +namespace ComputeSharp.D2D1.Uwp.Buffers; + +/// +/// Shared helpers for all types implementing . +/// +/// The type of elements in the list. +internal static class FixedCountList +{ + /// + /// Copies the entire instance to a target array range. + /// + /// The input instance. + /// The array to copy elements to. + /// The initial index within . + public static void CopyTo(IFixedCountList list, T[] array, int index) + { + if (array is null) + { + ThrowHelper.ThrowArgumentNullException(nameof(array), "The input array cannot be null."); + } + + Span span = array.AsSpan(index); + + if (list.Count > span.Length) + { + ThrowHelper.ThrowArgumentException(nameof(array), "The destination array range is smaller than the number of source items."); + } + + for (int i = 0; i < list.Count; i++) + { + span[i] = list[i]; + } + } + + /// + /// Copies the entire instance to a target array range. + /// + /// The input instance. + /// The array to copy elements to. + /// The initial index within . + public static void CopyTo(IFixedCountList list, Array array, int index) + { + if (array is null) + { + ThrowHelper.ThrowArgumentNullException(nameof(array), "The input array cannot be null."); + } + + if ((uint)index >= array.Length) + { + ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The starting array index is out of range."); + } + + int remainingLength = array.Length - index; + + if (list.Count > remainingLength) + { + ThrowHelper.ThrowArgumentException(nameof(array), "The destination array range is smaller than the number of source items."); + } + + for (int i = 0; i < list.Count; i++) + { + array.SetValue(list[i], i); + } + } + + /// + /// Creates an for an input instance. + /// + /// The input instance. + /// An instance for . + public static IEnumerator GetEnumerator(IFixedCountList list) + { + for (int i = 0; i < list.Count; i++) + { + yield return list[i]; + } + } + + /// + /// Searches for the specified object and returns the zero-based index of the first occurrence + /// within the range of elements in a given instance. + /// + /// The input instance. + /// + /// The object to locate in . The value can be for reference types. + /// + /// + /// The zero-based index of the first occurrence of item within , otherwise -1. + /// + public static int IndexOf(IFixedCountList list, T value) + { + for (int i = 0; i < list.Count; i++) + { + if (EqualityComparer.Default.Equals(list[i], value)) + { + return i; + } + } + + return -1; + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.Uwp/Buffers/IFixedCountList{T}.cs b/src/ComputeSharp.D2D1.Uwp/Buffers/IFixedCountList{T}.cs new file mode 100644 index 00000000..73ebb200 --- /dev/null +++ b/src/ComputeSharp.D2D1.Uwp/Buffers/IFixedCountList{T}.cs @@ -0,0 +1,17 @@ +using System.Collections; +using System.Collections.Generic; + +namespace ComputeSharp.D2D1.Uwp.Buffers; + +/// +/// An interface for a list with a fixed collection. +/// +/// The type of elements in the list. +interface IFixedCountList +{ + /// + int Count { get; } + + /// + T this[int index] { get; set; } +} \ No newline at end of file