optimize checking component existence

pull/2/head
cosmonaut 2022-04-06 20:07:38 -07:00
parent 21891da116
commit 0f4df1b948
1 changed files with 6 additions and 2 deletions

View File

@ -5,12 +5,15 @@ internal class IDStorage
private int nextID = 0; private int nextID = 0;
private readonly Stack<int> availableIDs = new Stack<int>(); private readonly Stack<int> availableIDs = new Stack<int>();
private readonly HashSet<int> availableIDHash = new HashSet<int>();
public int NextID() public int NextID()
{ {
if (availableIDs.Count > 0) if (availableIDs.Count > 0)
{ {
return availableIDs.Pop(); var id = availableIDs.Pop();
availableIDHash.Remove(id);
return id;
} }
else else
{ {
@ -22,11 +25,12 @@ internal class IDStorage
public bool Taken(int id) public bool Taken(int id)
{ {
return !availableIDs.Contains(id) && id < nextID; return !availableIDHash.Contains(id) && id < nextID;
} }
public void Release(int id) public void Release(int id)
{ {
availableIDs.Push(id); availableIDs.Push(id);
availableIDHash.Add(id);
} }
} }