99 lines
2.0 KiB
C#
99 lines
2.0 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
|
|||
|
namespace Thousandto.Core.Base
|
|||
|
{
|
|||
|
//Set集的堆栈
|
|||
|
public class SetStack<T>
|
|||
|
{
|
|||
|
//id关联的索引
|
|||
|
private Dictionary<int, int> _indexes = new Dictionary<int, int>();
|
|||
|
//数据列表
|
|||
|
private List<T> _items = new List<T>();
|
|||
|
|
|||
|
public int Count
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return _items.Count;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public List<T> Items
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return _items;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public T this[int index]
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return _items[index];
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public bool Push(T obj)
|
|||
|
{
|
|||
|
if (obj != null)
|
|||
|
{
|
|||
|
var id = obj.GetHashCode();
|
|||
|
if (!_indexes.ContainsKey(id))
|
|||
|
{
|
|||
|
_items.Add(obj);
|
|||
|
_indexes[id] = _items.Count - 1;
|
|||
|
return true;
|
|||
|
}
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
public void PushFast(T obj)
|
|||
|
{
|
|||
|
_items.Add(obj);
|
|||
|
_indexes[obj.GetHashCode()] = _items.Count - 1;
|
|||
|
}
|
|||
|
|
|||
|
public T Pop()
|
|||
|
{
|
|||
|
var cnt = _items.Count;
|
|||
|
if (cnt > 0)
|
|||
|
{
|
|||
|
var obj = _items[cnt - 1];
|
|||
|
_items.RemoveAt(cnt - 1);
|
|||
|
_indexes.Remove(obj.GetHashCode());
|
|||
|
return obj;
|
|||
|
}
|
|||
|
return default(T);
|
|||
|
}
|
|||
|
|
|||
|
public void Foreach(Action<T> func)
|
|||
|
{
|
|||
|
for (int i = 0; i < _items.Count; i++)
|
|||
|
{
|
|||
|
func(_items[i]);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public bool Contains(T obj)
|
|||
|
{
|
|||
|
return _indexes.ContainsKey(obj.GetHashCode());
|
|||
|
}
|
|||
|
|
|||
|
public void TrimExcess()
|
|||
|
{
|
|||
|
_items.TrimExcess();
|
|||
|
}
|
|||
|
|
|||
|
public void Clear()
|
|||
|
{
|
|||
|
_items.Clear();
|
|||
|
_indexes.Clear();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
}
|