1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
10 /// @file    MSVehicleContainer.h
11 /// @author  Christian Roessel
12 /// @author  Daniel Krajzewicz
13 /// @author  Michael Behrisch
14 /// @date    Mon, 12 Mar 2001
15 /// @version $Id$
16 ///
17 // vehicles sorted by their departures
18 /****************************************************************************/
19 #ifndef MSVehicleContainer_h
20 #define MSVehicleContainer_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <vector>
29 #include <iostream>
30 
31 
32 // ===========================================================================
33 // class declarations
34 // ===========================================================================
35 class MSVehicle;
36 
37 
38 // ===========================================================================
39 // class definitions
40 // ===========================================================================
41 /**
42  * @class MSVehicleContainer
43  * A storage for vehicles, mainly used by the vehicle/route loading structures
44  * and the insertion control. Stores vehicles in a heap of vehiclevector/departure-
45  * pairs.
46  */
47 class MSVehicleContainer {
48 public:
49     /// definition of a list of vehicles which have the same departure time
50     typedef std::vector<SUMOVehicle*> VehicleVector;
51 
52     /** definition of a structure storing the departure time and a list
53         of vehicles leaving at this time */
54     typedef std::pair<SUMOTime, VehicleVector> VehicleDepartureVector;
55 
56 public:
57     /// Constructor
58     MSVehicleContainer(int capacity = 10);
59 
60     /// Destructor
61     ~MSVehicleContainer();
62 
63     /// Adds a single vehicle
64     void add(SUMOVehicle* veh);
65 
66     /// Removes a single vehicle
67     void remove(SUMOVehicle* veh);
68 
69     /// Adds a container with vehicles departing at the given time
70     void add(SUMOTime time, const VehicleVector& cont);
71 
72     /// Returns the information whether any vehicles want to depart before the given time
73     bool anyWaitingBefore(SUMOTime time) const;
74 
75     /// Returns the uppermost vehicle vector
76     const VehicleVector& top();
77 
78     /// Returns the time the uppermost vehicle vector is assigned to
79     SUMOTime topTime() const;
80 
81     /// Removes the uppermost vehicle vector
82     void pop();
83 
84     /// Returns the information whether the container is empty
85     bool isEmpty() const;
86 
87     /// Returns the size of the container
88     int size() const;
89 
90     /// Prints the container (the departure times)
91     void showArray() const;
92 
93     /// Prints the contents of the container
94     friend std::ostream& operator << (std::ostream& strm,
95                                       MSVehicleContainer& cont);
96 
97 private:
98     /** @brief Replaces the existing single departure time vector by the one given
99     */
100     void addReplacing(const VehicleDepartureVector& cont);
101 
102     /** Returns the information whether the container must be extended */
103     bool isFull() const;
104 
105     /// Sort-criterion for vehicle departure lists
106     class VehicleDepartureVectorSortCrit {
107     public:
108         /// comparison operator
109         bool operator()(const VehicleDepartureVector& e1,
110                         const VehicleDepartureVector& e2) const;
111     };
112 
113     /// Searches for the VehicleDepartureVector with the wished depart
114     class DepartFinder {
115     public:
116         /// constructor
117         explicit DepartFinder(SUMOTime time);
118 
119         /// comparison operator
120         bool operator()(const VehicleDepartureVector& e) const;
121 
122     private:
123         /// the searched departure time
124         SUMOTime myTime;
125     };
126 
127     /// Number of elements in heap
128     int currentSize;
129 
130     /// Definition of the heap type
131     typedef std::vector<VehicleDepartureVector> VehicleHeap;
132 
133     /// The vehicle vector heap
134     VehicleHeap array;
135 
136     /// Moves the elements down
137     void percolateDown(int hole);
138 
139 };
140 
141 
142 #endif
143 
144 /****************************************************************************/
145 
146