📄 lightcollection.cs
字号:
{
throw new ArgumentOutOfRangeException("index", "Index must be within the bounds of the List.");
}
if (this._count == this._items.Length)
{
this.EnsureCapacity(this._count + 1);
}
if (index < this._count)
{
Array.Copy(this._items, index, this._items, (int)(index + 1), (int)(this._count - index));
}
this._items[index] = item;
this._count++;
if (ItemAdd != null) ItemAdd(this, new LightCollectionAddEventArgs(item, index));
}
/// <summary>
/// Move an element from an index to another
/// </summary>
/// <param name="index">Source index</param>
/// <param name="newIndex">Destination index</param>
public virtual void MoveByIndex(int index, int newIndex)
{
if (index == newIndex) return;
if (index < 0 || index >= _items.Length) throw new ArgumentOutOfRangeException("index");
if (newIndex < 0 || newIndex >= _items.Length) throw new ArgumentOutOfRangeException("newIndex");
int min = Math.Min(index, newIndex);
T[] newArray = new T[_items.Length - min];
int cp_index = index - min;
int cp_newIndex = newIndex - min;
Array.Copy(_items, min, newArray, 0, newArray.Length);
if (index > newIndex)
{
int pos = newIndex + 1;
_items[newIndex] = newArray[cp_index];
for (int i = 0; i < newArray.Length; i++)
{
if (i != (cp_index))
{
_items[pos] = newArray[cp_newIndex + i];
pos++;
}
}
}
else // index < newIndex
{
int pos = index;
_items[newIndex] = newArray[cp_index];
for (int i = cp_index + 1; i < newArray.Length; i++)
{
if (pos == (newIndex)) pos++;
_items[pos] = newArray[i];
pos++;
}
}
if(ItemMove!=null) ItemMove(this, new LightCollectionMoveEventArgs(_items[newIndex], newIndex, index));
}
/// <summary>
/// Move an element to a new index position
/// </summary>
/// <param name="item">Element to move</param>
/// <param name="newIndex">Destination index</param>
public virtual void Move(T item, int newIndex)
{
int index = this.IndexOf(item);
if (index < 0) return;
if(newIndex<0 || newIndex >= _count) return;
if (index == newIndex) return;
MoveByIndex(index, newIndex);
}
/// <summary>
/// Remove a T item
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public virtual bool Remove(T item)
{
int i = this.IndexOf(item);
if (i >= 0)
{
this.RemoveAt(i);
return true;
}
return false;
}
/// <summary>
/// Remove a T item at index
/// </summary>
/// <param name="index"></param>
public virtual void RemoveAt(int index)
{
if ((index < 0) || (index >= _count))
{
throw new ArgumentOutOfRangeException("index", index, "Index was out of range. Must be non-negative and less than the size of the collection."); // InternalGetResourceFromDefault("ArgumentOutOfRange_Index"));
}
T removedItem = _items[index];
Array.Copy(_items, index + 1, _items, index, _items.Length - index - 1);
Array.Resize<T>(ref _items, _count - 1);
_count--;
if (ItemRemove != null) ItemRemove(this, new LightCollectionRemoveEventArgs(removedItem, index));
}
/// <summary>
/// Reverse the order of the collection content
/// </summary>
public virtual void Reverse()
{
Array.Reverse(_items, 0, _count);
}
/// <summary>
/// Reverse the order of the collection content
/// </summary>
/// <param name="index">Start index</param>
/// <param name="length">Number of elements</param>
public virtual void Reverse(int index, int length)
{
if ((index + length) >= _count) throw new ArgumentOutOfRangeException((index >= _count) ? "index" : "length");
if (length <= 1) return;
Array.Reverse(_items, index, length);
}
/// <summary>
///
/// </summary>
/// <param name="index"></param>
/// <param name="length"></param>
public virtual void Sort(int index, int length)
{
Array.Sort<T>(_items, index, length);
}
/// <summary>
///
/// </summary>
public virtual void Sort()
{
Sort(0, _count);
}
/// <summary>
///
/// </summary>
/// <param name="keys"></param>
public virtual void Sort(Array keys)
{
if (keys.Length != this.Count /*_items.Length*/)
{
throw new Exception("The Length of \"keys\" does not match with \"items\" lenght");
}
Array.Sort(keys, _items);
}
/// <summary>
/// Swap two elements
/// </summary>
/// <param name="index1">First element index</param>
/// <param name="index2">Second element index</param>
public virtual void SwapByIndex(int index1, int index2)
{
if (index1 == index2) return;
if (index1 < 0 || index1 >= _items.Length) throw new ArgumentOutOfRangeException("index1");
if (index2 < 0 || index2 >= _items.Length) throw new ArgumentOutOfRangeException("index2");
T temp = _items[index2];
_items[index2] = _items[index1];
_items[index1] = temp;
}
/// <summary>
/// Swap two elements
/// </summary>
/// <param name="item1">First element</param>
/// <param name="item2">First element</param>
public virtual void Swap(T item1, T item2)
{
if (item1.Equals(item2)) return;
int index1 = this.IndexOf(item1);
int index2 = this.IndexOf(item2);
SwapByIndex(index1, index2);
}
/// <summary>
/// Reduce the collection buffer to real count size
/// </summary>
public virtual void TrimToSize()
{
this.Capacity = this._count;
}
/// <summary>
/// Try to get an item if exist
/// </summary>
/// <param name="index">Index of the item</param>
/// <param name="item">Return the item if exist, otherwise null</param>
/// <returns></returns>
public virtual bool TryGetItem(int index, out T item)
{
item = default(T);
if (index < 0 || index >= _items.Length) return false;
item = _items[index];
return true;
}
#endregion
#region ICloneable Members
object ICloneable.Clone()
{
return this.Clone();
}
#endregion
#region IList Members
int IList.Add(object value)
{
if (!value.GetType().Equals(typeof(T))) throw new ArgumentException("Invalid type " + value.GetType().ToString());
return this.Add((T)value);
}
void IList.Clear()
{
this.Clear();
}
bool IList.Contains(object value)
{
if (!value.GetType().Equals(typeof(T))) throw new ArgumentException("Invalid type " + value.GetType().ToString());
return this.Contains((T)value);
}
int IList.IndexOf(object value)
{
if (!value.GetType().Equals(typeof(T))) throw new ArgumentException("Invalid type " + value.GetType().ToString());
return this.IndexOf((T)value);
}
void IList.Insert(int index, object value)
{
if (!value.GetType().Equals(typeof(T))) throw new ArgumentException("Invalid type " + value.GetType().ToString());
this.Insert(index, (T)value);
}
bool IList.IsFixedSize
{
get { return false; }
}
bool IList.IsReadOnly
{
get { return false; }
}
void IList.Remove(object value)
{
if (!value.GetType().Equals(typeof(T))) throw new ArgumentException("Invalid type " + value.GetType().ToString());
this.Remove((T)value);
}
void IList.RemoveAt(int index)
{
this.RemoveAt(index);
}
object IList.this[int index]
{
get
{
return this[index];
}
set
{
if (!value.GetType().Equals(typeof(T))) throw new ArgumentException("Invalid type " + value.GetType().ToString());
this[index] = (T)value;
}
}
#endregion
#region ICollection Members
void ICollection.CopyTo(Array array, int index)
{
this.CopyTo(array, index);
}
int ICollection.Count
{
get
{
return this.Count;
}
}
bool ICollection.IsSynchronized
{
get
{
return false;
}
}
object ICollection.SyncRoot
{
get
{
return null;
}
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -