//既保留Queue的功能,有可以根据键删除项
public class QueueList<TKey,TValue>:List<TValue>
{
private List<TKey> keyList;
/// <summary>
/// 入队
/// </summary>
/// <param name="data"></param>
public void Enqueue(TKey key,TValue data)
{
if (keyList == null)
keyList = new List<TKey>();
lock (this)
{
this.Add(data);
keyList.Add(key);
}
}
/// <summary>
/// 出队
/// </summary>
/// <returns></returns>
public TValue Dequeue()
{
if (this.Count > 0)
{
lock (this)
{
TValue ret = this[0];
this.RemoveAt(0);
keyList.RemoveAt(0);
return ret;
}
}
else
{
throw new InvalidOperationException("列队为空");
}
}
/// <summary>
/// 返回位于 Queue 开始处的对象但不将其移除
/// </summary>
/// <returns></returns>
public TValue Peek()
{
return this[0];
}
/// <summary>
/// 根据条件获取值
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public TValue getByKey(TKey key)
{
int key_index = keyList.IndexOf(key);
if (key_index >= 0)
return this[key_index];
else
throw new Exception("没有找到值");
}
/// <summary>
/// 根据键删除数据
/// </summary>
/// <param name="key"></param>
public void RemoveByKey(TKey key)
{
lock (this)
{
int key_index = keyList.IndexOf(key);
if (key_index >= 0)
{
this.RemoveAt(key_index);
keyList.RemoveAt(key_index);
}
else
throw new Exception("没有找到值");
}
}
}