📄 lightcollection.cs
字号:
if (this._count > 0)
{
Array.Copy(this._items, 0, newArray, 0, this._count);
}
this._items = newArray;
}
else
{
this._items = new T[4];
}
}
}
#endregion
#region Public Properties
/// <summary>
/// Return a T element from the specified index
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public virtual T this[int index]
{
get
{
try
{
return _items[index];
}
catch
{
throw new IndexOutOfRangeException("Here is no item at index(" + index.ToString() + ")");
}
}
set
{
try
{
_items[index] = value;
}
catch
{
throw new IndexOutOfRangeException("Here is no item at index(" + index.ToString() + ")");
}
}
}
/// <summary>
/// Return the Count of this collection
/// </summary>
public int Count
{
get
{
return _count;
}
}
/// <summary>
/// Return a read-only collection
/// </summary>
public virtual ReadOnlyCollection<T> ReadOnlyCollection
{
get
{
if (_readonlyColl == null) _readonlyColl = new ReadOnlyCollection<T>(this);
return _readonlyColl;
}
}
#endregion
#region Public Methods
/// <summary>
/// Add a T item to this collection
/// </summary>
/// <param name="item">Item to add to this collection</param>
/// <returns>Return the Item index on this collection</returns>
public virtual int Add(T item)
{
if (this._count == this._items.Length)
{
this.EnsureCapacity(this._count + 1);
}
this._items[this._count] = item;
_count++; // non spostare
if (ItemAdd != null) ItemAdd(this, new LightCollectionAddEventArgs(item, this._count));
return _count-1;
}
/// <summary>
/// Add a T item to this collection
/// </summary>
/// <param name="item">Item to add to this collection</param>
/// <param name="sort">set to true for sort now the collection</param>
/// <returns>Return the Item index on this collection</returns>
public virtual int Add(T item, bool sort)
{
int i = Add(item);
if (sort)
this.Sort();
return i;
}
/// <summary>
/// Add a range of Items to this collection
/// </summary>
/// <param name="items">An ILightCollection<T> derived collection</param>
public virtual void AddRange(ILightCollection<T> items)
{
InsertRange(this._count, items.GetItems());
}
/// <summary>
/// Add a range of Items to this collection
/// </summary>
/// <param name="items">Array of T[] Items to add to this collection</param>
public virtual void AddRange(T[] items)
{
InsertRange(this._count, items);
}
/// <summary>
/// Get and Set the Collection Capacity
/// </summary>
public virtual int Capacity
{
get
{
return this._items.Length;
}
set
{
this.SetCollCapacity(value);
}
}
/// <summary>
/// Empty the collection
/// </summary>
public virtual void Clear()
{
_items = new T[] { };
_count = 0;
if (CollectionClear != null) CollectionClear(this, new EventArgs());
}
/// <summary>
/// Create a cloned colletrion
/// </summary>
/// <returns>A copy of collection</returns>
public LightCollection<T> Clone()
{
return new LightCollection<T>(this);
}
/// <summary>
/// Check if a T item is contained on this collection
/// </summary>
/// <param name="item">The item to check</param>
/// <returns>True if is contained otherwise False</returns>
public virtual bool Contains(T item)
{
return (IndexOf(item, 0, _count) >= 0);
}
/// <summary>
/// Copy collection to an array of T elements types
/// </summary>
/// <param name="array">An array of T elements types</param>
/// <param name="index">Start copy index</param>
public virtual void CopyTo(Array array, int index)
{
Array.Copy(_items, index, array, 0, _count);
}
/// <summary>
/// Find an item of the collection using a predicate
/// </summary>
/// <param name="match"></param>
/// <returns>Boolean Result</returns>
public virtual T Find(Predicate<T> match)
{
if (match == null)
{
throw new ArgumentNullException("match");
}
for (int i = 0; i < _count; i++)
{
if (match(_items[i]))
{
return _items[i];
}
}
T type;
type = default(T);
return type;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public virtual LightCollection<T>.Enumerator GetEnumerator()
{
return new LightCollection<T>.Enumerator(this);
}
/// <summary>
/// Get a copy of all collection items
/// </summary>
/// <returns>Array of T[]</returns>
public virtual T[] GetItems()
{
if (_count <= 0) return new T[0];
return GetItems(0, _count - 1);
}
/// <summary>
/// Get a copy of all collection items
/// </summary>
/// <returns>Array of T[]</returns>
public virtual T[] GetItems(int startIndex)
{
if (_count <= 0) return new T[0];
return GetItems(startIndex, _count - 1);
}
/// <summary>
///
/// </summary>
/// <param name="startIndex"></param>
/// <param name="finalIndex"></param>
/// <returns></returns>
public virtual T[] GetItems(int startIndex, int finalIndex)
{
if (_count <= 0) return new T[0];
if (finalIndex < startIndex)
{
throw new ArgumentOutOfRangeException("finalIndex", finalIndex, "finalIndex was out of range. Must be non-negative and less than the size of the collection.");
}
if ((startIndex < 0) || (startIndex > _count))
{
throw new ArgumentOutOfRangeException("startIndex", startIndex, "startIndex was out of range. Must be non-negative and less than the size of the collection.");
}
if (finalIndex > _count - 1)
{
throw new ArgumentOutOfRangeException("finalIndex", finalIndex, "finalIndex was out of range. Must be minor of the Count");
}
T[] newItems = new T[finalIndex - startIndex + 1];
Array.Copy(_items, startIndex, newItems, 0, finalIndex + 1);
return newItems;
}
/// <summary>
/// Get The Index of a item on the collection
/// </summary>
/// <param name="item">The item to search</param>
/// <returns>Index of T</returns>
public virtual int IndexOf(T item)
{
return IndexOf(item, 0, _count);
}
/// <summary>
/// Get The Index of a item on the collection
/// </summary>
/// <param name="item">The item to search</param>
/// <param name="index">The index where start search</param>
/// <returns>Index of T</returns>
public virtual int IndexOf(T item, int index)
{
return IndexOf(item, index, _count - index);
}
/// <summary>
/// Get The Index of a item on the collection
/// </summary>
/// <param name="item">The item to search</param>
/// <param name="index">The index where start search</param>
/// <param name="count">Number of max search to try</param>
/// <returns>Index of T</returns>
public virtual int IndexOf(T item, int index, int count)
{
return Array.IndexOf<T>(this._items, item, index, count);
}
/// <summary>
///
/// </summary>
/// <param name="index"></param>
/// <param name="items"></param>
public virtual void InsertRange(int index, ILightCollection<T> items)
{
InsertRange(index, items.GetItems());
}
/// <summary>
/// Insert a range of T[] elements to this collection starting from index
/// </summary>
/// <param name="index">The from where starting insert</param>
/// <param name="items">T[] array of elements</param>
public virtual void InsertRange(int index, T[] items)
{
if (items == null)
{
throw new ArgumentNullException("items", "Null Array");
}
if ((index < 0) || (index > this._count))
{
throw new ArgumentOutOfRangeException("index", "Index out of Range");
}
int num1 = items.Length;
if (num1 > 0)
{
this.EnsureCapacity(this._count + num1);
if (index < this._count)
{
Array.Copy(this._items, index, this._items, (int)(index + num1), (int)(this._count - index));
}
items.CopyTo(this._items, index);
this._count += num1;
}
if (ItemAddRange != null) ItemAddRange(this, new LightCollectionAddRangeEventArgs(items, index));
}
/// <summary>
/// Insert a T Item at index
/// </summary>
/// <param name="index">The index where insert the Item</param>
/// <param name="item">The item to insert</param>
public virtual void Insert(int index, T item)
{
if ((index < 0) || (index > this._count))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -