public void GeneratingMeshesAreNotDequeued() { var generating = new Dictionary <LegacyMeshId, MeshInfo>(); var queue = new MeshQueue(); for (int i = 0; i < 100; ++i) { var meshId = GetRandomMeshId(); var meshInfo = new MeshInfo { MeshId = meshId, ChangeState = MeshChangeState.Added, PriorityHint = Random.Range(0, 100) }; queue.EnqueueUnique(meshInfo); if (Random.Range(0f, 1f) < .5f) { generating[meshId] = meshInfo; } } while (generating.Count < queue.count) { bool result = queue.TryDequeue(generating, out MeshInfo meshInfo); Assert.That(result, "Could not dequeue a mesh info even though there are more items to dequeue."); Assert.That(!generating.ContainsKey(meshInfo.MeshId), "Should not dequeue a mesh info while it is generating."); } }
public void QueueIsUnique() { var generating = new Dictionary <LegacyMeshId, MeshInfo>(); var queue = new MeshQueue(); var uniqueMeshIds = new List <LegacyMeshId>(); for (int i = 0; i < 100; ++i) { LegacyMeshId meshId; if (i == 0 || Random.Range(0f, 1f) < .5f) { meshId = MakeMeshId((ulong)i, (ulong)i); uniqueMeshIds.Add(meshId); } else { meshId = uniqueMeshIds[Random.Range(0, uniqueMeshIds.Count - 1)]; } var meshInfo = new MeshInfo { MeshId = meshId, ChangeState = MeshChangeState.Added, PriorityHint = Random.Range(0, 100) }; queue.EnqueueUnique(meshInfo); Assert.That(uniqueMeshIds.Count == queue.count); } }
public void EnqueueUnique(MeshInfo meshInfo) { if (m_MeshSet.Contains(meshInfo.MeshId)) { UpdateExisting(meshInfo); } else { InsertNew(meshInfo); } }
void InsertNew(MeshInfo meshInfo) { int index = m_Queue.BinarySearch(meshInfo, s_MeshInfoComparer); if (index < 0) { index = ~index; } m_Queue.Insert(index, meshInfo); m_MeshSet.Add(meshInfo.MeshId); }
void UpdateExisting(MeshInfo meshInfo) { for (int i = 0; i < m_Queue.Count; ++i) { var existing = m_Queue[i]; if (existing.MeshId.Equals(meshInfo.MeshId)) { // Only need to do anything if they are not equal if (existing.PriorityHint != meshInfo.PriorityHint) { existing.PriorityHint = meshInfo.PriorityHint; m_Queue[i] = existing; m_Queue.Sort(s_MeshInfoComparer); } break; } } }
public bool TryDequeue(IReadOnlyDictionary <LegacyMeshId, MeshInfo> generating, out MeshInfo meshInfo) { for (int i = m_Queue.Count - 1; i >= 0; --i) { meshInfo = m_Queue[i]; if (!generating.ContainsKey(meshInfo.MeshId)) { m_Queue.RemoveAt(i); m_MeshSet.Remove(meshInfo.MeshId); return(true); } } meshInfo = default; return(false); }