Skip to content

Commit

Permalink
network refactor: Removed agents from class
Browse files Browse the repository at this point in the history
Refactored so that NetworkBase, UndirectedNetwork and DirectedNetwork
are not templated on the agent type. The network also does not contain
the agents.
  • Loading branch information
amritagos committed May 17, 2024
1 parent ba51a0f commit 4f697b4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 30 deletions.
9 changes: 3 additions & 6 deletions graph_lib/include/directed_network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,20 @@ namespace Graph {
Note: switch is equivalent to toggle + transpose, but much cheaper!
*/
template <typename AgentType, typename WeightType = double>
class DirectedNetwork : public NetworkBase<AgentType, WeightType> {
class DirectedNetwork : public NetworkBase<WeightType> {
public:
enum class EdgeDirection { Incoming, Outgoing };

using WeightT = WeightType;
using AgentT = AgentType;

DirectedNetwork() = default;

DirectedNetwork(size_t n_agents) : NetworkBase<AgentT>(n_agents) {}

DirectedNetwork(std::vector<AgentT> agents) : NetworkBase<AgentT>(agents) {}
DirectedNetwork(size_t n_agents) : NetworkBase<>(n_agents) {}

DirectedNetwork(std::vector<std::vector<size_t>> &&neighbour_list,
std::vector<std::vector<WeightT>> &&weight_list,
EdgeDirection direction)
: NetworkBase<AgentT>(std::move(neighbour_list), std::move(weight_list)),
: NetworkBase<>(std::move(neighbour_list), std::move(weight_list)),
_direction(direction) {}

/*
Expand Down
21 changes: 5 additions & 16 deletions graph_lib/include/network_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ namespace Graph {
/*
An abstract base class for undirected and directed networks.
*/
template <typename AgentType, typename WeightType = double> class NetworkBase {
template <typename WeightType = double> class NetworkBase {
public:
using WeightT = WeightType;
using AgentT = AgentType;
// @TODO: Make this private later
std::vector<AgentT> agents{}; // List of agents of type AgentType

protected:
std::vector<std::vector<size_t>>
neighbour_list{}; // Neighbour list for the connections
Expand All @@ -30,31 +28,22 @@ template <typename AgentType, typename WeightType = double> class NetworkBase {
NetworkBase() = default;

NetworkBase(size_t n_agents)
: agents(std::vector<AgentT>(n_agents)),
neighbour_list(
: neighbour_list(
std::vector<std::vector<size_t>>(n_agents, std::vector<size_t>{})),
weight_list(std::vector<std::vector<WeightT>>(n_agents,
std::vector<WeightT>{})) {
}

NetworkBase(std::vector<AgentT> agents)
: agents(agents), neighbour_list(std::vector<std::vector<size_t>>(
agents.size(), std::vector<size_t>{})),
weight_list(std::vector<std::vector<WeightT>>(agents.size(),
std::vector<WeightT>{})) {
}

NetworkBase(std::vector<std::vector<size_t>> &&neighbour_list,
std::vector<std::vector<WeightT>> &&weight_list)
: agents(std::vector<AgentT>(neighbour_list.size())),
neighbour_list(neighbour_list), weight_list(weight_list) {}
: neighbour_list(neighbour_list), weight_list(weight_list) {}

virtual ~NetworkBase() = default;

/*
Gives the total number of nodes in the network
*/
[[nodiscard]] std::size_t n_agents() const { return agents.size(); }
[[nodiscard]] std::size_t n_agents() const { return neighbour_list.size(); }

/*
Gives the number of edges connected to agent_idx
Expand Down
13 changes: 5 additions & 8 deletions graph_lib/include/undirected_network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@ namespace Graph {
/*
A class that represents an undirected graph using adjacency lists.
*/
template <typename AgentType, typename WeightType = double>
class UndirectedNetwork : public NetworkBase<AgentType, WeightType> {
template <typename WeightType = double>
class UndirectedNetwork : public NetworkBase<WeightType> {
public:
using WeightT = WeightType;
using AgentT = AgentType;

UndirectedNetwork() = default;

UndirectedNetwork(size_t n_agents) : NetworkBase<AgentT>(n_agents) {}

UndirectedNetwork(std::vector<AgentT> agents) : NetworkBase<AgentT>(agents) {}
UndirectedNetwork(size_t n_agents) : NetworkBase<>(n_agents) {}

UndirectedNetwork(std::vector<std::vector<size_t>> &&neighbour_list,
std::vector<std::vector<WeightT>> &&weight_list)
: NetworkBase<AgentT>(std::move(neighbour_list), std::move(weight_list)) {
}
: Graph::NetworkBase<>(std::move(neighbour_list),
std::move(weight_list)) {}

/*
Gives the number of edges connected to agent_idx
Expand Down

0 comments on commit 4f697b4

Please sign in to comment.