MSVehicleContainer.cpp
Go to the documentation of this file.00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029
00030 #include <algorithm>
00031 #include <cassert>
00032 #include "MSVehicle.h"
00033 #include "MSVehicleContainer.h"
00034
00035 #ifdef CHECK_MEMORY_LEAKS
00036 #include <foreign/nvwa/debug_new.h>
00037 #endif // CHECK_MEMORY_LEAKS
00038
00039
00040
00041
00042
00043
00044
00045
00046 bool
00047 MSVehicleContainer::VehicleDepartureVectorSortCrit::operator()
00048 (const VehicleDepartureVector& e1, const VehicleDepartureVector& e2) const {
00049 return e1.first < e2.first;
00050 }
00051
00052
00053
00054
00055
00056
00057 MSVehicleContainer::DepartFinder::DepartFinder(SUMOTime time)
00058 : myTime(time) {}
00059
00060
00061 bool
00062 MSVehicleContainer::DepartFinder::operator()
00063 (const VehicleDepartureVector& e) const {
00064 return myTime+DELTA_T > e.first && myTime<=e.first;
00065 }
00066
00067
00068
00069
00070
00071
00072 MSVehicleContainer::MSVehicleContainer(size_t capacity)
00073 : currentSize(0), array(capacity + 1, VehicleDepartureVector()) {}
00074
00075
00076 MSVehicleContainer::~MSVehicleContainer() {
00077
00078 }
00079
00080
00081 void
00082 MSVehicleContainer::add(MSVehicle *veh) {
00083
00084
00085 VehicleHeap::iterator i =
00086 find_if(array.begin()+1, array.begin()+currentSize+1, DepartFinder(veh->getDesiredDepart()));
00087 if (currentSize==0 || i==array.begin()+currentSize+1) {
00088
00089 VehicleDepartureVector newElem(veh->getDesiredDepart(), VehicleVector());
00090 newElem.second.push_back(veh);
00091 addReplacing(newElem);
00092 } else {
00093
00094 (*i).second.push_back(veh);
00095 }
00096 }
00097
00098
00099 void
00100 MSVehicleContainer::add(SUMOTime time, const VehicleVector &cont) {
00101 VehicleHeap::iterator j =
00102 find_if(array.begin()+1, array.begin()+currentSize+1,
00103 DepartFinder(time));
00104 if (currentSize==0 || j==array.begin()+currentSize+1) {
00105 VehicleDepartureVector newElem(time,
00106 VehicleVector(cont));
00107 addReplacing(newElem);
00108 } else {
00109 VehicleVector &stored = (*j).second;
00110 stored.reserve(stored.size() + cont.size());
00111 copy(cont.begin(), cont.end(), back_inserter(stored));
00112 }
00113 }
00114
00115
00116
00117 void
00118 MSVehicleContainer::addReplacing(const VehicleDepartureVector & x) {
00119 if (isFull()) {
00120 std::vector<VehicleDepartureVector> array2((array.size()-1)*2+1, VehicleDepartureVector());
00121 for (size_t i=array.size(); i-->0;) {
00122 assert(array2.size()>i);
00123 array2[i] = array[i];
00124 }
00125 array = array2;
00126 }
00127
00128
00129 int hole = ++currentSize;
00130 for (; hole > 1 && (x.first < array[ hole / 2 ].first); hole /= 2) {
00131 assert(array.size()>(size_t) hole);
00132 array[ hole ] = array[ hole / 2 ];
00133 }
00134 assert(array.size()>(size_t) hole);
00135 array[ hole ] = x;
00136 }
00137
00138
00139 bool
00140 MSVehicleContainer::anyWaitingFor(SUMOTime time) const {
00141 VehicleHeap::const_iterator j =
00142 find_if(array.begin()+1, array.begin()+currentSize+1, DepartFinder(time));
00143 return j!=array.begin()+currentSize+1;
00144 }
00145
00146
00147 const MSVehicleContainer::VehicleVector &
00148 MSVehicleContainer::top() {
00149 if (isEmpty())
00150 throw 1;
00151 assert(array.size()>1);
00152 return array[ 1 ].second;
00153 }
00154
00155
00156 SUMOTime
00157 MSVehicleContainer::topTime() const {
00158 if (isEmpty())
00159 throw 1;
00160 assert(array.size()>1);
00161 return array[ 1 ].first;
00162 }
00163
00164
00165 void
00166 MSVehicleContainer::pop()
00167
00168 {
00169 if (isEmpty())
00170 throw 1;
00171
00172 assert(array.size()>1);
00173 array[ 1 ] = array[ currentSize-- ];
00174 percolateDown(1);
00175 }
00176
00177
00178 bool
00179 MSVehicleContainer::isEmpty() const {
00180 return currentSize == 0;
00181 }
00182
00183
00184 bool
00185 MSVehicleContainer::isFull() const {
00186 return currentSize >= ((int) array.size()) - 1;
00187 }
00188
00189
00190 void
00191 MSVehicleContainer::percolateDown(int hole) {
00192 int child;
00193 assert(array.size()>(size_t)hole);
00194 VehicleDepartureVector tmp = array[ hole ];
00195
00196 for (; hole * 2 <= currentSize; hole = child) {
00197 child = hole * 2;
00198 if (child != currentSize && (array[ child + 1 ].first < array[ child ].first))
00199 child++;
00200 if ((array[ child ].first < tmp.first)) {
00201 assert(array.size()>(size_t) hole);
00202 array[ hole ] = array[ child ];
00203 } else
00204 break;
00205 }
00206 assert(array.size()>(size_t) hole);
00207 array[ hole ] = tmp;
00208 }
00209
00210
00211 size_t
00212 MSVehicleContainer::size() const {
00213 return currentSize;
00214 }
00215
00216
00217 void
00218 MSVehicleContainer::showArray() const {
00219 for (VehicleHeap::const_iterator i=array.begin()+1; i!=array.begin()+currentSize+1; ++i) {
00220 if (i!=array.begin()+1) {
00221 std::cout << ", ";
00222 }
00223 std::cout << (*i).first;
00224 }
00225 std::cout << std::endl << "-------------------------" << std::endl;
00226 }
00227
00228
00229 std::ostream &operator << (std::ostream &strm, MSVehicleContainer &cont) {
00230 strm << "------------------------------------" << std::endl;
00231 while (!cont.isEmpty()) {
00232 const MSVehicleContainer::VehicleVector &v = cont.top();
00233 for (MSVehicleContainer::VehicleVector::const_iterator i=v.begin(); i!=v.end(); ++i) {
00234 strm << (*i)->getDesiredDepart() << std::endl;
00235 }
00236 cont.pop();
00237 }
00238 return strm;
00239 }
00240
00241
00242
00243
00244