我这里有两句代码用到了类对象池,有兴趣的可以去看看
封装了一些双向链表的常用方法。都是不难的,只要思路清晰了就o98k了。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ResourceManager : Singleton<ResourceManager>
{
}
/// <summary>
/// 双向链表结构节点
/// </summary>
public class DoubleLinkedListNode<T> where T :class,new()
{
//上一个节点
public DoubleLinkedListNode<T> prev = null;
//下一个节点
public DoubleLinkedListNode<T> next = null;
//当前节点
public T t = null;
}
/// <summary>
/// 双向链表结构
/// </summary>
public class DoubleLinkedList<T> where T:class,new()
{
//表头
public DoubleLinkedListNode<T> Head = null;
//表尾
public DoubleLinkedListNode<T> Tail = null;
//类对象池缓存500个双向链表节点
protected ClassObjectPool<DoubleLinkedListNode<T>> doubleLinkedListPool = ObjectManager.Instance.GetOrCreateClassObjectPool<DoubleLinkedListNode<T>>(500);
protected int count = 0;
public int Count
{
get
{
return count;
}
}
/// <summary>
/// 添加一个节点到头部
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public DoubleLinkedListNode<T> AddToHead(T t)
{
DoubleLinkedListNode<T> pList = doubleLinkedListPool.Spawn(true);
pList.prev = null;
pList.next = null;
pList.t = t;
return AddToHead(pList);
}
/// <summary>
/// 添加一个节点到头部
/// </summary>
/// <param name="pNode"></param>
/// <returns></returns>
public DoubleLinkedListNode<T> AddToHead(DoubleLinkedListNode<T> pNode)
{
if (pNode == null)
return null;
pNode.prev = null;
if(Head==null)
{
Head = Tail = pNode;
}
else
{
pNode.next = Head;
Head.prev = pNode;
Head = pNode;
}
count++;
return Head;
}
/// <summary>
/// 添加一个节点到尾部
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public DoubleLinkedListNode<T> AddToTail(T t)
{
DoubleLinkedListNode<T> pList = doubleLinkedListPool.Spawn(true);
pList.prev = null;
pList.next = null;
pList.t = t;
return AddToTail(pList);
}
private DoubleLinkedListNode<T> AddToTail(DoubleLinkedListNode<T> pNode)
{
if (pNode == null)
return null;
pNode.next = null;
if(Tail==null)
{
Tail = Head = pNode;
}
else
{
pNode.prev = Tail;
Tail.next = pNode;
Tail = pNode;
}
count++;
return Tail;
}
/// <summary>
/// 移除某个节点
/// </summary>
/// <param name="pNode"></param>
public void RemoveNode(DoubleLinkedListNode<T> pNode)
{
if (pNode == null)
return;
if (pNode == Head)
Head = pNode.next;
if (pNode == Tail)
Tail = pNode.prev;
if (pNode.prev != null)
pNode.prev.next = pNode.next;
if (pNode.next != null)
pNode.next.prev = pNode.prev;
pNode.prev = pNode.next = null;
pNode.t = null;
doubleLinkedListPool.Recycle(pNode);
count--;
}
/// <summary>
/// 把某个节点移动到头部
/// </summary>
/// <param name="pNode"></param>
public void MoveToHead(DoubleLinkedListNode<T> pNode)
{
if (pNode == Head)
return;
if (pNode.prev == null && pNode.next == null)
return;
if (pNode == Tail)
Tail = pNode.prev;
if (pNode.prev != null)
pNode.prev.next = pNode.next;
if (pNode.next != null)
pNode.next.prev = pNode.prev;
Head.prev = pNode;
pNode.prev = null;
pNode.next = Head;
pNode = Head;
if (Tail == null)
Tail = Head;
}
}
public class CMapList<T> where T:class,new()
{
private DoubleLinkedList<T> DLinkList = new DoubleLinkedList<T>();
private Dictionary<T, DoubleLinkedListNode<T>> findMap = new Dictionary<T, DoubleLinkedListNode<T>>();
~CMapList()
{
Clear();
}
/// <summary>
/// 清空尾部节点
/// </summary>
public void Clear()
{
while(DLinkList.Tail!=null)
{
Remove(DLinkList.Tail.t);
}
}
/// <summary>
/// 插入节点到头部
/// </summary>
/// <param name="t"></param>
public void InsertToHead(T t)
{
DoubleLinkedListNode<T> node = null;
if(findMap.TryGetValue(t,out node)&&node!=null)
{
DLinkList.AddToHead(node);
return;
}
DLinkList.AddToHead(node);
findMap.Add(t, DLinkList.Head);
}
/// <summary>
/// 从表尾弹出一个节点
/// </summary>
public void Pop()
{
if(DLinkList.Tail!=null)
{
Remove(DLinkList.Tail.t);
}
}
/// <summary>
/// 删除某个节点
/// </summary>
/// <param name="t"></param>
public void Remove(T t)
{
DoubleLinkedListNode<T> node = null;
if(!findMap.TryGetValue(t,out node) || node==null)
{
return;
}
DLinkList.RemoveNode(node);
findMap.Remove(t);
}
/// <summary>
/// 获取到尾部节点
/// </summary>
/// <returns></returns>
public T Back()
{
return DLinkList.Tail == null ? null : DLinkList.Tail.t;
}
/// <summary>
/// 返回节点个数
/// </summary>
/// <returns></returns>
public int Size()
{
return findMap.Count;
}
/// <summary>
/// 查找是否存在该节点
/// </summary>
public bool Find(T t)
{
DoubleLinkedListNode<T> node = null;
if(!findMap.TryGetValue(t,out node) || node==null)
{
return false;
}
return true;
}
/// <summary>
/// 刷新节点,把节点移动到头部
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public bool Reflesh(T t)
{
DoubleLinkedListNode<T> node = null;
if(!findMap.TryGetValue(t,out node) || node==null)
{
return false;
}
DLinkList.MoveToHead(node);
return true;
}
}