Skip to content

Commit

Permalink
fix: use tryadd instead of containskey guard clause (#2299)
Browse files Browse the repository at this point in the history
* fix: use tryadd instead of containskey guard clause

* fix: physicscomponent simplify ifs

* fix: simplify ifs on freeimage wrapper

* fix:  use collection initializer

---------

Co-authored-by: IXLLEGACYIXL <[email protected]>
  • Loading branch information
IXLLEGACYIXL and IXLLEGACYIXL authored May 30, 2024
1 parent 4cf0d08 commit 9a643ce
Show file tree
Hide file tree
Showing 17 changed files with 31 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ public Dictionary<ObjectUrl, ObjectId> GetOutputObjects()
{
foreach (var outputObject in outputObjects)
{
if (!result.ContainsKey(outputObject.Key))
{
result.Add(outputObject.Key, outputObject.Value.ObjectId);
}
result.TryAdd(outputObject.Key, outputObject.Value.ObjectId);
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ internal void ResetAllOverridesRecursively([NotNull] IAssetNode rootNode, NodeIn
{
var visitor = new AssetGraphVisitorBase(Definition);
// If we're in scenario where rootNode is an object node and index is not empty, we might already have the node in the dictionary so let's check this in Visiting
visitor.Visiting += (node, path) => { if (!nodesToReset.ContainsKey(node)) nodesToReset.Add(node, NodeIndex.Empty); };
visitor.Visiting += (node, path) => { nodesToReset.TryAdd(node, NodeIndex.Empty); };
visitor.Visit(rootNode);
}
// Then we reconcile (recursively) with the base.
Expand Down
10 changes: 2 additions & 8 deletions sources/assets/Stride.Core.Assets/Analysis/AssetCollision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,10 @@ public static void Clean(Package package, ICollection<AssetItem> inputItems, ICo
var tuple = new Tuple<AssetId, UFile>(newId != AssetId.Empty ? newId : item.Id, newLocation ?? item.Location);
if (changed)
{
if (!itemRemap.ContainsKey(item))
{
itemRemap.Add(item, tuple);
}
itemRemap.TryAdd(item, tuple);
}

if (!idRemap.ContainsKey(item.Id))
{
idRemap.Add(item.Id, tuple);
}
idRemap.TryAdd(item.Id, tuple);
}

// Process assets
Expand Down
5 changes: 1 addition & 4 deletions sources/assets/Stride.Core.Assets/AssetRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -601,10 +601,7 @@ private static void RegisterAssetAssembly(Assembly assembly)
RegisteredDefaultAssetExtension[assetType] = extensions.FirstOrDefault();
foreach (var extension in extensions)
{
if (!RegisteredAssetFileExtensions.ContainsKey(extension))
{
RegisteredAssetFileExtensions.Add(extension, assetType);
}
RegisteredAssetFileExtensions.TryAdd(extension, assetType);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -758,9 +758,7 @@ public void NotifyCommandBuildStepStarted(CommandBuildStep commandBuildStep, Obj
{
lock (builderContext.CommandsInProgress)
{
if (!builderContext.CommandsInProgress.ContainsKey(commandHash))
builderContext.CommandsInProgress.Add(commandHash, commandBuildStep);

builderContext.CommandsInProgress.TryAdd(commandHash, commandBuildStep);
builder.ioMonitor.CommandStarted(commandBuildStep);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,7 @@ private Assembly LoadAssemblyFromPathInternal([NotNull] string assemblyFullPath)
var assemblyName = Path.GetFileNameWithoutExtension(runtimeFile.Path);

// TODO: Properly deal with file duplicates (same file in multiple package, or RID conflicts)
if (!dependenciesMapping.ContainsKey(assemblyName))
dependenciesMapping.Add(assemblyName, fullPath);
dependenciesMapping.TryAdd(assemblyName, fullPath);
}
}
}
Expand All @@ -295,8 +294,7 @@ private Assembly LoadAssemblyFromPathInternal([NotNull] string assemblyFullPath)
var assemblyName = Path.GetFileNameWithoutExtension(runtimeFile);

// TODO: Properly deal with file duplicates (same file in multiple package, or RID conflicts)
if (!dependenciesMapping.ContainsKey(assemblyName))
dependenciesMapping.Add(assemblyName, runtimeFile);
dependenciesMapping.TryAdd(assemblyName, runtimeFile);
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions sources/core/Stride.Core.Yaml/Schemas/SchemaBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2015 SharpYaml - Alexandre Mutel
// Copyright (c) 2015 SharpYaml - Alexandre Mutel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -347,9 +347,7 @@ protected void RegisterDefaultTagMapping(string tag, Type type, bool isDefault)
if (type == null)
throw new ArgumentNullException("type");

if (!mapTypeToShortTag.ContainsKey(type))
mapTypeToShortTag.Add(type, tag);

mapTypeToShortTag.TryAdd(type, tag);
if (isDefault)
{
mapShortTagToType[tag] = type;
Expand Down Expand Up @@ -473,4 +471,4 @@ public bool IsMatch(string value)
}
}
}
}
}
3 changes: 1 addition & 2 deletions sources/core/Stride.Core/Reflection/AssemblyRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ public static HashSet<string> FindCategories([NotNull] Assembly assembly)

public static void RegisterScanTypes([NotNull] Assembly assembly, ScanTypes types)
{
if (!AssemblyToScanTypes.ContainsKey(assembly))
AssemblyToScanTypes.Add(assembly, types);
AssemblyToScanTypes.TryAdd(assembly, types);
}

public static ScanTypes GetScanTypes([NotNull] Assembly assembly)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ public static void RegisterSerializationAssembly([NotNull] AssemblySerializers a
lock (Lock)
{
// Register it (so that we can get it back if unregistered)
if (!AvailableAssemblySerializers.ContainsKey(assemblySerializers.Assembly))
AvailableAssemblySerializers.Add(assemblySerializers.Assembly, assemblySerializers);
AvailableAssemblySerializers.TryAdd(assemblySerializers.Assembly, assemblySerializers);

// Check if already loaded
if (AssemblySerializers.Contains(assemblySerializers))
Expand Down Expand Up @@ -267,10 +266,7 @@ private static void RegisterSerializers([NotNull] AssemblySerializers assemblySe

foreach (var assemblySerializer in assemblySerializerPerProfile.Value)
{
if (!dataSerializers.ContainsKey(assemblySerializer.ObjectType))
{
dataSerializers.Add(assemblySerializer.ObjectType, assemblySerializer);
}
dataSerializers.TryAdd(assemblySerializer.ObjectType, assemblySerializer);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,8 +598,7 @@ private async Task UpdateAllNavigationMeshLinks()

private async Task UpdateNavigationMeshLink(AssetViewModel asset)
{
if (!navigationMeshAssets.ContainsKey(asset.Id))
navigationMeshAssets.Add(asset.Id, asset);
navigationMeshAssets.TryAdd(asset.Id, asset);

// Either add or remove the navigation mesh to the navigation mesh manager, which will then handle loading the navigation mesh whenever it gets compiler
// and then call NavigationMeshManagerOnChanged to update the shown navigation mesh
Expand Down
8 changes: 2 additions & 6 deletions sources/engine/Stride.Input/VirtualButton/VirtualButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,12 @@ private static void RegisterFromType(Type type)

private static void Register(VirtualButton virtualButton)
{
if (!mapIp.ContainsKey(virtualButton.Id))
if (mapIp.TryAdd(virtualButton.Id, virtualButton))
{
mapIp.Add(virtualButton.Id, virtualButton);
registered.Add(virtualButton);
}

if (!mapName.ContainsKey(virtualButton.Name))
{
mapName.Add(virtualButton.Name, virtualButton);
}
mapName.TryAdd(virtualButton.Name, virtualButton);
}
}
}
8 changes: 2 additions & 6 deletions sources/engine/Stride.Physics/Engine/PhysicsComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -758,12 +758,8 @@ public void IgnoreCollisionWith(PhysicsComponent other, CollisionState state)
{
if(ignoreCollisionBuffer != null || other.ignoreCollisionBuffer == null)
{
if(ignoreCollisionBuffer == null)
ignoreCollisionBuffer = new Dictionary<PhysicsComponent, CollisionState>();
if(ignoreCollisionBuffer.ContainsKey(other))
ignoreCollisionBuffer[other] = state;
else
ignoreCollisionBuffer.Add(other, state);
ignoreCollisionBuffer ??= [];
ignoreCollisionBuffer[other] = state;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,7 @@ private Entity CreateChildEntity(PhysicsComponent component, ColliderShape shape
debugPrimitive = shape.CreateUpdatableDebugPrimitive(graphicsDevice);
updatableDebugMeshCache[shape] = debugPrimitive;
}
if (!updatableDebugMeshes.ContainsKey(shape))
{
updatableDebugMeshes.Add(shape, debugPrimitive);
}
updatableDebugMeshes.TryAdd(shape, debugPrimitive);
}
else if (type == typeof(CapsuleColliderShape) || type == typeof(ConvexHullColliderShape) || type == typeof(StaticMeshColliderShape))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1013,8 +1013,7 @@ protected Statement VisitStatementAsFunctionInvocation(ExpressionStatement state
{
Expression conditionExpression;

if (!methodInvocationHandled.ContainsKey(methodInvocationExpression))
methodInvocationHandled.Add(methodInvocationExpression, true);
methodInvocationHandled.TryAdd(methodInvocationExpression, true);

base.Visit(statement);

Expand All @@ -1040,8 +1039,8 @@ protected Statement VisitStatementAsFunctionInvocation(ExpressionStatement state

if (methodInvocationExpression.Arguments.Count == 3)
{
if (!methodInvocationHandled.ContainsKey(methodInvocationExpression))
methodInvocationHandled.Add(methodInvocationExpression, true);
methodInvocationHandled.TryAdd(methodInvocationExpression, true);

base.Visit(statement);

var sinAssign =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ private void AddTerminal(TokenType type, Terminal term)
else if (tokenInfo.TokenCategory == TokenCategory.Typename || tokenInfo.TokenCategory == TokenCategory.Keyword)
{
var keyMap = (tokenInfo.IsCaseInsensitive) ? caseInsensitiveKeywordToTerminal : keywordToTerminal;
if (!keyMap.ContainsKey(term.Name))
keyMap.Add(term.Name, term);
keyMap.TryAdd(term.Name, term);
}
else
{
Expand Down
12 changes: 3 additions & 9 deletions sources/tools/Stride.Importer.3D/MeshConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,7 @@ private unsafe void GenerateUniqueNames(Dictionary<IntPtr, string> finalNames, L
tempNames.Add(itemName);

// count the occurences of this name
if (!itemNameTotalCount.ContainsKey(itemName))
itemNameTotalCount.Add(itemName, 1);
else
if (!itemNameTotalCount.TryAdd(itemName, 1))
itemNameTotalCount[itemName]++;
}

Expand All @@ -457,9 +455,7 @@ private unsafe void GenerateUniqueNames(Dictionary<IntPtr, string> finalNames, L

if (itemNameTotalCount[itemName] > 1)
{
if (!itemNameCurrentCount.ContainsKey(itemName))
itemNameCurrentCount.Add(itemName, 1);
else
if (!itemNameCurrentCount.TryAdd(itemName, 1))
itemNameCurrentCount[itemName]++;

itemName = itemName + "_" + itemNameCurrentCount[itemName].ToString(CultureInfo.InvariantCulture);
Expand Down Expand Up @@ -1293,9 +1289,7 @@ private ComputeTextureColor GetTextureReferenceNode(string vfsOutputPath, string
var referenceName = attachedReference.Url;

// find a new and correctName
if (!textureNameCount.ContainsKey(referenceName))
textureNameCount.Add(referenceName, 1);
else
if (!textureNameCount.TryAdd(referenceName, 1))
{
int count = textureNameCount[referenceName];
textureNameCount[referenceName] = count + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4323,15 +4323,10 @@ public static FIMETADATA FindFirstMetadata(
return result;
}
tag = new MetadataTag(_tag, model);
if (metaDataSearchHandler.ContainsKey(result))
{
metaDataSearchHandler[result] = model;
}
else
{
metaDataSearchHandler.Add(result, model);
}
return result;

metaDataSearchHandler[result] = model;

return result;
}

/// <summary>
Expand Down

0 comments on commit 9a643ce

Please sign in to comment.