📄 keyedcollection.cs
字号:
// Copyright (C) 2004 Riccardo Marzi <riccardo@dotnetfireball.net>
//
// Copyright (C) 2004 Sebastian Faltoni <sebastian@dotnetfireball.net>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Runtime.InteropServices;
#endregion
namespace Fireball.Collections.Generic
{
public class KeyedCollection<T> : IKeyedCollection<T>, IEnumerable
{
#region Nested Types
[Serializable, StructLayout(LayoutKind.Sequential)]
public struct Enumerator : IEnumerator<T>, IDisposable, IEnumerator
{
private KeyedCollection<T> m_KeyedColl;
private int m_Index;
private T m_Current;
public Enumerator(KeyedCollection<T> coll)
{
this.m_KeyedColl = coll;
this.m_Index = 0;
m_Current = default(T);
}
#region IEnumerator<T> Members
public T Current
{
get { return m_Current; }
}
#endregion
#region IDisposable Members
public void Dispose()
{
}
#endregion
#region IEnumerator Members
object System.Collections.IEnumerator.Current
{
get { return m_Current; }
}
public bool MoveNext()
{
if (this.m_Index < this.m_KeyedColl.Count)
{
this.m_Current = this.m_KeyedColl._items[this.m_Index];
this.m_Index++;
return true;
}
this.m_Index = this.m_KeyedColl.Count + 1;
this.m_Current = default(T);
return false;
}
public void Reset()
{
this.m_Index = 0;
this.m_Current = default(T);
}
#endregion
}
public class KeyedCollectionAddEventArgs : EventArgs
{
private T m_Item;
private int m_Index;
private string m_Key;
public T Item
{
get { return m_Item; }
set { m_Item = value; }
}
public int Index
{
get { return m_Index; }
set { m_Index = value; }
}
public string Key
{
get { return m_Key; }
set { m_Key = value; }
}
public KeyedCollectionAddEventArgs(T item, int index, string key)
{
m_Item = item;
m_Index = index;
m_Key = key;
}
}
public class KeyedCollectionRemoveEventArgs : EventArgs
{
private T m_Item;
private string m_Key;
private int m_Index;
public string Key
{
get { return m_Key; }
set { m_Key = value; }
}
public int Index
{
get { return m_Index; }
set { m_Index = value; }
}
public T Item
{
get { return m_Item; }
set { m_Item = value; }
}
public KeyedCollectionRemoveEventArgs(T removedItem, string key, int index)
{
m_Item = removedItem;
m_Key = key;
m_Index = index;
}
}
public class KeyedCollectionMoveEventArgs : EventArgs
{
private T m_Item;
private int m_NewIndex;
private int m_OldIndex;
private string m_Key;
public string Key
{
get { return m_Key; }
set { m_Key = value; }
}
public int OldIndex
{
get { return m_OldIndex; }
set { m_OldIndex = value; }
}
public T Item
{
get { return m_Item; }
set { m_Item = value; }
}
public int NewIndex
{
get { return m_NewIndex; }
set { m_NewIndex = value; }
}
public KeyedCollectionMoveEventArgs(T item, int newIndex, int oldIndex, string key)
{
m_Item = item;
m_NewIndex = newIndex;
m_OldIndex = oldIndex;
m_Key = key;
}
}
public class KeyedCollectionAddRangeEventArgs : EventArgs
{
private T[] m_Items;
private string[] m_Keys;
private int m_StartIndex;
public int StartIndex
{
get { return m_StartIndex; }
set { m_StartIndex = value; }
}
public string[] Keys
{
get { return m_Keys; }
set { m_Keys = value; }
}
public T[] Items
{
get { return m_Items; }
set { m_Items = value; }
}
public KeyedCollectionAddRangeEventArgs(T[] items, string[] keys, int startIndex)
{
m_Items = items;
m_Keys = keys;
m_StartIndex = startIndex;
}
}
#endregion
#region Delegates
public delegate void KeyedCollectionAddHandler(object sender, KeyedCollectionAddEventArgs e);
public delegate void KeyedCollectionRemoveHandler(object sender, KeyedCollectionRemoveEventArgs e);
public delegate void KeyedCollectionMoveHandler(object sender, KeyedCollectionMoveEventArgs e);
public delegate void KeyedCollectionAddRangeHandler(object sender, KeyedCollectionAddRangeEventArgs e);
#endregion
#region Events
public event KeyedCollectionAddHandler ItemAdd;
public event KeyedCollectionRemoveHandler ItemRemove;
public event KeyedCollectionMoveHandler ItemMove;
public event KeyedCollectionAddRangeHandler ItemAddRange;
public event EventHandler CollectionClear;
public event EventHandler CollectionChanged;
#endregion
#region Private Fields
private LightCollection<string> _keys;
private LightCollection<T> _items;
private ReadOnlyKeyedCollection<T> _readonlyColl;
#endregion
#region Constructor
public KeyedCollection()
: this(4)
{
}
public KeyedCollection(int initialSize)
{
_keys = new LightCollection<string>(initialSize);
_items = new LightCollection<T>(initialSize);
_readonlyColl = null;
}
public KeyedCollection(string[] keys, T[] items)
{
_keys = new LightCollection<string>(keys);
_items = new LightCollection<T>(items);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -