Skip to content

Commit

Permalink
Misc TraitDictionary style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
reaperrr authored and teinarss committed Apr 10, 2021
1 parent aa834db commit e161d9d
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions OpenRA.Game/TraitDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public T Get(Actor actor)
var result = GetOrDefault(actor);
if (result == null)
throw new InvalidOperationException("Actor {0} does not have trait of type `{1}`".F(actor.Info.Name, typeof(T)));

return result;
}

Expand All @@ -169,10 +170,12 @@ public T GetOrDefault(Actor actor)
++Queries;
var index = actors.BinarySearchMany(actor.ActorID);
if (index >= actors.Count || actors[index] != actor)
return default(T);
else if (index + 1 < actors.Count && actors[index + 1] == actor)
return default;

if (index + 1 < actors.Count && actors[index + 1] == actor)
throw new InvalidOperationException("Actor {0} has multiple traits of type `{1}`".F(actor.Info.Name, typeof(T)));
else return traits[index];

return traits[index];
}

public IEnumerable<T> GetMultiple(uint actor)
Expand Down Expand Up @@ -229,6 +232,7 @@ public IEnumerable<Actor> Actors()
var current = actors[i];
if (current == last)
continue;

yield return current;
last = current;
}
Expand All @@ -244,6 +248,7 @@ public IEnumerable<Actor> Actors(Func<T, bool> predicate)
var current = actors[i];
if (current == last || !predicate(traits[i]))
continue;

yield return current;
last = current;
}
Expand Down Expand Up @@ -282,9 +287,11 @@ public void RemoveActor(uint actor)
var startIndex = actors.BinarySearchMany(actor);
if (startIndex >= actors.Count || actors[startIndex].ActorID != actor)
return;

var endIndex = startIndex + 1;
while (endIndex < actors.Count && actors[endIndex].ActorID == actor)
endIndex++;

var count = endIndex - startIndex;
actors.RemoveRange(startIndex, count);
traits.RemoveRange(startIndex, count);
Expand Down

0 comments on commit e161d9d

Please sign in to comment.