289 lines
7.3 KiB
C#
289 lines
7.3 KiB
C#
|
/********************************************************************
|
||
|
文件: 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<TKey, TValue> where TKey : IComparable
|
||
|
{
|
||
|
public LinkedSequence<TKey, TValue> 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<TValue> where TValue : IComparable
|
||
|
{
|
||
|
public LinkedSequence<TValue> Sequence;
|
||
|
public TValue Value { get; set; }
|
||
|
|
||
|
public void SetAsLastSibling()
|
||
|
{
|
||
|
Sequence.SetAsLastSibling(this);
|
||
|
}
|
||
|
|
||
|
public void SetAsFirstSibling()
|
||
|
{
|
||
|
Sequence.SetAsFirstSibling(this);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
public class LinkedSequence<TValue> : IEnumerable<LinkedNode<TValue>> where TValue : IComparable
|
||
|
{
|
||
|
private List<LinkedNode<TValue>> linkedList = new List<LinkedNode<TValue>>();
|
||
|
|
||
|
public LinkedNode<TValue> this[int index]
|
||
|
{
|
||
|
get
|
||
|
{
|
||
|
return linkedList[index];
|
||
|
}
|
||
|
set
|
||
|
{
|
||
|
linkedList[index] = value;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public LinkedNode<TValue> this[TValue tvalue]
|
||
|
{
|
||
|
get
|
||
|
{
|
||
|
foreach (var node in linkedList)
|
||
|
{
|
||
|
if (node.Value.CompareTo(tvalue) == 0)
|
||
|
{
|
||
|
return node;
|
||
|
}
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
set
|
||
|
{
|
||
|
int index = 0;
|
||
|
LinkedNode<TValue> 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<TValue> Add(TValue value)
|
||
|
{
|
||
|
LinkedNode<TValue> node;
|
||
|
if (Contains(value))
|
||
|
{
|
||
|
node = GetLinkedNode(value);
|
||
|
node.SetAsLastSibling();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
node = new LinkedNode<TValue>();
|
||
|
node.Sequence = this;
|
||
|
node.Value = value;
|
||
|
}
|
||
|
linkedList.Add(node);
|
||
|
|
||
|
return node;
|
||
|
}
|
||
|
|
||
|
public void Remove(TValue value)
|
||
|
{
|
||
|
linkedList.Remove(GetLinkedNode(value));
|
||
|
}
|
||
|
|
||
|
public LinkedNode<TValue> 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<TValue> node)
|
||
|
{
|
||
|
int index = linkedList.IndexOf(node);
|
||
|
if (index != -1 && index != linkedList.Count - 1)
|
||
|
{
|
||
|
linkedList.Remove(node);
|
||
|
linkedList.Add(node);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void SetAsFirstSibling(LinkedNode<TValue> node)
|
||
|
{
|
||
|
int index = linkedList.IndexOf(node);
|
||
|
if (index != -1 && index != 0)
|
||
|
{
|
||
|
linkedList.Remove(node);
|
||
|
linkedList.Insert(0, node);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public IEnumerator<LinkedNode<TValue>> GetEnumerator()
|
||
|
{
|
||
|
for (int i = 0; i < linkedList.Count; i++)
|
||
|
{
|
||
|
LinkedNode<TValue> node = linkedList[i];
|
||
|
yield return node;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
IEnumerator IEnumerable.GetEnumerator()
|
||
|
{
|
||
|
for (int i = 0; i < linkedList.Count; i++)
|
||
|
{
|
||
|
LinkedNode<TValue> node = linkedList[i];
|
||
|
yield return node;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void Clear()
|
||
|
{
|
||
|
linkedList.Clear();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public class LinkedSequence<TKey, TValue> : IEnumerable<KeyValuePair<TKey, LinkedNode<TKey, TValue>>> where TKey : IComparable
|
||
|
{
|
||
|
private DictionaryEx<TKey, LinkedNode<TKey, TValue>> linkedDic = new DictionaryEx<TKey, LinkedNode<TKey, TValue>>();
|
||
|
//private List<LinkedNode> LinkedList;
|
||
|
|
||
|
public LinkedNode<TKey, TValue> this[TKey key]
|
||
|
{
|
||
|
get
|
||
|
{
|
||
|
return linkedDic[key];
|
||
|
}
|
||
|
set
|
||
|
{
|
||
|
linkedDic[key] = value;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public int Count { get { return linkedDic.Count; } }
|
||
|
|
||
|
public LinkedNode<TKey, TValue> Add(TKey key, TValue value)
|
||
|
{
|
||
|
LinkedNode<TKey, TValue> node;
|
||
|
if (linkedDic.ContainsKey(key))
|
||
|
{
|
||
|
node = linkedDic[key];
|
||
|
node.Value = value;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
node = new LinkedNode<TKey, TValue>();
|
||
|
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<KeyValuePair<TKey, LinkedNode<TKey, TValue>>> GetEnumerator()
|
||
|
{
|
||
|
for (int i = 0; i < linkedDic.Count; i++)
|
||
|
{
|
||
|
KeyValuePair<TKey, LinkedNode<TKey, TValue>> dicPair = linkedDic.ElementAt(i);
|
||
|
yield return dicPair;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
IEnumerator IEnumerable.GetEnumerator()
|
||
|
{
|
||
|
for (int i = 0; i < linkedDic.Count; i++)
|
||
|
{
|
||
|
KeyValuePair<TKey, LinkedNode<TKey, TValue>> dicPair = linkedDic.ElementAt(i);
|
||
|
yield return dicPair;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|