3 using System.Collections.Generic;
4 using System.Collections.ObjectModel;
17 private Dictionary<Car, Cell> cellsWithCarInPreviousStep =
new Dictionary<Car, Cell>();
22 public ReadOnlyDictionary<Car, Cell> CellsWithCarInPreviousStep
24 get {
return new ReadOnlyDictionary<Car, Cell>(cellsWithCarInPreviousStep); }
31 private Dictionary<Car, Cell> cellsWithCarInCurrentStep =
new Dictionary<Car, Cell>();
36 public ReadOnlyDictionary<Car, Cell> CellsWithCarInCurrentStep
38 get {
return new ReadOnlyDictionary<Car, Cell>(cellsWithCarInCurrentStep); }
46 internal void SwapCarsPreviousAndCurrentDictionaries()
48 Dictionary<Car, Cell> helperDict = cellsWithCarInPreviousStep;
49 cellsWithCarInPreviousStep = cellsWithCarInCurrentStep;
50 cellsWithCarInCurrentStep = helperDict;
56 internal void ProcessCarsOutsideOfTopology()
60 HashSet<Car> carsToRemove =
new HashSet<Car>();
63 foreach (KeyValuePair<Car, Cell> frontCellWithCar
in cellsWithCarInCurrentStep)
65 Car car = frontCellWithCar.Key;
66 if (!car.IsStillInTopology)
68 car.RemoveCarFromCells();
69 carsToRemove.Add(car);
73 foreach (
Car carToRemove
in carsToRemove)
75 cellsWithCarInPreviousStep.Remove(carToRemove);
76 cellsWithCarInCurrentStep.Remove(carToRemove);
85 internal void PerformTransitionForAllCars(Random random, TimeSpan currentModelTime)
88 foreach (KeyValuePair<Car, Cell> frontCellWithCar
in cellsWithCarInPreviousStep)
90 Car car = frontCellWithCar.Key;
91 Cell newFrontCell = car.PerformTransition(random);
94 if (followingSensor != null)
97 if (!car.IsRecordedBySensor)
100 followingSensor.AddOutputRecord(currentModelTime, car.NewSpeed);
103 cellsWithCarInCurrentStep[car] = newFrontCell;
110 internal void PrepareCarsForNextSimulationStep()
112 foreach (KeyValuePair<Car, Cell> frontCellWithCar
in cellsWithCarInCurrentStep)
114 Car car = frontCellWithCar.Key;
115 car.PrepareForNextSimulationStep();
124 internal void AddNewCar(
Car newCar,
Cell cell)
126 cellsWithCarInPreviousStep.Add(newCar, cell);
127 cellsWithCarInCurrentStep.Add(newCar, cell);
133 internal void ClearCars()
135 foreach (KeyValuePair<Car, Cell> frontCellWithCar
in cellsWithCarInPreviousStep)
137 frontCellWithCar.Key.RemoveCarFromCells();
140 cellsWithCarInPreviousStep.Clear();
141 cellsWithCarInCurrentStep.Clear();
Class representing sensor.
Cell FollowingCell
Link to following cell.
Manages cars in cellular topology.