Skip to content

Commit

Permalink
Refactor getters and setters (#163)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Simone Balducci <[email protected]>
Co-authored-by: Simone Balducci <[email protected]>
  • Loading branch information
3 people authored Mar 27, 2024
1 parent e8bfe15 commit 5b6d875
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 402 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.16.0)

project(dms VERSION 1.2.2 LANGUAGES CXX)
project(dms VERSION 1.2.3 LANGUAGES CXX)

# set the C++ standard
set(CMAKE_CXX_STANDARD 20)
Expand Down
103 changes: 12 additions & 91 deletions src/dsm/headers/Agent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ namespace dsm {
Agent(Id id, Id itineraryId, Id srcNodeId);
/// @brief Set the street occupied by the agent
/// @param streetId The id of the street currently occupied by the agent
void setStreetId(Id streetId);
void setStreetId(Id streetId) { m_streetId = streetId; }
/// @brief Set the source node id of the agent
/// @param srcNodeId The id of the source node of the agent
void setSourceNodeId(Id srcNodeId);
void setSourceNodeId(Id srcNodeId) { m_srcNodeId = srcNodeId; }
/// @brief Set the agent's itinerary
/// @param itineraryId The agent's itinerary
void setItineraryId(Id itineraryId);
void setItineraryId(Id itineraryId) { m_itineraryId = itineraryId; }
/// @brief Set the agent's speed
/// @param speed, The agent's speed
/// @throw std::invalid_argument, if speed is negative
Expand All @@ -75,7 +75,7 @@ namespace dsm {
/// @throw std::underflow_error, if delay has reached its minimum value
void decrementDelay();
/// @brief Increment the agent's distance by its speed * 1 second
void incrementDistance();
void incrementDistance() { m_distance += m_speed; }
/// @brief Increment the agent's distance by a given value
/// @param distance The value to increment the agent's distance byù
/// @throw std::invalid_argument, if distance is negative
Expand All @@ -92,28 +92,28 @@ namespace dsm {

/// @brief Get the agent's id
/// @return The agent's id
Id id() const;
Id id() const { return m_id; }
/// @brief Get the agent's itinerary
/// @return The agent's itinerary
Id itineraryId() const;
Id itineraryId() const { return m_itineraryId; }
/// @brief Get the id of the street currently occupied by the agent
/// @return The id of the street currently occupied by the agent
std::optional<Id> streetId() const;
std::optional<Id> streetId() const { return m_streetId; }
/// @brief Get the id of the source node of the agent
/// @return The id of the source node of the agent
std::optional<Id> srcNodeId() const;
std::optional<Id> srcNodeId() const { return m_srcNodeId; }
/// @brief Get the agent's speed
/// @return The agent's speed
double speed() const;
double speed() const { return m_speed; }
/// @brief Get the agent's delay
/// @return The agent's delay
Delay delay() const;
Delay delay() const { return m_delay; }
/// @brief Get the agent's travelled distance
/// @return The agent's travelled distance
double distance() const;
double distance() const { return m_distance; }
/// @brief Get the agent's travel time
/// @return The agent's travel time
unsigned int time() const;
unsigned int time() const { return m_time; }
};

template <typename Id, typename Size, typename Delay>
Expand All @@ -139,25 +139,6 @@ namespace dsm {
m_distance{0.},
m_time{0} {}

template <typename Id, typename Size, typename Delay>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size> &&
is_numeric_v<Delay>)
void Agent<Id, Size, Delay>::setStreetId(Id streetId) {
m_streetId = streetId;
}
template <typename Id, typename Size, typename Delay>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size> &&
is_numeric_v<Delay>)
void Agent<Id, Size, Delay>::setSourceNodeId(Id srcNodeId) {
m_srcNodeId = srcNodeId;
}
template <typename Id, typename Size, typename Delay>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size> &&
is_numeric_v<Delay>)
void Agent<Id, Size, Delay>::setItineraryId(Id itineraryId) {
m_itineraryId = itineraryId;
}

template <typename Id, typename Size, typename Delay>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size> &&
is_numeric_v<Delay>)
Expand Down Expand Up @@ -195,12 +176,6 @@ namespace dsm {
--m_delay;
}

template <typename Id, typename Size, typename Delay>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size> &&
is_numeric_v<Delay>)
void Agent<Id, Size, Delay>::incrementDistance() {
m_distance += m_speed; // actually m_speed * 1 second
}
template <typename Id, typename Size, typename Delay>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size> &&
is_numeric_v<Delay>)
Expand Down Expand Up @@ -229,60 +204,6 @@ namespace dsm {
}
m_time += time;
}

template <typename Id, typename Size, typename Delay>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size> &&
is_numeric_v<Delay>)
Id Agent<Id, Size, Delay>::id() const {
return m_id;
}

template <typename Id, typename Size, typename Delay>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size> &&
is_numeric_v<Delay>)
std::optional<Id> Agent<Id, Size, Delay>::streetId() const {
return m_streetId;
}
template <typename Id, typename Size, typename Delay>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size> &&
is_numeric_v<Delay>)
std::optional<Id> Agent<Id, Size, Delay>::srcNodeId() const {
return m_srcNodeId;
}
template <typename Id, typename Size, typename Delay>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size> &&
is_numeric_v<Delay>)
double Agent<Id, Size, Delay>::speed() const {
return m_speed;
}

template <typename Id, typename Size, typename Delay>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size> &&
is_numeric_v<Delay>)
Delay Agent<Id, Size, Delay>::delay() const {
return m_delay;
}

template <typename Id, typename Size, typename Delay>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size> &&
is_numeric_v<Delay>)
double Agent<Id, Size, Delay>::distance() const {
return m_distance;
}

template <typename Id, typename Size, typename Delay>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size> &&
is_numeric_v<Delay>)
unsigned int Agent<Id, Size, Delay>::time() const {
return m_time;
}

template <typename Id, typename Size, typename Delay>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size> &&
is_numeric_v<Delay>)
Id Agent<Id, Size, Delay>::itineraryId() const {
return m_itineraryId;
}
}; // namespace dsm

#endif
51 changes: 10 additions & 41 deletions src/dsm/headers/Dynamics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ namespace dsm {
void setItineraries(std::span<Itinerary<Id>> itineraries);
/// @brief Set the seed for the graph's random number generator
/// @param seed The seed
void setSeed(unsigned int seed);
void setSeed(unsigned int seed) { m_generator.seed(seed); };
/// @brief Set the minim speed rateo, i.e. the minim speed with respect to the speed limit
/// @param minSpeedRateo The minim speed rateo
/// @throw std::invalid_argument If the minim speed rateo is not between 0 and 1
Expand Down Expand Up @@ -137,13 +137,17 @@ namespace dsm {
const Graph<Id, Size>& graph() const { return m_graph; };
/// @brief Get the itineraries
/// @return const std::unordered_map<Id, Itinerary<Id>>&, The itineraries
const std::unordered_map<Id, std::unique_ptr<Itinerary<Id>>>& itineraries() const;
const std::unordered_map<Id, std::unique_ptr<Itinerary<Id>>>& itineraries() const {
return m_itineraries;
}
/// @brief Get the agents
/// @return const std::unordered_map<Id, Agent<Id>>&, The agents
const std::map<Id, std::unique_ptr<Agent<Id, Size, Delay>>>& agents() const;
const std::map<Id, std::unique_ptr<Agent<Id, Size, Delay>>>& agents() const {
return m_agents;
}
/// @brief Get the time
/// @return TimePoint The time
TimePoint time() const;
TimePoint time() const { return m_time; }

/// @brief Add an agent to the simulation
/// @param agent The agent
Expand Down Expand Up @@ -388,13 +392,6 @@ namespace dsm {
});
}

template <typename Id, typename Size, typename Delay>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size> &&
is_numeric_v<Delay>)
void Dynamics<Id, Size, Delay>::setSeed(unsigned int seed) {
m_generator.seed(seed);
}

template <typename Id, typename Size, typename Delay>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size> &&
is_numeric_v<Delay>)
Expand Down Expand Up @@ -483,29 +480,6 @@ namespace dsm {
++this->m_time;
}

template <typename Id, typename Size, typename Delay>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size> &&
is_numeric_v<Delay>)
const std::unordered_map<Id, std::unique_ptr<Itinerary<Id>>>&
Dynamics<Id, Size, Delay>::itineraries() const {
return m_itineraries;
}

template <typename Id, typename Size, typename Delay>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size> &&
is_numeric_v<Delay>)
const std::map<Id, std::unique_ptr<Agent<Id, Size, Delay>>>&
Dynamics<Id, Size, Delay>::agents() const {
return this->m_agents;
}

template <typename Id, typename Size, typename Delay>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size> &&
is_numeric_v<Delay>)
TimePoint Dynamics<Id, Size, Delay>::time() const {
return m_time;
}

template <typename Id, typename Size, typename Delay>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size> &&
is_numeric_v<Delay>)
Expand Down Expand Up @@ -818,7 +792,7 @@ namespace dsm {
FirstOrderDynamics() = delete;
/// @brief Construct a new First Order Dynamics object
/// @param graph, The graph representing the network
FirstOrderDynamics(Graph<Id, Size>& graph);
FirstOrderDynamics(Graph<Id, Size>& graph) : Dynamics<Id, Size, Delay>(graph){};
/// @brief Set the speed of an agent
/// @param agentId The id of the agent
/// @throw std::invalid_argument, If the agent is not found
Expand All @@ -839,12 +813,6 @@ namespace dsm {
Measurement<double> streetMeanSpeed(double threshold, bool above) const override;
};

template <typename Id, typename Size, typename Delay>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size> &&
std::unsigned_integral<Delay>)
FirstOrderDynamics<Id, Size, Delay>::FirstOrderDynamics(Graph<Id, Size>& graph)
: Dynamics<Id, Size, Delay>(graph) {}

template <typename Id, typename Size, typename Delay>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size> &&
std::unsigned_integral<Delay>)
Expand Down Expand Up @@ -876,6 +844,7 @@ namespace dsm {
++n;
}
}

for (const auto& [angle, agentId] :
this->m_graph.nodeSet().at(street->nodePair().second)->agents()) {
const auto& agent{this->m_agents.at(agentId)};
Expand Down
48 changes: 11 additions & 37 deletions src/dsm/headers/Graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,25 @@ namespace dsm {

/// @brief Get the graph's adjacency matrix
/// @return A std::shared_ptr to the graph's adjacency matrix
const SparseMatrix<Id, bool>& adjMatrix() const;
const SparseMatrix<Id, bool>& adjMatrix() const { return m_adjacency; }
/// @brief Get the graph's node map
/// @return A std::unordered_map containing the graph's nodes
const std::unordered_map<Id, std::unique_ptr<Node<Id, Size>>>& nodeSet() const;
const std::unordered_map<Id, std::unique_ptr<Node<Id, Size>>>& nodeSet() const {
return m_nodes;
}
/// @brief Get the graph's node map
/// @return A std::unordered_map containing the graph's nodes
std::unordered_map<Id, std::unique_ptr<Node<Id, Size>>>& nodeSet();
std::unordered_map<Id, std::unique_ptr<Node<Id, Size>>>& nodeSet() { return m_nodes; }
/// @brief Get the graph's street map
/// @return A std::unordered_map containing the graph's streets
const std::unordered_map<Id, std::unique_ptr<Street<Id, Size>>>& streetSet() const;
const std::unordered_map<Id, std::unique_ptr<Street<Id, Size>>>& streetSet() const {
return m_streets;
}
/// @brief Get the graph's street map
/// @return A std::unordered_map containing the graph's streets
std::unordered_map<Id, std::unique_ptr<Street<Id, Size>>>& streetSet();
std::unordered_map<Id, std::unique_ptr<Street<Id, Size>>>& streetSet() {
return m_streets;
}
/// @brief Get a street from the graph
/// @param source The source node
/// @param destination The destination node
Expand Down Expand Up @@ -648,38 +654,6 @@ namespace dsm {
addStreets(std::forward<Tn>(streets)...);
}

template <typename Id, typename Size>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size>)
const SparseMatrix<Id, bool>& Graph<Id, Size>::adjMatrix() const {
return m_adjacency;
}

template <typename Id, typename Size>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size>)
const std::unordered_map<Id, std::unique_ptr<Node<Id, Size>>>&
Graph<Id, Size>::nodeSet() const {
return m_nodes;
}

template <typename Id, typename Size>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size>)
std::unordered_map<Id, std::unique_ptr<Node<Id, Size>>>& Graph<Id, Size>::nodeSet() {
return m_nodes;
}

template <typename Id, typename Size>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size>)
const std::unordered_map<Id, std::unique_ptr<Street<Id, Size>>>&
Graph<Id, Size>::streetSet() const {
return m_streets;
}

template <typename Id, typename Size>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size>)
std::unordered_map<Id, std::unique_ptr<Street<Id, Size>>>& Graph<Id, Size>::streetSet() {
return m_streets;
}

template <typename Id, typename Size>
requires(std::unsigned_integral<Id> && std::unsigned_integral<Size>)
const std::unique_ptr<Street<Id, Size>>* Graph<Id, Size>::street(Id source,
Expand Down
23 changes: 3 additions & 20 deletions src/dsm/headers/Itinerary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ namespace dsm {

/// @brief Get the itinerary's id
/// @return Id, The itinerary's id
Id id() const;
Id id() const { return m_id; }
/// @brief Get the itinerary's destination
/// @return Id, The itinerary's destination
Id destination() const;
Id destination() const { return m_destination; }
/// @brief Get the itinerary's path
/// @return SparseMatrix<Id, bool>, An adjacency matrix made by a SparseMatrix representing the
/// itinerary's path
const SparseMatrix<Id, bool>& path() const;
const SparseMatrix<Id, bool>& path() const { return m_path; }
};

template <typename Id>
Expand Down Expand Up @@ -85,23 +85,6 @@ namespace dsm {
}
m_path = std::move(path);
}

template <typename Id>
requires(std::unsigned_integral<Id>)
Id Itinerary<Id>::id() const {
return m_id;
}
template <typename Id>
requires(std::unsigned_integral<Id>)
Id Itinerary<Id>::destination() const {
return m_destination;
}
template <typename Id>
requires(std::unsigned_integral<Id>)
const SparseMatrix<Id, bool>& Itinerary<Id>::path() const {
return m_path;
}

}; // namespace dsm

#endif
Loading

0 comments on commit 5b6d875

Please sign in to comment.