//The MIT License (MIT)
//http://bornagain.codeplex.com/license
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BornAgain.Engine.Core
{
///
/// Represents a loose abstraction of an arraylist, where
/// the buffer array is available for public access.
///
/// The type of element.
public class ArrayCollection : IDisposable
{
public T this[int index]
{
get
{
return _array[index];
}
}
private T[] _array;
///
/// Gets the array.
///
public T[] Array
{
get
{
return _array;
}
}
private int _currentCount;
///
/// Gets the current amount of items in the array.
///
public int CurrentCount
{
get
{
return _currentCount;
}
}
///
/// Gets the total amount of items the array can
/// currently hold.
///
public int CurrentCapacity
{
get
{
return _array.Length;
}
}
private bool _isFixed;
///
/// Whether or not the array collection is fixed in length.
///
public bool IsFixed
{
get
{
return _isFixed;
}
set
{
_isFixed = value;
}
}
///
/// Creates a new instance of the
/// class with the specified starting capacity.
///
/// The starting capacity.
public ArrayCollection(int count, bool isFixed)
{
if (count < 10)
count = 10;
_array = new T[count];
_isFixed = isFixed;
}
///
/// Creates a new instance of the
/// class with the specified starting capacity. The collection is
/// fixed.
///
/// The starting capacity.
public ArrayCollection(int count)
: this(count, true)
{
}
///
/// Adds an item to the array collection.
///
/// The item to add.
public int Add(T value)
{
if (_currentCount == CurrentCapacity)
{
if (_isFixed)
return -1;
else
Grow();
}
_array[_currentCount] = value;
return _currentCount++;
}
///
/// Clears the array collection.
///
public void Clear()
{
_currentCount = 0;
}
///
/// Sets the array collection.
///
/// The position to start copying into.
/// The array to copy from.
/// The start index within .
/// The amount of items to copy.
public void Set(int offset, T[] value, int start, int count)
{
// Preconditions.
if (value == null)
throw new ArgumentNullException("value");
if (start + count > value.Length)
throw new ArgumentOutOfRangeException("start + count");
if (offset < 0)
throw new ArgumentOutOfRangeException("offset");
// Ensure size.
if (CurrentCapacity < offset + count)
Resize(offset + count);
// Set the current item.
_currentCount = offset;
// Add the item.
for (int i = start; i < start + count; i++)
{
Add(value[i]);
}
}
///
/// Resizes the array.
///
///
private void Resize(int count)
{
System.Array.Resize(ref _array, count);
}
///
/// Grows the array by 10%.
///
private void Grow()
{
Resize((int)(CurrentCapacity * 1.1f));
}
#region Dispose
///
/// Whether or not this object has been disposed.
///
private bool _isDisposed;
///
/// Finalizer.
///
~ArrayCollection()
{
Dispose(false);
}
///
/// Called when the object is being disposed.
///
/// True if being called from ,
/// false if being called from the finalizer.
private void Dispose(bool disposing)
{
if (_isDisposed)
return;
_isDisposed = true;
_array = new T[0];
}
void IDisposable.Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}