From 49aa24ba6b769481ba4631c06658985ec313997c Mon Sep 17 00:00:00 2001
From: Markus Quaritsch <markus.quaritsch@tugraz.at>
Date: Tue, 10 May 2016 11:21:53 +0200
Subject: [PATCH] fix: sorted list does not allow duplicates

---
 .../Models/Simulation/Impl/VehicleContainer.cs      | 13 +++++++------
 .../VectoCoreTest/Models/Simulation/FactoryTest.cs  |  4 ++--
 .../Models/Simulation/PowerTrainBuilderTest.cs      |  2 +-
 3 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/VectoCore/VectoCore/Models/Simulation/Impl/VehicleContainer.cs b/VectoCore/VectoCore/Models/Simulation/Impl/VehicleContainer.cs
index ae89d01ed9..d13a116fb8 100644
--- a/VectoCore/VectoCore/Models/Simulation/Impl/VehicleContainer.cs
+++ b/VectoCore/VectoCore/Models/Simulation/Impl/VehicleContainer.cs
@@ -48,8 +48,8 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl
 {
 	public class VehicleContainer : LoggingObject, IVehicleContainer
 	{
-		internal readonly SortedList<int, VectoSimulationComponent> Components =
-			new SortedList<int, VectoSimulationComponent>();
+		private List<Tuple<int, VectoSimulationComponent>> _components =
+			new List<Tuple<int, VectoSimulationComponent>>();
 
 		internal IEngineInfo Engine;
 		internal IGearboxInfo Gearbox;
@@ -227,7 +227,8 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl
 					commitPriority = 3;
 				});
 
-			Components.Add(-commitPriority, component);
+			_components.Add(Tuple.Create(commitPriority, component));
+			_components = _components.OrderBy(x => x.Item1).Reverse().ToList();
 		}
 
 
@@ -236,8 +237,8 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl
 			Log.Info("VehicleContainer committing simulation. time: {0}, dist: {1}, speed: {2}", time,
 				ExecutionMode == ExecutionMode.EngineOnly ? null : Distance, VehicleSpeed);
 
-			foreach (var component in Components) {
-				component.Value.CommitSimulationStep(ModData);
+			foreach (var component in _components) {
+				component.Item2.CommitSimulationStep(ModData);
 			}
 
 			if (ModData != null) {
@@ -261,7 +262,7 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl
 
 		public IReadOnlyCollection<VectoSimulationComponent> SimulationComponents()
 		{
-			return new ReadOnlyCollection<VectoSimulationComponent>(Components.Select(x => x.Value).ToList());
+			return new ReadOnlyCollection<VectoSimulationComponent>(_components.Select(x => x.Item2).ToList());
 		}
 
 		public Meter Distance
diff --git a/VectoCore/VectoCoreTest/Models/Simulation/FactoryTest.cs b/VectoCore/VectoCoreTest/Models/Simulation/FactoryTest.cs
index 8eaad5afbe..3546a59338 100644
--- a/VectoCore/VectoCoreTest/Models/Simulation/FactoryTest.cs
+++ b/VectoCore/VectoCoreTest/Models/Simulation/FactoryTest.cs
@@ -60,7 +60,7 @@ namespace TUGraz.VectoCore.Tests.Models.Simulation
 			var run = factory.SimulationRuns().First();
 			var vehicleContainer = (VehicleContainer)run.GetContainer();
 
-			Assert.AreEqual(11, vehicleContainer.Components.Count);
+			Assert.AreEqual(11, vehicleContainer.SimulationComponents().Count);
 
 			Assert.IsInstanceOfType(vehicleContainer.Gearbox, typeof(Gearbox), "gearbox not installed");
 			Assert.IsInstanceOfType(vehicleContainer.Engine, typeof(CombustionEngine), "engine not installed");
@@ -112,7 +112,7 @@ namespace TUGraz.VectoCore.Tests.Models.Simulation
 			var run = factory.SimulationRuns().First();
 
 			var vehicleContainer = (VehicleContainer)run.GetContainer();
-			Assert.AreEqual(11, vehicleContainer.Components.Count);
+			Assert.AreEqual(11, vehicleContainer.SimulationComponents().Count);
 
 			Assert.IsInstanceOfType(vehicleContainer.Gearbox, typeof(Gearbox), "gearbox not installed");
 			Assert.IsInstanceOfType(vehicleContainer.Engine, typeof(CombustionEngine), "engine not installed");
diff --git a/VectoCore/VectoCoreTest/Models/Simulation/PowerTrainBuilderTest.cs b/VectoCore/VectoCoreTest/Models/Simulation/PowerTrainBuilderTest.cs
index 2c7db66943..767ddfa82f 100644
--- a/VectoCore/VectoCoreTest/Models/Simulation/PowerTrainBuilderTest.cs
+++ b/VectoCore/VectoCoreTest/Models/Simulation/PowerTrainBuilderTest.cs
@@ -65,7 +65,7 @@ namespace TUGraz.VectoCore.Tests.Models.Simulation
 			var powerTrain = builder.Build(runData);
 
 			Assert.IsInstanceOfType(powerTrain, typeof(IVehicleContainer));
-			Assert.AreEqual(11, powerTrain.Components.Count);
+			Assert.AreEqual(11, powerTrain.SimulationComponents().Count);
 
 			Assert.IsInstanceOfType(powerTrain.Engine, typeof(CombustionEngine));
 			Assert.IsInstanceOfType(powerTrain.Gearbox, typeof(Gearbox));
-- 
GitLab