Steven Leal
4 年前
当前提交
71bcc834
共有 9 个文件被更改,包括 241 次插入 和 76 次删除
-
10com.unity.perception/CHANGELOG.md
-
36com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Utilities/GameObjectOneWayCache.cs
-
18com.unity.perception/Runtime/Randomization/Randomizers/RandomizerTag.cs
-
8com.unity.perception/Runtime/Randomization/Randomizers/RandomizerTagManager.cs
-
81com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs
-
6com.unity.perception/Runtime/Randomization/Scenarios/UnitySimulationScenario.cs
-
78com.unity.perception/Tests/Runtime/Randomization/RandomizerTests/RandomizerTagTests.cs
-
77com.unity.perception/Runtime/Randomization/Randomizers/OrderedSet.cs
-
3com.unity.perception/Runtime/Randomization/Randomizers/OrderedSet.cs.meta
|
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
|
|||
namespace UnityEngine.Perception.Randomization.Randomizers |
|||
{ |
|||
/// <summary>
|
|||
/// This collection has the properties of a HashSet that also preserves insertion order. As such, this data
|
|||
/// structure demonstrates the following time complexities:
|
|||
/// O(1) lookup, O(1) add, O(1) remove, and O(n) traversal
|
|||
/// </summary>
|
|||
/// <typeparam name="T">The item type to store in this collection</typeparam>
|
|||
class OrderedSet<T> : ICollection<T> |
|||
{ |
|||
readonly IDictionary<T, LinkedListNode<T>> m_Dictionary; |
|||
readonly LinkedList<T> m_LinkedList; |
|||
|
|||
public OrderedSet() : this(EqualityComparer<T>.Default) { } |
|||
|
|||
public OrderedSet(IEqualityComparer<T> comparer) |
|||
{ |
|||
m_Dictionary = new Dictionary<T, LinkedListNode<T>>(comparer); |
|||
m_LinkedList = new LinkedList<T>(); |
|||
} |
|||
|
|||
public int Count => m_Dictionary.Count; |
|||
|
|||
public virtual bool IsReadOnly => m_Dictionary.IsReadOnly; |
|||
|
|||
void ICollection<T>.Add(T item) |
|||
{ |
|||
Add(item); |
|||
} |
|||
|
|||
public bool Add(T item) |
|||
{ |
|||
if (m_Dictionary.ContainsKey(item)) return false; |
|||
var node = m_LinkedList.AddLast(item); |
|||
m_Dictionary.Add(item, node); |
|||
return true; |
|||
} |
|||
|
|||
public void Clear() |
|||
{ |
|||
m_LinkedList.Clear(); |
|||
m_Dictionary.Clear(); |
|||
} |
|||
|
|||
public bool Remove(T item) |
|||
{ |
|||
var found = m_Dictionary.TryGetValue(item, out var node); |
|||
if (!found) return false; |
|||
m_Dictionary.Remove(item); |
|||
m_LinkedList.Remove(node); |
|||
return true; |
|||
} |
|||
|
|||
public IEnumerator<T> GetEnumerator() |
|||
{ |
|||
return m_LinkedList.GetEnumerator(); |
|||
} |
|||
|
|||
IEnumerator IEnumerable.GetEnumerator() |
|||
{ |
|||
return GetEnumerator(); |
|||
} |
|||
|
|||
public bool Contains(T item) |
|||
{ |
|||
return m_Dictionary.ContainsKey(item); |
|||
} |
|||
|
|||
public void CopyTo(T[] array, int arrayIndex) |
|||
{ |
|||
m_LinkedList.CopyTo(array, arrayIndex); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 2b6c83991e2c4c88af9b2b43c90121c7 |
|||
timeCreated: 1612730504 |
撰写
预览
正在加载...
取消
保存
Reference in new issue