Skip to content

Instantly share code, notes, and snippets.

@ThePyProgrammer
Created June 1, 2021 06:01
Show Gist options
  • Save ThePyProgrammer/bf35e078240423fd3c5c65cdd9e6841d to your computer and use it in GitHub Desktop.
Save ThePyProgrammer/bf35e078240423fd3c5c65cdd9e6841d to your computer and use it in GitHub Desktop.
A Simplified Array System written in C#, made for Functionality.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace App
{
class SuperArray<R> : IEnumerable<R>
{
R[] BaseArray;
int Length
{
get
{
return BaseArray.Length;
}
}
public SuperArray()
{
BaseArray = new R[0];
}
public SuperArray(int capacity)
{
BaseArray = new R[capacity];
}
public SuperArray(params R[] values)
{
BaseArray = values;
}
public SuperArray(SuperArray<R> values)
{
BaseArray = values.BaseArray;
}
public SuperArray<R> Add(params R[] values)
{
int size1 = BaseArray.Length,
size2 = values.Length;
R[] newArray = new R[size1 + size2];
int i = 0;
for (; i < size1 + size2; i++)
{
if (i < size1) newArray[i] = BaseArray[i];
else newArray[i] = values[i - size1];
}
BaseArray = newArray;
return this;
}
public SuperArray<R> Add(SuperArray<R> values)
{
return Add(values.BaseArray);
}
public static SuperArray<int> range(int start, int stop, int step = 1)
{
var array = new SuperArray<int>();
for (int i = start; i < stop; i += step)
{
array.Add(i);
}
return array;
}
public static SuperArray<int> range(int stop)
{
return range(0, stop);
}
public SuperArray<T> Map<T>(Func<R, T> func)
{
var array = new SuperArray<T>();
foreach (var item in BaseArray)
array.Add(func(item));
return array;
}
public T Fold<T>(T acc, Func<R, T, T> func)
{
foreach (var item in BaseArray) acc = func(item, acc);
return acc;
}
public SuperArray<R> Filter(Func<R, bool> pred)
{
var array = new SuperArray<R>();
foreach (var item in BaseArray)
{
if (pred(item)) array.Add(item);
}
return array;
}
public SuperArray<R> Filter(R pred)
{
return Filter(element => element.Equals(pred));
}
public int Find(Func<R, bool> pred)
{
for (int i = 0; i < Length; i++)
{
var value = BaseArray[i];
if (pred(value)) return i;
}
return -1;
}
public int Find(R pred)
{
return Find(element => element.Equals(pred));
}
public int IndexOf(R pred)
{
return Find(pred);
}
public int IndexOf(Func<R, bool> pred)
{
return Find(pred);
}
public int LastIndexOf(Func<R, bool> pred)
{
int reversedIndex = Reverse().IndexOf(pred);
Reverse();
return Length - 1 - reversedIndex;
}
public int LastIndexOf(R pred)
{
return LastIndexOf(element => element.Equals(pred));
}
public SuperArray<int> FindAll(Func<R, bool> pred)
{
var indices = new SuperArray<int>();
for (int i = 0; i < Length; i++)
{
if (pred(BaseArray[i])) indices.Add(i);
}
return indices;
}
public SuperArray<int> FindAll(R pred)
{
return FindAll(element => element.Equals(pred));
}
public SuperArray<R> ForEach(Action<R> func)
{
foreach (var item in BaseArray) func(item);
return this;
}
public String Join(String sep = " ", String prefix = "", String postfix = "")
{
String str = "";
for (int i = 0; i < Length; i++)
{
str += BaseArray[i].ToString();
if (i + 1 < Length) str += sep;
}
return str;
}
public SuperArray<R> Reverse()
{
var array = new SuperArray<R>();
for (int i = Length - 1; i >= 0; i--) array.Add(BaseArray[i]);
BaseArray = array.BaseArray;
return this;
}
public R Pop()
{
return Pop(Length - 1);
}
public R Pop(int index)
{
index %= Length;
R[] newArray = new R[Length - 1];
for (int i = 0; i < index; i++) newArray[i] = BaseArray[i];
for (int i = index + 1; i < Length; i++) newArray[i - 1] = BaseArray[i];
R poppedValue = BaseArray[index];
BaseArray = newArray;
return poppedValue;
}
public SuperArray<R> Sort(bool descending = false)
{
Array.Sort(BaseArray);
if (descending) Reverse();
return this;
}
private void remove(int index)
{
index %= Length;
R[] newArray = new R[Length - 1];
for (int i = 0; i < index; i++) newArray[i] = BaseArray[i];
for (int i = index + 1; i < Length; i++) newArray[i - 1] = BaseArray[i];
BaseArray = newArray;
}
public SuperArray<R> Remove(params int[] indices)
{
SuperArray<int> arr = new SuperArray<int>(indices).Sort(descending: true);
arr.ForEach(remove);
return this;
}
public SuperArray<R> Remove(SuperArray<int> indices)
{
return Remove(indices.BaseArray);
}
public SuperArray<R> RemoveAll(Func<R, bool> pred)
{
Remove(FindAll(pred));
return this;
}
public SuperArray<R> RemoveAll(R value)
{
return RemoveAll(pred: element => element.Equals(value));
}
public SuperArray<R> Replace(Func<R, bool> pred, Func<R, R> replacer)
{
for (int i = 0; i < Length; i++)
{
var value = BaseArray[i];
if (pred(value)) BaseArray[i] = replacer(value);
}
return this;
}
public SuperArray<R> Replace(Func<R, bool> pred, R replacer)
{
return Replace(pred, element => replacer);
}
public SuperArray<R> Replace(R pred, Func<R, R> replacer)
{
return Replace(element => element.Equals(pred), replacer);
}
public SuperArray<R> Replace(R pred, R replacer)
{
return Replace(element => element.Equals(pred), element => replacer);
}
public int Push(params R[] values)
{
Add(values);
return Length;
}
public R Shift()
{
return Pop(0);
}
public SuperArray<R> Slice(int start = 0, int end = -1, int step = 1)
{
if (end == -1) end = Length;
var array = new SuperArray<R>();
foreach (var i in range(start, end, step))
{
array.Add(BaseArray[i % Length]);
}
return array;
}
public SuperArray<R> Slice(int end)
{
return Slice(0, end);
}
public SuperArray<R> Unshift(params R[] values)
{
BaseArray = new SuperArray<R>(values).Add(this).BaseArray;
return this;
}
public bool Any(Func<R, bool> callback)
{
foreach (var i in BaseArray)
{
if (callback(i)) return true;
}
return false;
}
public bool Some(Func<R, bool> callback)
{
return Any(callback);
}
public bool All(Func<R, bool> callback)
{
foreach (var i in BaseArray)
{
if (!callback(i)) return false;
}
return true;
}
public bool All(Func<R, int, bool> callback)
{
for (int i = 0; i < Length; i++)
{
if (!callback(get(i), i)) return false;
}
return true;
}
public bool Every(Func<R, bool> callback)
{
return All(callback);
}
public bool Every(Func<R, int, bool> callback)
{
return All(callback);
}
public T Reduce<T>(Func<T, R, int, SuperArray<R>, T> callback, T acc)
{
for (int i = 0; i < BaseArray.Length; i++)
{
acc = callback(acc, BaseArray[i], i, this);
}
return acc;
}
public SuperArray<R> Fill(Func<R, R> callback)
{
return Replace(element => true, callback);
}
public bool Contains(Func<R, bool> pred)
{
foreach (var item in BaseArray)
{
if (pred(item)) return true;
}
return false;
}
public bool Contains(R pred)
{
return Contains(element => element.Equals(pred));
}
public bool Includes(Func<R, bool> pred)
{
return Contains(pred);
}
public bool Includes(R pred)
{
return Includes(pred);
}
public SuperArray<R> Splice(int start, int number, params R[] replacements)
{
var before = Slice(end: start);
var after = Slice(start: start + number);
BaseArray = before.Add(replacements).Add(after).BaseArray;
return this;
}
public SuperArray<R> Insert(int index, params R[] items)
{
return Splice(index, 0, items);
}
public R get(int index)
{
return this[index];
}
public bool Equals(SuperArray<R> other)
{
if (Length != other.Length) return false;
return Every((element, index) => element.Equals(other[index]));
}
public IEnumerator<R> GetEnumerator()
{
foreach (var item in BaseArray)
{
yield
return item;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public static SuperArray<R> operator +(SuperArray<R> a, SuperArray<R> b)
{
return a.Add(b);
}
public static SuperArray<R> operator -(SuperArray<R> a, SuperArray<R> b)
{
foreach (var item in b)
{
a.Remove(a.FindAll(item));
}
return a;
}
public R this[int index]
{
get
{
return BaseArray[index % Length];
}
set
{
BaseArray[index % Length] = value;
}
}
public override String ToString()
{
return Join(sep: ", ", postfix: "{ ", prefix: " }");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment