SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ROVehicleCont.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A container for vehicles sorted by their departure time
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <string>
35 #include <queue>
36 #include "ROVehicle.h"
37 #include "ROHelper.h"
38 #include "ROVehicleCont.h"
39 
40 #ifdef CHECK_MEMORY_LEAKS
41 #include <foreign/nvwa/debug_new.h>
42 #endif // CHECK_MEMORY_LEAKS
43 
44 
45 // ===========================================================================
46 // method definitions
47 // ===========================================================================
49 
50 
52 
53 
54 const ROVehicle*
56  if (size() == 0) {
57  return 0;
58  }
59  return mySorted.top();
60 }
61 
62 
63 bool
64 ROVehicleCont::add(const std::string& id, ROVehicle* item) {
65  if (NamedObjectCont<ROVehicle*>::add(id, item)) {
66  mySorted.push(item);
67  return true;
68  }
69  return false;
70 }
71 
72 
73 void
75  mySorted = std::priority_queue<ROVehicle*, std::vector<ROVehicle*>, ROVehicleByDepartureComperator>();
77 }
78 
79 
80 bool
81 ROVehicleCont::erase(const std::string& id) {
82  const ROVehicle* const topVeh = getTopVehicle();
83  bool wasTop = topVeh != 0 && topVeh->getID() == id;
85  return false;
86  }
87  if (wasTop) {
88  mySorted.pop();
89  } else {
90  rebuildSorted();
91  }
92  return true;
93 }
94 
95 
96 void
98  mySorted = std::priority_queue<ROVehicle*, std::vector<ROVehicle*>, ROVehicleByDepartureComperator>();
99  std::map<std::string, ROVehicle*>::const_iterator i;
100  const std::map<std::string, ROVehicle*>& mmap = getMyMap();
101  for (i = mmap.begin(); i != mmap.end(); ++i) {
102  mySorted.push((*i).second);
103  }
104 }
105 
106 
107 /****************************************************************************/
const std::string & getID() const
Returns the id of the vehicle.
Definition: ROVehicle.h:99
void clear()
Deletes all vehicles stored; clears the lists.
A map of named object pointers.
virtual bool add(const std::string &id, ROVehicle *item)
Adds a vehicle to the container.
void clear()
Removes all items from the container (deletes them, too)
void rebuildSorted()
Rebuild the internal, sorted list.
A vehicle as used by router.
Definition: ROVehicle.h:57
std::priority_queue< ROVehicle *, std::vector< ROVehicle * >, ROVehicleByDepartureComperator > mySorted
The sorted vehicle list.
const ROVehicle * getTopVehicle() const
Returns the vehicle that departs most early.
unsigned int size() const
Returns the number of items within the container.
bool erase(const std::string &id)
Tries to remove (and delete) the named vehicle.
const IDMap & getMyMap() const
ROVehicleCont()
Constructor.
A function for sorting vehicles by their departure time.
Definition: ROHelper.h:50
~ROVehicleCont()
Destructor.