SUMO - Simulation of Urban MObility

MSVehicleContainer.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // vehicles sorted by their departures
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright (C) 2001-2011 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 
00020 
00021 // ===========================================================================
00022 // included modules
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 <iterator>
00033 #include "MSVehicle.h"
00034 #include "MSVehicleContainer.h"
00035 
00036 #ifdef CHECK_MEMORY_LEAKS
00037 #include <foreign/nvwa/debug_new.h>
00038 #endif // CHECK_MEMORY_LEAKS
00039 
00040 
00041 // ===========================================================================
00042 // method definitions
00043 // ===========================================================================
00044 /* -------------------------------------------------------------------------
00045  * methods from MSVehicleContainer::VehicleDepartureVectorSortCrit
00046  * ----------------------------------------------------------------------- */
00047 bool
00048 MSVehicleContainer::VehicleDepartureVectorSortCrit::operator()
00049 (const VehicleDepartureVector& e1, const VehicleDepartureVector& e2) const {
00050     return e1.first < e2.first;
00051 }
00052 
00053 
00054 
00055 /* -------------------------------------------------------------------------
00056  * methods from MSVehicleContainer::DepartFinder
00057  * ----------------------------------------------------------------------- */
00058 MSVehicleContainer::DepartFinder::DepartFinder(SUMOTime time)
00059     : myTime(time) {}
00060 
00061 
00062 bool
00063 MSVehicleContainer::DepartFinder::operator()
00064 (const VehicleDepartureVector& e) const {
00065     return myTime+DELTA_T > e.first && myTime<=e.first;
00066 }
00067 
00068 
00069 
00070 /* -------------------------------------------------------------------------
00071  * methods from MSVehicleContainer
00072  * ----------------------------------------------------------------------- */
00073 MSVehicleContainer::MSVehicleContainer(size_t capacity)
00074     : currentSize(0), array(capacity + 1, VehicleDepartureVector()) {}
00075 
00076 
00077 MSVehicleContainer::~MSVehicleContainer() {
00078     // !!! vehicles are deleted in MSVehicle
00079 }
00080 
00081 
00082 void
00083 MSVehicleContainer::add(SUMOVehicle* veh) {
00084     // check whether a new item shall be added or the vehicle may be
00085     //  added to an existing list
00086     VehicleHeap::iterator i =
00087         find_if(array.begin()+1, array.begin()+currentSize+1, DepartFinder(veh->getParameter().depart));
00088     if (currentSize==0 || i==array.begin()+currentSize+1) {
00089         // a new heap-item is necessary
00090         VehicleDepartureVector newElem(veh->getParameter().depart, VehicleVector());
00091         newElem.second.push_back(veh);
00092         addReplacing(newElem);
00093     } else {
00094         // add vehicle to an existing heap-item
00095         (*i).second.push_back(veh);
00096     }
00097 }
00098 
00099 
00100 void
00101 MSVehicleContainer::add(SUMOTime time, const VehicleVector& cont) {
00102     VehicleHeap::iterator j =
00103         find_if(array.begin()+1, array.begin()+currentSize+1,
00104                 DepartFinder(time));
00105     if (currentSize==0 || j==array.begin()+currentSize+1) {
00106         VehicleDepartureVector newElem(time,
00107                                        VehicleVector(cont));
00108         addReplacing(newElem);
00109     } else {
00110         VehicleVector& stored = (*j).second;
00111         stored.reserve(stored.size() + cont.size());
00112         copy(cont.begin(), cont.end(), back_inserter(stored));
00113     }
00114 }
00115 
00116 
00117 
00118 void
00119 MSVehicleContainer::addReplacing(const VehicleDepartureVector& x) {
00120     if (isFull()) {
00121         std::vector<VehicleDepartureVector> array2((array.size()-1)*2+1, VehicleDepartureVector());
00122         for (size_t i=array.size(); i-->0;) {
00123             assert(array2.size()>i);
00124             array2[i] = array[i];
00125         }
00126         array = array2;
00127     }
00128 
00129     // Percolate up
00130     int hole = ++currentSize;
00131     for (; hole > 1 && (x.first < array[ hole / 2 ].first); hole /= 2) {
00132         assert(array.size()>(size_t) hole);
00133         array[ hole ] = array[ hole / 2 ];
00134     }
00135     assert(array.size()>(size_t) hole);
00136     array[ hole ] = x;
00137 }
00138 
00139 
00140 bool
00141 MSVehicleContainer::anyWaitingFor(SUMOTime time) const {
00142     VehicleHeap::const_iterator j =
00143         find_if(array.begin()+1, array.begin()+currentSize+1, DepartFinder(time));
00144     return j!=array.begin()+currentSize+1;
00145 }
00146 
00147 
00148 const MSVehicleContainer::VehicleVector&
00149 MSVehicleContainer::top() {
00150     if (isEmpty()) {
00151         throw 1;    
00152     }
00153     assert(array.size()>1);
00154     return array[ 1 ].second;
00155 }
00156 
00157 
00158 SUMOTime
00159 MSVehicleContainer::topTime() const {
00160     if (isEmpty()) {
00161         throw 1;    
00162     }
00163     assert(array.size()>1);
00164     return array[ 1 ].first;
00165 }
00166 
00167 
00168 void
00169 MSVehicleContainer::pop()
00170 
00171 {
00172     if (isEmpty()) {
00173         throw 1;    
00174     }
00175 
00176     assert(array.size()>1);
00177     array[ 1 ] = array[ currentSize-- ];
00178     percolateDown(1);
00179 }
00180 
00181 
00182 bool
00183 MSVehicleContainer::isEmpty() const {
00184     return currentSize == 0;
00185 }
00186 
00187 
00188 bool
00189 MSVehicleContainer::isFull() const {
00190     return currentSize >= ((int) array.size()) - 1;
00191 }
00192 
00193 
00194 void
00195 MSVehicleContainer::percolateDown(int hole) {
00196     int child;
00197     assert(array.size()>(size_t)hole);
00198     VehicleDepartureVector tmp = array[ hole ];
00199 
00200     for (; hole * 2 <= currentSize; hole = child) {
00201         child = hole * 2;
00202         if (child != currentSize && (array[ child + 1 ].first < array[ child ].first)) {
00203             child++;
00204         }
00205         if ((array[ child ].first < tmp.first)) {
00206             assert(array.size()>(size_t) hole);
00207             array[ hole ] = array[ child ];
00208         } else {
00209             break;
00210         }
00211     }
00212     assert(array.size()>(size_t) hole);
00213     array[ hole ] = tmp;
00214 }
00215 
00216 
00217 size_t
00218 MSVehicleContainer::size() const {
00219     return currentSize;
00220 }
00221 
00222 
00223 void
00224 MSVehicleContainer::showArray() const {
00225     for (VehicleHeap::const_iterator i=array.begin()+1; i!=array.begin()+currentSize+1; ++i) {
00226         if (i!=array.begin()+1) {
00227             std::cout << ", ";
00228         }
00229         std::cout << (*i).first;
00230     }
00231     std::cout << std::endl << "-------------------------" << std::endl;
00232 }
00233 
00234 
00235 std::ostream& operator << (std::ostream& strm, MSVehicleContainer& cont) {
00236     strm << "------------------------------------" << std::endl;
00237     while (!cont.isEmpty()) {
00238         const MSVehicleContainer::VehicleVector& v = cont.top();
00239         for (MSVehicleContainer::VehicleVector::const_iterator i=v.begin(); i!=v.end(); ++i) {
00240             strm << (*i)->getParameter().depart << std::endl;
00241         }
00242         cont.pop();
00243     }
00244     return strm;
00245 }
00246 
00247 
00248 
00249 /****************************************************************************/
00250 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines