///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 //
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
// rights of fair usage, the disclaimer and warranty conditions. //
///////////////////////////////////////////////////////////////////////////
using System;
namespace wasSharp
{
public static class Arrays
{
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
///
/// Gets an array element at a given modulo index.
///
/// the array type
/// a positive or negative index of the element
/// the array
/// an array element
public static T GetElementAt(T[] data, int index)
{
return index < 0 ? data[(index % data.Length + data.Length) % data.Length] : data[index % data.Length];
}
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
///
/// Gets a sub-array from an array.
///
/// the array type
/// the array
/// the start index
/// the stop index (-1 denotes the end)
/// the array slice between start and stop
public static T[] GetSubArray(T[] data, int start, int stop)
{
if (stop.Equals(-1))
stop = data.Length - 1;
var result = new T[stop - start + 1];
Array.Copy(data, start, result, 0, stop - start + 1);
return result;
}
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
///
/// Delete a sub-array and return the result.
///
/// the array type
/// the array
/// the start index
/// the stop index (-1 denotes the end)
/// the array without elements between start and stop
public static T[] DeleteSubArray(T[] data, int start, int stop)
{
if (stop.Equals(-1))
stop = data.Length - 1;
var result = new T[data.Length - (stop - start) - 1];
Array.Copy(data, 0, result, 0, start);
Array.Copy(data, stop + 1, result, start, data.Length - stop - 1);
return result;
}
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
///
/// Concatenate multiple arrays.
///
/// the array type
/// multiple arrays
/// a flat array with all arrays concatenated
public static T[] ConcatenateArrays(params T[][] arrays)
{
var resultLength = 0;
foreach (var o in arrays)
{
resultLength += o.Length;
}
var result = new T[resultLength];
var offset = 0;
for (var x = 0; x < arrays.Length; x++)
{
arrays[x].CopyTo(result, offset);
offset += arrays[x].Length;
}
return result;
}
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
///
/// Permutes an array in reverse a given number of times.
///
/// the array type
/// the array
/// the number of times to permute
/// the array with the elements permuted
public static T[] ReversePermuteArrayElements(T[] input, int times)
{
if (times.Equals(0)) return input;
var slice = new T[input.Length];
Array.Copy(input, 1, slice, 0, input.Length - 1);
Array.Copy(input, 0, slice, input.Length - 1, 1);
return ReversePermuteArrayElements(slice, --times);
}
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
///
/// Permutes an array forward a given number of times.
///
/// the array type
/// the array
/// the number of times to permute
/// the array with the elements permuted
public static T[] ForwardPermuteArrayElements(T[] input, int times)
{
if (times.Equals(0)) return input;
var slice = new T[input.Length];
Array.Copy(input, input.Length - 1, slice, 0, 1);
Array.Copy(input, 0, slice, 1, input.Length - 1);
return ForwardPermuteArrayElements(slice, --times);
}
}
}