浏览代码
Merge pull request #195 from Unity-Technologies/ordered-tag-queries
Merge pull request #195 from Unity-Technologies/ordered-tag-queries
Fixed issues with determinism in Unity Simulation/main
GitHub
4 年前
当前提交
1aa47792
共有 9 个文件被更改,包括 265 次插入 和 90 次删除
-
12com.unity.perception/CHANGELOG.md
-
66com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Utilities/GameObjectOneWayCache.cs
-
26com.unity.perception/Runtime/Randomization/Randomizers/RandomizerTag.cs
-
8com.unity.perception/Runtime/Randomization/Randomizers/RandomizerTagManager.cs
-
79com.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/LinkedHashSet.cs
-
3com.unity.perception/Runtime/Randomization/Randomizers/LinkedHashSet.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) insertion, O(1) removal, and O(n) traversal
|
|||
/// </summary>
|
|||
/// <typeparam name="T">The item type to store in this collection</typeparam>
|
|||
class LinkedHashSet<T> : ICollection<T> |
|||
{ |
|||
readonly IDictionary<T, LinkedListNode<T>> m_Dictionary; |
|||
readonly LinkedList<T> m_LinkedList; |
|||
|
|||
public LinkedHashSet() : this(EqualityComparer<T>.Default) { } |
|||
|
|||
public LinkedHashSet(IEqualityComparer<T> comparer) |
|||
{ |
|||
m_Dictionary = new Dictionary<T, LinkedListNode<T>>(comparer); |
|||
m_LinkedList = new LinkedList<T>(); |
|||
} |
|||
|
|||
public int Count => m_Dictionary.Count; |
|||
|
|||
public 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