博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
05.双向链表
阅读量:6676 次
发布时间:2019-06-25

本文共 5371 字,大约阅读时间需要 17 分钟。

在这里插入图片描述

在这里插入图片描述
01.类对象池

using System.Collections;using System.Collections.Generic;public class ClassObjectPool
where 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/

你可能感兴趣的文章
解决Unable to resolve target 'android-7'报错
查看>>
显示python的site-packages路径
查看>>
Connections could not be acquired from the unde...
查看>>
UIAlertView 总结
查看>>
邮件服务器:SMTP协议原始命令码和工作原理
查看>>
在Sublime Text中配置 JavaScript 控制台(JavaScript Console in Sublime Text)
查看>>
python使用os模块获取当前目录
查看>>
DNS服务(一)——DNS原理及其解析过程详解
查看>>
卸载linux软件总结
查看>>
redhat 6.5 安装和配置zabbix客户端
查看>>
硬链接和软链接(2)
查看>>
几种REST服务JAVA客户端类库
查看>>
什么是Hijax?Hijax的原理及优缺点介绍
查看>>
Linux面试记录
查看>>
端口状态说明 LISTENING、ESTABLISHED、TIME_WAIT及CLOSE_WAIT
查看>>
OutOfMemoryError: GC overhead limit exceede
查看>>
python os模块常用函数使用方法大全
查看>>
我的友情链接
查看>>
【2016-03-17】移动互联网时代,看好你的隐私
查看>>
linux命令:编译安装postfix邮件服务
查看>>