using System;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// generic list class with indexer and enumerator
/// freeware 2011 by admin of codezentrale.6x.to
/// </summary>
/// <typeparam name="T">datatype you want to manage</typeparam>
public class GenericList<T> : IEnumerable, IDisposable
{
/// <summary>
/// internal class, with interface for IEnumerable and IEnumerator
/// </summary>
private class GenericListEnumerator : IEnumerator
{
private int pos = -1;
private GenericList<T> _t;
public GenericListEnumerator(GenericList<T> t)
{
this._t = t;
}
public bool MoveNext()
{
if (pos < _t.Count - 1)
{
pos++;
return true;
}
else
{
return false;
}
}
public void Reset()
{
pos = -1;
}
public object Current
{
get
{
try
{
return _t[pos];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
private List<T> _ObjectList = new List<T>();
private bool _bDisposed = false;
/// <summary>
/// object counter
/// </summary>
public int Count
{
get { return _ObjectList.Count; }
}
/// <summary>
/// indexer for direct index based access (e.g. mylist[1])
/// </summary>
/// <param name="pos"></param>
/// <returns></returns>
public T this[int pos]
{
get { return _ObjectList[pos]; }
set { _ObjectList[pos] = value; }
}
public GenericList()
{
}
// destructor, makro for 'protected override void Finalize()'
~GenericList()
{
this.Dispose(false);
}
// public Dispose-method for cleanup
public void Dispose()
{
this.Dispose(true);
}
// internal Dispose-method
private void Dispose(bool bDisposing)
{
if (!_bDisposed)
{
if (bDisposing)
{
// e.g. free managed resources here
}
}
_bDisposed = true;
}
/// <summary>
/// interface function for IEnumerable and IEnumerator
/// </summary>
/// <returns></returns>
public IEnumerator GetEnumerator()
{
return new GenericListEnumerator(this);
}
/// <summary>
/// add object to list
/// </summary>
/// <param name="obj">your object</param>
public void Add(T obj)
{
_ObjectList.Add(obj);
}
/// <summary>
/// clear list
/// </summary>
public void Clear()
{
_ObjectList.Clear();
}
/// <summary>
/// swap objects inside list
/// </summary>
/// <param name="index1">index number one</param>
/// <param name="index2">index number one</param>
public void Swap(int index1, int index2)
{
if ((index1 >= 0) && (index2 >= 0) && (index1 < _ObjectList.Count) && (index2 < _ObjectList.Count))
{
T temp = _ObjectList[index1];
_ObjectList[index1] = _ObjectList[index2];
_ObjectList[index2] = temp;
}
}
/// <summary>
/// remove object from list
/// </summary>
/// <param name="obj"></param>
public void Remove(T obj)
{
_ObjectList.Remove(obj);
}
}