From 78a4987ed1061f023cad819ab57422bcbbd213fa Mon Sep 17 00:00:00 2001 From: siimav Date: Sun, 23 Apr 2023 13:56:11 +0300 Subject: [PATCH] Remove unnecessary double dictionary lookups --- .../TestFlightAPI/TestFlightPartData.cs | 5 +- TestFlightCore/TestFlightCore/TestFlight.cs | 61 ++++++++++--------- .../TestFlightCore/TestFlightRnDScenario.cs | 5 +- .../TestFlightCore/TestFlightWindow.cs | 3 +- 4 files changed, 37 insertions(+), 37 deletions(-) diff --git a/TestFlightAPI/TestFlightAPI/TestFlightPartData.cs b/TestFlightAPI/TestFlightAPI/TestFlightPartData.cs index 29fd334..0367219 100644 --- a/TestFlightAPI/TestFlightAPI/TestFlightPartData.cs +++ b/TestFlightAPI/TestFlightAPI/TestFlightPartData.cs @@ -67,10 +67,7 @@ public int GetInt(string key) public void SetValue(string key, object value) { key = key.ToLowerInvariant(); - if (partData.ContainsKey(key)) - partData[key] = value.ToString(); - else - partData.Add(key, value.ToString()); + partData[key] = value.ToString(); } public void AddValue(string key, float value) diff --git a/TestFlightCore/TestFlightCore/TestFlight.cs b/TestFlightCore/TestFlightCore/TestFlight.cs index c6c9edc..682729e 100644 --- a/TestFlightCore/TestFlightCore/TestFlight.cs +++ b/TestFlightCore/TestFlightCore/TestFlight.cs @@ -480,10 +480,7 @@ public string GetValue(string key) public void SetValue(string key, string value) { key = key.ToLowerInvariant(); - if (saveData.ContainsKey(key)) - saveData[key] = value; - else - saveData.Add(key, value); + saveData[key] = value; } private void DecodeRawSaveData() @@ -616,10 +613,8 @@ public string PartWithNoData(string partList) // Get access to a part's data store for further set/get public TestFlightPartData GetPartDataForPart(string partName) { - TestFlightPartData returnValue = null; - - if (partData.ContainsKey(partName)) - returnValue = partData[partName]; + TestFlightPartData returnValue; + partData.TryGetValue(partName, out returnValue); return returnValue; } @@ -627,19 +622,17 @@ public TestFlightPartData GetPartDataForPart(string partName) // Sets the existing partData for this part, or adds a new one public void SetPartDataForPart(string partName, TestFlightPartData newData) { - if (partData.ContainsKey(partName)) - partData[partName] = newData; - else - partData.Add(partName, newData); + partData[partName] = newData; } // New noscope Format // This is a utility method that will return the "flightData" value directly or -1 if not found public float GetFlightDataForPartName(string partName) { - if (partData.ContainsKey(partName)) + TestFlightPartData data; + if (partData.TryGetValue(partName, out data)) { - return partData[partName].flightData; + return data.flightData; } else return -1f; @@ -647,16 +640,18 @@ public float GetFlightDataForPartName(string partName) // This is a utility method that will return the "transferData" value directly or -1 if not found public float GetTransferDataForPartName(string partName) { - if (partData.ContainsKey(partName)) - return partData[partName].transferData; + TestFlightPartData data; + if (partData.TryGetValue(partName, out data)) + return data.transferData; else return -1f; } // This is a utility method that will return the "researchData" value directly or -1 if not found public float GetResearchDataForPartName(string partName) { - if (partData.ContainsKey(partName)) - return partData[partName].researchData; + TestFlightPartData data; + if (partData.TryGetValue(partName, out data)) + return data.researchData; else return -1f; } @@ -665,8 +660,9 @@ public float GetResearchDataForPartName(string partName) // This is a utility method that sets the "flightData" value directly public void SetFlightDataForPartName(string partName, float data) { - if (partData.ContainsKey(partName)) - partData[partName].flightData = data; + TestFlightPartData pd; + if (partData.TryGetValue(partName, out pd)) + pd.flightData = data; else { TestFlightPartData newData = new TestFlightPartData(); @@ -678,8 +674,9 @@ public void SetFlightDataForPartName(string partName, float data) // This is a utility method that sets the "transferData" value directly public void SetTransferDataForPartName(string partName, float data) { - if (partData.ContainsKey(partName)) - partData[partName].transferData = data; + TestFlightPartData pd; + if (partData.TryGetValue(partName, out pd)) + pd.transferData = data; else { TestFlightPartData newData = new TestFlightPartData(); @@ -691,8 +688,9 @@ public void SetTransferDataForPartName(string partName, float data) // This is a utility method that sets the "researchData" value directly public void SetResearchDataForPartName(string partName, float data) { - if (partData.ContainsKey(partName)) - partData[partName].researchData = data; + TestFlightPartData pd; + if (partData.TryGetValue(partName, out pd)) + pd.researchData = data; else { TestFlightPartData newData = new TestFlightPartData(); @@ -704,8 +702,9 @@ public void SetResearchDataForPartName(string partName, float data) // This is a utility method that adds the "flightData" value directly public void AddFlightDataForPartName(string partName, float data) { - if (partData.ContainsKey(partName)) - partData[partName].flightData += data; + TestFlightPartData pd; + if (partData.TryGetValue(partName, out pd)) + pd.flightData += data; else { TestFlightPartData newData = new TestFlightPartData(); @@ -717,8 +716,9 @@ public void AddFlightDataForPartName(string partName, float data) // This is a utility method that adds the "transferData" value directly public void AddTransferDataForPartName(string partName, float data) { - if (partData.ContainsKey(partName)) - partData[partName].transferData += data; + TestFlightPartData pd; + if (partData.TryGetValue(partName, out pd)) + pd.transferData += data; else { TestFlightPartData newData = new TestFlightPartData(); @@ -730,8 +730,9 @@ public void AddTransferDataForPartName(string partName, float data) // This is a utility method that adds the "researchData" value directly public void AddResearchDataForPartName(string partName, float data) { - if (partData.ContainsKey(partName)) - partData[partName].researchData += data; + TestFlightPartData pd; + if (partData.TryGetValue(partName, out pd)) + pd.researchData += data; else { TestFlightPartData newData = new TestFlightPartData(); diff --git a/TestFlightCore/TestFlightCore/TestFlightRnDScenario.cs b/TestFlightCore/TestFlightCore/TestFlightRnDScenario.cs index 11a4800..30f1e44 100644 --- a/TestFlightCore/TestFlightCore/TestFlightRnDScenario.cs +++ b/TestFlightCore/TestFlightCore/TestFlightRnDScenario.cs @@ -342,9 +342,10 @@ public bool GetPartResearchState(string partName) public void SetPartResearchState(string partName, bool researchActive) { - if (activeTeams.ContainsKey(partName)) + TestFlightRnDTeam teams; + if (activeTeams.TryGetValue(partName, out teams)) { - activeTeams[partName].ResearchActive = researchActive; + teams.ResearchActive = researchActive; } } diff --git a/TestFlightCore/TestFlightCore/TestFlightWindow.cs b/TestFlightCore/TestFlightCore/TestFlightWindow.cs index 1197c5a..d333624 100644 --- a/TestFlightCore/TestFlightCore/TestFlightWindow.cs +++ b/TestFlightCore/TestFlightCore/TestFlightWindow.cs @@ -273,7 +273,8 @@ internal override void DrawWindow(Int32 id) // Display information on active vessel Guid currentVessel = FlightGlobals.ActiveVessel.id; - if (masterStatus.ContainsKey(currentVessel) && masterStatus[currentVessel].allPartsStatus.Count > 0) + MasterStatusItem statusItem; + if (masterStatus.TryGetValue(currentVessel, out statusItem) && statusItem.allPartsStatus.Count > 0) { tfScenario.userSettings.currentMSDScrollPosition = GUILayout.BeginScrollView(tfScenario.userSettings.currentMSDScrollPosition); foreach (PartStatus status in masterStatus[currentVessel].allPartsStatus)