diff --git a/Nerd_STF.dll b/Nerd_STF.dll index c1bd85b..3218a2d 100644 Binary files a/Nerd_STF.dll and b/Nerd_STF.dll differ diff --git a/Scripts/Filesaving.cs b/Scripts/Files.cs similarity index 53% rename from Scripts/Filesaving.cs rename to Scripts/Files.cs index 0392759..1fb28f7 100644 --- a/Scripts/Filesaving.cs +++ b/Scripts/Files.cs @@ -1,188 +1,261 @@ -using System; -using Nerd_STF.Lists; -using System.IO; -using System.Runtime.Serialization.Formatters.Binary; - -namespace Nerd_STF.Filesaving -{ - [Obsolete(nameof(BinaryFile) + " uses the " + nameof(BinaryFormatter) + ", which is considered dangerous. Go to 'https://aka.ms/binaryformatter/' for more information.")] - public class BinaryFile : File - { - public BinaryFile(string path) => Path = path; - public BinaryFile(string path, object data) - { - Data = data; - Path = path; - } - - public static BinaryFile Load(string path) - { - BinaryFile file = new(path); - FileStream stream = new(path, FileMode.Open); - BinaryFormatter formatter = new(); - file.Data = formatter.Deserialize(stream); - stream.Close(); - return file; - } - - public override void Erase() => Data = null; - public override void Load(bool erase = true) - { - if (erase) Erase(); - FileStream stream = new(Path, FileMode.Open); - BinaryFormatter formatter = new(); - Data = formatter.Deserialize(stream); - stream.Close(); - } - public override void Save() - { - FileStream stream = new(Path, FileMode.Create); - BinaryFormatter formatter = new(); - formatter.Serialize(stream, Data); - stream.Close(); - } - } - public class ByteFile : File> - { - public byte this[int index] - { - get - { - if (index < 0 || index >= Data.Length) throw new ArgumentOutOfRangeException(nameof(index)); - return Data[index]; - } - set { Data[index] = value; } - } - - public ByteFile(string path) => Path = path; - public ByteFile(string path, byte[] data) - { - Data = new List(data); - Path = path; - } - public ByteFile(string path, List data) - { - Data = data; - Path = path; - } - - public static ByteFile Load(string path) - { - ByteFile file = new(path); - FileStream stream = new(file.Path, FileMode.Open); - for (long i = 0; i < stream.Length; i++) file.Data.Add((byte)stream.ReadByte()); - stream.Close(); - return file; - } - - public override void Erase() => Data = new(); - public void Fill(int length, byte fill = 0) => Data = new List(length, fill); - public override void Load(bool erase = true) - { - if (erase) Erase(); - FileStream stream = new(Path, FileMode.Open); - for (long i = 0; i < stream.Length; i++) Data.Add((byte)stream.ReadByte()); - stream.Close(); - } - public void Remove(int start, int amount) - { - List old = Data; - Data = new List(old.Length - amount); - for (int i = 0; i < old.Length; i++) - { - if (i > start && i < start + amount) i = start + amount; - Data[i] = old[i]; - } - } - public override void Save() - { - FileStream stream = new(Path, FileMode.Create); - foreach (byte b in Data) stream.WriteByte(b); - stream.Close(); - } - public void Write(byte write, bool toFile = false) - { - Data += write; - if (toFile) - { - FileStream stream = new(Path, FileMode.Append); - stream.WriteByte(write); - stream.Close(); - } - } - public void Write(List write, bool toFile = false) - { - Data += write; - if (toFile) - { - FileStream stream = new(Path, FileMode.Append); - foreach (byte b in write) stream.WriteByte(b); - stream.Close(); - } - } - } - public class TextFile : File - { - public TextFile(string path) => Path = path; - public TextFile(string path, string data) - { - Data = data; - Path = path; - } - - public static TextFile Load(string path) - { - TextFile file = new(path); - FileStream stream = new(file.Path, FileMode.Open); - for (long i = 0; i < stream.Length; i++) file.Data += ((char)stream.ReadByte()); - stream.Close(); - return file; - } - - public override void Erase() => Data = ""; - public override void Load(bool erase = true) - { - if (erase) Erase(); - FileStream stream = new(Path, FileMode.Open); - for (long i = 0; i < stream.Length; i++) Data += (char)stream.ReadByte(); - stream.Close(); - } - public void Remove(int start, int amount) => Data = Data.Remove(start, amount); - public override void Save() - { - FileStream stream = new(Path, FileMode.Create); - foreach (byte b in Data) stream.WriteByte(b); - stream.Close(); - } - public void Write(char write, bool toFile = false) - { - Data += write; - if (toFile) - { - FileStream stream = new(Path, FileMode.Append); - stream.WriteByte((byte)write); - stream.Close(); - } - } - public void Write(string write, bool toFile = false) - { - Data += write; - if (toFile) - { - FileStream stream = new(Path, FileMode.Append); - foreach (byte b in write) stream.WriteByte(b); - stream.Close(); - } - } - } - - public abstract class File - { - public T Data { get; set; } - public string Path { get; set; } - - public abstract void Erase(); - public abstract void Load(bool erase = true); - public abstract void Save(); - } +using System; +using System.Text; +using Nerd_STF.Lists; +using System.IO; +using System.Runtime.Serialization.Formatters.Binary; + +namespace Nerd_STF.File.Saving +{ + [Obsolete(nameof(BinaryFile) + " uses the " + nameof(BinaryFormatter) + ", which is considered dangerous. Go to 'https://aka.ms/binaryformatter/' for more information.")] + [Serializable] + public class BinaryFile + { + public object Data { get; set; } + public string Path { get; set; } + + public BinaryFile(string path) => Path = path; + public BinaryFile(string path, object data) + { + Data = data; + Path = path; + } + + public static BinaryFile Load(string path) + { + BinaryFile file = new(path); + FileStream stream = new(path, FileMode.Open); + BinaryFormatter formatter = new(); + file.Data = formatter.Deserialize(stream); + stream.Close(); + return file; + } + + public void Erase() => Data = null; + public void Load() + { + FileStream stream = new(Path, FileMode.Open); + BinaryFormatter formatter = new(); + Data = formatter.Deserialize(stream); + stream.Close(); + } + public void Save() + { + FileStream stream = new(Path, FileMode.Create); + BinaryFormatter formatter = new(); + formatter.Serialize(stream, Data); + stream.Close(); + } + } + [Obsolete(nameof(BinaryFile) + " uses the " + nameof(BinaryFormatter) + ", which is considered dangerous. Go to 'https://aka.ms/binaryformatter/' for more information.")] + [Serializable] + public class BinaryFile + { + public T Data { get; set; } + public string Path { get; set; } + + public BinaryFile(string path) => Path = path; + public BinaryFile(string path, T data) + { + Data = data; + Path = path; + } + + public static BinaryFile Load(string path) + { + BinaryFile file = new(path); + FileStream stream = new(path, FileMode.Open); + BinaryFormatter formatter = new(); + file.Data = (T)formatter.Deserialize(stream); + stream.Close(); + return file; + } + + public void Erase() => Data = default; + public void Load() + { + FileStream stream = new(Path, FileMode.Open); + BinaryFormatter formatter = new(); + Data = (T)formatter.Deserialize(stream); + stream.Close(); + } + public void Save() + { + FileStream stream = new(Path, FileMode.Create); + BinaryFormatter formatter = new(); + formatter.Serialize(stream, Data); + stream.Close(); + } + } + [Serializable] + public class ByteFile : File> + { + public byte this[int index] + { + get + { + if (index < 0 || index >= Data.Length) throw new ArgumentOutOfRangeException(nameof(index)); + return Data[index]; + } + set { Data[index] = value; } + } + + public ByteFile(string path) => Path = path; + public ByteFile(string path, params byte[] data) + { + Data = new List(data); + Path = path; + } + public ByteFile(string path, List data) + { + Data = data; + Path = path; + } + + public static ByteFile Load(string path) + { + ByteFile file = new(path); + FileStream stream = new(file.Path, FileMode.Open); + byte[] b = new byte[stream.Length]; + while (stream.Read(b, 0, b.Length) > 0) ; + file.Data = new(b); + stream.Close(); + return file; + } + + public override void Erase() => Data = new(); + public void Fill(int length, byte fill = 0) => Data = new List(length, fill); + public override void Load(bool erase = true) + { + if (erase) Erase(); + FileStream stream = new(Path, FileMode.Open); + byte[] b = new byte[stream.Length]; + while (stream.Read(b, 0, b.Length) > 0) ; + Data.AddRange(b); + stream.Close(); + } + public void Remove(int start, int amount) + { + List old = Data; + Data = new List(old.Length - amount); + for (int i = 0; i < old.Length; i++) + { + if (i > start && i < start + amount) i = start + amount; + Data[i] = old[i]; + } + } + public override void Save() + { + FileStream stream = new(Path, FileMode.Create); + stream.Write(Data.ToArray(), 0, Data.Length); + stream.Close(); + } + public override bool TryLoad(out File> file) + { + bool success = false; + try + { + file = new ByteFile(Path); + FileStream stream = new(file.Path, FileMode.Open); + byte[] b = new byte[stream.Length]; + while (stream.Read(b, 0, b.Length) > 0) ; + file.Data.AddRange(b); + stream.Close(); + success = true; + } + catch { file = null; } + + return success; + } + public void Write(byte write, bool toFile = false) + { + Data += write; + if (toFile) Save(); + } + public override void Write(List write, bool toFile = false) + { + Data += write; + if (toFile) Save(); + } + } + [Serializable] + public class TextFile : File + { + public TextFile(string path) => Path = path; + public TextFile(string path, string data) + { + Data = data; + Path = path; + } + + public static TextFile Load(string path) + { + TextFile file = new(path); + FileStream stream = new(file.Path, FileMode.Open); + byte[] b = new byte[stream.Length]; + while (stream.Read(b, 0, b.Length) > 0) ; + file.Data += Encoding.Default.GetString(b); + stream.Close(); + return file; + } + + public override void Erase() => Data = ""; + public override void Load(bool erase = true) + { + if (erase) Erase(); + FileStream stream = new(Path, FileMode.Open); + byte[] b = new byte[stream.Length]; + while (stream.Read(b, 0, b.Length) > 0) ; + Data += Encoding.Default.GetString(b); + stream.Close(); + } + public void Remove(int start, int amount) => Data = Data.Remove(start, amount); + public override void Save() + { + FileStream stream = new(Path, FileMode.Create); + byte[] b = Encoding.Default.GetBytes(Data); + stream.Write(b, 0, b.Length); + stream.Close(); + } + public override bool TryLoad(out File file) + { + bool success = false; + try + { + file = new TextFile(Path); + FileStream stream = new(file.Path, FileMode.Open); + byte[] b = new byte[stream.Length]; + while (stream.Read(b, 0, b.Length) > 0) ; + file.Data += Encoding.Default.GetString(b); + stream.Close(); + success = true; + } + catch { file = null; } + + return success; + } + public void Write(char write, bool toFile = false) + { + Data += write; + if (toFile) Save(); + } + public override void Write(string write, bool toFile = false) + { + Data += write; + if (toFile) Save(); + } + } + + [Serializable] + public abstract class File + { + public T Data { get; set; } + public bool Exists => System.IO.File.Exists(Path); + public string Path { get; set; } + + public abstract void Erase(); + public abstract void Load(bool erase = true); + public abstract void Save(); + public abstract bool TryLoad(out File file); + public abstract void Write(T write, bool toFile = false); + } } \ No newline at end of file diff --git a/Scripts/General.cs b/Scripts/General.cs index 1b6a633..c5640da 100644 --- a/Scripts/General.cs +++ b/Scripts/General.cs @@ -1,28 +1,22 @@ -using System; -using System.Linq; -using System.Security.Cryptography; +using System; +using System.Text; namespace Nerd_STF { public static class Hashes { - public static int Default(object obj) - { - return obj.GetHashCode(); - } + public static int Default(object obj) => obj.GetHashCode(); + public static byte[] MD5(byte[] input) => System.Security.Cryptography.MD5.Create().ComputeHash(input); public static string MD5(string input) { - var md5 = System.Security.Cryptography.MD5.Create(); + System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create(); - byte[] inputB = System.Text.Encoding.ASCII.GetBytes(input); + byte[] inputB = Encoding.ASCII.GetBytes(input); byte[] hash = md5.ComputeHash(inputB); - var builder = new System.Text.StringBuilder(); - for (int i = 0; i < hash.Length; i++) - { - builder.Append(hash[i].ToString("X2")); - } - return builder.ToString(); + string s = ""; + for (int i = 0; i < hash.Length; i++) s += hash[i].ToString("X2"); + return s; } public static uint SchechterTurbulence(uint seed) { @@ -35,6 +29,68 @@ namespace Nerd_STF return seed; } + public static byte[] SHA1(byte[] input) => System.Security.Cryptography.SHA1.Create().ComputeHash(input); + public static string SHA1(string input) + { + System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create(); + + byte[] inputB = Encoding.ASCII.GetBytes(input); + byte[] hash = sha1.ComputeHash(inputB); + + string s = ""; + for (int i = 0; i < hash.Length; i++) s += hash[i].ToString("X2"); + return s; + } + public static byte[] SHA256(byte[] input) => System.Security.Cryptography.SHA256.Create().ComputeHash(input); + public static string SHA256(string input) + { + System.Security.Cryptography.SHA256 sha256 = System.Security.Cryptography.SHA256.Create(); + + byte[] inputB = Encoding.ASCII.GetBytes(input); + byte[] hash = sha256.ComputeHash(inputB); + + string s = ""; + for (int i = 0; i < hash.Length; i++) s += hash[i].ToString("X2"); + return s; + } + public static byte[] SHA384(byte[] input) => System.Security.Cryptography.SHA384.Create().ComputeHash(input); + public static string SHA384(string input) + { + System.Security.Cryptography.SHA384 sha384 = System.Security.Cryptography.SHA384.Create(); + + byte[] inputB = Encoding.ASCII.GetBytes(input); + byte[] hash = sha384.ComputeHash(inputB); + + string s = ""; + for (int i = 0; i < hash.Length; i++) s += hash[i].ToString("X2"); + return s; + } + public static byte[] SHA512(byte[] input) => System.Security.Cryptography.SHA512.Create().ComputeHash(input); + public static string SHA512(string input) + { + System.Security.Cryptography.SHA512 sha512 = System.Security.Cryptography.SHA512.Create(); + + byte[] inputB = Encoding.ASCII.GetBytes(input); + byte[] hash = sha512.ComputeHash(inputB); + + string s = ""; + for (int i = 0; i < hash.Length; i++) s += hash[i].ToString("X2"); + return s; + } + } + + public static class Misc + { + public static string PlaceMaker(int num) + { + return num.ToString()[^1] switch + { + '1' => num + "st", + '2' => num + "nd", + '3' => num + "rd", + _ => num + "th", + }; + } } public static class Stats @@ -46,32 +102,18 @@ namespace Nerd_STF "Github: https://https://github.com/that-one-nerd", "Itch: https://that-one-nerd.itch.io/" }; - public static readonly string Version = "2021.0"; + public static readonly string Version = "2021.2"; } + [Serializable] public struct Optional { - public bool Exists - { - get - { - return Value != null; - } - } + public bool Exists => Value != null; public T Value { get; internal set; } - public Optional(T input) - { - Value = input; - } + public Optional(T input) => Value = input; - public static explicit operator T(Optional input) - { - return input.Value; - } - public static explicit operator Optional(T input) - { - return new Optional(input); - } + public static explicit operator T(Optional input) => input.Value; + public static explicit operator Optional(T input) => new(input); } -} +} \ No newline at end of file diff --git a/Scripts/Interfaces.cs b/Scripts/Interfaces.cs deleted file mode 100644 index e00893c..0000000 --- a/Scripts/Interfaces.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Nerd_STF.Interfaces -{ - public interface INegatives - { - public T Absolute { get; } - public bool IsNegative { get; } - public T Negative { get; } - public T Positive { get; } - } -} \ No newline at end of file diff --git a/Scripts/Lists.cs b/Scripts/Lists.cs index 63eccbd..356295a 100644 --- a/Scripts/Lists.cs +++ b/Scripts/Lists.cs @@ -1,541 +1,465 @@ using System; using System.Collections; +using System.Collections.Generic; +using System.Collections.ObjectModel; using Nerd_STF.Mathematics; namespace Nerd_STF.Lists { - public class Matrix + [Serializable] + public class List : IEnumerable, IEnumerable { - internal List> lists; + public static List Empty => new(); - public Vector2 Length - { - get - { - return new(lists.Get(0).Length, lists.Length); - } - } - public int LengthX - { - get - { - return lists.Get(0).Length; - } - } - public int LengthY - { - get - { - return lists.Length; - } - } + internal T[] array; - public static Matrix Empty + public int this[T item] { - get - { - return new Matrix { lists = List>.Empty }; - } - } - - public Matrix() - { - lists = new List>(1, new List(1, default)); - } - public Matrix(int lengthX, int lengthY) - { - if (lengthX < 1) throw new ArgumentOutOfRangeException(nameof(lengthX), "Do not include a length of less than 1"); - if (lengthY < 1) throw new ArgumentOutOfRangeException(nameof(lengthY), "Do not include a width of less than 1"); - lists = new List>(lengthY, new List(lengthX)); - } - public Matrix(int lengthX, int lengthY, T inputAll) - { - if (lengthX < 1) throw new ArgumentOutOfRangeException(nameof(lengthX), "Do not include a length of less than 1"); - if (lengthY < 1) throw new ArgumentOutOfRangeException(nameof(lengthY), "Do not include a width of less than 1"); - lists = new List>(lengthY, new List(lengthX, inputAll)); - } - public T this[int indexX, int indexY] - { - get - { - return Get(indexX, indexY); - } - - set - { - Set(indexX, indexY, value); - } - } - - public void Add(Matrix input, DirectionType addDir) - { - if (addDir == DirectionType.y) - { - foreach (List list in input.lists) - { - AddY(list); - } - return; - } - - foreach (List list in input.lists) - { - AddX(list); - } - } - public void AddX() - { - foreach (List list in lists) - { - list.Add(); - } - } - public void AddX(T input) - { - foreach (List list in lists) - { - list.Add(input); - } - } - public void AddX(T[] input) - { - foreach (T t in input) - { - AddX(t); - } - } - public void AddX(List input) - { - foreach (T t in input) - { - AddX(t); - } - } - public void AddY() - { - lists.Add(new List(lists.Get(0).Length)); - } - public void AddY(T input) - { - lists.Add(new List(lists.Get(0).Length, input)); - } - public void AddY(T[] input) - { - if (input.Length > lists.Get(0).Length) throw new OverflowException(); - lists.Add(new List(input)); - } - public void AddY(List input) - { - if (input.Length > lists.Get(0).Length) throw new OverflowException(); - lists.Add(input); - } - public bool Check(int placeX, int placeY) - { - return lists.Get(placeY).Get(placeX) != null; - } - public bool Compare(T input) - { - foreach (List list in lists) - { - if (list.Compare(input)) return true; - } - - return false; - } - public void Convert(T input) - { - foreach (List list in lists) - { - list.Convert(input); - } - } - public void Convert(T[] input) - { - foreach (List list in lists) - { - list.Convert(input); - } - } - public void Convert(List input) - { - foreach (List list in lists) - { - list.Convert(input); - } - } - public void Convert(Matrix input) - { - lists = input.lists; - } - public int Count() - { - int returned = 0; - - foreach (List list in lists) - { - returned += list.Count(); - } - - return returned; - } - public int Count(DirectionType type) - { - if (type == DirectionType.y) return LengthY; - return LengthX; - } - public Vector2 CountXY() - { - return Length; - } - public T Get(int placeX, int placeY) - { - return lists.Get(placeY).Get(placeX); - } - public void Get(int placeX, int placeY, out T output) - { - output = Get(placeX, placeY); - } - public List GetAll() - { - List returned = new(); - - foreach (List list in lists) - { - returned.Add(list); - } - - return returned; - } - public void GetAll(out List output) - { - List returned = new(); - - foreach (List list in lists) - { - returned.Add(list); - } - - output = returned; - } - public IEnumerator GetEnumerator() - { - foreach (List list in lists) - { - foreach (T t in list) - { - yield return t; - } - } - } - public void Remove(int placeX, int placeY) - { - lists.Get(placeY).Remove(placeX); - } - public void Set(int placeX, int placeY, T input) - { - lists.Get(placeY).Set(placeX, input); - } - public void SetAll(T input) - { - for (int i = 0; i < lists.Length; i++) - { - for (int j = 0; j < lists.Get(i).Length; j++) - { - lists.Get(i).Set(j, input); - } - } - } - - public static Matrix AllDefault(int lengthX, int lengthY) - { - return new(lengthX, lengthY, default); - } - - public override bool Equals(object obj) - { - return base.Equals(obj); - } - public bool Equals(Matrix other) - { - return GetAll() == other.GetAll(); - } - public override int GetHashCode() - { - return base.GetHashCode(); - } - public override string ToString() - { - return lists.ToString(); - } - public string ToString(bool showAll) - { - if (showAll) - { - string r = ""; - for (int i = 0; i < lists.Length; i++) - { - for (int j = 0; j < lists.Get(i).Length; j++) - { - r += lists.Get(i).Get(j); - if (j != lists.Get(i).Length - 1) r += ", "; - } - if (i != lists.Length - 1) r += "\n"; - } - return r; - } - else - { - return ToString(); - } - } - - public static bool operator ==(Matrix a, Matrix b) - { - return a.Equals(b); - } - public static bool operator !=(Matrix a, Matrix b) - { - return !a.Equals(b); - } - - public enum DirectionType - { - x, - y, - } - } - - public class List - { - internal T[] list; - - public int Length - { - get - { - return list.Length; - } + get => FindIndex(item); + set => Insert(value, item); } public T this[int index] { - get - { - return Get(index); - } - set - { - Set(index, value); - } + get => array[index]; + set => array[index] = value; } - public static List Empty - { - get - { - return new List(Array.Empty()); - } - } + public List Duplicate => new(array); + public bool IsEmpty => array == Array.Empty(); + public bool IsNull => array == null; + public bool IsNullOrEmpty => IsNull || IsEmpty; + public int Length => array.Length; - public List() + public List() => array = Array.Empty(); + public List(params T[] items) => array = items; + public List(int length) => array = new T[length]; + public List(int length, T itemAll) { - list = Array.Empty(); + array = new T[length]; + for (int i = 0; i < array.Length; i++) array[i] = itemAll; } - public List(int length) + public List(IEnumerable items) { - list = new T[length]; - for (int i = 0; i < length; i++) - { - list[i] = default; - } - } - public List(int length, T inputAll) - { - list = new T[length]; - for (int i = 0; i < list.Length; i++) - { - list[i] = inputAll; - } - } - public List(T[] input) - { - list = Array.Empty(); - if (input != default) list = input; - } - public List(List input) - { - list = Array.Empty(); - if (input.list != default) list = input.list; - } + int length = 0; + foreach (object _ in items) length++; + array = new T[length]; - public void Add() + AddRange(items); + } + public List(IEnumerable items) { - T[] before = list; + int length = 0; + foreach (object _ in items) length++; + if (length == 0) array = Array.Empty(); - if (before.Length == 0) - { - list = new T[1]; - list[0] = default; - } + AddRange(items); + } + public List(List list) => array = list.array; + + public void Add() => Add(default); + public void Add(T item) + { + if (array == null || array == Array.Empty()) array = new T[1] { item }; else { - list = new T[before.Length + 1]; - int place = 0; - while (place < before.Length) - { - list[place] = before[place]; - place++; - } - list[place] = default; + T[] old = array; + array = new T[old.Length + 1]; + for (int i = 0; i < old.Length; i++) array[i] = old[i]; + array[old.Length] = item; } } - public void Add(T add) + public void AddRange(IEnumerable items) { foreach (T t in items) Add(t); } + public void AddRange(IEnumerable items) { foreach (T t in items) Add(t); } + public void AddRange(List items) { foreach (T t in items) Add(t); } + public void AddRange(params T[] items) { foreach (T t in items) Add(t); } + public bool Any(Predicate predicate) { - T[] before = list; - - if (before.Length == 0) - { - list = new T[1]; - list[0] = add; - } - else - { - list = new T[before.Length + 1]; - int place = 0; - while (place < before.Length) - { - list[place] = before[place]; - place++; - } - list[place] = add; - } - } - public void Add(T[] add) - { - foreach (T input in add) - { - Add(input); - } - } - public void Add(List add) - { - Add(add.list); - } - public bool Check(int place) - { - return list[place] != null; - } - public bool Compare(T input) - { - foreach (T place in list) - { - if (place == null) continue; - if (place.Equals(input)) return true; - } - + foreach (T t in array) if (predicate.Invoke(t)) return true; return false; } - public void Convert(T input) + public bool Any(T match) { - for (int i = 0; i < list.Length; i++) - { - list[i] = input; - } + foreach (T t in array) if (t.Equals(match)) return true; + return false; } - public void Convert(T[] input) + public void Clear(bool resetSize = false) { - list = input; + if (resetSize) array = Array.Empty(); + else { for (int i = 0; i < array.Length; i++) array[i] = default; } } - public void Convert(List input) + public bool Contains(Predicate predicate) { - Convert(input.list); + foreach (T t in array) if (predicate.Invoke(t)) return true; + return false; + } + public bool Contains(T match) + { + foreach (T t in array) if (t.Equals(match)) return true; + return false; } public int Count() { - int returned = 0; - foreach (T _ in list) - { - returned++; - } - return returned; + int r = 0; + foreach (T _ in array) r++; + return r; } - public T Get(int place) + public int Count(Predicate predicate) { - return list[place]; + if (!Contains(predicate)) return 0; + int r = 0; + foreach (T t in array) if (predicate.Invoke(t)) r++; + return r; } - public void Get(int place, out T output) + public int Count(T match) { - output = Get(place); + if (!Contains(match)) return 0; + int r = 0; + foreach (T t in array) if (t.Equals(match)) r++; + return r; } - public T[] GetAll() + public void Fill(T item) { for (int i = 0; i < Length; i++) array[i] = item; } + public T Find(Predicate predicate) { - return list; + foreach (T t in array) if (predicate.Invoke(t)) return t; + throw new Exception("Parameter " + nameof(predicate) + " does not exist in the list."); } - public IEnumerator GetEnumerator() + public T Find(T match) { - return list.GetEnumerator(); + foreach (T t in array) if (t.Equals(match)) return t; + throw new Exception("Parameter " + nameof(match) + " does not exist in the list."); } - public void Remove(int place) + public T Find(Predicate predicate, int start) { - list[place] = default; + for (int i = start; i < array.Length; i++) if (predicate.Invoke(array[i])) return array[i]; + throw new Exception("Parameter " + nameof(predicate) + " does not exist in the list."); } - public void Remove(int place, bool shift) + public T Find(T match, int start) { - list[place] = default; - if (shift) - { - for (int i = place; i < list.Length - 1; i++) - { - list[i] = list[i + 1]; - } - T[] save = list; - list = new T[save.Length - 1]; - for (int i = 0; i < save.Length - 1; i++) - { - list[i] = save[i]; - } - } + for (int i = start; i < array.Length; i++) if (array[i].Equals(match)) return array[i]; + throw new Exception("Parameter " + nameof(match) + " does not exist in the list."); } - public void Set(int place, T input) + public T Find(Predicate predicate, int start, int max) { - list[place] = input; + for (int i = start; i <= max; i++) if (predicate.Invoke(array[i])) return array[i]; + throw new Exception("Parameter " + nameof(predicate) + " does not exist in the list."); } - public void Set(T[] input) + public T Find(T match, int start, int max) { - list = input; + for (int i = start; i <= max; i++) if (array[i].Equals(match)) return array[i]; + throw new Exception("Parameter " + nameof(match) + " does not exist in the list."); } - public void Set(List input) + public T FindOrDefault(Predicate predicate) { - Set(input.list); + foreach (T t in array) if (predicate.Invoke(t)) return t; + return default; } - public void SetAll(T input) + public T FindOrDefault(T match) { - for (int i = 0; i < list.Length; i++) - { - list[i] = input; - } + foreach (T t in array) if (t.Equals(match)) return t; + return default; } + public T FindOrDefault(Predicate predicate, int start) + { + for (int i = start; i < array.Length; i++) if (predicate.Invoke(array[i])) return array[i]; + return default; + } + public T FindOrDefault(T match, int start) + { + for (int i = start; i < array.Length; i++) if (array[i].Equals(match)) return array[i]; + return default; + } + public T FindOrDefault(Predicate predicate, int start, int max) + { + for (int i = start; i <= max; i++) if (predicate.Invoke(array[i])) return array[i]; + return default; + } + public T FindOrDefault(T match, int start, int max) + { + for (int i = start; i <= max; i++) if (array[i].Equals(match)) return array[i]; + return default; + } + public List FindAll(Predicate predicate) + { + List r = new(); + foreach (T t in array) if (predicate.Invoke(t)) r.Add(t); + return r; + } + public List FindAll(T match) + { + List r = new(); + foreach (T t in array) if (t.Equals(match)) r.Add(t); + return r; + } + public List FindAll(Predicate predicate, int start) + { + List r = new(); + for (int i = start; i < array.Length; i++) if (predicate.Invoke(array[i])) r.Add(array[i]); + return r; + } + public List FindAll(T match, int start) + { + List r = new(); + for (int i = start; i < array.Length; i++) if (array[i].Equals(match)) r.Add(array[i]); + return r; + } + public List FindAll(Predicate predicate, int start, int max) + { + List r = new(); + for (int i = start; i <= max; i++) if (predicate.Invoke(array[i])) r.Add(array[i]); + return r; + } + public List FindAll(T match, int start, int max) + { + List r = new(); + for (int i = start; i <= max; i++) if (array[i].Equals(match)) r.Add(array[i]); + return r; + } + public T FindLast(Predicate predicate) + { + for (int i = array.Length - 1; i >= 0; i--) if (predicate.Invoke(array[i])) return array[i]; + throw new Exception("Parameter " + nameof(predicate) + " does not exist in the list."); + } + public T FindLast(T match) + { + for (int i = array.Length - 1; i >= 0; i--) if (array[i].Equals(match)) return array[i]; + throw new Exception("Parameter " + nameof(match) + " does not exist in the list."); + } + public T FindLast(Predicate predicate, int start) + { + for (int i = array.Length - 1; i >= start; i--) if (predicate.Invoke(array[i])) return array[i]; + throw new Exception("Parameter " + nameof(predicate) + " does not exist in the list."); + } + public T FindLast(T match, int start) + { + for (int i = array.Length - 1; i >= start; i--) if (array[i].Equals(match)) return array[i]; + throw new Exception("Parameter " + nameof(match) + " does not exist in the list."); + } + public T FindLast(Predicate predicate, int start, int max) + { + for (int i = max; i >= start; i--) if (predicate.Invoke(array[i])) return array[i]; + throw new Exception("Parameter " + nameof(predicate) + " does not exist in the list."); + } + public T FindLast(T match, int start, int max) + { + for (int i = max; i >= start; i--) if (array[i].Equals(match)) return array[i]; + throw new Exception("Parameter " + nameof(match) + " does not exist in the list."); + } + public T FindLastOrDefault(Predicate predicate) + { + for (int i = array.Length - 1; i >= 0; i--) if (predicate.Invoke(array[i])) return array[i]; + return default; + } + public T FindLastOrDefault(T match) + { + for (int i = array.Length - 1; i >= 0; i--) if (array[i].Equals(match)) return array[i]; + return default; + } + public T FindLastOrDefault(Predicate predicate, int start) + { + for (int i = array.Length - 1; i >= start; i--) if (predicate.Invoke(array[i])) return array[i]; + return default; + } + public T FindLastOrDefault(T match, int start) + { + for (int i = array.Length - 1; i >= start; i--) if (array[i].Equals(match)) return array[i]; + return default; + } + public T FindLastOrDefault(Predicate predicate, int start, int max) + { + for (int i = max; i >= start; i--) if (predicate.Invoke(array[i])) return array[i]; + return default; + } + public T FindLastOrDefault(T match, int start, int max) + { + for (int i = max; i >= start; i--) if (array[i].Equals(match)) return array[i]; + return default; + } + public int FindIndex(Predicate predicate) + { + for (int i = 0; i < array.Length; i++) if (predicate.Invoke(array[i])) return i; + return -1; + } + public int FindIndex(T match) + { + for (int i = 0; i < array.Length; i++) if (array[i].Equals(match)) return i; + return -1; + } + public int FindIndex(Predicate predicate, int start) + { + for (int i = start; i < array.Length; i++) if (predicate.Invoke(array[i])) return i; + return -1; + } + public int FindIndex(T match, int start) + { + for (int i = start; i < array.Length; i++) if (array[i].Equals(match)) return i; + return -1; + } + public int FindIndex(Predicate predicate, int start, int max) + { + for (int i = start; i <= max; i++) if (predicate.Invoke(array[i])) return i; + return -1; + } + public int FindIndex(T match, int start, int max) + { + for (int i = start; i <= max; i++) if (array[i].Equals(match)) return i; + return -1; + } + public List FindAllIndex() + { + List ret = new(); + for (int i = 0; i < array.Length; i++) ret.Add(i); + return ret; + } + public List FindAllIndex(Predicate predicate) + { + List r = new(); + for (int i = 0; i < array.Length; i++) if (predicate.Invoke(array[i])) r.Add(i); + return r; + } + public List FindAllIndex(T match) + { + List r = new(); + for (int i = 0; i < array.Length; i++) if (array[i].Equals(match)) r.Add(i); + return r; + } + public List FindAllIndex(int start) + { + List ret = new(); + for (int i = start; i < array.Length; i++) ret.Add(i); + return ret; + } + public List FindAllIndex(Predicate predicate, int start) + { + List r = new(); + for (int i = start; i < array.Length; i++) if (predicate.Invoke(array[i])) r.Add(i); + return r; + } + public List FindAllIndex(T match, int start) + { + List r = new(); + for (int i = start; i < array.Length; i++) if (array[i].Equals(match)) r.Add(i); + return r; + } + public List FindAllIndex(int start, int max) + { + List ret = new(); + for (int i = start; i <= max; i++) ret.Add(i); + return ret; + } + public List FindAllIndex(Predicate predicate, int start, int max) + { + List r = new(); + for (int i = start; i <= max; i++) if (predicate.Invoke(array[i])) r.Add(i); + return r; + } + public List FindAllIndex(T match, int start, int max) + { + List r = new(); + for (int i = start; i <= max; i++) if (array[i].Equals(match)) r.Add(i); + return r; + } + public int FindLastIndex(Predicate predicate) + { + for (int i = array.Length - 1; i >= 0; i--) if (predicate.Invoke(array[i])) return i; + return -1; + } + public int FindLastIndex(T match) + { + for (int i = array.Length - 1; i >= 0; i--) if (array[i].Equals(match)) return i; + return -1; + } + public int FindLastIndex(Predicate predicate, int start) + { + for (int i = array.Length - 1; i >= start; i--) if (predicate.Invoke(array[i])) return i; + return -1; + } + public int FindLastIndex(T match, int start) + { + for (int i = array.Length - 1; i >= start; i--) if (array[i].Equals(match)) return i; + return -1; + } + public int FindLastIndex(Predicate predicate, int start, int max) + { + for (int i = max; i >= start; i--) if (predicate.Invoke(array[i])) return i; + return -1; + } + public int FindLastIndex(T match, int start, int max) + { + for (int i = max; i >= start; i--) if (array[i].Equals(match)) return i; + return -1; + } + public IEnumerator GetEnumerator() => array.GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() => (IEnumerator)array.GetEnumerator(); + public List GetRange(int start, int max) + { + List r = new(); + for (int i = start; i <= max; i++) r.Add(array[i]); + return r; + } + public void Insert(int index, T item) + { + T[] old = array; + array = new T[old.Length + 1]; + for (int i = 0; i < index; i++) array[i] = old[i]; + array[index] = item; + for (int i = index + 1; i < array.Length; i++) array[i] = old[i - 1]; + } + public void InsertRange(int index, IEnumerable items) + { + List list = new(items); - public static List AllDefault(int length) - { - return new(length, default); + T[] old = array; + array = new T[old.Length + list.Length]; + for (int i = 0; i < index; i++) array[i] = old[i]; + for (int i = 0; i < list.Length; i++) array[index + i] = list[i]; + for (int i = index + list.Length; i < array.Length; i++) array[i] = old[i - list.Length]; } - - public override bool Equals(object obj) + public bool MatchesAll(Predicate predicate) => FindAll(predicate).array == array; + public bool MatchesAll(T match) => FindAll(match).array == array; + public void Randomize() { - return base.Equals(obj); - } - public bool Equals(List other) - { - bool returned = true; - if (Length == other.Length) + List newL = new(); + List possibleIndexes = FindAllIndex(); + for (int i = 0; i < possibleIndexes.Length; i++) { - for (int i = 0; i < Length; i++) - { - returned &= Get(i).Equals(other.Get(i)); - } + int index = possibleIndexes[new Random().Next(0, possibleIndexes.Length)]; + newL.Add(array[index]); + possibleIndexes.Remove(x => x == index); } - return returned; + array = newL.ToArray(); } + public void Remove(Predicate predicate) => Remove(FindIndex(predicate)); + public void Remove(T item) => Remove(FindIndex(item)); + public void Remove(int index) + { + List newList = new(); + for (int i = 0; i < array.Length; i++) if (i != index) newList.Add(array[i]); + array = newList.array; + } + public void RemoveAll(Predicate predicate) { foreach (int i in FindAllIndex(predicate)) Remove(i); } + public void RemoveAll(T match) { foreach (int i in FindAllIndex(match)) Remove(i); } + public void RemoveLast(Predicate predicate) => Remove(FindLastIndex(predicate)); + public void RemoveLast(T item) => Remove(FindLastIndex(item)); + public void RemoveRange(int index, int max) + { + List newList = new(); + for (int i = 0; i < array.Length; i++) if (i < index || i > max) newList.Add(array[i]); + array = newList.array; + } + public void Reverse() + { + T[] old = array; + array = new T[old.Length]; + + for (int i = old.Length - 1; i >= 0; i--) array[i] = old[i]; + } + public void Shuffle() => Randomize(); + public T[] ToArray() => array; + public ReadOnlyList ToReadOnly() { return new ReadOnlyList(array); } + public ReadOnlyCollection ToSystemReadOnly() { return new ReadOnlyCollection(array); } + public System.Collections.Generic.List ToSystemList() { return new System.Collections.Generic.List(array); } + + public override bool Equals(object obj) => base.Equals(obj); public bool Equals(T[] other) { bool returned = true; @@ -543,86 +467,54 @@ namespace Nerd_STF.Lists { for (int i = 0; i < Length; i++) { - returned &= Get(i).Equals(other[i]); + returned &= array[i].Equals(other[i]); } } return returned; } - public override int GetHashCode() + public bool Equals(List list) { - return base.GetHashCode(); + if (Length != list.Length) return false; + bool equal = true; + for (int i = 0; i < Length; i++) equal &= array[i].Equals(list[i]); + return equal; } - public override string ToString() + public bool Equals(IEnumerable list) => Equals(new List(list)); + public override int GetHashCode() => base.GetHashCode(); + public override string ToString() => ToString(false); + public string ToString(bool showAll = false) { - return list.ToString(); - } - public string ToString(bool showAll) - { - if (showAll) - { - string r = ""; - for (int i = 0; i < list.Length; i++) - { - r += list[i].ToString(); - if (i != list.Length - 1) r += "\n"; - } - return r; - } - else - { - return ToString(); - } + string ret = "List of " + Length + " Elements (" + typeof(T).ToString() + ")"; + if (showAll) for (int i = 0; i < Length; i++) ret += "\n" + i + ": " + array[i]; + return ret; } - public static List operator +(List a, List b) + public static List operator +(List a, T b) { a.Add(b); return a; } + public static List operator +(List a, IEnumerable b) + { + a.AddRange(b); + return a; + } + public static List operator +(List a, List b) + { + a.AddRange(b); + return a; + } public static List operator +(List a, T[] b) { - a.Add(b); + a.AddRange(b); return a; } - public static List operator +(T[] a, List b) - { - List returned = new(a); - returned.Add(b); - return returned; - } - public static List operator +(List a, int add) - { - List returned = new(a.Length + add); - int i = 0; - while (i < a.Length) - { - returned.Set(i, a.Get(i)); - i++; - } - while (i < returned.Length) - { - returned.Set(i, default); - } - return returned; - } - public static List operator +(List a, T b) - { - a.Add(b); - - return a; - } - public static List operator +(T a, List b) - { - b.Add(a); - - return b; - } public static List operator -(List a, int remove) { List returned = new(a.Length - remove); for (int i = 0; i < returned.Length; i++) { - returned.Set(i, a.Get(i)); + returned[i] = a[i]; } return returned; } @@ -642,25 +534,12 @@ namespace Nerd_STF.Lists } return a; } - public static List operator *(List a, int multiplier) + public static List operator -(List a, T b) { - List returned = new(a.Length * multiplier); - int i = 0; - while (i < a.Length) - { - returned.Set(i, a.Get(i)); - i++; - } - while (i < returned.Length) - { - returned.Set(i, default); - } - return returned; - } - public static bool operator ==(List a, List b) - { - return a.Equals(b); + a.Remove(b); + return a; } + public static bool operator ==(List a, List b) => a.Equals(b); public static bool operator ==(List a, T[] b) { return a.Equals(b); @@ -669,10 +548,7 @@ namespace Nerd_STF.Lists { return b.Equals(a); } - public static bool operator !=(List a, List b) - { - return !a.Equals(b); - } + public static bool operator !=(List a, List b) => !a.Equals(b); public static bool operator !=(List a, T[] b) { return !a.Equals(b); @@ -681,14 +557,539 @@ namespace Nerd_STF.Lists { return !b.Equals(a); } + } - public static explicit operator T[](List list) + [Serializable] + [Obsolete("This class will be removed or heavily modified in a future release.")] + public class Matrix + { + internal List> lists; + + public Vector2 Length => new(lists[0].Length, lists.Length); + public int LengthX => lists[0].Length; + public int LengthY => lists.Length; + + public static Matrix Empty => new() { lists = List>.Empty }; + + public Matrix() => lists = new List>(1, new List(1, default)); + public Matrix(int lengthX, int lengthY) { - return list.list; + if (lengthX < 1) throw new ArgumentOutOfRangeException(nameof(lengthX), "Do not include a length of less than 1"); + if (lengthY < 1) throw new ArgumentOutOfRangeException(nameof(lengthY), "Do not include a width of less than 1"); + lists = new List>(lengthY, new List(lengthX)); } - public static explicit operator List(T[] list) + public Matrix(int lengthX, int lengthY, T inputAll) { - return new List(list); + if (lengthX < 1) throw new ArgumentOutOfRangeException(nameof(lengthX), "Do not include a length of less than 1"); + if (lengthY < 1) throw new ArgumentOutOfRangeException(nameof(lengthY), "Do not include a width of less than 1"); + lists = new List>(lengthY, new List(lengthX, inputAll)); + } + public T this[int indexX, int indexY] + { + get => Get(indexX, indexY); + set => Set(indexX, indexY, value); + } + + public void Add(Matrix input, DirectionType addDir) + { + if (addDir == DirectionType.y) + { + foreach (List list in input.lists) AddY(list); + return; + } + + foreach (List list in input.lists) AddX(list); + } + public void AddX() { foreach (List list in lists) list.Add(); } + public void AddX(T input) { foreach (List list in lists) list.Add(input); } + public void AddX(T[] input) { foreach (T t in input) AddX(t); } + public void AddX(List input) { foreach (T t in input) AddX(t); } + public void AddY() => lists.Add(new List(lists[0].Length)); + public void AddY(T input) => lists.Add(new List(lists[0].Length, input)); + public void AddY(T[] input) + { + if (input.Length > lists[0].Length) throw new OverflowException(); + lists.Add(new List(input)); + } + public void AddY(List input) + { + if (input.Length > lists[0].Length) throw new OverflowException(); + lists.Add(input); + } + public bool Check(int placeX, int placeY) => lists[placeY][placeX] != null; + public bool Compare(T input) + { + foreach (List list in lists) if (list.Contains(input)) return true; + + return false; + } + public void Convert(T input) { for (int i = 0; i < lists.Length; i++) lists[i] = new(input); } + public void Convert(T[] input) { for (int i = 0; i < lists.Length; i++) lists[i] = new(input); } + public void Convert(List input) { for (int i = 0; i < lists.Length; i++) lists[i] = input; } + public void Convert(Matrix input) => lists = input.lists; + public int Count() + { + int returned = 0; + + foreach (List list in lists) returned += list.Count(); + + return returned; + } + public int Count(DirectionType type) + { + if (type == DirectionType.y) return LengthY; + return LengthX; + } + public Vector2 CountXY() => Length; + public T Get(int placeX, int placeY) => lists[placeY][placeX]; + public void Get(int placeX, int placeY, out T output) => output = Get(placeX, placeY); + public List GetAll() + { + List returned = new(); + + foreach (List list in lists) returned.AddRange(list); + + return returned; + } + public void GetAll(out List output) + { + List returned = new(); + + foreach (List list in lists) returned.AddRange(list); + + output = returned; + } + public IEnumerator GetEnumerator() { foreach (List list in lists) foreach (T t in list) yield return t; } + public void Remove(int placeX, int placeY) => lists[placeY].Remove(placeX); + public void Set(int placeX, int placeY, T input) => lists[placeY][placeX] = input; + public void SetAll(T input) { for (int i = 0; i < lists.Length; i++) for (int j = 0; j < lists[i].Length; j++) lists[i][j] = input; } + + public static Matrix AllDefault(int lengthX, int lengthY) => new(lengthX, lengthY, default); + + public override bool Equals(object obj) => base.Equals(obj); + public bool Equals(Matrix other) => GetAll() == other.GetAll(); + public override int GetHashCode() => base.GetHashCode(); + public override string ToString() => lists.ToString(); + public string ToString(bool showAll) + { + if (showAll) + { + string r = ""; + for (int i = 0; i < lists.Length; i++) + { + for (int j = 0; j < lists[i].Length; j++) + { + r += lists[i][j]; + if (j != lists[i].Length - 1) r += ", "; + } + if (i != lists.Length - 1) r += "\n"; + } + return r; + } + else return ToString(); + } + + public static bool operator ==(Matrix a, Matrix b) => a.Equals(b); + public static bool operator !=(Matrix a, Matrix b) => !a.Equals(b); + + public enum DirectionType + { + x, + y, } } + + [Serializable] + public class ReadOnlyList : IEnumerable, IEnumerable + { + public static ReadOnlyList Empty => new(); + + internal T[] array; + + public T this[int index] => array[index]; + + public List Duplicate => new(array); + public bool IsEmpty => array == Array.Empty(); + public bool IsNull => array == null; + public bool IsNullOrEmpty => IsNull || IsEmpty; + public int Length => array.Length; + + public ReadOnlyList() => array = Array.Empty(); + public ReadOnlyList(params T[] items) => array = items; + public ReadOnlyList(int length) => array = new T[length]; + public ReadOnlyList(int length, T itemAll) + { + array = new T[length]; + for (int i = 0; i < array.Length; i++) array[i] = itemAll; + } + public ReadOnlyList(IEnumerable items) => array = new List(items).ToArray(); + + public bool Contains(Predicate predicate) + { + foreach (T t in array) if (predicate.Invoke(t)) return true; + return false; + } + public bool Contains(T match) + { + foreach (T t in array) if (t.Equals(match)) return true; + return false; + } + public int Count() + { + int r = 0; + foreach (T _ in array) r++; + return r; + } + public int Count(Predicate predicate) + { + if (!Contains(predicate)) return 0; + int r = 0; + foreach (T t in array) if (predicate.Invoke(t)) r++; + return r; + } + public int Count(T match) + { + if (!Contains(match)) return 0; + int r = 0; + foreach (T t in array) if (t.Equals(match)) r++; + return r; + } + public T Find(Predicate predicate) + { + foreach (T t in array) if (predicate.Invoke(t)) return t; + throw new Exception("Parameter " + nameof(predicate) + " does not exist in the list."); + } + public T Find(T match) + { + foreach (T t in array) if (t.Equals(match)) return t; + throw new Exception("Parameter " + nameof(match) + " does not exist in the list."); + } + public T Find(Predicate predicate, int start) + { + for (int i = start; i < array.Length; i++) if (predicate.Invoke(array[i])) return array[i]; + throw new Exception("Parameter " + nameof(predicate) + " does not exist in the list."); + } + public T Find(T match, int start) + { + for (int i = start; i < array.Length; i++) if (array[i].Equals(match)) return array[i]; + throw new Exception("Parameter " + nameof(match) + " does not exist in the list."); + } + public T Find(Predicate predicate, int start, int max) + { + for (int i = start; i <= max; i++) if (predicate.Invoke(array[i])) return array[i]; + throw new Exception("Parameter " + nameof(predicate) + " does not exist in the list."); + } + public T Find(T match, int start, int max) + { + for (int i = start; i <= max; i++) if (array[i].Equals(match)) return array[i]; + throw new Exception("Parameter " + nameof(match) + " does not exist in the list."); + } + public T FindOrDefault(Predicate predicate) + { + foreach (T t in array) if (predicate.Invoke(t)) return t; + return default; + } + public T FindOrDefault(T match) + { + foreach (T t in array) if (t.Equals(match)) return t; + return default; + } + public T FindOrDefault(Predicate predicate, int start) + { + for (int i = start; i < array.Length; i++) if (predicate.Invoke(array[i])) return array[i]; + return default; + } + public T FindOrDefault(T match, int start) + { + for (int i = start; i < array.Length; i++) if (array[i].Equals(match)) return array[i]; + return default; + } + public T FindOrDefault(Predicate predicate, int start, int max) + { + for (int i = start; i <= max; i++) if (predicate.Invoke(array[i])) return array[i]; + return default; + } + public T FindOrDefault(T match, int start, int max) + { + for (int i = start; i <= max; i++) if (array[i].Equals(match)) return array[i]; + return default; + } + public List FindAll(Predicate predicate) + { + List r = new(); + foreach (T t in array) if (predicate.Invoke(t)) r.Add(t); + return r; + } + public List FindAll(T match) + { + List r = new(); + foreach (T t in array) if (t.Equals(match)) r.Add(t); + return r; + } + public List FindAll(Predicate predicate, int start) + { + List r = new(); + for (int i = start; i < array.Length; i++) if (predicate.Invoke(array[i])) r.Add(array[i]); + return r; + } + public List FindAll(T match, int start) + { + List r = new(); + for (int i = start; i < array.Length; i++) if (array[i].Equals(match)) r.Add(array[i]); + return r; + } + public List FindAll(Predicate predicate, int start, int max) + { + List r = new(); + for (int i = start; i <= max; i++) if (predicate.Invoke(array[i])) r.Add(array[i]); + return r; + } + public List FindAll(T match, int start, int max) + { + List r = new(); + for (int i = start; i <= max; i++) if (array[i].Equals(match)) r.Add(array[i]); + return r; + } + public T FindLast(Predicate predicate) + { + for (int i = array.Length - 1; i >= 0; i--) if (predicate.Invoke(array[i])) return array[i]; + throw new Exception("Parameter " + nameof(predicate) + " does not exist in the list."); + } + public T FindLast(T match) + { + for (int i = array.Length - 1; i >= 0; i--) if (array[i].Equals(match)) return array[i]; + throw new Exception("Parameter " + nameof(match) + " does not exist in the list."); + } + public T FindLast(Predicate predicate, int start) + { + for (int i = array.Length - 1; i >= start; i--) if (predicate.Invoke(array[i])) return array[i]; + throw new Exception("Parameter " + nameof(predicate) + " does not exist in the list."); + } + public T FindLast(T match, int start) + { + for (int i = array.Length - 1; i >= start; i--) if (array[i].Equals(match)) return array[i]; + throw new Exception("Parameter " + nameof(match) + " does not exist in the list."); + } + public T FindLast(Predicate predicate, int start, int max) + { + for (int i = max; i >= start; i--) if (predicate.Invoke(array[i])) return array[i]; + throw new Exception("Parameter " + nameof(predicate) + " does not exist in the list."); + } + public T FindLast(T match, int start, int max) + { + for (int i = max; i >= start; i--) if (array[i].Equals(match)) return array[i]; + throw new Exception("Parameter " + nameof(match) + " does not exist in the list."); + } + public T FindLastOrDefault(Predicate predicate) + { + for (int i = array.Length - 1; i >= 0; i--) if (predicate.Invoke(array[i])) return array[i]; + return default; + } + public T FindLastOrDefault(T match) + { + for (int i = array.Length - 1; i >= 0; i--) if (array[i].Equals(match)) return array[i]; + return default; + } + public T FindLastOrDefault(Predicate predicate, int start) + { + for (int i = array.Length - 1; i >= start; i--) if (predicate.Invoke(array[i])) return array[i]; + return default; + } + public T FindLastOrDefault(T match, int start) + { + for (int i = array.Length - 1; i >= start; i--) if (array[i].Equals(match)) return array[i]; + return default; + } + public T FindLastOrDefault(Predicate predicate, int start, int max) + { + for (int i = max; i >= start; i--) if (predicate.Invoke(array[i])) return array[i]; + return default; + } + public T FindLastOrDefault(T match, int start, int max) + { + for (int i = max; i >= start; i--) if (array[i].Equals(match)) return array[i]; + return default; + } + public int FindIndex(Predicate predicate) + { + for (int i = 0; i < array.Length; i++) if (predicate.Invoke(array[i])) return i; + return -1; + } + public int FindIndex(T match) + { + for (int i = 0; i < array.Length; i++) if (array[i].Equals(match)) return i; + return -1; + } + public int FindIndex(Predicate predicate, int start) + { + for (int i = start; i < array.Length; i++) if (predicate.Invoke(array[i])) return i; + return -1; + } + public int FindIndex(T match, int start) + { + for (int i = start; i < array.Length; i++) if (array[i].Equals(match)) return i; + return -1; + } + public int FindIndex(Predicate predicate, int start, int max) + { + for (int i = start; i <= max; i++) if (predicate.Invoke(array[i])) return i; + return -1; + } + public int FindIndex(T match, int start, int max) + { + for (int i = start; i <= max; i++) if (array[i].Equals(match)) return i; + return -1; + } + public List FindAllIndex() + { + List ret = new(); + for (int i = 0; i < array.Length; i++) ret.Add(i); + return ret; + } + public List FindAllIndex(Predicate predicate) + { + List r = new(); + for (int i = 0; i < array.Length; i++) if (predicate.Invoke(array[i])) r.Add(i); + return r; + } + public List FindAllIndex(T match) + { + List r = new(); + for (int i = 0; i < array.Length; i++) if (array[i].Equals(match)) r.Add(i); + return r; + } + public List FindAllIndex(int start) + { + List ret = new(); + for (int i = start; i < array.Length; i++) ret.Add(i); + return ret; + } + public List FindAllIndex(Predicate predicate, int start) + { + List r = new(); + for (int i = start; i < array.Length; i++) if (predicate.Invoke(array[i])) r.Add(i); + return r; + } + public List FindAllIndex(T match, int start) + { + List r = new(); + for (int i = start; i < array.Length; i++) if (array[i].Equals(match)) r.Add(i); + return r; + } + public List FindAllIndex(int start, int max) + { + List ret = new(); + for (int i = start; i <= max; i++) ret.Add(i); + return ret; + } + public List FindAllIndex(Predicate predicate, int start, int max) + { + List r = new(); + for (int i = start; i <= max; i++) if (predicate.Invoke(array[i])) r.Add(i); + return r; + } + public List FindAllIndex(T match, int start, int max) + { + List r = new(); + for (int i = start; i <= max; i++) if (array[i].Equals(match)) r.Add(i); + return r; + } + public int FindLastIndex(Predicate predicate) + { + for (int i = array.Length - 1; i >= 0; i--) if (predicate.Invoke(array[i])) return i; + return -1; + } + public int FindLastIndex(T match) + { + for (int i = array.Length - 1; i >= 0; i--) if (array[i].Equals(match)) return i; + return -1; + } + public int FindLastIndex(Predicate predicate, int start) + { + for (int i = array.Length - 1; i >= start; i--) if (predicate.Invoke(array[i])) return i; + return -1; + } + public int FindLastIndex(T match, int start) + { + for (int i = array.Length - 1; i >= start; i--) if (array[i].Equals(match)) return i; + return -1; + } + public int FindLastIndex(Predicate predicate, int start, int max) + { + for (int i = max; i >= start; i--) if (predicate.Invoke(array[i])) return i; + return -1; + } + public int FindLastIndex(T match, int start, int max) + { + for (int i = max; i >= start; i--) if (array[i].Equals(match)) return i; + return -1; + } + public IEnumerator GetEnumerator() => array.GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() => (IEnumerator)array.GetEnumerator(); + public List GetRange(int start, int max) + { + List r = new(); + for (int i = start; i <= max; i++) r.Add(array[i]); + return r; + } + public bool MatchesAll(Predicate predicate) => FindAll(predicate).array == array; + public bool MatchesAll(T match) => FindAll(match).array == array; + public T[] ToArray() => array; + public List ToList() => new(array); + public System.Collections.Generic.List ToSystemList() => new(array); + public ReadOnlyCollection ToSystemReadOnly() => new(array); + + public override bool Equals(object obj) => base.Equals(obj); + public bool Equals(T[] other) + { + bool returned = true; + if (Length == other.Length) + { + for (int i = 0; i < Length; i++) + { + returned &= array[i].Equals(other[i]); + } + } + return returned; + } + public bool Equals(ReadOnlyList list) + { + if (Length != list.Length) return false; + bool equal = true; + for (int i = 0; i < Length; i++) equal &= array[i].Equals(list[i]); + return equal; + } + public bool Equals(IEnumerable list) => Equals(new ReadOnlyList(list)); + public override int GetHashCode() => base.GetHashCode(); + public override string ToString() => ToString(false); + public string ToString(bool showAll = false) + { + string ret = "List of " + Length + " Elements (" + typeof(T).ToString() + ")"; + if (showAll) for (int i = 0; i < Length; i++) ret += "\n" + i + ": " + array[i]; + return ret; + } + + public static bool operator ==(ReadOnlyList a, ReadOnlyList b) => a.Equals(b); + public static bool operator ==(ReadOnlyList a, T[] b) + { + return a.Equals(b); + } + public static bool operator ==(T[] a, ReadOnlyList b) + { + return b.Equals(a); + } + public static bool operator ==(IEnumerable a, ReadOnlyList b) => a.Equals(b); + public static bool operator ==(ReadOnlyList a, IEnumerable b) => a.Equals(b); + public static bool operator !=(ReadOnlyList a, ReadOnlyList b) => !a.Equals(b); + public static bool operator !=(ReadOnlyList a, T[] b) + { + return !a.Equals(b); + } + public static bool operator !=(T[] a, ReadOnlyList b) + { + return !b.Equals(a); + } + public static bool operator !=(IEnumerable a, ReadOnlyList b) => !a.Equals(b); + public static bool operator !=(ReadOnlyList a, IEnumerable b) => !a.Equals(b); + } } \ No newline at end of file diff --git a/Scripts/Mathematics.cs b/Scripts/Mathematics.cs index 991219d..1830e35 100644 --- a/Scripts/Mathematics.cs +++ b/Scripts/Mathematics.cs @@ -1,9 +1,10 @@ -using System; -using Nerd_STF.Interfaces; +using Nerd_STF.Mathematics.Interfaces; +using System; namespace Nerd_STF.Mathematics { - public struct Angle : INegatives + [Serializable] + public struct Angle : IMathFunctions, INegatives { public float value; @@ -12,14 +13,8 @@ namespace Nerd_STF.Mathematics get { Angle returned = this; - while (returned.value >= 360) - { - returned.value -= 360; - } - while (returned.value < 0) - { - returned.value += 360; - } + while (returned.value >= 360) returned.value -= 360; + while (returned.value < 0) returned.value += 360; return returned; } } @@ -32,74 +27,14 @@ namespace Nerd_STF.Mathematics return returned; } } - public bool IsAcute - { - get - { - Angle returned = Clamped; - - return returned.value > 0 && returned.value < 90; - } - } - public bool IsClamped - { - get - { - return value < 360 && value >= 0; - } - } - public bool IsNegative - { - get - { - return value < 0; - } - } - public bool IsObtuse - { - get - { - Angle returned = Clamped; - - return returned.value > 90 && returned.value < 180; - } - } - public bool IsReflex - { - get - { - Angle returned = Clamped; - - return returned.value > 180 && returned.value < 360; - } - } - public bool IsRight - { - get - { - Angle returned = Clamped; - - return returned.value == 90; - } - } - public bool IsStraight - { - get - { - Angle returned = Clamped; - - return returned.value == 180; - } - } - public bool IsZero - { - get - { - Angle returned = Clamped; - - return returned.value == 0; - } - } + public bool IsAcute => Clamped.value > 0 && Clamped.value < 90; + public bool IsClamped => value < 360 && value >= 0; + public bool IsNegative => value < 0; + public bool IsObtuse => Clamped.value > 90 && Clamped.value < 180; + public bool IsReflex => Clamped.value > 180 && Clamped.value < 360; + public bool IsRight => Clamped.value == 90; + public bool IsStraight => Clamped.value == 180; + public bool IsZero => Clamped.value == 0; public Angle Negative { get @@ -109,13 +44,7 @@ namespace Nerd_STF.Mathematics return returned; } } - public Angle Positive - { - get - { - return Absolute; - } - } + public Angle Positive => Absolute; public AngleType Type { get @@ -130,236 +59,77 @@ namespace Nerd_STF.Mathematics } } - public Angle(float degree) - { - value = degree; - } + public Angle(float degree) => value = degree; - public static Angle Acute - { - get - { - return new Angle(45); - } - } - public static Angle Obtuse - { - get - { - return new Angle(135); - } - } - public static Angle Reflex - { - get - { - return new Angle(270); - } - } - public static Angle Right - { - get - { - return new Angle(90); - } - } - public static Angle Straight - { - get - { - return new Angle(180); - } - } - public static Angle Zero - { - get - { - return new Angle(0); - } - } + public static Angle Acute => new(45); + public static Angle Obtuse => new(135); + public static Angle Reflex => new(270); + public static Angle Right => new(90); + public static Angle Straight => new(180); + public static Angle Zero => new(0); - public override bool Equals(object obj) - { - return base.Equals(obj); - } - public bool Equals(Angle other) - { - return value == other.value; - } - public bool Equals(float other) - { - return value == other; - } - public override int GetHashCode() - { - return base.GetHashCode(); - } - public override string ToString() - { - return value.ToString() + "°"; - } - public string ToString(string format) - { - return value.ToString(format) + "°"; - } + public override bool Equals(object obj) => base.Equals(obj); + public bool Equals(Angle other) => value == other.value; + public bool Equals(float other) => value == other; + public override int GetHashCode() => base.GetHashCode(); + public override string ToString() => value.ToString() + "°"; + public string ToString(string format) => value.ToString(format) + "°"; - public static Angle Average(params Angle[] input) + public Angle Average(params Angle[] objs) { - float[] average = new float[input.Length]; - for (int i = 0; i < input.Length; i++) - { - average[i] = input[i].value; - } + float[] average = new float[objs.Length + 1]; + for (int i = 0; i < objs.Length; i++) average[i] = objs[i].value; + average[objs.Length] = value; return new(Math.Average(average)); } - public static Angle Max(params Angle[] input) + public Angle Max(params Angle[] objs) { - float[] max = new float[input.Length]; - for (int i = 0; i < input.Length; i++) - { - max[i] = input[i].value; - } + float[] max = new float[objs.Length + 1]; + for (int i = 0; i < objs.Length; i++) max[i] = objs[i].value; + max[objs.Length] = value; return new(Math.Max(max)); } - public static Angle Min(params Angle[] input) + public Angle Min(params Angle[] objs) { - float[] min = new float[input.Length]; - for (int i = 0; i < input.Length; i++) - { - min[i] = input[i].value; - } + float[] min = new float[objs.Length + 1]; + for (int i = 0; i < objs.Length; i++) min[i] = objs[i].value; + min[objs.Length] = value; return new(Math.Min(min)); } - public static Angle operator +(Angle a, Angle b) - { - return new(a.value + b.value); - } - public static Angle operator +(Angle a, float b) - { - return new(a.value + b); - } - public static Angle operator +(float a, Angle b) - { - return new(a + b.value); - } - public static Angle operator -(Angle a, Angle b) - { - return new(a.value - b.value); - } - public static Angle operator -(Angle a, float b) - { - return new(a.value - b); - } - public static Angle operator -(float a, Angle b) - { - return new(a - b.value); - } - public static Angle operator *(Angle a, Angle b) - { - return new(a.value * b.value); - } - public static Angle operator *(Angle a, float b) - { - return new(a.value * b); - } - public static Angle operator *(float a, Angle b) - { - return new(a * b.value); - } - public static Angle operator /(Angle a, Angle b) - { - return new(a.value / b.value); - } - public static Angle operator /(Angle a, float b) - { - return new(a.value / b); - } - public static Angle operator /(float a, Angle b) - { - return new Angle(a / b.value); - } - public static bool operator ==(Angle a, Angle b) - { - return a.Equals(b); - } - public static bool operator ==(Angle a, float b) - { - return a.Equals(b); - } - public static bool operator ==(float a, Angle b) - { - return b.Equals(a); - } - public static bool operator !=(Angle a, Angle b) - { - return !a.Equals(b); - } - public static bool operator !=(Angle a, float b) - { - return !a.Equals(b); - } - public static bool operator !=(float a, Angle b) - { - return !b.Equals(a); - } - public static bool operator >(Angle a, Angle b) - { - return a.value > b.value; - } - public static bool operator >(Angle a, float b) - { - return a.value > b; - } - public static bool operator >(float a, Angle b) - { - return a > b.value; - } - public static bool operator <(Angle a, Angle b) - { - return a.value < b.value; - } - public static bool operator <(Angle a, float b) - { - return a.value < b; - } - public static bool operator <(float a, Angle b) - { - return a < b.value; - } - public static bool operator >=(Angle a, Angle b) - { - return a.value > b.value || a.Equals(b); - } - public static bool operator >=(Angle a, float b) - { - return a.value > b || a.Equals(b); - } - public static bool operator >=(float a, Angle b) - { - return a > b.value || b.Equals(a); - } - public static bool operator <=(Angle a, Angle b) - { - return a.value < b.value || a.Equals(b); - } - public static bool operator <=(Angle a, float b) - { - return a.value < b || a.Equals(b); - } - public static bool operator <=(float a, Angle b) - { - return a < b.value || b.Equals(a); - } + public static Angle operator +(Angle a, Angle b) => new(a.value + b.value); + public static Angle operator +(Angle a, float b) => new(a.value + b); + public static Angle operator +(float a, Angle b) => new(a + b.value); + public static Angle operator -(Angle a, Angle b) => new(a.value - b.value); + public static Angle operator -(Angle a, float b) => new(a.value - b); + public static Angle operator -(float a, Angle b) => new(a - b.value); + public static Angle operator *(Angle a, Angle b) => new(a.value * b.value); + public static Angle operator *(Angle a, float b) => new(a.value * b); + public static Angle operator *(float a, Angle b) => new(a * b.value); + public static Angle operator /(Angle a, Angle b) => new(a.value / b.value); + public static Angle operator /(Angle a, float b) => new(a.value / b); + public static Angle operator /(float a, Angle b) => new(a / b.value); + public static bool operator ==(Angle a, Angle b) => a.Equals(b); + public static bool operator ==(Angle a, float b) => a.Equals(b); + public static bool operator ==(float a, Angle b) => b.Equals(a); + public static bool operator !=(Angle a, Angle b) => !a.Equals(b); + public static bool operator !=(Angle a, float b) => !a.Equals(b); + public static bool operator !=(float a, Angle b) => !b.Equals(a); + public static bool operator >(Angle a, Angle b) => a.value > b.value; + public static bool operator >(Angle a, float b) => a.value > b; + public static bool operator >(float a, Angle b) => a > b.value; + public static bool operator <(Angle a, Angle b) => a.value < b.value; + public static bool operator <(Angle a, float b) => a.value < b; + public static bool operator <(float a, Angle b) => a < b.value; + public static bool operator >=(Angle a, Angle b) => a.value > b.value || a.Equals(b); + public static bool operator >=(Angle a, float b) => a.value > b || a.Equals(b); + public static bool operator >=(float a, Angle b) => a > b.value || b.Equals(a); + public static bool operator <=(Angle a, Angle b) => a.value < b.value || a.Equals(b); + public static bool operator <=(Angle a, float b) => a.value < b || a.Equals(b); + public static bool operator <=(float a, Angle b) => a < b.value || b.Equals(a); - public static explicit operator float(Angle input) - { - return input.value; - } - public static explicit operator Angle(float input) - { - return new Angle(input); - } + public static explicit operator float(Angle input) => input.value; + public static explicit operator Angle(float input) => new(input); public enum AngleType { @@ -372,45 +142,16 @@ namespace Nerd_STF.Mathematics } } - public struct Color + [Serializable] + public struct Color : IMathFunctions { public float r, g, b, a; - public bool IsBlue - { - get - { - return b != 0; - } - } - public bool IsClear - { - get - { - return a == 0; - } - } - public bool IsGreen - { - get - { - return g != 0; - } - } - public bool IsRed - { - get - { - return a != 0; - } - } - public bool IsTransparent - { - get - { - return a > 0 && a < 1; - } - } + public bool IsBlue => b != 0; + public bool IsClear => a == 0; + public bool IsGreen => g != 0; + public bool IsRed => a != 0; + public bool IsTransparent => a > 0 && a < 1; public bool IsBroken { get @@ -424,94 +165,22 @@ namespace Nerd_STF.Mathematics } } - public static Color Black - { - get - { - return new Color(0, 0, 0); - } - } - public static Color Blue - { - get - { - return new Color(0, 0, 1); - } - } - public static Color Clear - { - get - { - return new Color(0, 0, 0, 0); - } - } - public static Color Cyan - { - get - { - return new Color(0, 1, 1); - } - } - public static Color Gray - { - get - { - return new Color(0.5f, 0.5f, 0.5f); - } - } - public static Color Green - { - get - { - return new Color(0, 1, 0); - } - } - public static Color Magenta - { - get - { - return new Color(1, 0, 1); - } - } - public static Color Orange - { - get - { - return new Color(1, 0.5f, 0); - } - } - public static Color Purple - { - get - { - return new Color(0.5f, 0, 1); - } - } - public static Color Red - { - get - { - return new Color(1, 0, 0); - } - } - public static Color White - { - get - { - return new Color(1, 1, 1); - } - } - public static Color Yellow - { - get - { - return new Color(1, 1, 0); - } - } + public static Color Black => new(0, 0, 0); + public static Color Blue => new(0, 0, 1); + public static Color Clear => new(0, 0, 0, 0); + public static Color Cyan => new(0, 1, 1); + public static Color Gray => new(0.5f, 0.5f, 0.5f); + public static Color Green => new(0, 1, 0); + public static Color Magenta => new(1, 0, 1); + public static Color Orange => new(1, 0.5f, 0); + public static Color Purple => new(0.5f, 0, 1); + public static Color Red => new(1, 0, 0); + public static Color White => new(1, 1, 1); + public static Color Yellow => new(1, 1, 0); public Color(float r, float g, float b) { - this = new Color(r, g, b, 1); + this = new(r, g, b, 1); } public Color(float r, float g, float b, float a) { @@ -534,272 +203,114 @@ namespace Nerd_STF.Mathematics a = Math.Clamp(a, 0, 1); } - public static Color Average(params Color[] input) + public Color Average(params Color[] objs) { - float[] averageR = new float[input.Length]; - float[] averageG = new float[input.Length]; - float[] averageB = new float[input.Length]; - float[] averageA = new float[input.Length]; - for (int i = 0; i < input.Length; i++) + float[] averageR = new float[objs.Length + 1]; + float[] averageG = new float[objs.Length + 1]; + float[] averageB = new float[objs.Length + 1]; + float[] averageA = new float[objs.Length + 1]; + for (int i = 0; i < objs.Length; i++) { - averageR[i] = input[i].r; - averageG[i] = input[i].g; - averageB[i] = input[i].b; - averageA[i] = input[i].a; + averageR[i] = objs[i].r; + averageG[i] = objs[i].g; + averageB[i] = objs[i].b; + averageA[i] = objs[i].a; } + averageR[objs.Length] = r; + averageG[objs.Length] = g; + averageB[objs.Length] = b; + averageA[objs.Length] = a; return new(Math.Average(averageR), Math.Average(averageG), Math.Average(averageB), Math.Average(averageA)); } - public static Color Max(params Color[] input) + public Color Max(params Color[] objs) { - float[] maxR = new float[input.Length]; - float[] maxG = new float[input.Length]; - float[] maxB = new float[input.Length]; - float[] maxA = new float[input.Length]; - for (int i = 0; i < input.Length; i++) + float[] maxR = new float[objs.Length + 1]; + float[] maxG = new float[objs.Length + 1]; + float[] maxB = new float[objs.Length + 1]; + float[] maxA = new float[objs.Length + 1]; + for (int i = 0; i < objs.Length; i++) { - maxR[i] = input[i].r; - maxG[i] = input[i].g; - maxB[i] = input[i].b; - maxA[i] = input[i].a; + maxR[i] = objs[i].r; + maxG[i] = objs[i].g; + maxB[i] = objs[i].b; + maxA[i] = objs[i].a; } + maxR[objs.Length] = r; + maxG[objs.Length] = g; + maxB[objs.Length] = b; + maxA[objs.Length] = a; return new(Math.Max(maxR), Math.Max(maxG), Math.Max(maxB), Math.Max(maxA)); } - public static Color Min(params Color[] input) + public Color Min(params Color[] objs) { - float[] minR = new float[input.Length]; - float[] minG = new float[input.Length]; - float[] minB = new float[input.Length]; - float[] minA = new float[input.Length]; - for (int i = 0; i < input.Length; i++) + float[] minR = new float[objs.Length + 1]; + float[] minG = new float[objs.Length + 1]; + float[] minB = new float[objs.Length + 1]; + float[] minA = new float[objs.Length + 1]; + for (int i = 0; i < objs.Length; i++) { - minR[i] = input[i].r; - minG[i] = input[i].g; - minB[i] = input[i].b; - minA[i] = input[i].a; + minR[i] = objs[i].r; + minG[i] = objs[i].g; + minB[i] = objs[i].b; + minA[i] = objs[i].a; } + minR[objs.Length] = r; + minG[objs.Length] = g; + minB[objs.Length] = b; + minA[objs.Length] = a; return new(Math.Min(minR), Math.Min(minG), Math.Min(minB), Math.Min(minA)); } - public override bool Equals(object obj) - { - return base.Equals(obj); - } - public bool Equals(Color other) - { - return r == other.r && g == other.g && b == other.b && a == other.a; - } - public override int GetHashCode() - { - return base.GetHashCode(); - } - public override string ToString() - { - return "R: " + r.ToString() + " | G: " + g.ToString() + " | B: " + b.ToString() + " | A: " + a.ToString(); - } - public string ToString(string format) - { - return "R: " + r.ToString(format) + " | G: " + g.ToString(format) + " | B: " + b.ToString(format) + " | A: " + a.ToString(format); - } + public override bool Equals(object obj) => base.Equals(obj); + public bool Equals(Color other) => r == other.r && g == other.g && b == other.b && a == other.a; + public override int GetHashCode() => base.GetHashCode(); + public override string ToString() => "R: " + r.ToString() + " | G: " + g.ToString() + " | B: " + b.ToString() + " | A: " + a.ToString(); + public string ToString(string format) => "R: " + r.ToString(format) + " | G: " + g.ToString(format) + " | B: " + b.ToString(format) + " | A: " + a.ToString(format); - public static Color operator +(Color a, Color b) - { - return new Color(a.r + b.r, a.g + b.g, a.b + b.b, a.a + b.a); - } - public static Color operator +(Color a, float b) - { - return new Color(a.r + b, a.g + b, a.b + b, a.a + b); - } - public static Color operator +(float a, Color b) - { - return new Color(a + b.r, a + b.g, a + b.b, a + b.a); - } - public static Color operator -(Color a, Color b) - { - return new Color(a.r - b.r, a.g - b.g, a.b - b.b, a.a - b.a); - } - public static Color operator -(Color a, float b) - { - return new Color(a.r - b, a.g - b, a.b - b, a.a - b); - } - public static Color operator -(float a, Color b) - { - return new Color(a - b.r, a - b.g, a - b.b, a - b.a); - } - public static Color operator *(Color a, Color b) - { - return new Color(a.r * b.r, a.g * b.g, a.b * b.b, a.a * b.a); - } - public static Color operator *(Color a, float b) - { - return new Color(a.r * b, a.g * b, a.b * b, a.a * b); - } - public static Color operator *(float a, Color b) - { - return new Color(a * b.r, a * b.g, a * b.b, a * b.a); - } - public static Color operator /(Color a, Color b) - { - return new Color(a.r / b.r, a.g / b.g, a.b / b.b, a.a / b.a); - } - public static Color operator /(Color a, float b) - { - return new Color(a.r / b, a.g / b, a.b / b, a.a / b); - } - public static Color operator /(float a, Color b) - { - return new Color(a / b.r, a / b.g, a / b.b, a / b.a); - } - public static bool operator ==(Color a, Color b) - { - return a.Equals(b); - } - public static bool operator !=(Color a, Color b) - { - return !a.Equals(b); - } + public static Color operator +(Color a, Color b) => new(a.r + b.r, a.g + b.g, a.b + b.b, a.a + b.a); + public static Color operator +(Color a, float b) => new(a.r + b, a.g + b, a.b + b, a.a + b); + public static Color operator +(float a, Color b) => new(a + b.r, a + b.g, a + b.b, a + b.a); + public static Color operator -(Color a, Color b) => new(a.r - b.r, a.g - b.g, a.b - b.b, a.a - b.a); + public static Color operator -(Color a, float b) => new(a.r - b, a.g - b, a.b - b, a.a - b); + public static Color operator -(float a, Color b) => new(a - b.r, a - b.g, a - b.b, a - b.a); + public static Color operator *(Color a, Color b) => new(a.r * b.r, a.g * b.g, a.b * b.b, a.a * b.a); + public static Color operator *(Color a, float b) => new(a.r * b, a.g * b, a.b * b, a.a * b); + public static Color operator *(float a, Color b) => new(a * b.r, a * b.g, a * b.b, a * b.a); + public static Color operator /(Color a, Color b) => new(a.r / b.r, a.g / b.g, a.b / b.b, a.a / b.a); + public static Color operator /(Color a, float b) => new(a.r / b, a.g / b, a.b / b, a.a / b); + public static Color operator /(float a, Color b) => new(a / b.r, a / b.g, a / b.b, a / b.a); + public static bool operator ==(Color a, Color b) => a.Equals(b); + public static bool operator !=(Color a, Color b) => !a.Equals(b); - public static implicit operator Color(ColorByte input) - { - return new Color(input.r / 255, input.g / 255, input.b / 255, input.a / 255); - } - public static explicit operator Color(Vector3 input) - { - return new Color(input.x, input.y, input.z, 1); - } - public static explicit operator Color(Vector4 input) - { - return new Color(input.x, input.y, input.z, input.w); - } + public static implicit operator Color(ColorByte input) => new(input.r / 255, input.g / 255, input.b / 255, input.a / 255); + public static explicit operator Color(Vector3 input) => new(input.x, input.y, input.z, 1); + public static explicit operator Color(Vector4 input) => new(input.x, input.y, input.z, input.w); } - public struct ColorByte + [Serializable] + public struct ColorByte : IMathFunctions { public byte r, g, b, a; - public bool IsBlue - { - get - { - return b != byte.MinValue; - } - } - public bool IsClear - { - get - { - return a == byte.MinValue; - } - } - public bool IsGreen - { - get - { - return g != byte.MinValue; - } - } - public bool IsRed - { - get - { - return a != byte.MinValue; - } - } - public bool IsTransparent - { - get - { - return a > byte.MinValue && a < byte.MaxValue; - } - } + public bool IsBlue => b != byte.MinValue; + public bool IsClear => a == byte.MinValue; + public bool IsGreen => g != byte.MinValue; + public bool IsRed => a != byte.MinValue; + public bool IsTransparent => a > byte.MinValue && a < byte.MaxValue; - public static ColorByte Black - { - get - { - return new ColorByte(0, 0, 0); - } - } - public static ColorByte Blue - { - get - { - return new ColorByte(0, 0, 255); - } - } - public static ColorByte Clear - { - get - { - return new ColorByte(0, 0, 0, 0); - } - } - public static ColorByte Cyan - { - get - { - return new ColorByte(0, 255, 255); - } - } - public static ColorByte Gray - { - get - { - return new ColorByte(128, 128, 128); - } - } - public static ColorByte Green - { - get - { - return new ColorByte(0, 255, 0); - } - } - public static ColorByte Magenta - { - get - { - return new ColorByte(255, 0, 255); - } - } - public static ColorByte Orange - { - get - { - return new ColorByte(1, 128, 0); - } - } - public static ColorByte Purple - { - get - { - return new ColorByte(128, 0, 1); - } - } - public static ColorByte Red - { - get - { - return new ColorByte(255, 0, 0); - } - } - public static ColorByte White - { - get - { - return new ColorByte(255, 255, 255); - } - } - public static ColorByte Yellow - { - get - { - return new ColorByte(255, 255, 0); - } - } + public static ColorByte Black => new(0, 0, 0); + public static ColorByte Blue => new(0, 0, 255); + public static ColorByte Clear => new(0, 0, 0, 0); + public static ColorByte Cyan => new(0, 255, 255); + public static ColorByte Gray => new(128, 128, 128); + public static ColorByte Green => new(0, 255, 0); + public static ColorByte Magenta => new(255, 0, 255); + public static ColorByte Orange => new(1, 128, 0); + public static ColorByte Purple => new(128, 0, 1); + public static ColorByte Red => new(255, 0, 0); + public static ColorByte White => new(255, 255, 255); + public static ColorByte Yellow => new(255, 255, 0); - public ColorByte(byte r, byte g, byte b) - { - this = new ColorByte(r, g, b, byte.MaxValue); - } + public ColorByte(byte r, byte g, byte b) => this = new(r, g, b, byte.MaxValue); public ColorByte(byte r, byte g, byte b, byte a) { this.r = r; @@ -807,10 +318,7 @@ namespace Nerd_STF.Mathematics this.b = b; this.a = a; } - public ColorByte(int r, int g, int b) - { - this = new ColorByte(r, g, b, byte.MaxValue); - } + public ColorByte(int r, int g, int b) => this = new(r, g, b, byte.MaxValue); public ColorByte(int r, int g, int b, int a) { this.r = (byte)r; @@ -819,134 +327,86 @@ namespace Nerd_STF.Mathematics this.a = (byte)a; } - public static ColorByte Average(params ColorByte[] input) + public ColorByte Average(params ColorByte[] objs) { - float[] averageR = new float[input.Length]; - float[] averageG = new float[input.Length]; - float[] averageB = new float[input.Length]; - float[] averageA = new float[input.Length]; - for (int i = 0; i < input.Length; i++) + float[] averageR = new float[objs.Length + 1]; + float[] averageG = new float[objs.Length + 1]; + float[] averageB = new float[objs.Length + 1]; + float[] averageA = new float[objs.Length + 1]; + for (int i = 0; i < objs.Length; i++) { - averageR[i] = input[i].r; - averageG[i] = input[i].g; - averageB[i] = input[i].b; - averageA[i] = input[i].a; + averageR[i] = objs[i].r; + averageG[i] = objs[i].g; + averageB[i] = objs[i].b; + averageA[i] = objs[i].a; } + averageR[objs.Length] = r; + averageG[objs.Length] = g; + averageB[objs.Length] = b; + averageA[objs.Length] = a; return new(Math.RoundToInt(Math.Average(averageR)), Math.RoundToInt(Math.Average(averageG)), Math.RoundToInt(Math.Average(averageB)), Math.RoundToInt(Math.Average(averageA))); } - public static ColorByte Max(params ColorByte[] input) + public ColorByte Max(params ColorByte[] objs) { - float[] maxR = new float[input.Length]; - float[] maxG = new float[input.Length]; - float[] maxB = new float[input.Length]; - float[] maxA = new float[input.Length]; - for (int i = 0; i < input.Length; i++) + float[] maxR = new float[objs.Length + 1]; + float[] maxG = new float[objs.Length + 1]; + float[] maxB = new float[objs.Length + 1]; + float[] maxA = new float[objs.Length + 1]; + for (int i = 0; i < objs.Length; i++) { - maxR[i] = input[i].r; - maxG[i] = input[i].g; - maxB[i] = input[i].b; - maxA[i] = input[i].a; + maxR[i] = objs[i].r; + maxG[i] = objs[i].g; + maxB[i] = objs[i].b; + maxA[i] = objs[i].a; } + maxR[objs.Length] = r; + maxG[objs.Length] = g; + maxB[objs.Length] = b; + maxA[objs.Length] = a; return new(Math.RoundToInt(Math.Max(maxR)), Math.RoundToInt(Math.Max(maxG)), Math.RoundToInt(Math.Max(maxB)), Math.RoundToInt(Math.Max(maxA))); } - public static ColorByte Min(params ColorByte[] input) + public ColorByte Min(params ColorByte[] objs) { - float[] minR = new float[input.Length]; - float[] minG = new float[input.Length]; - float[] minB = new float[input.Length]; - float[] minA = new float[input.Length]; - for (int i = 0; i < input.Length; i++) + float[] minR = new float[objs.Length + 1]; + float[] minG = new float[objs.Length + 1]; + float[] minB = new float[objs.Length + 1]; + float[] minA = new float[objs.Length + 1]; + for (int i = 0; i < objs.Length; i++) { - minR[i] = input[i].r; - minG[i] = input[i].g; - minB[i] = input[i].b; - minA[i] = input[i].a; + minR[i] = objs[i].r; + minG[i] = objs[i].g; + minB[i] = objs[i].b; + minA[i] = objs[i].a; } + minR[objs.Length] = r; + minG[objs.Length] = g; + minB[objs.Length] = b; + minA[objs.Length] = a; return new(Math.RoundToInt(Math.Min(minR)), Math.RoundToInt(Math.Min(minG)), Math.RoundToInt(Math.Min(minB)), Math.RoundToInt(Math.Min(minA))); } - public override bool Equals(object obj) - { - return base.Equals(obj); - } - public bool Equals(ColorByte other) - { - return r == other.r && g == other.g && b == other.b && a == other.a; - } - public override int GetHashCode() - { - return base.GetHashCode(); - } - public override string ToString() - { - return "R: " + r.ToString() + " | G: " + g.ToString() + " | B: " + b.ToString() + " | A: " + a.ToString(); - } - public string ToString(string format) - { - return "R: " + r.ToString(format) + " | G: " + g.ToString(format) + " | B: " + b.ToString(format) + " | A: " + a.ToString(format); - } + public override bool Equals(object obj) => base.Equals(obj); + public bool Equals(ColorByte other) => r == other.r && g == other.g && b == other.b && a == other.a; + public override int GetHashCode() => base.GetHashCode(); + public override string ToString() => "R: " + r.ToString() + " | G: " + g.ToString() + " | B: " + b.ToString() + " | A: " + a.ToString(); + public string ToString(string format) => "R: " + r.ToString(format) + " | G: " + g.ToString(format) + " | B: " + b.ToString(format) + " | A: " + a.ToString(format); - public static ColorByte operator +(ColorByte a, ColorByte b) - { - return new ColorByte(a.r + b.r, a.g + b.g, a.b + b.b, a.a + b.a); - } - public static ColorByte operator +(ColorByte a, byte b) - { - return new ColorByte(a.r + b, a.g + b, a.b + b, a.a + b); - } - public static ColorByte operator +(byte a, ColorByte b) - { - return new ColorByte(a + b.r, a + b.g, a + b.b, a + b.a); - } - public static ColorByte operator -(ColorByte a, ColorByte b) - { - return new ColorByte(a.r - b.r, a.g - b.g, a.b - b.b, a.a - b.a); - } - public static ColorByte operator -(ColorByte a, byte b) - { - return new ColorByte(a.r - b, a.g - b, a.b - b, a.a - b); - } - public static ColorByte operator -(byte a, ColorByte b) - { - return new ColorByte(a - b.r, a - b.g, a - b.b, a - b.a); - } - public static ColorByte operator *(ColorByte a, ColorByte b) - { - return new ColorByte(a.r * b.r, a.g * b.g, a.b * b.b, a.a * b.a); - } - public static ColorByte operator *(ColorByte a, byte b) - { - return new ColorByte(a.r * b, a.g * b, a.b * b, a.a * b); - } - public static ColorByte operator *(byte a, ColorByte b) - { - return new ColorByte(a * b.r, a * b.g, a * b.b, a * b.a); - } - public static ColorByte operator /(ColorByte a, ColorByte b) - { - return new ColorByte(a.r / b.r, a.g / b.g, a.b / b.b, a.a / b.a); - } - public static ColorByte operator /(ColorByte a, byte b) - { - return new ColorByte(a.r / b, a.g / b, a.b / b, a.a / b); - } - public static ColorByte operator /(byte a, ColorByte b) - { - return new ColorByte(a / b.r, a / b.g, a / b.b, a / b.a); - } - public static bool operator ==(ColorByte a, ColorByte b) - { - return a.Equals(b); - } - public static bool operator !=(ColorByte a, ColorByte b) - { - return !a.Equals(b); - } + public static ColorByte operator +(ColorByte a, ColorByte b) => new(a.r + b.r, a.g + b.g, a.b + b.b, a.a + b.a); + public static ColorByte operator +(ColorByte a, byte b) => new(a.r + b, a.g + b, a.b + b, a.a + b); + public static ColorByte operator +(byte a, ColorByte b) => new(a + b.r, a + b.g, a + b.b, a + b.a); + public static ColorByte operator -(ColorByte a, ColorByte b) => new(a.r - b.r, a.g - b.g, a.b - b.b, a.a - b.a); + public static ColorByte operator -(ColorByte a, byte b) => new(a.r - b, a.g - b, a.b - b, a.a - b); + public static ColorByte operator -(byte a, ColorByte b) => new(a - b.r, a - b.g, a - b.b, a - b.a); + public static ColorByte operator *(ColorByte a, ColorByte b) => new(a.r * b.r, a.g * b.g, a.b * b.b, a.a * b.a); + public static ColorByte operator *(ColorByte a, byte b) => new(a.r * b, a.g * b, a.b * b, a.a * b); + public static ColorByte operator *(byte a, ColorByte b) => new(a * b.r, a * b.g, a * b.b, a * b.a); + public static ColorByte operator /(ColorByte a, ColorByte b) => new(a.r / b.r, a.g / b.g, a.b / b.b, a.a / b.a); + public static ColorByte operator /(ColorByte a, byte b) => new(a.r / b, a.g / b, a.b / b, a.a / b); + public static ColorByte operator /(byte a, ColorByte b) => new(a / b.r, a / b.g, a / b.b, a / b.a); + public static bool operator ==(ColorByte a, ColorByte b) => a.Equals(b); + public static bool operator !=(ColorByte a, ColorByte b) => !a.Equals(b); - public static explicit operator ColorByte(Color input) - { - return new ColorByte((byte)(input.r * 255), (byte)(input.g * 255), (byte)(input.b * 255), (byte)(input.a * 255)); - } + public static explicit operator ColorByte(Color input) => new((byte)(input.r * 255), (byte)(input.g * 255), (byte)(input.b * 255), (byte)(input.a * 255)); } public static class Math @@ -964,20 +424,14 @@ namespace Nerd_STF.Mathematics public static float Add(params float[] input) { float returned = 0; - foreach (float f in input) - { - returned += f; - } + foreach (float f in input) returned += f; return returned; } public static float Average(params float[] input) { float returned = 0; - foreach (float f in input) - { - returned += f; - } + foreach (float f in input) returned += f; returned /= input.Length; return returned; } @@ -1005,50 +459,35 @@ namespace Nerd_STF.Mathematics public static float Divide(params float[] input) { float returned = input[0]; - for (uint i = 1; i < input.Length; i++) - { - returned /= input[i]; - } + for (uint i = 1; i < input.Length; i++) returned /= input[i]; return returned; } public static float Max(params float[] input) { - float returned = input[1]; - for (uint i = 0; i < input.Length; i++) - { - if (input[i] > returned) returned = input[1]; - } + float returned = input[0]; + for (uint i = 0; i < input.Length; i++) if (input[i] > returned) returned = input[i]; return returned; } public static float Min(params float[] input) { - float returned = input[1]; - for (uint i = 0; i < input.Length; i++) - { - if (input[i] < returned) returned = input[1]; - } + float returned = input[0]; + for (uint i = 0; i < input.Length; i++) if (input[i] < returned) returned = input[0]; return returned; } public static float Multiply(params float[] input) { float returned = 1; - foreach (float f in input) - { - returned *= f; - } + foreach (float f in input) returned *= f; return returned; } public static float Power(float input, int power) { float returned = 1; - for (uint i = 0; i < Absolute(power); i++) - { - returned *= input; - } + for (uint i = 0; i < Absolute(power); i++) returned *= input; if (power < 0) returned = 1 / returned; return returned; } @@ -1060,62 +499,33 @@ namespace Nerd_STF.Mathematics return value; } - public static float Round(float value, float dividend) - { - return Round(value / dividend) * dividend; - } - public static int RoundToInt(float value) - { - return (int)Round(value); - } + public static float Round(float value, float dividend) => Round(value / dividend) * dividend; + public static int RoundToInt(float value) => (int)Round(value); public static float Subtract(params float[] input) { float returned = input[0]; - for (uint i = 1; i < input.Length; i++) - { - returned -= input[i]; - } + for (uint i = 1; i < input.Length; i++) returned -= input[i]; return returned; } public static class Formulas { - public static float CircleArea(float radius) - { - return Pi * radius * radius; - } - public static float CircleCircum(float radius) - { - return 2 * radius * Pi; - } - public static float CircleDiam(float radius) - { - return radius * 2; - } - public static float CircleRadius(float circumference) - { - return circumference / Pi / 2; - } + public static float CircleArea(float radius) => Pi * radius * radius; + public static float CircleCircum(float radius) => 2 * radius * Pi; + public static float CircleDiam(float radius) => radius * 2; + public static float CircleRadius(float circumference) => circumference / Pi / 2; - public static float Perimeter(params float[] sideLengths) - { - return Add(sideLengths); - } + public static float Perimeter(params float[] sideLengths) => Add(sideLengths); - public static float RectangleArea(float length, float width) - { - return length * width; - } + public static float RectangleArea(float length, float width) => length * width; - public static float SquareArea(float length) - { - return RectangleArea(length, length); - } + public static float SquareArea(float length) => RectangleArea(length, length); } } - public struct Percent : INegatives + [Serializable] + public struct Percent : IMathFunctions, INegatives { public float value; @@ -1123,39 +533,15 @@ namespace Nerd_STF.Mathematics { get { - Percent returned = new (value); + Percent returned = new(value); if (returned < 0) returned *= -1; return returned; } } - public bool IsFull - { - get - { - return value == 100; - } - } - public bool IsNegative - { - get - { - return value < 0; - } - } - public bool IsOverflow - { - get - { - return value > 100; - } - } - public bool IsZero - { - get - { - return value == 0; - } - } + public bool IsFull { get => value == 100; } + public bool IsNegative { get => value < 0; } + public bool IsOverflow { get => value > 100; } + public bool IsZero { get => value == 0; } public Percent Negative { get @@ -1165,260 +551,90 @@ namespace Nerd_STF.Mathematics return returned; } } - public Percent Positive - { - get - { - return Absolute; - } - } + public Percent Positive { get => Absolute; } - public static Percent Full - { - get - { - return new Percent(100); - } - } - public static Percent One - { - get - { - return new Percent(1); - } - } - public static Percent Zero - { - get - { - return new Percent(0); - } - } + public static Percent Full { get => new(100); } + public static Percent One { get => new(1); } + public static Percent Zero { get => new(0); } - public Percent(float value) - { - this = new Percent(value, 0, 100); - } - public Percent(float value, float maxValue) - { - this = new Percent(value, 0, maxValue); - } - public Percent(float value, float minValue, float maxValue) - { - this.value = value / (maxValue - minValue); - } + public Percent(float value) => this = new(value, 0, 100); + public Percent(float value, float maxValue) => this = new(value, 0, maxValue); + public Percent(float value, float minValue, float maxValue) => this.value = value / (maxValue - minValue); - public static Percent Average(params Percent[] input) + public Percent Average(params Percent[] objs) { - float[] average = new float[input.Length]; - for (int i = 0; i < input.Length; i++) - { - average[i] = input[i].value; - } + float[] average = new float[objs.Length + 1]; + for (int i = 0; i < objs.Length; i++) average[i] = objs[i].value; + average[objs.Length] = value; return new(Math.Average(average)); } - public static Percent Max(params Percent[] input) + public Percent Max(params Percent[] objs) { - float[] max = new float[input.Length]; - for (int i = 0; i < input.Length; i++) - { - max[i] = input[i].value; - } + float[] max = new float[objs.Length]; + for (int i = 0; i < objs.Length; i++) max[i] = objs[i].value; + max[objs.Length] = value; return new(Math.Max(max)); } - public static Percent Min(params Percent[] input) + public Percent Min(params Percent[] objs) { - float[] min = new float[input.Length]; - for (int i = 0; i < input.Length; i++) - { - min[i] = input[i].value; - } + float[] min = new float[objs.Length]; + for (int i = 0; i < objs.Length; i++) min[i] = objs[i].value; + min[objs.Length] = value; return new(Math.Min(min)); } - public override bool Equals(object obj) - { - return base.Equals(obj); - } - public bool Equals(float other) - { - return value == other || value == (other / 100); - } - public bool Equals(Percent other) - { - return value == other.value; - } - public override int GetHashCode() - { - return base.GetHashCode(); - } - public override string ToString() - { - return value.ToString() + "%"; - } - public string ToString(string format) - { - return value.ToString(format) + "%"; - } + public override bool Equals(object obj) => base.Equals(obj); + public bool Equals(float other) => value == other || value == (other / 100); + public bool Equals(Percent other) => value == other.value; + public override int GetHashCode() => base.GetHashCode(); + public override string ToString() => value.ToString() + "%"; + public string ToString(string format) => value.ToString(format) + "%"; - public static Percent operator +(Percent a, Percent b) - { - return new Percent { value = a.value + b.value }; - } - public static Percent operator +(Percent a, float b) - { - return new Percent { value = a.value + (b / 100) }; - } - public static Percent operator +(float a, Percent b) - { - return new Percent { value = (a / 100) + b.value }; - } - public static Percent operator -(Percent a, Percent b) - { - return new Percent { value = a.value - b.value }; - } - public static Percent operator -(Percent a, float b) - { - return new Percent { value = a.value - (b / 100) }; - } - public static Percent operator -(float a, Percent b) - { - return new Percent { value = (a / 100) + b.value }; - } - public static Percent operator *(Percent a, Percent b) - { - return new Percent { value = a.value * b.value }; - } - public static Percent operator *(Percent a, float b) - { - return new Percent { value = a.value * (b / 100) }; - } - public static Percent operator *(float a, Percent b) - { - return new Percent { value = (a / 100) + b.value }; - } - public static Percent operator /(Percent a, Percent b) - { - return new Percent { value = a.value / b.value }; - } - public static Percent operator /(Percent a, float b) - { - return new Percent { value = a.value / b / 100 }; - } - public static Percent operator /(float a, Percent b) - { - return new Percent { value = (a / 100) + b.value }; - } - public static bool operator ==(Percent a, Percent b) - { - return a.Equals(b); - } - public static bool operator ==(Percent a, float b) - { - return a.Equals(b); - } - public static bool operator ==(float a, Percent b) - { - return b.Equals(a); - } - public static bool operator !=(Percent a, Percent b) - { - return !a.Equals(b); - } - public static bool operator !=(Percent a, float b) - { - return !a.Equals(b); - } - public static bool operator !=(float a, Percent b) - { - return !b.Equals(a); - } - public static bool operator >(Percent a, Percent b) - { - return a.value > b.value; - } - public static bool operator >(Percent a, float b) - { - return a.value > b; - } - public static bool operator >(float a, Percent b) - { - return a > b.value; - } - public static bool operator <(Percent a, Percent b) - { - return a.value < b.value; - } - public static bool operator <(Percent a, float b) - { - return a.value < b; - } - public static bool operator <(float a, Percent b) - { - return a < b.value; - } - public static bool operator >=(Percent a, Percent b) - { - return a.value > b.value || a.Equals(b); - } - public static bool operator >=(Percent a, float b) - { - return a.value > b || a.Equals(b); - } - public static bool operator >=(float a, Percent b) - { - return a > b.value || b.Equals(a); - } - public static bool operator <=(Percent a, Percent b) - { - return a.value < b.value || a.Equals(b); - } - public static bool operator <=(Percent a, float b) - { - return a.value < b || a.Equals(b); - } - public static bool operator <=(float a, Percent b) - { - return a < b.value || b.Equals(a); - } + public static Percent operator +(Percent a, Percent b) => new() { value = a.value + b.value }; + public static Percent operator +(Percent a, float b) => new() { value = a.value + (b / 100) }; + public static Percent operator +(float a, Percent b) => new() { value = (a / 100) + b.value }; + public static Percent operator -(Percent a, Percent b) => new() { value = a.value - b.value }; + public static Percent operator -(Percent a, float b) => new() { value = a.value - (b / 100) }; + public static Percent operator -(float a, Percent b) => new() { value = (a / 100) + b.value }; + public static Percent operator *(Percent a, Percent b) => new() { value = a.value * b.value }; + public static Percent operator *(Percent a, float b) => new() { value = a.value * (b / 100) }; + public static Percent operator *(float a, Percent b) => new() { value = (a / 100) + b.value }; + public static Percent operator /(Percent a, Percent b) => new() { value = a.value / b.value }; + public static Percent operator /(Percent a, float b) => new() { value = a.value / b / 100 }; + public static Percent operator /(float a, Percent b) => new() { value = (a / 100) + b.value }; + public static bool operator ==(Percent a, Percent b) => a.Equals(b); + public static bool operator ==(Percent a, float b) => a.Equals(b); + public static bool operator ==(float a, Percent b) => b.Equals(a); + public static bool operator !=(Percent a, Percent b) => !a.Equals(b); + public static bool operator !=(Percent a, float b) => !a.Equals(b); + public static bool operator !=(float a, Percent b) => !b.Equals(a); + public static bool operator >(Percent a, Percent b) => a.value > b.value; + public static bool operator >(Percent a, float b) => a.value > b; + public static bool operator >(float a, Percent b) => a > b.value; + public static bool operator <(Percent a, Percent b) => a.value < b.value; + public static bool operator <(Percent a, float b) => a.value < b; + public static bool operator <(float a, Percent b) => a < b.value; + public static bool operator >=(Percent a, Percent b) => a.value > b.value || a.Equals(b); + public static bool operator >=(Percent a, float b) => a.value > b || a.Equals(b); + public static bool operator >=(float a, Percent b) => a > b.value || b.Equals(a); + public static bool operator <=(Percent a, Percent b) => a.value < b.value || a.Equals(b); + public static bool operator <=(Percent a, float b) => a.value < b || a.Equals(b); + public static bool operator <=(float a, Percent b) => a < b.value || b.Equals(a); - public static explicit operator float(Percent input) - { - return input.value; - } - public static explicit operator Percent(float input) - { - return new Percent(input); - } + public static explicit operator float(Percent input) => input.value; + public static explicit operator Percent(float input) => new(input); } - public struct Vector + [Serializable] + public struct Vector : IMathFunctions { public Angle direction; public float strength; - public Vector Inverse - { - get - { - return new(direction.value - 180, -strength); - } - } - public Vector Reflected - { - get - { - return new(360 - direction, strength); - } - } + public Vector Inverse { get => new(direction.value - 180, -strength); } + public Vector Reflected { get => new(360 - direction, strength); } - public static Vector Zero - { - get - { - return new Vector(0, 0); - } - } + public static Vector Zero { get => new(0, 0); } public Vector(Angle direction, float strength, bool clampDir = true) { @@ -1426,313 +642,159 @@ namespace Nerd_STF.Mathematics this.direction = direction; this.strength = strength; } - public Vector(float direction, float strength, bool clampDir = true) - { - this = new Vector(new Angle(direction), strength, clampDir); - } + public Vector(float direction, float strength, bool clampDir = true) => this = new Vector(new Angle(direction), strength, clampDir); - public static Vector Average(params Vector[] input) + public Vector Average(params Vector[] objs) { - float[] averageD = new float[input.Length]; - float[] averageS = new float[input.Length]; - for (int i = 0; i < input.Length; i++) + float[] averageD = new float[objs.Length + 1]; + float[] averageS = new float[objs.Length + 1]; + for (int i = 0; i < objs.Length; i++) { - averageD[i] = input[i].direction.Clamped.value; - averageS[i] = input[i].strength; + averageD[i] = objs[i].direction.Clamped.value; + averageS[i] = objs[i].strength; } + averageD[objs.Length] = direction.Clamped.value; + averageS[objs.Length] = strength; return new(Math.Average(averageD), Math.Average(averageS)); } - public static Vector Max(params Vector[] input) + public Vector Max(params Vector[] objs) { - float[] maxD = new float[input.Length]; - float[] maxS = new float[input.Length]; - for (int i = 0; i < input.Length; i++) + float[] maxD = new float[objs.Length + 1]; + float[] maxS = new float[objs.Length + 1]; + for (int i = 0; i < objs.Length; i++) { - maxD[i] = input[i].direction.Clamped.value; - maxS[i] = input[i].strength; + maxD[i] = objs[i].direction.Clamped.value; + maxS[i] = objs[i].strength; } + maxD[objs.Length] = direction.Clamped.value; + maxS[objs.Length] = strength; return new(Math.Max(maxD), Math.Max(maxS)); } - public static Vector Min(params Vector[] input) + public Vector Min(params Vector[] objs) { - float[] minD = new float[input.Length]; - float[] minS = new float[input.Length]; - for (int i = 0; i < input.Length; i++) + float[] minD = new float[objs.Length + 1]; + float[] minS = new float[objs.Length + 1]; + for (int i = 0; i < objs.Length; i++) { - minD[i] = input[i].direction.Clamped.value; - minS[i] = input[i].strength; + minD[i] = objs[i].direction.Clamped.value; + minS[i] = objs[i].strength; } + minD[objs.Length] = direction.Clamped.value; + minS[objs.Length] = strength; return new(Math.Min(minD), Math.Min(minS)); } - public override bool Equals(object obj) - { - return base.Equals(obj); - } - public bool Equals(Vector other) - { - return direction == other.direction && strength == other.strength; - } - public override int GetHashCode() - { - return base.GetHashCode(); - } - public override string ToString() - { - return "D: " + direction.ToString() + " | S: " + strength.ToString(); - } - public string ToString(string format) - { - return "D: " + direction.ToString(format) + " | S: " + strength.ToString(format); - } + public override bool Equals(object obj) => base.Equals(obj); + public bool Equals(Vector other) => direction == other.direction && strength == other.strength; + public override int GetHashCode() => base.GetHashCode(); + public override string ToString() => "D: " + direction.ToString() + " | S: " + strength.ToString(); + public string ToString(string format) => "D: " + direction.ToString(format) + " | S: " + strength.ToString(format); - public static Vector operator +(Vector a, Vector b) - { - return new Vector(a.direction + b.direction, a.strength + b.strength, false); - } - public static Vector operator -(Vector a, Vector b) - { - return new Vector(a.direction - b.direction, a.strength - b.strength, false); - } - public static Vector operator *(Vector a, Vector b) - { - return new Vector(a.direction * b.direction, a.strength * b.strength, false); - } - public static Vector operator /(Vector a, Vector b) - { - return new Vector(a.direction / b.direction, a.strength / b.strength, false); - } - public static bool operator ==(Vector a, Vector b) - { - return a.Equals(b); - } - public static bool operator !=(Vector a, Vector b) - { - return !a.Equals(b); - } + public static Vector operator +(Vector a, Vector b) => new(a.direction + b.direction, a.strength + b.strength, false); + public static Vector operator -(Vector a, Vector b) => new(a.direction - b.direction, a.strength - b.strength, false); + public static Vector operator *(Vector a, Vector b) => new(a.direction * b.direction, a.strength * b.strength, false); + public static Vector operator /(Vector a, Vector b) => new(a.direction / b.direction, a.strength / b.strength, false); + public static bool operator ==(Vector a, Vector b) => a.Equals(b); + public static bool operator !=(Vector a, Vector b) => !a.Equals(b); } - public struct Vector2 + [Serializable] + public struct Vector2 : IMathFunctions { public float x, y; - public static Vector2 NegativeInfinity - { - get - { - return new Vector2(float.NegativeInfinity, float.NegativeInfinity); - } - } - public static Vector2 One - { - get - { - return new Vector2(1, 1); - } - } - public static Vector2 PositiveInfinity - { - get - { - return new Vector2(float.PositiveInfinity, float.PositiveInfinity); - } - } - public static Vector2 Zero - { - get - { - return new Vector2(0, 0); - } - } + public static Vector2 NegativeInfinity { get => new(float.NegativeInfinity, float.NegativeInfinity); } + public static Vector2 One { get => new(1, 1); } + public static Vector2 PositiveInfinity { get => new(float.PositiveInfinity, float.PositiveInfinity); } + public static Vector2 Zero { get => new(0, 0); } - public Vector2(float x) - { - this = new Vector2(x, 0); - } + public Vector2(float x) => this = new Vector2(x, 0); public Vector2(float x, float y) { this.x = x; this.y = y; } - public static Vector2 Average(params Vector2[] input) + public Vector2 Average(params Vector2[] objs) { - float[] averageX = new float[input.Length]; - float[] averageY = new float[input.Length]; - for (int i = 0; i < input.Length; i++) + float[] averageX = new float[objs.Length + 1]; + float[] averageY = new float[objs.Length + 1]; + for (int i = 0; i < objs.Length; i++) { - averageX[i] = input[i].x; - averageY[i] = input[i].y; + averageX[i] = objs[i].x; + averageY[i] = objs[i].y; } + averageX[objs.Length] = x; + averageY[objs.Length] = y; return new(Math.Average(averageX), Math.Average(averageY)); } - public static Vector2 Max(params Vector2[] input) + public Vector2 Max(params Vector2[] objs) { - float[] maxX = new float[input.Length]; - float[] maxY = new float[input.Length]; - for (int i = 0; i < input.Length; i++) + float[] maxX = new float[objs.Length + 1]; + float[] maxY = new float[objs.Length + 1]; + for (int i = 0; i < objs.Length; i++) { - maxX[i] = input[i].x; - maxY[i] = input[i].y; + maxX[i] = objs[i].x; + maxY[i] = objs[i].y; } + maxX[objs.Length] = x; + maxY[objs.Length] = y; return new(Math.Max(maxX), Math.Max(maxY)); } - public static Vector2 Min(params Vector2[] input) + public Vector2 Min(params Vector2[] objs) { - float[] minX = new float[input.Length]; - float[] minY = new float[input.Length]; - for (int i = 0; i < input.Length; i++) + float[] minX = new float[objs.Length + 1]; + float[] minY = new float[objs.Length + 1]; + for (int i = 0; i < objs.Length; i++) { - minX[i] = input[i].x; - minY[i] = input[i].y; + minX[i] = objs[i].x; + minY[i] = objs[i].y; } + minX[objs.Length] = x; + minY[objs.Length] = y; return new(Math.Min(minX), Math.Min(minY)); } - public override bool Equals(object obj) - { - return base.Equals(obj); - } - public bool Equals(Vector2 other) - { - return x == other.x && y == other.y; - } - public override int GetHashCode() - { - return base.GetHashCode(); - } - public override string ToString() - { - return "X: " + x.ToString() + " | Y: " + y.ToString(); - } - public string ToString(string format) - { - return "X: " + x.ToString(format) + " | Y: " + y.ToString(format); - } + public override bool Equals(object obj) => base.Equals(obj); + public bool Equals(Vector2 other) => x == other.x && y == other.y; + public override int GetHashCode() => base.GetHashCode(); + public override string ToString() => "X: " + x.ToString() + " | Y: " + y.ToString(); + public string ToString(string format) => "X: " + x.ToString(format) + " | Y: " + y.ToString(format); - public static Vector2 operator +(Vector2 a, Vector2 b) - { - return new Vector2(a.x + b.x, a.y + b.y); - } - public static Vector2 operator +(Vector2 a, float b) - { - return new Vector2(a.x + b, a.y + b); - } - public static Vector2 operator +(float a, Vector2 b) - { - return new Vector2(a + b.x, a + b.y); - } - public static Vector2 operator -(Vector2 a, Vector2 b) - { - return new Vector2(a.x - b.x, a.y - b.y); - } - public static Vector2 operator -(Vector2 a, float b) - { - return new Vector2(a.x - b, a.y - b); - } - public static Vector2 operator -(float a, Vector2 b) - { - return new Vector2(a - b.x, a - b.y); - } - public static Vector2 operator *(Vector2 a, Vector2 b) - { - return new Vector2(a.x * b.x, a.y * b.y); - } - public static Vector2 operator *(Vector2 a, float b) - { - return new Vector2(a.x * b, a.y * b); - } - public static Vector2 operator *(float a, Vector2 b) - { - return new Vector2(a * b.x, a * b.y); - } - public static Vector2 operator /(Vector2 a, Vector2 b) - { - return new Vector2(a.x / b.x, a.y / b.y); - } - public static Vector2 operator /(Vector2 a, float b) - { - return new Vector2(a.x / b, a.y / b); - } - public static Vector2 operator /(float a, Vector2 b) - { - return new Vector2(a / b.x, a / b.y); - } - public static bool operator ==(Vector2 a, Vector2 b) - { - return a.Equals(b); - } - public static bool operator !=(Vector2 a, Vector2 b) - { - return !a.Equals(b); - } - public static bool operator >(Vector2 a, Vector2 b) - { - return a.x > b.x && a.y > b.y; - } - public static bool operator <(Vector2 a, Vector2 b) - { - return a.x < b.x && a.y < b.y; - } - public static bool operator >=(Vector2 a, Vector2 b) - { - return (a.x > b.x && a.y > b.y) || a.Equals(b); - } - public static bool operator <=(Vector2 a, Vector2 b) - { - return (a.x < b.x && a.y < b.y) || a.Equals(b); - } + public static Vector2 operator +(Vector2 a, Vector2 b) => new(a.x + b.x, a.y + b.y); + public static Vector2 operator +(Vector2 a, float b) => new(a.x + b, a.y + b); + public static Vector2 operator +(float a, Vector2 b) => new(a + b.x, a + b.y); + public static Vector2 operator -(Vector2 a, Vector2 b) => new(a.x - b.x, a.y - b.y); + public static Vector2 operator -(Vector2 a, float b) => new(a.x - b, a.y - b); + public static Vector2 operator -(float a, Vector2 b) => new(a - b.x, a - b.y); + public static Vector2 operator *(Vector2 a, Vector2 b) => new(a.x * b.x, a.y * b.y); + public static Vector2 operator *(Vector2 a, float b) => new(a.x * b, a.y * b); + public static Vector2 operator *(float a, Vector2 b) => new(a * b.x, a * b.y); + public static Vector2 operator /(Vector2 a, Vector2 b) => new(a.x / b.x, a.y / b.y); + public static Vector2 operator /(Vector2 a, float b) => new(a.x / b, a.y / b); + public static Vector2 operator /(float a, Vector2 b) => new(a / b.x, a / b.y); + public static bool operator ==(Vector2 a, Vector2 b) => a.Equals(b); + public static bool operator !=(Vector2 a, Vector2 b) => !a.Equals(b); + public static bool operator >(Vector2 a, Vector2 b) => a.x > b.x && a.y > b.y; + public static bool operator <(Vector2 a, Vector2 b) => a.x < b.x && a.y < b.y; + public static bool operator >=(Vector2 a, Vector2 b) => (a.x > b.x && a.y > b.y) || a.Equals(b); + public static bool operator <=(Vector2 a, Vector2 b) => (a.x < b.x && a.y < b.y) || a.Equals(b); - public static explicit operator Vector2(Vector3 input) - { - return new Vector2(input.x, input.y); - } - public static explicit operator Vector2(Vector4 input) - { - return new Vector2(input.x, input.y); - } + public static explicit operator Vector2(Vector3 input) => new(input.x, input.y); + public static explicit operator Vector2(Vector4 input) => new(input.x, input.y); } - public struct Vector3 + [Serializable] + public struct Vector3 : IMathFunctions { public float x, y, z; - public static Vector3 NegativeInfinity - { - get - { - return new Vector3(float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity); - } - } - public static Vector3 One - { - get - { - return new Vector3(1, 1, 1); - } - } - public static Vector3 PositiveInfinity - { - get - { - return new Vector3(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity); - } - } - public static Vector3 Zero - { - get - { - return new Vector3(0, 0, 0); - } - } + public static Vector3 NegativeInfinity { get => new(float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity); } + public static Vector3 One { get => new(1, 1, 1); } + public static Vector3 PositiveInfinity { get => new(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity); } + public static Vector3 Zero { get => new(0, 0, 0); } - public Vector3(float x) - { - this = new Vector3(x, 0, 0); - } - public Vector3(float x, float y) - { - this = new Vector3(x, y, 0); - } + public Vector3(float x) => this = new(x, 0, 0); + public Vector3(float x, float y) => this = new(x, y, 0); public Vector3(float x, float y, float z) { this.x = x; @@ -1740,198 +802,97 @@ namespace Nerd_STF.Mathematics this.z = z; } - public static Vector3 Average(params Vector3[] input) + public Vector3 Average(params Vector3[] objs) { - float[] averageX = new float[input.Length]; - float[] averageY = new float[input.Length]; - float[] averageZ = new float[input.Length]; - for (int i = 0; i < input.Length; i++) + float[] averageX = new float[objs.Length + 1]; + float[] averageY = new float[objs.Length + 1]; + float[] averageZ = new float[objs.Length + 1]; + for (int i = 0; i < objs.Length; i++) { - averageX[i] = input[i].x; - averageY[i] = input[i].y; - averageZ[i] = input[i].z; + averageX[i] = objs[i].x; + averageY[i] = objs[i].y; + averageZ[i] = objs[i].z; } + averageX[objs.Length] = x; + averageY[objs.Length] = y; + averageZ[objs.Length] = z; return new(Math.Average(averageX), Math.Average(averageY), Math.Average(averageZ)); } - public static Vector3 Max(params Vector3[] input) + public Vector3 Max(params Vector3[] objs) { - float[] maxX = new float[input.Length]; - float[] maxY = new float[input.Length]; - float[] maxZ = new float[input.Length]; - for (int i = 0; i < input.Length; i++) + float[] maxX = new float[objs.Length + 1]; + float[] maxY = new float[objs.Length + 1]; + float[] maxZ = new float[objs.Length + 1]; + for (int i = 0; i < objs.Length; i++) { - maxX[i] = input[i].x; - maxY[i] = input[i].y; - maxZ[i] = input[i].z; + maxX[i] = objs[i].x; + maxY[i] = objs[i].y; + maxZ[i] = objs[i].z; } + maxX[objs.Length] = x; + maxY[objs.Length] = y; + maxZ[objs.Length] = z; return new(Math.Max(maxX), Math.Max(maxY), Math.Max(maxZ)); } - public static Vector3 Min(params Vector3[] input) + public Vector3 Min(params Vector3[] objs) { - float[] minX = new float[input.Length]; - float[] minY = new float[input.Length]; - float[] minZ = new float[input.Length]; - for (int i = 0; i < input.Length; i++) + float[] minX = new float[objs.Length + 1]; + float[] minY = new float[objs.Length + 1]; + float[] minZ = new float[objs.Length + 1]; + for (int i = 0; i < objs.Length; i++) { - minX[i] = input[i].x; - minY[i] = input[i].y; - minZ[i] = input[i].z; + minX[i] = objs[i].x; + minY[i] = objs[i].y; + minZ[i] = objs[i].z; } + minX[objs.Length] = x; + minY[objs.Length] = y; + minZ[objs.Length] = z; return new(Math.Min(minX), Math.Min(minY), Math.Min(minZ)); } - public override bool Equals(object obj) - { - return base.Equals(obj); - } - public bool Equals(Vector3 other) - { - return x == other.x && y == other.y && z == other.z; - } - public override int GetHashCode() - { - return base.GetHashCode(); - } - public override string ToString() - { - return "X: " + x.ToString() + " | Y: " + y.ToString() + " | Z:" + z.ToString(); - } - public string ToString(string format) - { - return "X: " + x.ToString(format) + " | Y: " + y.ToString(format) + " | Z:" + z.ToString(format); - } + public override bool Equals(object obj) => base.Equals(obj); + public bool Equals(Vector3 other) => x == other.x && y == other.y && z == other.z; + public override int GetHashCode() => base.GetHashCode(); + public override string ToString() => "X: " + x.ToString() + " | Y: " + y.ToString() + " | Z:" + z.ToString(); + public string ToString(string format) => "X: " + x.ToString(format) + " | Y: " + y.ToString(format) + " | Z:" + z.ToString(format); - public static Vector3 operator +(Vector3 a, Vector3 b) - { - return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z); - } - public static Vector3 operator +(Vector3 a, float b) - { - return new Vector3(a.x + b, a.y + b, a.z + b); - } - public static Vector3 operator +(float a, Vector3 b) - { - return new Vector3(a + b.x, a + b.y, a + b.z); - } - public static Vector3 operator -(Vector3 a, Vector3 b) - { - return new Vector3(a.x - b.x, a.y - b.y, a.z - b.z); - } - public static Vector3 operator -(Vector3 a, float b) - { - return new Vector3(a.x - b, a.y - b, a.z - b); - } - public static Vector3 operator -(float a, Vector3 b) - { - return new Vector3(a - b.x, a - b.y, a - b.z); - } - public static Vector3 operator *(Vector3 a, Vector3 b) - { - return new Vector3(a.x * b.x, a.y * b.y, a.z * b.z); - } - public static Vector3 operator *(Vector3 a, float b) - { - return new Vector3(a.x * b, a.y * b, a.z * b); - } - public static Vector3 operator *(float a, Vector3 b) - { - return new Vector3(a * b.x, a * b.y, a * b.z); - } - public static Vector3 operator /(Vector3 a, Vector3 b) - { - return new Vector3(a.x / b.x, a.y / b.y, a.z / b.z); - } - public static Vector3 operator /(Vector3 a, float b) - { - return new Vector3(a.x / b, a.y / b, a.z / b); - } - public static Vector3 operator /(float a, Vector3 b) - { - return new Vector3(a / b.x, a / b.y, a / b.z); - } - public static bool operator ==(Vector3 a, Vector3 b) - { - return a.Equals(b); - } - public static bool operator !=(Vector3 a, Vector3 b) - { - return !a.Equals(b); - } - public static bool operator >(Vector3 a, Vector3 b) - { - return a.x > b.x && a.y > b.y && a.z > b.z; - } - public static bool operator <(Vector3 a, Vector3 b) - { - return a.x < b.x && a.y < b.y && a.z < b.z; - } - public static bool operator >=(Vector3 a, Vector3 b) - { - return (a.x > b.x && a.y > b.y && a.z > b.z) || a.Equals(b); - } - public static bool operator <=(Vector3 a, Vector3 b) - { - return (a.x < b.x && a.y < b.y && a.z < b.z) || a.Equals(b); - } + public static Vector3 operator +(Vector3 a, Vector3 b) => new(a.x + b.x, a.y + b.y, a.z + b.z); + public static Vector3 operator +(Vector3 a, float b) => new(a.x + b, a.y + b, a.z + b); + public static Vector3 operator +(float a, Vector3 b) => new(a + b.x, a + b.y, a + b.z); + public static Vector3 operator -(Vector3 a, Vector3 b) => new(a.x - b.x, a.y - b.y, a.z - b.z); + public static Vector3 operator -(Vector3 a, float b) => new(a.x - b, a.y - b, a.z - b); + public static Vector3 operator -(float a, Vector3 b) => new(a - b.x, a - b.y, a - b.z); + public static Vector3 operator *(Vector3 a, Vector3 b) => new(a.x * b.x, a.y * b.y, a.z * b.z); + public static Vector3 operator *(Vector3 a, float b) => new(a.x * b, a.y * b, a.z * b); + public static Vector3 operator *(float a, Vector3 b) => new(a * b.x, a * b.y, a * b.z); + public static Vector3 operator /(Vector3 a, Vector3 b) => new(a.x / b.x, a.y / b.y, a.z / b.z); + public static Vector3 operator /(Vector3 a, float b) => new(a.x / b, a.y / b, a.z / b); + public static Vector3 operator /(float a, Vector3 b) => new(a / b.x, a / b.y, a / b.z); + public static bool operator ==(Vector3 a, Vector3 b) => a.Equals(b); + public static bool operator !=(Vector3 a, Vector3 b) => !a.Equals(b); + public static bool operator >(Vector3 a, Vector3 b) => a.x > b.x && a.y > b.y && a.z > b.z; + public static bool operator <(Vector3 a, Vector3 b) => a.x < b.x && a.y < b.y && a.z < b.z; + public static bool operator >=(Vector3 a, Vector3 b) => (a.x > b.x && a.y > b.y && a.z > b.z) || a.Equals(b); + public static bool operator <=(Vector3 a, Vector3 b) => (a.x < b.x && a.y < b.y && a.z < b.z) || a.Equals(b); - public static implicit operator Vector3(Color input) - { - return new Vector3(input.r, input.g, input.b); - } - public static implicit operator Vector3(Vector2 input) - { - return new Vector3(input.x, input.y); - } - public static explicit operator Vector3(Vector4 input) - { - return new Vector3(input.x, input.y, input.z); - } + public static implicit operator Vector3(Color input) => new(input.r, input.g, input.b); + public static implicit operator Vector3(Vector2 input) => new(input.x, input.y); + public static explicit operator Vector3(Vector4 input) => new(input.x, input.y, input.z); } - public struct Vector4 + [Serializable] + public struct Vector4 : IMathFunctions { public float x, y, z, w; - public static Vector4 NegativeInfinity - { - get - { - return new Vector4(float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity); - } - } - public static Vector4 One - { - get - { - return new Vector4(1, 1, 1, 1); - } - } - public static Vector4 PositiveInfinity - { - get - { - return new Vector4(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity); - } - } - public static Vector4 Zero - { - get - { - return new Vector4(0, 0, 0, 0); - } - } + public static Vector4 NegativeInfinity { get => new(float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity); } + public static Vector4 One { get => new(1, 1, 1, 1); } + public static Vector4 PositiveInfinity { get => new(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity); } + public static Vector4 Zero { get => new(0, 0, 0, 0); } - public Vector4(float x) - { - this = new Vector4(x, 0, 0, 0); - } - public Vector4(float x, float y) - { - this = new Vector4(x, y, 0, 0); - } - public Vector4(float x, float y, float z) - { - this = new Vector4(x, y, z, 0); - } + public Vector4(float x) => this = new(x, 0, 0, 0); + public Vector4(float x, float y) => this = new(x, y, 0, 0); + public Vector4(float x, float y, float z) => this = new(x, y, z, 0); public Vector4(float x, float y, float z, float w) { this.x = x; @@ -1940,157 +901,109 @@ namespace Nerd_STF.Mathematics this.w = w; } - public static Vector4 Average(params Vector4[] input) + public Vector4 Average(params Vector4[] objs) { - float[] averageX = new float[input.Length]; - float[] averageY = new float[input.Length]; - float[] averageZ = new float[input.Length]; - float[] averageW = new float[input.Length]; - for (int i = 0; i < input.Length; i++) + float[] averageX = new float[objs.Length + 1]; + float[] averageY = new float[objs.Length + 1]; + float[] averageZ = new float[objs.Length + 1]; + float[] averageW = new float[objs.Length + 1]; + for (int i = 0; i < objs.Length; i++) { - averageX[i] = input[i].x; - averageY[i] = input[i].y; - averageZ[i] = input[i].z; - averageW[i] = input[i].w; + averageX[i] = objs[i].x; + averageY[i] = objs[i].y; + averageZ[i] = objs[i].z; + averageW[i] = objs[i].w; } + averageX[objs.Length] = x; + averageY[objs.Length] = y; + averageZ[objs.Length] = z; + averageW[objs.Length] = w; return new(Math.Average(averageX), Math.Average(averageY), Math.Average(averageZ), Math.Average(averageW)); } - public static Vector4 Max(params Vector4[] input) + public Vector4 Max(params Vector4[] objs) { - float[] maxX = new float[input.Length]; - float[] maxY = new float[input.Length]; - float[] maxZ = new float[input.Length]; - float[] maxW = new float[input.Length]; - for (int i = 0; i < input.Length; i++) + float[] maxX = new float[objs.Length + 1]; + float[] maxY = new float[objs.Length + 1]; + float[] maxZ = new float[objs.Length + 1]; + float[] maxW = new float[objs.Length + 1]; + for (int i = 0; i < objs.Length; i++) { - maxX[i] = input[i].x; - maxY[i] = input[i].y; - maxZ[i] = input[i].z; - maxW[i] = input[i].w; + maxX[i] = objs[i].x; + maxY[i] = objs[i].y; + maxZ[i] = objs[i].z; + maxW[i] = objs[i].w; } + maxX[objs.Length] = x; + maxY[objs.Length] = y; + maxZ[objs.Length] = z; + maxW[objs.Length] = w; return new(Math.Max(maxX), Math.Max(maxY), Math.Max(maxZ), Math.Max(maxW)); } - public static Vector4 Min(params Vector4[] input) + public Vector4 Min(params Vector4[] objs) { - float[] minX = new float[input.Length]; - float[] minY = new float[input.Length]; - float[] minZ = new float[input.Length]; - float[] minW = new float[input.Length]; - for (int i = 0; i < input.Length; i++) + float[] minX = new float[objs.Length + 1]; + float[] minY = new float[objs.Length + 1]; + float[] minZ = new float[objs.Length + 1]; + float[] minW = new float[objs.Length + 1]; + for (int i = 0; i < objs.Length; i++) { - minX[i] = input[i].x; - minY[i] = input[i].y; - minZ[i] = input[i].z; - minW[i] = input[i].w; + minX[i] = objs[i].x; + minY[i] = objs[i].y; + minZ[i] = objs[i].z; + minW[i] = objs[i].w; } + minX[objs.Length] = x; + minY[objs.Length] = y; + minZ[objs.Length] = z; + minW[objs.Length] = w; return new(Math.Min(minX), Math.Min(minY), Math.Min(minZ), Math.Min(minW)); } - public override bool Equals(object obj) + public override bool Equals(object obj) => base.Equals(obj); + public bool Equals(Vector4 other) => x == other.x && y == other.y && z == other.z && w == other.w; + public override int GetHashCode() => base.GetHashCode(); + public override string ToString() => "X: " + x.ToString() + " | Y: " + y.ToString() + " | Z: " + z.ToString() + " | W: " + w.ToString(); + public string ToString(string format) => "X: " + x.ToString(format) + " | Y: " + y.ToString(format) + " | Z: " + z.ToString(format) + " | W: " + w.ToString(format); + + public static Vector4 operator +(Vector4 a, Vector4 b) => new(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w); + public static Vector4 operator +(Vector4 a, float b) => new(a.x + b, a.y + b, a.z + b, a.w + b); + public static Vector4 operator +(float a, Vector4 b) => new(a + b.x, a + b.y, a + b.z, a + b.w); + public static Vector4 operator -(Vector4 a, Vector4 b) => new(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w); + public static Vector4 operator -(Vector4 a, float b) => new(a.x - b, a.y - b, a.z - b, a.w - b); + public static Vector4 operator -(float a, Vector4 b) => new(a - b.x, a - b.y, a - b.z, a - b.w); + public static Vector4 operator *(Vector4 a, Vector4 b) => new(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w); + public static Vector4 operator *(Vector4 a, float b) => new(a.x * b, a.y * b, a.z * b, a.w * b); + public static Vector4 operator *(float a, Vector4 b) => new(a * b.x, a * b.y, a * b.z, a * b.w); + public static Vector4 operator /(Vector4 a, Vector4 b) => new(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w); + public static Vector4 operator /(Vector4 a, float b) => new(a.x / b, a.y / b, a.z / b, a.w / b); + public static Vector4 operator /(float a, Vector4 b) => new(a / b.x, a / b.y, a / b.z, a / b.w); + public static bool operator ==(Vector4 a, Vector4 b) => a.Equals(b); + public static bool operator !=(Vector4 a, Vector4 b) => !a.Equals(b); + public static bool operator >(Vector4 a, Vector4 b) => a.x > b.x && a.y > b.y && a.z > b.z && a.w > b.w; + public static bool operator <(Vector4 a, Vector4 b) => a.x < b.x && a.y < b.y && a.z < b.z && a.w < b.w; + public static bool operator >=(Vector4 a, Vector4 b) => (a.x > b.x && a.y > b.y && a.z > b.z && a.w > b.w) || a.Equals(b); + public static bool operator <=(Vector4 a, Vector4 b) => (a.x < b.x && a.y < b.y && a.z < b.z && a.w < b.w) || a.Equals(b); + + public static implicit operator Vector4(Color input) => new(input.r, input.g, input.b, input.a); + public static implicit operator Vector4(Vector2 input) => new(input.x, input.y); + public static implicit operator Vector4(Vector3 input) => new(input.x, input.y, input.z); + } + + namespace Interfaces + { + public interface IMathFunctions { - return base.Equals(obj); - } - public bool Equals(Vector4 other) - { - return x == other.x && y == other.y && z == other.z && w == other.w; - } - public override int GetHashCode() - { - return base.GetHashCode(); - } - public override string ToString() - { - return "X: " + x.ToString() + " | Y: " + y.ToString() + " | Z: " + z.ToString() + " | W: " + w.ToString(); - } - public string ToString(string format) - { - return "X: " + x.ToString(format) + " | Y: " + y.ToString(format) + " | Z: " + z.ToString(format) + " | W: " + w.ToString(format); + public T Average(params T[] objs); + public T Max(params T[] objs); + public T Min(params T[] objs); } - public static Vector4 operator +(Vector4 a, Vector4 b) + public interface INegatives { - return new Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w); - } - public static Vector4 operator +(Vector4 a, float b) - { - return new Vector4(a.x + b, a.y + b, a.z + b, a.w + b); - } - public static Vector4 operator +(float a, Vector4 b) - { - return new Vector4(a + b.x, a + b.y, a + b.z, a + b.w); - } - public static Vector4 operator -(Vector4 a, Vector4 b) - { - return new Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w); - } - public static Vector4 operator -(Vector4 a, float b) - { - return new Vector4(a.x - b, a.y - b, a.z - b, a.w - b); - } - public static Vector4 operator -(float a, Vector4 b) - { - return new Vector4(a - b.x, a - b.y, a - b.z, a - b.w); - } - public static Vector4 operator *(Vector4 a, Vector4 b) - { - return new Vector4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w); - } - public static Vector4 operator *(Vector4 a, float b) - { - return new Vector4(a.x * b, a.y * b, a.z * b, a.w * b); - } - public static Vector4 operator *(float a, Vector4 b) - { - return new Vector4(a * b.x, a * b.y, a * b.z, a * b.w); - } - public static Vector4 operator /(Vector4 a, Vector4 b) - { - return new Vector4(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w); - } - public static Vector4 operator /(Vector4 a, float b) - { - return new Vector4(a.x / b, a.y / b, a.z / b, a.w / b); - } - public static Vector4 operator /(float a, Vector4 b) - { - return new Vector4(a / b.x, a / b.y, a / b.z, a / b.w); - } - public static bool operator ==(Vector4 a, Vector4 b) - { - return a.Equals(b); - } - public static bool operator !=(Vector4 a, Vector4 b) - { - return !a.Equals(b); - } - public static bool operator >(Vector4 a, Vector4 b) - { - return a.x > b.x && a.y > b.y && a.z > b.z && a.w > b.w; - } - public static bool operator <(Vector4 a, Vector4 b) - { - return a.x < b.x && a.y < b.y && a.z < b.z && a.w < b.w; - } - public static bool operator >=(Vector4 a, Vector4 b) - { - return (a.x > b.x && a.y > b.y && a.z > b.z && a.w > b.w) || a.Equals(b); - } - public static bool operator <=(Vector4 a, Vector4 b) - { - return (a.x < b.x && a.y < b.y && a.z < b.z && a.w < b.w) || a.Equals(b); - } - - public static implicit operator Vector4(Color input) - { - return new Vector4(input.r, input.g, input.b, input.a); - } - public static implicit operator Vector4(Vector2 input) - { - return new Vector4(input.x, input.y); - } - public static implicit operator Vector4(Vector3 input) - { - return new Vector4(input.x, input.y, input.z); + public T Absolute { get; } + public bool IsNegative { get; } + public T Negative { get; } + public T Positive { get; } } } } \ No newline at end of file diff --git a/changelog.md b/changelog.md index ae9fccc..72c6c80 100644 --- a/changelog.md +++ b/changelog.md @@ -23,11 +23,143 @@ * public static class Stats # Version 2021.1: - Mainly centered around files and filesaving. + This update is mainly centered around files and filesaving. * Nerd_STF + Nerd_STF.Filesaving + File + BinaryFile + ByteFile + TextFile - - public class Miscellaneous \ No newline at end of file + - public class Miscellaneous + +# Version 2021.2: + This update is centered around lists. + * Nerd_STF: + * public static class Hashes: + + public static string SHA1(string) + + public static string SHA256(string) + + public static string SHA384(string) + + public static string SHA512(string) + + public static byte[] MD5(byte[]) + + public static byte[] SHA1(byte[]) + + public static byte[] SHA256(byte[]) + + public static byte[] SHA384(byte[]) + + public static byte[] SHA512(byte[]) + = Made `public static string MD5(string)` include more of my own scripting + * Nerd_STF.File: + = Nerd_STF.Filesaving: Moved to Nerd_STF.File.Saving + * Nerd_STF.File.Saving: + + public class BinaryFile + * public class ByteFile: + + public override bool TryLoad(out File>) + = Made `public static ByteFile Load(string)` load files faster + = Made `public override void Load(bool)` load files faster + = Made `public override void Save()` save files faster + = Made `public override void Write(byte, bool)` save files faster + = Made `public override void Write(List, bool)` save files faster + * public abstract class File: + + public bool Exists; + + public abstract bool TryLoad(out File) + * public class TextFile: + + public override bool TryLoad(out File) + = Made `public static TextFile Load(string)` load files faster + = Made `public override void Load(bool)` load files faster + = Made `public override Save()` save files faster + = Made `public override void Write(char, bool)` save files faster + = Made `public override void Write(string, bool)` save files faster + - Nerd_STF.Interfaces: + = Moved `public interface INegatives` to `Nerd_STF.Mathematics.Interfaces` + * Nerd_STF.Lists: + + public class ReadOnlyList + = public class List: Completely reworked everything in `List` + + public int this[T] { get; set; } + + public List(IEnumerable) + + public bool IsEmpty { get; } + + public bool IsNull { get; } + + public bool IsNullOrEmpty { get; } + + public bool Contains(Predicate) + + public int Count (Predicate) + + public void AddRange(IEnumerable) + + public bool Any(Predicate) + + public bool Any(T) + + public T Find(Predicate) + + public T Find(Predicate, int) + + public T Find(Predicate, int, int) + + public T FindOrDefault(Predicate) + + public T FindOrDefault(T) + + public T FindOrDefault(Predicate, int) + + public T FindOrDefault(T, int) + + public T FindOrDefault(Predicate, int, int) + + public T FindOrDefault(T, int, int) + + public List FindAll(Predicate) + + public List FindAll(Predicate, int) + + public List FindAll(Predicate, int, int) + + public List FindLast(Predicate) + + public List FindLast(Predicate, int) + + public List FindLast(Predicate, int, int) + + public T FindLastOrDefault(Predicate) + + public T FindLastOrDefault(T) + + public T FindLastOrDefault(Predicate, int) + + public T FindLastOrDefault(T, int) + + public T FindLastOrDefault(Predicate, int, int) + + public T FindLastOrDefault(T, int, int) + + public int FindIndex(Predicate) + + public int FindIndex(Predicate, int) + + public int FindIndex(Predicate, int, int) + + public List FindAllIndex(Predicate) + + public List FindAllIndex(Predicate, int) + + public List FindAllIndex(Predicate, int, int) + + public int FindLastIndex(Predicate) + + public int FindLastIndex(Predicate, int) + + public int FindLastIndex(Predicate, int, int) + + public bool MatchesAll(Predicate) + + public void Remove(Predicate) + + public void RemoveAll(Predicate) + + public void RemoveAll(T) + + public void RemoveLast(Predicate) + + public void RemoveLast(T) + + IEnumerator IEnumerable.GetEnumerator() + + public void Randomize() + + public List FindAllIndex() + + public void Shuffle() + + public List FindAllIndex(int) + + public List FindAllIndex(int, int) + + public ReadOnlyList ToReadOnly() + = Made `public List Duplicate()` a readonly variable and better (`public List Duplicate { get; }`) + = Renamed the internal array to `array,` as opposed to `list` + = Renamed `public void Add(T[])` to `public void AddRange(T[])` + = Renamed `public void Add(List)` to `public void AddRange(List)` + = Renamed `public bool Compare(T)` to `public bool Contains(T)` + = Renamed `public void Remove(int, bool)` to `public void Remove(int)` + = Renamed `public void SetAll(T)` to `public void Fill(T)` + = Made `public string ToString(bool)` count up from zero instead of one when the bool is set to true. + = Renamed `public ReadOnlyCollection AsReadOnly()` to `public ReadOnlyCollection ToSystemReadOnly()` + - public bool Check(int) + - public void Convert(T) + - public void Convert(T[]) + - public void Convert(List) + - public T Get(int) + - public void Get(int, out T) + - public T[] GetAll() + - public void Set(int, T) + - public void Set(T[]) + - public void Set(List) + - public static List AllDefault(int) + - public static List operator +(T[], List) + - public static List operator +(List, int) + - public static List operator +(T, List) + - public static List operator *(List, int) + - public static explicit operator T[](List) + - public static explicit operator List(T[]) + = Marked `public class Nerd_STF.Lists.Matrix` as deprecated. This class will be removed or heavily modified in a future release. Also removed all instances of removed List methods and replaced them with work-arounds. + * Nerd_STF.Mathematics: + = Marked `public struct Angle` as serializable + = Marked `public struct Color` as serializable + = Marked `public struct ColorByte` as serializable + = Marked `public struct Percent` as serializable + = Marked `public struct Vector` as serializable + = Marked `public struct Vector2` as serializable + = Marked `public struct Vector3` as serializable + = Marked `public struct Vector4` as serializable + + public static class Misc + + public static string PlaceMaker(int) \ No newline at end of file