using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
namespace Amemiya.Extensions
{
public static class ArrayExtensions
{
///
/// Convert a struct into a binary array.
///
/// The binary array
public static byte[] StructToBytes(object structObj)
{
var size = Marshal.SizeOf(structObj);
var buffer = Marshal.AllocHGlobal(size);
try
{
Marshal.StructureToPtr(structObj, buffer, true);
var bytes = new byte[size];
Marshal.Copy(buffer, bytes, 0, size);
return bytes;
}
finally
{
Marshal.FreeHGlobal(buffer);
}
}
///
/// Combine two arrays into one
///
/// The new array
public static T[] CombineWith(this T[] source, T[] with)
{
if (source == null || source.Length == 0)
return with;
var newBytes = new T[source.Length + with.Length];
SetRange(newBytes, source, 0);
SetRange(newBytes, with, source.Length);
return newBytes;
}
///
/// Compare the two arrays
///
/// True if they are equal
public static bool EqualWith(this T[] source, T[] compareWith)
{
if (source.Length != compareWith.Length)
return false;
if (source.Where((t, i) => !EqualityComparer.Default.Equals(t, compareWith[i])).Any())
return false;
return true;
}
///
/// Expand current array to a lager one
///
/// The new array
public static byte[] ExpandTo(this byte[] source, int newLength)
{
if (source.Length > newLength)
return source;
var b = new byte[newLength];
FillWith(b, (byte)0);
SetRange(b, source, 0);
return b;
}
///
/// Fill every item of current array with "with"
///
public static void FillWith(this T[] source, T with)
{
for (var i = 0; i < source.Length; i++)
source[i] = with;
}
///
/// Locate an array index in current array
///
/// -1 if not-found
public static int IndexOf(this T[] source, T[] arrayToFind, int startAt, bool isBackward)
{
if (isBackward)
{
for (var index = startAt - arrayToFind.Length; index >= 0; --index)
{
if (EqualWith(Slice(source, index, arrayToFind.Length + index), arrayToFind))
return index;
}
return -1;
}
for (var index = startAt; index <= source.Length - arrayToFind.Length; ++index)
{
if (EqualWith(Slice(source, index, arrayToFind.Length + index), arrayToFind))
return index;
}
return -1;
}
///
/// Set current array to bytesValue, starting from startFrom
///
public static void SetRange(this T[] source, T[] bytesValue, int startFrom)
{
if (source.Length < bytesValue.Length + startFrom)
throw new Exception("dest too small");
Array.Copy(bytesValue, 0, source, startFrom, bytesValue.Length);
}
public static T[] Slice(this T[] source, int start, int end)
{
if (end < 0)
{
end = source.Length + end;
}
var len = end - start;
var res = new T[len];
for (var i = 0; i < len; i++)
{
res[i] = source[i + start];
}
return res;
}
}
}