sharp-extensions/Array.cs

124 lines
2.6 KiB
C#

using System;
using System.Runtime.CompilerServices;
using System.Collections.Generic;
using System.Threading;
namespace sharp.extensions
{
public static class ArrayExtension
{
public static bool ArrayEquals<T>(this T[] a,T[] b){
if (a.Length != b.Length){
return false;
}
for (int n = 0; n < a.Length;n++){
if (!a[n].Equals(b[n])){
return false;
}
}
return true;
}
public static T[] Extend<T>(this T[] source, int len)
{
if (source.Length < len)
{
T[] c = new T[len];
Array.Copy(source, 0, c, 0, source.Length);
Fill<T>(c, default(T),source.Length);
return c;
}
return source;
}
public static T[] Resize<T>(this T[] source, int len)
{
if (source.Length < len)
{
T[] c = new T[len];
Array.Copy(source, 0, c, 0, source.Length);
return c;
} else if (source.Length > len){
return Segment(source, 0, len);
}
return Segment(source, 0);
}
public static T[] Segment<T>(this T[] source, int start) {
return Segment(source, start, source.Length - start);
}
public static T[] Segment<T>(this T[] source,int start,int len){
T[] temp = new T[len];
if ((start + len) > source.Length){
len = source.Length - start;
}
Array.Copy(source,start,temp,0,len);
return temp;
}
public static T[] Reverse<T>(this T[] source){
T[] t = new T[source.Length];
Array.Copy(source,t,source.Length);
Array.Reverse(t);
return t;
}
public static void Fill<T>(this T[] a, T value)
{
Fill(a, value, 0, a.Length);
}
public static void Fill<T>(this T[] a, T value,int start)
{
Fill(a, value, start, a.Length - start);
}
public static void Fill<T>(this T[] a, T value,int start,int len)
{
for (int n = start; n < (start+len); n++)
{
a[n] = value;
}
}
public static T[] Insert<T>(this T[] a, T[] insert, int start,int len) {
Array.Copy(insert,0,a,start,len);
return a;
}
public static T[] Insert<T>(this T[] a,T[] insert,int start){
return Insert(a, insert, start, insert.Length);
}
public static T[] Combine<T>(this T[] a,T[] b){
T[] r = new T[a.Length + b.Length];
Array.Copy(a,r,a.Length);
Array.Copy(b,0,r,a.Length,b.Length);
return r;
}
public static T[] Remove<T>(this T[] a,T[] b){
List<T> rl = new List<T>(a);
foreach (T element in b){
rl.Remove(element);
}
return rl.ToArray();
}
public static int IndexOf<T>(this T[] a,T e){
for (int n = 0; n < a.Length;n++){
if (Object.Equals(a[n],e)){
return n;
}
}
return -1;
}
public static T[] Add<T>(this T[] a,T e)
{
T[] r = a.Extend<T>(a.Length + 1);
r[a.Length] = e;
return r;
}
}
}