Introduce FirstOrDefault extensions method for Array.Find and List.Find.

This allows the LINQ spelling to be used, but benefits from the performance improvement of the specific methods for these classes that provide the same result.
This commit is contained in:
RoosterDragon
2023-11-17 09:49:27 +00:00
committed by Gustas
parent acca837142
commit e6914f707a
54 changed files with 83 additions and 89 deletions

View File

@@ -9,7 +9,6 @@
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Activities;
@@ -163,7 +162,7 @@ namespace OpenRA.Mods.Common.Traits
if (world.Map.Contains(cell))
{
var explored = subject.Actor.Owner.Shroud.IsExplored(cell);
var cannotMove = Array.Find(subjects, a => !a.Trait.Info.MoveIntoShroud).Trait;
var cannotMove = subjects.FirstOrDefault(a => !a.Trait.Info.MoveIntoShroud).Trait;
var blocked = !explored && cannotMove != null;
if (isAssaultMove)

View File

@@ -305,7 +305,7 @@ namespace OpenRA.Mods.Common.Traits
// HACK: Use of this function requires that there is one squad of this type.
Squad GetSquadOfType(SquadType type)
{
return Squads.Find(s => s.Type == type);
return Squads.FirstOrDefault(s => s.Type == type);
}
Squad RegisterNewSquad(IBot bot, SquadType type, (Actor Actor, WVec Offset) target = default)

View File

@@ -9,7 +9,6 @@
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Activities;
@@ -143,7 +142,7 @@ namespace OpenRA.Mods.Common.Traits
return;
var currentTransform = self.CurrentActivity as Transform;
var transform = Array.Find(transforms, t => !t.IsTraitDisabled && !t.IsTraitPaused);
var transform = transforms.FirstOrDefault(t => !t.IsTraitDisabled && !t.IsTraitPaused);
if (transform == null && currentTransform == null)
return;

View File

@@ -9,7 +9,6 @@
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Activities;
@@ -95,7 +94,7 @@ namespace OpenRA.Mods.Common.Traits
return;
var currentTransform = self.CurrentActivity as Transform;
var transform = Array.Find(transforms, t => !t.IsTraitDisabled && !t.IsTraitPaused);
var transform = transforms.FirstOrDefault(t => !t.IsTraitDisabled && !t.IsTraitPaused);
if (transform == null && currentTransform == null)
return;

View File

@@ -9,7 +9,6 @@
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Activities;
@@ -121,7 +120,7 @@ namespace OpenRA.Mods.Common.Traits
return;
var currentTransform = self.CurrentActivity as Transform;
var transform = Array.Find(transforms, t => !t.IsTraitDisabled && !t.IsTraitPaused);
var transform = transforms.FirstOrDefault(t => !t.IsTraitDisabled && !t.IsTraitPaused);
if (transform == null && currentTransform == null)
return;

View File

@@ -9,7 +9,6 @@
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Activities;
@@ -129,7 +128,7 @@ namespace OpenRA.Mods.Common.Traits
return;
var currentTransform = self.CurrentActivity as Transform;
var transform = Array.Find(transforms, t => !t.IsTraitDisabled && !t.IsTraitPaused);
var transform = transforms.FirstOrDefault(t => !t.IsTraitDisabled && !t.IsTraitPaused);
if (transform == null && currentTransform == null)
return;

View File

@@ -9,7 +9,6 @@
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Activities;
@@ -123,7 +122,7 @@ namespace OpenRA.Mods.Common.Traits
return;
var currentTransform = self.CurrentActivity as Transform;
var transform = Array.Find(transforms, t => !t.IsTraitDisabled && !t.IsTraitPaused);
var transform = transforms.FirstOrDefault(t => !t.IsTraitDisabled && !t.IsTraitPaused);
if (transform == null && currentTransform == null)
return;

View File

@@ -84,7 +84,7 @@ namespace OpenRA.Mods.Common.Traits
{
if (external == null || !external.CanGrantCondition(self))
{
external = externals.Find(t => t.CanGrantCondition(self));
external = externals.FirstOrDefault(t => t.CanGrantCondition(self));
if (external == null)
break;
}

View File

@@ -310,7 +310,7 @@ namespace OpenRA.Mods.Common.Traits
protected override IEnumerable<IRenderable> Render(WorldRenderer wr, World world) { yield break; }
protected override IEnumerable<IRenderable> RenderAboveShroud(WorldRenderer wr, World world)
{
var minelayer = minelayers.Find(m => m.IsInWorld && !m.IsDead);
var minelayer = minelayers.FirstOrDefault(m => m.IsInWorld && !m.IsDead);
if (minelayer == null)
yield break;

View File

@@ -84,7 +84,7 @@ namespace OpenRA.Mods.Common.Traits
if (allProductionPaused)
return;
var item = Queue.Find(i => !i.Paused);
var item = Queue.FirstOrDefault(i => !i.Paused);
if (item == null)
return;

View File

@@ -9,7 +9,6 @@
*/
#endregion
using System;
using System.Collections.Generic;
using OpenRA.Graphics;
using OpenRA.Traits;
@@ -34,7 +33,7 @@ namespace OpenRA.Mods.Common.Traits
// TODO: This won't make sense for MP saves
var localPlayer = worldRenderer.World.LocalPlayer;
if ((localPlayer != null && localPlayer.PlayerActor != self) ||
(localPlayer == null && self.Owner != Array.Find(self.World.Players, p => p.IsBot)))
(localPlayer == null && self.Owner != self.World.Players.FirstOrDefault(p => p.IsBot)))
return null;
var nodes = new List<MiniYamlNode>()

View File

@@ -27,7 +27,7 @@ namespace OpenRA.Mods.Common.Traits
{
CancelUnbuildableItems();
var item = Queue.Find(i => !i.Paused);
var item = Queue.FirstOrDefault(i => !i.Paused);
if (item == null)
return;

View File

@@ -175,7 +175,7 @@ namespace OpenRA.Mods.Common.Traits
.Where(p => p.AcceptsPlug(plugInfo.Type))
.ToList();
var pluggable = pluggables.Find(p => a.Location + p.Info.Offset == targetLocation)
var pluggable = pluggables.FirstOrDefault(p => a.Location + p.Info.Offset == targetLocation)
?? pluggables.FirstOrDefault();
if (pluggable == null)

View File

@@ -515,7 +515,7 @@ namespace OpenRA.Mods.Common.Traits
protected virtual void PauseProduction(string itemName, bool paused)
{
Queue.Find(a => a.Item == itemName)?.Pause(paused);
Queue.FirstOrDefault(a => a.Item == itemName)?.Pause(paused);
}
protected virtual void CancelProduction(string itemName, uint numberToCancel)

View File

@@ -151,7 +151,7 @@ namespace OpenRA.Mods.Common.Traits
Arrow GetArrow(double degree)
{
var arrow = Array.Find(directionArrows, d => d.EndAngle >= degree);
var arrow = directionArrows.FirstOrDefault(d => d.EndAngle >= degree);
return arrow ?? directionArrows[0];
}

View File

@@ -222,7 +222,7 @@ namespace OpenRA.Mods.Common.Traits
if (!Ready)
return;
var power = Instances.Find(i => !i.IsTraitPaused);
var power = Instances.FirstOrDefault(i => !i.IsTraitPaused);
if (power == null)
return;

View File

@@ -332,7 +332,7 @@ namespace OpenRA.Mods.Common.Traits
public EditorActorPreview this[string id]
{
get { return previews.Find(p => p.ID.Equals(id, StringComparison.OrdinalIgnoreCase)); }
get { return previews.FirstOrDefault(p => p.ID.Equals(id, StringComparison.OrdinalIgnoreCase)); }
}
}
}