/******************************************************************** 文件: LinkedSequence.cs 作者: 梦语 邮箱: 1982614048@qq.com 创建时间: 2024/03/29 17:28:19 最后修改: 梦语 最后修改时间: 2024/04/03 16:04:31 功能: 自定义顺序链表 *********************************************************************/ using System.Collections; using System.Linq; using System.Collections.Generic; using UnityEngine; using System; namespace Ether { public class LinkedNode where TKey : IComparable { public LinkedSequence Sequence; public TKey Key { get; set; } public TValue Value { get; set; } public void SetAsLastSibling() { Sequence.SetAsLastSibling(Key); } public void SetAsFirstSibling() { Sequence.SetAsFirstSibling(Key); } } public class LinkedNode where TValue : IComparable { public LinkedSequence Sequence; public TValue Value { get; set; } public void SetAsLastSibling() { Sequence.SetAsLastSibling(this); } public void SetAsFirstSibling() { Sequence.SetAsFirstSibling(this); } } public class LinkedSequence : IEnumerable> where TValue : IComparable { private List> linkedList = new List>(); public LinkedNode this[int index] { get { return linkedList[index]; } set { linkedList[index] = value; } } public LinkedNode this[TValue tvalue] { get { foreach (var node in linkedList) { if (node.Value.CompareTo(tvalue) == 0) { return node; } } return null; } set { int index = 0; LinkedNode tempNode = null; for (int i = 0; i < linkedList.Count; i++) { var node = linkedList[i]; if (node.Value.CompareTo(tvalue) == 0) { index = i; tempNode = node; break; } } if (tempNode == null) { linkedList.Add(value); } else { linkedList[index] = value; } } } public int Count { get { return linkedList.Count; } } public LinkedNode Add(TValue value) { LinkedNode node; if (Contains(value)) { node = GetLinkedNode(value); node.SetAsLastSibling(); } else { node = new LinkedNode(); node.Sequence = this; node.Value = value; } linkedList.Add(node); return node; } public void Remove(TValue value) { linkedList.Remove(GetLinkedNode(value)); } public LinkedNode GetLinkedNode(TValue value) { foreach (var node in linkedList) { if (node.Value.Equals(value)) { return node; } } return null; } public bool Contains(TValue value) { foreach (var node in linkedList) { if (node.Value.Equals(value)) { return true; } } return false; } public void SetAsLastSibling(LinkedNode node) { int index = linkedList.IndexOf(node); if (index != -1 && index != linkedList.Count - 1) { linkedList.Remove(node); linkedList.Add(node); } } public void SetAsFirstSibling(LinkedNode node) { int index = linkedList.IndexOf(node); if (index != -1 && index != 0) { linkedList.Remove(node); linkedList.Insert(0, node); } } public IEnumerator> GetEnumerator() { for (int i = 0; i < linkedList.Count; i++) { LinkedNode node = linkedList[i]; yield return node; } } IEnumerator IEnumerable.GetEnumerator() { for (int i = 0; i < linkedList.Count; i++) { LinkedNode node = linkedList[i]; yield return node; } } public void Clear() { linkedList.Clear(); } } public class LinkedSequence : IEnumerable>> where TKey : IComparable { private DictionaryEx> linkedDic = new DictionaryEx>(); //private List LinkedList; public LinkedNode this[TKey key] { get { return linkedDic[key]; } set { linkedDic[key] = value; } } public int Count { get { return linkedDic.Count; } } public LinkedNode Add(TKey key, TValue value) { LinkedNode node; if (linkedDic.ContainsKey(key)) { node = linkedDic[key]; node.Value = value; } else { node = new LinkedNode(); node.Sequence = this; node.Key = key; node.Value = value; } linkedDic.Add(key, node); return node; } public void Remove(TKey key) { linkedDic.Remove(key); } public bool ContainsKey(TKey key) { return linkedDic.ContainsKey(key); } public void Clear() { linkedDic.Clear(); } public void SetAsLastSibling(TKey key) { linkedDic.SetAsLastSibling(key); } public void SetAsFirstSibling(TKey key) { linkedDic.SetAsFirstSibling(key); } public IEnumerator>> GetEnumerator() { for (int i = 0; i < linkedDic.Count; i++) { KeyValuePair> dicPair = linkedDic.ElementAt(i); yield return dicPair; } } IEnumerator IEnumerable.GetEnumerator() { for (int i = 0; i < linkedDic.Count; i++) { KeyValuePair> dicPair = linkedDic.ElementAt(i); yield return dicPair; } } } }