Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSVehicleContainer.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
20// vehicles sorted by their departures
21/****************************************************************************/
22#include <config.h>
23
24#include <algorithm>
25#include <cassert>
26#include <iterator>
27#include "MSVehicle.h"
28#include "MSVehicleContainer.h"
29
30
31// ===========================================================================
32// method definitions
33// ===========================================================================
34/* -------------------------------------------------------------------------
35 * methods from MSVehicleContainer::VehicleDepartureVectorSortCrit
36 * ----------------------------------------------------------------------- */
37bool
38MSVehicleContainer::VehicleDepartureVectorSortCrit::operator()
39(const VehicleDepartureVector& e1, const VehicleDepartureVector& e2) const {
40 return e1.first < e2.first;
41}
42
43
44
45/* -------------------------------------------------------------------------
46 * methods from MSVehicleContainer::DepartFinder
47 * ----------------------------------------------------------------------- */
50
51
52bool
53MSVehicleContainer::DepartFinder::operator()
54(const VehicleDepartureVector& e) const {
55 return myTime + DELTA_T > e.first && myTime <= e.first;
56}
57
58
59
60/* -------------------------------------------------------------------------
61 * methods from MSVehicleContainer
62 * ----------------------------------------------------------------------- */
65
66
68 // !!! vehicles are deleted in MSVehicle
69}
70
71
72void
74 // check whether a new item shall be added or the vehicle may be
75 // added to an existing list
76 VehicleHeap::iterator i =
77 find_if(array.begin() + 1, array.begin() + currentSize + 1, DepartFinder(veh->getParameter().depart));
78 if (currentSize == 0 || i == array.begin() + currentSize + 1) {
79 // a new heap-item is necessary
80 const SUMOTime delay = veh->getParameter().depart % DELTA_T;
81 const SUMOTime depart = veh->getParameter().depart + (delay == 0 ? 0 : DELTA_T - delay);
82 VehicleDepartureVector newElem(depart, VehicleVector());
83 newElem.second.push_back(veh);
84 addReplacing(newElem);
85 } else {
86 // add vehicle to an existing heap-item
87 (*i).second.push_back(veh);
88 }
89}
90
91
92void
94 // check whether a new item shall be added or the vehicle may be
95 // added to an existing list
96 VehicleHeap::iterator i =
97 find_if(array.begin() + 1, array.begin() + currentSize + 1, DepartFinder(veh->getParameter().depart));
98 if (!(currentSize == 0 || i == array.begin() + currentSize + 1)) {
99 // remove vehicle from an existing heap-item
100 (*i).second.erase(std::remove((*i).second.begin(), (*i).second.end(), veh), (*i).second.end());
101 }
102}
103
104
105void
107 VehicleHeap::iterator j =
108 find_if(array.begin() + 1, array.begin() + currentSize + 1,
109 DepartFinder(time));
110 if (currentSize == 0 || j == array.begin() + currentSize + 1) {
111 VehicleDepartureVector newElem(time,
112 VehicleVector(cont));
113 addReplacing(newElem);
114 } else {
115 VehicleVector& stored = (*j).second;
116 stored.reserve(stored.size() + cont.size());
117 copy(cont.begin(), cont.end(), back_inserter(stored));
118 }
119}
120
121
122
123void
125 if (isFull()) {
126 std::vector<VehicleDepartureVector> array2((array.size() - 1) * 2 + 1, VehicleDepartureVector());
127 for (int i = (int)array.size(); i-- > 0;) {
128 assert(i < (int)array2.size());
129 array2[i] = array[i];
130 }
131 array = array2;
132 }
133
134 // Percolate up
135 int hole = ++currentSize;
136 for (; hole > 1 && (x.first < array[ hole / 2 ].first); hole /= 2) {
137 assert((int)array.size() > hole);
138 array[hole] = array[ hole / 2 ];
139 }
140 assert((int)array.size() > hole);
141 array[hole] = x;
142}
143
144
145bool
147 return !isEmpty() && topTime() <= time;
148}
149
150
153 if (isEmpty()) {
154 throw 1;
155 }
156 assert(array.size() > 1);
157 return array[ 1 ].second;
158}
159
160
163 if (isEmpty()) {
164 throw 1;
165 }
166 assert(array.size() > 1);
167 return array[ 1 ].first;
168}
169
170
171void
173
174{
175 if (isEmpty()) {
176 throw 1;
177 }
178
179 assert(array.size() > 1);
180 array[ 1 ] = array[ currentSize-- ];
181 percolateDown(1);
182}
183
184
185bool
187 return currentSize == 0;
188}
189
190
191bool
193 return currentSize >= ((int) array.size()) - 1;
194}
195
196
197void
199 int child;
200 assert((int)array.size() > hole);
201 VehicleDepartureVector tmp = array[ hole ];
202
203 for (; hole * 2 <= currentSize; hole = child) {
204 child = hole * 2;
205 if (child != currentSize && (array[child + 1].first < array[child].first)) {
206 child++;
207 }
208 if ((array[ child ].first < tmp.first)) {
209 assert((int)array.size() > hole);
210 array[hole] = array[child];
211 } else {
212 break;
213 }
214 }
215 assert((int)array.size() > hole);
216 array[hole] = tmp;
217}
218
219
220int
222 return currentSize;
223}
224
225
226void
228 for (VehicleHeap::const_iterator i = array.begin() + 1; i != array.begin() + currentSize + 1; ++i) {
229 if (i != array.begin() + 1) {
230 std::cout << ", ";
231 }
232 std::cout << (*i).first;
233 }
234 std::cout << std::endl << "-------------------------" << std::endl;
235}
236
237
238std::ostream& operator << (std::ostream& strm, MSVehicleContainer& cont) {
239 strm << "------------------------------------" << std::endl;
240 while (!cont.isEmpty()) {
241 const MSVehicleContainer::VehicleVector& v = cont.top();
242 for (MSVehicleContainer::VehicleVector::const_iterator i = v.begin(); i != v.end(); ++i) {
243 strm << (*i)->getParameter().depart << std::endl;
244 }
245 cont.pop();
246 }
247 return strm;
248}
249
250void
252 for (VehicleDepartureVector& vdv : array) {
253 vdv.first = 0;
254 vdv.second.clear();
255 }
256 currentSize = 0;
257}
258
259/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
std::ostream & operator<<(std::ostream &strm, MSVehicleContainer &cont)
SUMOTime DELTA_T
Definition SUMOTime.cpp:38
Searches for the VehicleDepartureVector with the wished depart.
DepartFinder(SUMOTime time)
constructor
bool anyWaitingBefore(SUMOTime time) const
Returns the information whether any vehicles want to depart before the given time.
std::pair< SUMOTime, VehicleVector > VehicleDepartureVector
void remove(SUMOVehicle *veh)
Removes a single vehicle.
void percolateDown(int hole)
Moves the elements down.
void add(SUMOVehicle *veh)
Adds a single vehicle.
void addReplacing(const VehicleDepartureVector &cont)
Replaces the existing single departure time vector by the one given.
int size() const
Returns the size of the container.
void pop()
Removes the uppermost vehicle vector.
int currentSize
Number of elements in heap.
void showArray() const
Prints the container (the departure times)
MSVehicleContainer(int capacity=10)
Constructor.
VehicleHeap array
The vehicle vector heap.
SUMOTime topTime() const
Returns the time the uppermost vehicle vector is assigned to.
~MSVehicleContainer()
Destructor.
std::vector< SUMOVehicle * > VehicleVector
definition of a list of vehicles which have the same departure time
void clearState()
Remove all vehicles before quick-loading state.
bool isEmpty() const
Returns the information whether the container is empty.
const VehicleVector & top()
Returns the uppermost vehicle vector.
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle's parameter (including departure definition)
Representation of a vehicle.
Definition SUMOVehicle.h:62