Skip to content

Commit 7fdad38

Browse files
committed
Refactored garbage generators
1 parent 51d4cd9 commit 7fdad38

File tree

23 files changed

+274
-311
lines changed

23 files changed

+274
-311
lines changed

Source/ORTS.Common/Filter.cs

Lines changed: 51 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
using System.Collections;
2020
using System.Collections.Generic;
2121
using System.Linq;
22-
using System.Text;
2322

2423
namespace ORTS.Common
2524
{
@@ -48,6 +47,12 @@ namespace ORTS.Common
4847
/// </summary>
4948
public class IIRFilter
5049
{
50+
int NCoef;
51+
List<float> ACoef;
52+
List<float> BCoef;
53+
float[] x;
54+
float[] y;
55+
5156
public IIRFilter()
5257
{
5358
/**************************************************************
@@ -73,25 +78,24 @@ Z domain Poles
7378
z = 0.599839 + j -0.394883
7479
z = 0.599839 + j 0.394883
7580
***************************************************************/
76-
ACoef = new ArrayList();
77-
ACoef.Add(0.00023973435363423468);
78-
ACoef.Add(0.00047946870726846936);
79-
ACoef.Add(0.00023973435363423468);
81+
ACoef = new List<float>
82+
{
83+
0.00023973435363423468f,
84+
0.00047946870726846936f,
85+
0.00023973435363423468f
86+
};
8087

81-
BCoef = new ArrayList();
82-
BCoef.Add(1.00000000000000000000);
83-
BCoef.Add(-1.94607498611971570000);
84-
BCoef.Add(0.94703573071858904000);
88+
BCoef = new List<float>
89+
{
90+
1.00000000000000000000f,
91+
-1.94607498611971570000f,
92+
0.94703573071858904000f
93+
};
8594

8695
NCoef = A.Count - 1;
8796

88-
x = new ArrayList();
89-
y = new ArrayList();
90-
for (int i = 0; i <= NCoef; i++)
91-
{
92-
x.Add(0.0);
93-
y.Add(0.0);
94-
}
97+
x = new float[NCoef + 1];
98+
y = new float[NCoef + 1];
9599

96100
FilterType = FilterTypes.Bessel;
97101
}
@@ -102,19 +106,14 @@ Z domain Poles
102106
/// <param name="a">A coefficients of the filter</param>
103107
/// <param name="b">B coefficients of the filter</param>
104108
/// <param name="type">Filter type</param>
105-
public IIRFilter(ArrayList a, ArrayList b, FilterTypes type)
109+
public IIRFilter(List<float> a, List<float> b, FilterTypes type)
106110
{
107111
FilterType = type;
108112
NCoef = a.Count - 1;
109113
ACoef = a;
110114
BCoef = b;
111-
x = new ArrayList();
112-
y = new ArrayList();
113-
for (int i = 0; i <= NCoef; i++)
114-
{
115-
x.Add(0.0);
116-
y.Add(0.0);
117-
}
115+
x = new float[NCoef + 1];
116+
y = new float[NCoef + 1];
118117
}
119118

120119
/// <summary>
@@ -126,11 +125,10 @@ public IIRFilter(ArrayList a, ArrayList b, FilterTypes type)
126125
/// <param name="samplingPeriod">Filter sampling period</param>
127126
public IIRFilter(FilterTypes type, int order, float cutoffFrequency, float samplingPeriod)
128127
{
129-
NCoef = order;
130-
A = new ArrayList();
131-
B = new ArrayList();
132-
133128
FilterType = type;
129+
NCoef = order;
130+
A = new List<float>();
131+
B = new List<float>();
134132

135133
switch (type)
136134
{
@@ -147,39 +145,25 @@ public IIRFilter(FilterTypes type, int order, float cutoffFrequency, float sampl
147145
NCoef = A.Count - 1;
148146
ACoef = A;
149147
BCoef = B;
150-
x = new ArrayList();
151-
y = new ArrayList();
152-
for (int i = 0; i <= NCoef; i++)
153-
{
154-
x.Add(0.0);
155-
y.Add(0.0);
156-
}
148+
x = new float[NCoef + 1];
149+
y = new float[NCoef + 1];
157150
}
158151

159-
int NCoef;
160-
ArrayList ACoef;
161-
ArrayList BCoef;
162-
163152
/// <summary>
164153
/// A coefficients of the filter
165154
/// </summary>
166-
public ArrayList A
155+
public List<float> A
167156
{
168157
set
169158
{
170-
if(NCoef <= 0)
159+
if (NCoef <= 0)
171160
NCoef = value.Count - 1;
172-
x = new ArrayList();
173-
y = new ArrayList();
174-
for (int i = 0; i <= NCoef; i++)
175-
{
176-
x.Add(0.0);
177-
y.Add(0.0);
178-
}
161+
x = new float[NCoef + 1];
162+
y = new float[NCoef + 1];
179163
if (ACoef == null)
180-
ACoef = new ArrayList();
164+
ACoef = new List<float>();
181165
ACoef.Clear();
182-
foreach (object obj in value)
166+
foreach (var obj in value)
183167
{
184168
ACoef.Add(obj);
185169
}
@@ -193,23 +177,18 @@ public ArrayList A
193177
/// <summary>
194178
/// B coefficients of the filter
195179
/// </summary>
196-
public ArrayList B
180+
public List<float> B
197181
{
198182
set
199183
{
200-
if(NCoef <= 0)
184+
if (NCoef <= 0)
201185
NCoef = value.Count - 1;
202-
x = new ArrayList();
203-
y = new ArrayList();
204-
for (int i = 0; i <= NCoef; i++)
205-
{
206-
x.Add(0.0);
207-
y.Add(0.0);
208-
}
186+
x = new float[NCoef + 1];
187+
y = new float[NCoef + 1];
209188
if (BCoef == null)
210-
BCoef = new ArrayList();
189+
BCoef = new List<float>();
211190
BCoef.Clear();
212-
foreach (object obj in value)
191+
foreach (var obj in value)
213192
{
214193
BCoef.Add(obj);
215194
}
@@ -220,9 +199,6 @@ public ArrayList B
220199
}
221200
}
222201

223-
ArrayList y;
224-
ArrayList x;
225-
226202
private float cuttoffFreqRadpS;
227203
/// <summary>
228204
/// Filter Cut off frequency in Radians
@@ -282,18 +258,18 @@ public enum FilterTypes
282258
public float Filter(float NewSample)
283259
{
284260
//shift the old samples
285-
for (int n = NCoef; n > 0; n--)
261+
for (int n = x.Length - 1; n > 0; n--)
286262
{
287263
x[n] = x[n - 1];
288264
y[n] = y[n - 1];
289265
}
290266
//Calculate the new output
291267
x[0] = NewSample;
292-
y[0] = (float)Convert.ToDouble(ACoef[0]) * (float)Convert.ToDouble(x[0]);
268+
y[0] = ACoef[0] * x[0];
293269
for (int n = 1; n <= NCoef; n++)
294-
y[0] = (float)Convert.ToDouble(y[0]) + (float)Convert.ToDouble(ACoef[n]) * (float)Convert.ToDouble(x[n]) - (float)Convert.ToDouble(BCoef[n]) * (float)Convert.ToDouble(y[n]);
270+
y[0] += ACoef[n] * x[n] - BCoef[n] * y[n];
295271

296-
return (float)y[0];
272+
return y[0];
297273
}
298274

299275
/// <summary>
@@ -322,31 +298,29 @@ public float Filter(float NewSample, float samplingPeriod)
322298
throw new NotImplementedException("Other filter types are not implemented yet. Try to use constant sampling period and Filter(float NewSample) version of this method.");
323299
}
324300
//shift the old samples
325-
for (int n = NCoef; n > 0; n--)
301+
for (int n = x.Length - 1; n > 0; n--)
326302
{
327303
x[n] = x[n - 1];
328304
y[n] = y[n - 1];
329305
}
330306
//Calculate the new output
331307
x[0] = NewSample;
332-
y[0] = (float)ACoef[0] * (float)x[0];
308+
y[0] = ACoef[0] * x[0];
333309
for (int n = 1; n <= NCoef; n++)
334-
{
335-
y[0] = (float)Convert.ToDouble(y[0]) + (float)Convert.ToDouble(ACoef[n]) * (float)Convert.ToDouble(x[n]) - (float)Convert.ToDouble(BCoef[n]) * (float)Convert.ToDouble(y[n]);
336-
}
310+
y[0] += ACoef[n] * x[n] - BCoef[n] * y[n];
337311

338-
return (float)y[0];
312+
return y[0];
339313
}
340314

341315
/// <summary>
342316
/// Resets all buffers of the filter
343317
/// </summary>
344318
public void Reset()
345319
{
346-
for (int i = 0; i < x.Count; i++)
320+
for (int i = 0; i < x.Length; i++)
347321
{
348-
x[i] = 0.0;
349-
y[i] = 0.0;
322+
x[i] = 0.0f;
323+
y[i] = 0.0f;
350324
}
351325
}
352326
/// <summary>

Source/ORTS.Settings/InputSettings.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public class InputSettings : SettingsBase
6767

6868
public static readonly UserCommandInput[] DefaultCommands = new UserCommandInput[Enum.GetNames(typeof(UserCommand)).Length];
6969
public readonly UserCommandInput[] Commands = new UserCommandInput[Enum.GetNames(typeof(UserCommand)).Length];
70+
static readonly UserCommand[] UserCommandValues = (UserCommand[])Enum.GetValues(typeof(UserCommand));
7071

7172
static InputSettings()
7273
{
@@ -91,7 +92,7 @@ UserCommand GetCommand(string name)
9192

9293
UserCommand[] GetCommands()
9394
{
94-
return (UserCommand[])Enum.GetValues(typeof(UserCommand));
95+
return UserCommandValues;
9596
}
9697

9798
public override object GetDefaultValue(string name)

Source/Orts.Formats.Msts/SignalConfigurationFile.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public class SignalConfigurationFile
4343
{
4444
/// <summary>Name-indexed list of available signal functions</summary>
4545
public IDictionary<string, SignalFunction> SignalFunctions;
46+
/// <summary>Allocation-free MstsName-indexed list of available signal functions</summary>
47+
public static Dictionary<MstsSignalFunction, SignalFunction> MstsSignalFunctions;
4648
/// <summary>List of OR defined subtypes for Norman signals</summary>
4749
public IList<string> ORTSNormalSubtypes;
4850
/// <summary>Name-indexed list of available light textures</summary>
@@ -80,6 +82,19 @@ public SignalConfigurationFile(string filenamewithpath, bool ORTSMode)
8082
{ SignalFunction.UNKNOWN.Name, SignalFunction.UNKNOWN }
8183
};
8284

85+
// and the allocation-free version of the above
86+
MstsSignalFunctions = new Dictionary<MstsSignalFunction, SignalFunction>
87+
{
88+
{ MstsSignalFunction.NORMAL, SignalFunction.NORMAL },
89+
{ MstsSignalFunction.DISTANCE, SignalFunction.DISTANCE },
90+
{ MstsSignalFunction.REPEATER, SignalFunction.REPEATER },
91+
{ MstsSignalFunction.SHUNTING, SignalFunction.SHUNTING },
92+
{ MstsSignalFunction.INFO, SignalFunction.INFO },
93+
{ MstsSignalFunction.SPEED, SignalFunction.SPEED },
94+
{ MstsSignalFunction.ALERT, SignalFunction.ALERT },
95+
{ MstsSignalFunction.UNKNOWN, SignalFunction.UNKNOWN }
96+
};
97+
8398
// preset empty OR normal subtypes
8499
ORTSNormalSubtypes = new List<String>();
85100

@@ -444,17 +459,22 @@ public class SignalFunction : IEquatable<SignalFunction>
444459

445460
public readonly string Name;
446461
public readonly MstsSignalFunction MstsFunction;
462+
readonly int HashCode;
447463

448464
public SignalFunction(MstsSignalFunction mstsFunction)
449465
{
450466
Name = mstsFunction.ToString();
451467
MstsFunction = mstsFunction;
468+
469+
HashCode = Name.GetHashCode() ^ MstsFunction.GetHashCode();
452470
}
453471

454472
public SignalFunction(string name, MstsSignalFunction mstsFunction)
455473
{
456474
Name = name;
457475
MstsFunction = mstsFunction;
476+
477+
HashCode = Name.GetHashCode() ^ MstsFunction.GetHashCode();
458478
}
459479

460480
public override string ToString()
@@ -505,7 +525,7 @@ public bool Equals(SignalFunction other)
505525

506526
public override int GetHashCode()
507527
{
508-
return Name.GetHashCode() ^ MstsFunction.GetHashCode();
528+
return HashCode;
509529
}
510530
}
511531
#endregion

Source/Orts.Simulation/Simulation/AIs/AITrain.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ public enum AI_START_MOVEMENT
129129
public static float minStopDistanceM = 3.0f; // minimum clear distance for stopping at signal in station
130130
public static float signalApproachDistanceM = 20.0f; // final approach to signal
131131

132+
private readonly List<ObjectItemInfo> processedList = new List<ObjectItemInfo>(); // internal processing list for CheckSignalObjects()
133+
132134
#if WITH_PATH_DEBUG
133135
// Only for EnhancedActCompatibility
134136
public string currentAIState = "";
@@ -1164,10 +1166,12 @@ public void CheckSignalObjects()
11641166
}
11651167

11661168
float validSpeed = AllowedMaxSpeedMpS;
1167-
List<ObjectItemInfo> processedList = new List<ObjectItemInfo>();
1169+
processedList.Clear();
11681170

1169-
foreach (ObjectItemInfo thisInfo in SignalObjectItems.Where(item => !item.speed_isWarning))
1171+
foreach (ObjectItemInfo thisInfo in SignalObjectItems)
11701172
{
1173+
if (thisInfo.speed_isWarning)
1174+
continue;
11711175

11721176
// check speedlimit
11731177
if (CheckTrain)

0 commit comments

Comments
 (0)