本文共 5371 字,大约阅读时间需要 17 分钟。
01.类对象池using System.Collections;using System.Collections.Generic;public class ClassObjectPoolwhere T:class,new(){ protected Stack m_Pool=new Stack (); protected int m_MaxCount = 0; protected int m_NoRecycleCount = 0; public ClassObjectPool(int maxCount) { this.m_MaxCount = maxCount; for (int i = 0; i < maxCount; i++) { m_Pool.Push(new T()); } } public T Spawn(bool creatPoolEmpty) { if (m_Pool.Count>0) { T t = m_Pool.Pop(); if (t==null) { if (creatPoolEmpty) { t=new T(); } } m_NoRecycleCount++; return t; } else { if (creatPoolEmpty) { T t2 = new T(); m_NoRecycleCount++; return t2; } } return null; } public bool Recyle(T obj) { if (obj==null) { return false; } if (m_Pool.Count>=m_MaxCount&&m_MaxCount>0) { obj = null; return false; } m_Pool.Push(obj); m_NoRecycleCount--; return true; }}
using System;using System.Collections;using System.Collections.Generic;public class ObjectManager : Singleton{ protected Dictionary m_ClassPollDic=new Dictionary (); public ClassObjectPool GetOrCreatClassPool (int maxCount) where T:class,new() { Type type = typeof(T); object obj = null; if (!m_ClassPollDic.TryGetValue(type,out obj)||obj==null) { ClassObjectPool newPool=new ClassObjectPool (maxCount); m_ClassPollDic.Add(type,newPool); return newPool; } return obj as ClassObjectPool ; }}
02.双向链表
using System.Collections;using System.Collections.Generic;using UnityEngine;public class ResourceManager:Singleton{ }public class DoubleLinkedListNode where T:class,new(){ public DoubleLinkedListNode prev = null; public DoubleLinkedListNode next = null; public T t = null;}public class DoubleLinedList where T : class, new(){ public DoubleLinkedListNode Head = null; public DoubleLinkedListNode Tail = null; public ClassObjectPool > m_DoubleLinkPool = ObjectManager.Instance.GetOrCreatClassPool >(500); protected int m_Count = 0; public int Count { get { return m_Count; } } public DoubleLinkedListNode AddHead(T t) { DoubleLinkedListNode plist = m_DoubleLinkPool.Spawn(true); plist.prev = null; plist.next = null; plist.t = t; return AddHead(plist); } public DoubleLinkedListNode AddHead(DoubleLinkedListNode plist) { if (plist == null) { return null; } plist.prev = null; if (Head == null) { Head = Tail = plist; } else { plist.next = Head; Head.prev = plist; Head = plist; } m_Count++; return plist; } public DoubleLinkedListNode AddTail(T t) { DoubleLinkedListNode plist = m_DoubleLinkPool.Spawn(true); plist.prev = null; plist.next = null; plist.t = t; return AddTail(plist); } public DoubleLinkedListNode AddTail(DoubleLinkedListNode plist) { if (plist == null) { return null; } plist.next = null; if (Tail == null) { Head = Tail = plist; } else { plist.prev = Tail; Tail.next = plist; Tail = plist; } m_Count++; return plist; } public void RemoveNode(DoubleLinkedListNode 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.next = pNode.prev = null; pNode.t = null; m_Count--; m_DoubleLinkPool.Recyle(pNode); } public void MoveToHead(DoubleLinkedListNode pNode) { if (pNode == null || 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; } pNode.prev = null; pNode.next = Head; Head.prev = pNode; Head = pNode; if (Tail==null) { Tail = Head; } }}public class CMapList where T:class,new(){ DoubleLinedList m_DLink=new DoubleLinedList (); Dictionary > m_FindMap=new Dictionary >(); ~CMapList() { Clear(); } public void Clear() { while (m_DLink.Tail!=null) { Remove(m_DLink.Tail.t); } } public void InsertToHead(T t) { DoubleLinkedListNode node = null; if (m_FindMap.TryGetValue(t,out node)&&node!=null) { m_DLink.AddHead(node); return; } m_DLink.AddHead(t); m_FindMap.Add(t, m_DLink.Head); } public void Pop() { if (m_DLink.Tail!=null) { Remove(m_DLink.Tail.t); } } public void Remove(T t) { DoubleLinkedListNode node = null; if (!m_FindMap.TryGetValue(t, out node) || node == null) { return; } m_DLink.RemoveNode(node); m_FindMap.Remove(t); } public T Back() { return m_DLink.Tail == null ? null : m_DLink.Tail.t; } public int Size() { return m_DLink.Count; } public bool Find(T t) { DoubleLinkedListNode node = null; return m_FindMap.TryGetValue(t, out node); } public bool Reflesh(T t) { DoubleLinkedListNode node = null; if (!m_FindMap.TryGetValue(t, out node)) { return false; } m_DLink.MoveToHead(node); return true; }}
转载地址:http://ofrxo.baihongyu.com/