天天看点

05.双向链表

05.双向链表
05.双向链表
using System.Collections;
using System.Collections.Generic;

public class ClassObjectPool<T> where  T:class,new()
{
    protected Stack<T> m_Pool=new Stack<T>();
    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<ObjectManager>
{
    protected Dictionary<Type,object> m_ClassPollDic=new Dictionary<Type, object>();
    public ClassObjectPool<T> GetOrCreatClassPool<T>(int maxCount) where T:class,new()
    {
        Type type = typeof(T);
        object obj = null;
        if (!m_ClassPollDic.TryGetValue(type,out obj)||obj==null)
        {
            ClassObjectPool<T> newPool=new ClassObjectPool<T>(maxCount);
            m_ClassPollDic.Add(type,newPool);
            return newPool;
        }
        return obj as ClassObjectPool<T>;
        
    }
}      
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ResourceManager:Singleton<ResourceManager>
{ 
   
  
       
}

public class DoubleLinkedListNode<T> where T:class,new()
{
    public DoubleLinkedListNode<T> prev = null;
    public DoubleLinkedListNode<T> next = null;
    public T t = null;
}


public class DoubleLinedList<T> where T : class, new()
{
    public DoubleLinkedListNode<T> Head = null;
    public DoubleLinkedListNode<T> Tail = null;

    public ClassObjectPool<DoubleLinkedListNode<T>> m_DoubleLinkPool =
        ObjectManager.Instance.GetOrCreatClassPool<DoubleLinkedListNode<T>>(500);

    protected int m_Count = 0;

    public int Count
    {
        get { return m_Count; }
    }


    public DoubleLinkedListNode<T> AddHead(T t)
    {
        DoubleLinkedListNode<T> plist = m_DoubleLinkPool.Spawn(true);
        plist.prev = null;
        plist.next = null;
        plist.t = t;
        return AddHead(plist);
    }

    public DoubleLinkedListNode<T> AddHead(DoubleLinkedListNode<T> 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<T> AddTail(T t)
    {
        DoubleLinkedListNode<T> plist = m_DoubleLinkPool.Spawn(true);
        plist.prev = null;
        plist.next = null;
        plist.t = t;
        return AddTail(plist);
    }

    public DoubleLinkedListNode<T> AddTail(DoubleLinkedListNode<T> 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<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.next = pNode.prev = null;
        pNode.t = null;
        m_Count--;
        m_DoubleLinkPool.Recyle(pNode);
    }

    public void MoveToHead(DoubleLinkedListNode<T> 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<T> where T:class,new()
{
    DoubleLinedList<T> m_DLink=new DoubleLinedList<T>();
    Dictionary<T,DoubleLinkedListNode<T>> m_FindMap=new Dictionary<T, DoubleLinkedListNode<T>>();

    ~CMapList()
    {
        Clear();
    }
    public void Clear()
    {
        while (m_DLink.Tail!=null)
        {
            Remove(m_DLink.Tail.t);
        }
    }

    public void InsertToHead(T t)
    {
        DoubleLinkedListNode<T> 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<T> 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<T> node = null;
        return m_FindMap.TryGetValue(t, out node);
    }

    public bool Reflesh(T t)
    {
        DoubleLinkedListNode<T> node = null;
        if (!m_FindMap.TryGetValue(t, out node))
        {
            return false;
        }

        m_DLink.MoveToHead(node);
        return true;
    }
}