From 51ad1974d532e413d83367d47b0e6136c04af4ee Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Wed, 7 Jun 2023 12:53:24 -0400 Subject: [PATCH] Added a new ND array. Maybe a list will come soon. --- Nerd_STF/NDArray.cs | 115 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 Nerd_STF/NDArray.cs diff --git a/Nerd_STF/NDArray.cs b/Nerd_STF/NDArray.cs new file mode 100644 index 0000000..0b808c4 --- /dev/null +++ b/Nerd_STF/NDArray.cs @@ -0,0 +1,115 @@ +namespace Nerd_STF; + +public class NDArray : IEnumerable, IEquatable> +{ + public int Dimensions => dimensions; + public int[] Lengths => sizes; + public long FullLength + { + get + { + long prod = 1; + foreach (int size in sizes) prod *= size; + return prod; + } + } + + protected readonly T[] arr; + protected readonly int dimensions; + protected readonly int[] sizes; + + public NDArray() + { + arr = Array.Empty(); + dimensions = 0; + sizes = Array.Empty(); + } + public NDArray(int dimensions) + { + arr = Array.Empty(); + this.dimensions = dimensions; + sizes = new int[dimensions]; + + Array.Fill(sizes, 0); + } + public NDArray(int dimensions, int allLengths) + { + this.dimensions = dimensions; + sizes = new int[dimensions]; + Array.Fill(sizes, allLengths); + + arr = new T[Mathf.Product(sizes)]; + + Console.WriteLine("Gay ass balls"); + } + public NDArray(int[] lengths) + { + arr = new T[Mathf.Product(lengths)]; + dimensions = lengths.Length; + sizes = lengths; + } + public NDArray(int dimensions, int[] lengths) + { + if (dimensions != lengths.Length) throw new InvalidSizeException("Dimension count doesn't match length count."); + + arr = new T[Mathf.Product(lengths)]; + this.dimensions = lengths.Length; + sizes = lengths; + } + public NDArray(T[] items, int[] lengths) + { + arr = items; + dimensions = lengths.Length; + sizes = lengths; + + if (arr.Length != Mathf.Product(lengths)) throw new InvalidSizeException("Too many or too few items were provided."); + } + public NDArray(T[] items, int dimensions, int[] lengths) + { + if (dimensions != lengths.Length) throw new InvalidSizeException("Dimension count doesn't match length count."); + + arr = items; + this.dimensions = lengths.Length; + sizes = lengths; + + if (arr.Length != Mathf.Product(lengths)) throw new InvalidSizeException("Too many or too few items were provided."); + } + + public T this[params int[] indexes] + { + get => arr[FlattenIndex(indexes)]; + set => arr[FlattenIndex(indexes)] = value; + } + + private int FlattenIndex(params int[] indexes) + { + if (indexes.Length != sizes.Length) throw new InvalidSizeException("Too many or too few indexes were provided."); + + int ind = indexes[^1]; + Console.WriteLine($"Start at {ind}"); + for (int i = indexes.Length - 2; i >= 0; i--) + { + Console.WriteLine($"[{ind}] * {sizes[i]} + {indexes[i]}"); + ind = ind * sizes[i] + indexes[i]; + } + return ind; + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + public IEnumerator GetEnumerator() + { + foreach (T item in arr) yield return item; + } + + public override bool Equals(object? obj) + { + if (obj is null) return false; + else if (obj is NDArray arr) return Equals(arr); + return false; + } + public bool Equals(NDArray? obj) => obj is not null && arr.Equals(obj.arr); + public override int GetHashCode() => base.GetHashCode(); + + public static bool operator ==(NDArray a, NDArray b) => a.Equals(b); + public static bool operator !=(NDArray a, NDArray b) => !a.Equals(b); +}