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    RODFRouteCont.h
11 /// @author  Daniel Krajzewicz
12 /// @author  Michael Behrisch
13 /// @date    Thu, 16.03.2006
14 /// @version $Id$
15 ///
16 // A container for routes
17 /****************************************************************************/
18 #ifndef RODFRouteCont_h
19 #define RODFRouteCont_h
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <vector>
28 #include <map>
29 #include <utils/common/UtilExceptions.h>
30 #include "RODFRouteDesc.h"
31 
32 
33 // ===========================================================================
34 // class declarations
35 // ===========================================================================
36 class RODFNet;
37 class OutputDevice;
38 
39 
40 // ===========================================================================
41 // class definitions
42 // ===========================================================================
43 /**
44  * @class RODFRouteCont
45  * @brief A container for DFROUTER-routes
46  *
47  * The route id is (re)set as soon as the route is added.
48  *
49  * As sometimes several routes can be used between two edges and have to be
50  *  identified, the number of routes connecting them is stored for each
51  *  edge pair "myConnectionOccurences" and the route is named using this
52  *  information, @see addRouteDesc.
53  *
54  * @see RODFRouteDesc
55  */
56 class RODFRouteCont {
57 public:
58     /// @brief Constructor
59     RODFRouteCont();
60 
61     /// @brief Destructor
62     ~RODFRouteCont();
63 
64 
65     /** @brief Adds a route to the container
66      *
67      * If the same route is already known, its "overallProb" is increased
68      *  by the value stored in the given route.
69      *
70      * An id for the route is generated if it is unset, yet. The id is
71      *  computed and set via "setID".
72      *
73      * @param[in] desc The route description to add
74      * @see setID
75      */
76     void addRouteDesc(RODFRouteDesc& desc);
77 
78 
79     /** @brief Removes the given route description from the container
80      *
81      * All routes are regarded as being same if they pass the same edges.
82      *  This is done via the "route_finder".
83      *
84      * @param[in] desc The route description to remove
85      * @return Whether the route was removed (a similar was found)
86      * @see RODFRouteCont::route_finder
87      */
88     bool removeRouteDesc(RODFRouteDesc& desc);
89 
90 
91     /** @brief Saves routes
92      *
93      * @param[in, out] saved The list of ids of routes that shall not be saved (were saved before)
94      * @param[in] prependix The prependix for route names
95      * @param[out] out The device the routes shall written to
96      * @return Whether at least one route was saved
97      * @exception IOError not yet implemented
98      */
99     bool save(std::vector<std::string>& saved,
100               const std::string& prependix, OutputDevice& out);
101 
102 
103     /** @brief Returns the container of stored routes
104      * @return The stored routes
105      */
get()106     std::vector<RODFRouteDesc>& get() {
107         return myRoutes;
108     }
109 
110 
111     /** @brief Sorts routes by their distance (length)
112      *
113      * Done using by_distance_sorter.
114      * @see RODFRouteCont::by_distance_sorter
115      */
116     void sortByDistance();
117 
118 
119     /** @brief Removes "illegal" routes
120      *
121      * "illegal" routes means edge combinations that shall not be passed.
122      *
123      * @param[in] illegals List of edge combinations that shall not be passed
124      * @todo Not used, yet
125      */
126     void removeIllegal(const std::vector<ROEdgeVector >& illegals);
127 
128 
129 protected:
130     /** @brief Computes and sets the id of a route
131      *
132      * The id is <FIRST_EDGE>_to_<LAST_EDGE>_<RUNNING> where <RUNNING>
133      *  is the number of routes which connect <FIRST_EDGE> and <LAST_EDGE>.
134      *
135      * @param[in] desc The route description to add
136      */
137     void setID(RODFRouteDesc& desc) const;
138 
139 
140     /** @brief A class for sorting route descriptions by their length */
141     class by_distance_sorter {
142     public:
143         /// @brief Constructor
by_distance_sorter()144         explicit by_distance_sorter() { }
145 
146         /// @brief Sorting function; compares RODFRouteDesc::distance2Last
operator()147         int operator()(const RODFRouteDesc& p1, const RODFRouteDesc& p2) {
148             return p1.distance2Last < p2.distance2Last;
149         }
150     };
151 
152 
153     /** @brief A class for finding a same route (one that passes the same edges) */
154     class route_finder {
155     public:
156         /** @brief onstructor
157          * @param[in] desc The route description to which a same shall be found
158          */
route_finder(const RODFRouteDesc & desc)159         explicit route_finder(const RODFRouteDesc& desc) : myDesc(desc) { }
160 
161         /**  @brief The comparing function; compares passed edges */
operator()162         bool operator()(const RODFRouteDesc& desc) {
163             return myDesc.edges2Pass == desc.edges2Pass;
164         }
165 
166     private:
167         /// @brief The route description for which a same shall be found
168         const RODFRouteDesc& myDesc;
169 
170     private:
171         /// @brief invalidated assignment operator
172         route_finder& operator=(const route_finder&);
173     };
174 
175 protected:
176     /// @brief Stored route descriptions
177     std::vector<RODFRouteDesc> myRoutes;
178 
179     /// @brief Counts how many routes connecting the key-edges were already stored
180     mutable std::map<std::pair<ROEdge*, ROEdge*>, int> myConnectionOccurences;
181 
182 
183 };
184 
185 
186 #endif
187 
188 /****************************************************************************/
189 
190