SUMO - Simulation of Urban MObility
ROVehicleCont.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00009 // A container for vehicles sorted by their departure time
00010 /****************************************************************************/
00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00013 /****************************************************************************/
00014 //
00015 //   This file is part of SUMO.
00016 //   SUMO is free software: you can redistribute it and/or modify
00017 //   it under the terms of the GNU General Public License as published by
00018 //   the Free Software Foundation, either version 3 of the License, or
00019 //   (at your option) any later version.
00020 //
00021 /****************************************************************************/
00022 
00023 
00024 // ===========================================================================
00025 // included modules
00026 // ===========================================================================
00027 #ifdef _MSC_VER
00028 #include <windows_config.h>
00029 #else
00030 #include <config.h>
00031 #endif
00032 
00033 #include <string>
00034 #include <utils/common/NamedObjectCont.h>
00035 #include <queue>
00036 #include "ROVehicle.h"
00037 #include "ROHelper.h"
00038 #include "ROVehicleCont.h"
00039 
00040 #ifdef CHECK_MEMORY_LEAKS
00041 #include <foreign/nvwa/debug_new.h>
00042 #endif // CHECK_MEMORY_LEAKS
00043 
00044 
00045 // ===========================================================================
00046 // method definitions
00047 // ===========================================================================
00048 ROVehicleCont::ROVehicleCont() {}
00049 
00050 
00051 ROVehicleCont::~ROVehicleCont() {}
00052 
00053 
00054 const ROVehicle*
00055 ROVehicleCont::getTopVehicle() const {
00056     if (size() == 0) {
00057         return 0;
00058     }
00059     return mySorted.top();
00060 }
00061 
00062 
00063 bool
00064 ROVehicleCont::add(const std::string& id, ROVehicle* item) {
00065     if (NamedObjectCont<ROVehicle*>::add(id, item)) {
00066         mySorted.push(item);
00067         return true;
00068     }
00069     return false;
00070 }
00071 
00072 
00073 void
00074 ROVehicleCont::clear() {
00075     mySorted = std::priority_queue<ROVehicle*, std::vector<ROVehicle*>, ROVehicleByDepartureComperator>();
00076     NamedObjectCont<ROVehicle*>::clear();
00077 }
00078 
00079 
00080 bool
00081 ROVehicleCont::erase(const std::string& id) {
00082     const ROVehicle* const topVeh = getTopVehicle();
00083     bool wasTop = topVeh != 0 && topVeh->getID() == id;
00084     if (!NamedObjectCont<ROVehicle*>::erase(id)) {
00085         return false;
00086     }
00087     if (wasTop) {
00088         mySorted.pop();
00089     } else {
00090         rebuildSorted();
00091     }
00092     return true;
00093 }
00094 
00095 
00096 void
00097 ROVehicleCont::rebuildSorted() {
00098     mySorted = std::priority_queue<ROVehicle*, std::vector<ROVehicle*>, ROVehicleByDepartureComperator>();
00099     std::map<std::string, ROVehicle*>::const_iterator i;
00100     const std::map<std::string, ROVehicle*> &mmap = getMyMap();
00101     for (i = mmap.begin(); i != mmap.end(); ++i) {
00102         mySorted.push((*i).second);
00103     }
00104 }
00105 
00106 
00107 /****************************************************************************/
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines