//http://isotopescreencapture.codeplex.com/
//The MIT License (MIT)
using System.Collections.Generic;
namespace Isotope.Collections
{
public static class ArrayUtil
{
///
/// Places elements from an enumerable into an array. If there are not enough items to fill the array an exception is thrown
///
///
///
///
public static void FillArray(T[] array, IEnumerable items)
{
if (array == null)
{
throw new System.ArgumentNullException("array");
}
if (items == null)
{
throw new System.ArgumentNullException("items");
}
_FillArray(array, items, () => { throw new System.ArgumentException("Not enough items to fill array", "items"); });
}
///
/// Places elements from an enumerable into an array. If there are not enough items to fill the array, the default value is used
///
///
///
///
///
public static void FillArray(T[] array, IEnumerable items, T default_value)
{
if (array == null)
{
throw new System.ArgumentNullException("array");
}
if (items == null)
{
throw new System.ArgumentNullException("items");
}
_FillArray(array, items, () => default_value);
}
private static void _FillArray(T[] array, IEnumerable items, System.Func func_default)
{
if (array == null)
{
throw new System.ArgumentNullException("array");
}
if (items == null)
{
throw new System.ArgumentNullException("items");
}
if (func_default == null)
{
throw new System.ArgumentNullException("func_default");
}
using (var e = items.GetEnumerator())
{
for (int i = 0; i < array.Length; i++)
{
bool move_ok = e.MoveNext();
if (move_ok)
{
array[i] = e.Current;
}
else
{
array[i] = func_default();
}
}
}
}
}
}