forked from xmnovotny/VoxelTycoon-ScheduleStopwatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVehicleScheduleData.Snapshot.cs
198 lines (181 loc) · 7.42 KB
/
VehicleScheduleData.Snapshot.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using VoxelTycoon;
using VoxelTycoon.Tracks;
using VoxelTycoon.Tracks.Tasks;
namespace ScheduleStopwatch
{
public partial class VehicleScheduleData
{
private struct TaskSnapshot
{
public VehicleStationLocation location;
public int index, version;
public bool nonstop;
public RootTask task;
public TaskSnapshot(RootTask task, int index)
{
this.task = task;
location = task.Destination.VehicleStationLocation;
version = VehicleScheduleHelper.Current.GetRootTaskVersion(task);
nonstop = task.Behavior == RootTaskBehavior.NonStop;
this.index = index;
}
}
private class SnapshotComparsion
{
public HashSet<RootTask> removed;
public HashSet<RootTask> changed; //change in the RootTask
public HashSet<RootTask> incomingRouteChange; //task with singifinant change for measurment travel time from the task before
public SnapshotComparsion()
{
removed = new HashSet<RootTask>();
changed = new HashSet<RootTask>();
incomingRouteChange = new HashSet<RootTask>();
}
public bool IsDifference
{
get
{
return removed.Count > 0 || changed.Count > 0 || incomingRouteChange.Count > 0;
}
}
}
private class Snapshot
{
public ReadOnlyCollection<TaskSnapshot> TaskSnapshots
{
get
{
return _taskSnapshots.AsReadOnly();
}
}
public Dictionary<RootTask, int> TaskToIndex
{
get
{
if (_taskToIndex == null)
{
_taskToIndex = new Dictionary<RootTask, int>();
foreach (var snapshot in _taskSnapshots)
{
_taskToIndex.Add(snapshot.task, snapshot.index);
}
}
return _taskToIndex;
}
}
public TaskSnapshot this[int index]
{
get
{
if (index<_taskSnapshots.Count)
{
return _taskSnapshots[index];
}
throw new IndexOutOfRangeException("Index out of range");
}
}
public int Count
{
get
{
return _taskSnapshots.Count;
}
}
private Dictionary<RootTask, int> _taskToIndex;
private readonly List<TaskSnapshot> _taskSnapshots = new List<TaskSnapshot>();
public Snapshot(VehicleSchedule schedule)
{
ImmutableList<RootTask> tasks = schedule.GetTasks();
int count = tasks.Count;
for (int i = 0; i < count; i++)
{
RootTask task = tasks[i];
_taskSnapshots.Add(new TaskSnapshot(task, i));
}
}
public IEnumerable<RootTask> GetNonNonstopTasks()
{
foreach(TaskSnapshot snapshot in _taskSnapshots)
{
if (!snapshot.nonstop)
{
yield return snapshot.task;
}
}
yield break;
}
public IEnumerable<(VehicleStationLocation location, bool nonstop, RootTask task)> GetLocationsWithNonstopInfo()
{
foreach (TaskSnapshot snapshot in _taskSnapshots)
{
yield return (snapshot.location, snapshot.nonstop, snapshot.task);
}
yield break;
}
public SnapshotComparsion CompareWithNewer(Snapshot newSnapshot)
{
SnapshotComparsion result = new SnapshotComparsion();
if (_taskSnapshots.Count > 0)
{
Dictionary<RootTask, int> oldTaskToIndex = TaskToIndex;
Dictionary<RootTask, int> newTaskToIndex = newSnapshot.TaskToIndex;
bool travelChanged = false;
int? lastNewIndex = null, expectedNewIndex;
RootTask firstNonNonstopInOld = null;
if (newTaskToIndex.TryGetValue(_taskSnapshots[_taskSnapshots.Count - 1].task, out var i))
lastNewIndex = i;
//find missing and changed
foreach (TaskSnapshot oldSnapshot in _taskSnapshots)
{
if (lastNewIndex.HasValue)
expectedNewIndex = (lastNewIndex.Value + 1) % newSnapshot.Count;
else
expectedNewIndex = null;
if (!oldSnapshot.nonstop && firstNonNonstopInOld == null)
{
firstNonNonstopInOld = oldSnapshot.task;
}
if ((!newTaskToIndex.TryGetValue(oldSnapshot.task, out var idx) || (newSnapshot[idx].nonstop && !oldSnapshot.nonstop)))
{
if (!oldSnapshot.nonstop)
{
result.removed.Add(oldSnapshot.task);
}
travelChanged = true;
lastNewIndex = null;
}
else
{
if ((expectedNewIndex.HasValue && expectedNewIndex.Value != idx) || (oldSnapshot.nonstop && !newSnapshot[idx].nonstop) || newSnapshot[idx].location != oldSnapshot.location)
{
travelChanged = true;
lastNewIndex = null;
}
if (!oldSnapshot.nonstop)
{
if (newSnapshot[idx].version != oldSnapshot.version)
{
result.changed.Add(oldSnapshot.task);
}
if (travelChanged == true)
{
result.incomingRouteChange.Add(oldSnapshot.task);
travelChanged = newSnapshot[idx].location != oldSnapshot.location; //when the location is changed, we must delete incoming and outcoming travel times
}
}
lastNewIndex = idx;
}
}
if (firstNonNonstopInOld != null && travelChanged && !result.incomingRouteChange.Contains(firstNonNonstopInOld))
{
result.incomingRouteChange.Add(firstNonNonstopInOld);
}
}
return result;
}
}
}
}