Skip to content

Commit

Permalink
Network: the set_weights and set_neighbours functions, now, also work…
Browse files Browse the repository at this point in the history
… with std::span
  • Loading branch information
MSallermann committed Mar 8, 2024
1 parent 19f7ab0 commit 1dd9ab4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
8 changes: 3 additions & 5 deletions include/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,18 @@ class Network

std::span<const size_t> get_neighbours( std::size_t agent_idx ) const;
std::span<size_t> get_neighbours( std::size_t agent_idx );

std::span<const WeightT> get_weights( std::size_t agent_idx ) const;
std::span<WeightT> get_weights( std::size_t agent_idx );

std::size_t get_n_edges( std::size_t agent_idx ) const;

void set_weights( std::size_t agent_idx, const std::vector<WeightT> & weights );
void set_weights( std::size_t agent_idx, std::span<const WeightT> weights );

void set_neighbours_and_weights(
std::size_t agent_idx, const std::vector<size_t> & buffer_neighbours, const WeightT & weight );
std::size_t agent_idx, std::span<const size_t> buffer_neighbours, const WeightT & weight );

void set_neighbours_and_weights(
std::size_t agent_idx, const std::vector<size_t> & buffer_neighbours,
const std::vector<WeightT> & buffer_weights );
std::size_t agent_idx, std::span<const size_t> buffer_neighbours, std::span<const WeightT> buffer_weights );

void push_back_neighbour_and_weight( size_t i, size_t j, WeightT w );

Expand Down
23 changes: 10 additions & 13 deletions src/network.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "network.hpp"
#include "connectivity.hpp"
#include <fmt/format.h>
#include <algorithm>
#include <cstddef>
#include <stdexcept>

Expand Down Expand Up @@ -37,20 +38,16 @@ std::span<size_t> Seldon::Network::get_neighbours( std::size_t agent_idx )
}

void Seldon::Network::set_neighbours_and_weights(
std::size_t agent_idx, const std::vector<size_t> & buffer_neighbours, const WeightT & weight )
std::size_t agent_idx, std::span<const size_t> buffer_neighbours, const WeightT & weight )
{
neighbour_list[agent_idx] = buffer_neighbours;

neighbour_list[agent_idx].assign( buffer_neighbours.begin(), buffer_neighbours.end() );
weight_list[agent_idx].resize( buffer_neighbours.size() );
for( auto & w : weight_list[agent_idx] )
{
w = weight;
}
std::fill( weight_list[agent_idx].begin(), weight_list[agent_idx].end(), weight );
}

void Seldon::Network::set_neighbours_and_weights(
std::size_t agent_idx, const std::vector<size_t> & buffer_neighbours,
const std::vector<Seldon::Network::WeightT> & buffer_weights )
std::size_t agent_idx, std::span<const size_t> buffer_neighbours,
std::span<const Seldon::Network::WeightT> buffer_weights )
{
if( buffer_neighbours.size() != buffer_weights.size() )
[[unlikely]]
Expand All @@ -59,8 +56,8 @@ void Seldon::Network::set_neighbours_and_weights(
"Network::set_neighbours_and_weights: both buffers need to have the same length!" );
}

neighbour_list[agent_idx] = buffer_neighbours;
weight_list[agent_idx] = buffer_weights;
neighbour_list[agent_idx].assign( buffer_neighbours.begin(), buffer_neighbours.end() );
weight_list[agent_idx].assign( buffer_weights.begin(), buffer_weights.end() );
}

void Seldon::Network::push_back_neighbour_and_weight( size_t i, size_t j, WeightT w )
Expand All @@ -84,14 +81,14 @@ std::size_t Seldon::Network::get_n_edges( std::size_t agent_idx ) const
return neighbour_list[agent_idx].size();
}

void Seldon::Network::set_weights( std::size_t agent_idx, const std::vector<Seldon::Network::WeightT> & weights )
void Seldon::Network::set_weights( std::size_t agent_idx, std::span<const Seldon::Network::WeightT> weights )
{
if( neighbour_list[agent_idx].size() != weights.size() )
[[unlikely]]
{
throw std::runtime_error( "Network::set_weights: tried to set weights of the wrong size!" );
}
weight_list[agent_idx] = weights;
weight_list[agent_idx].assign( weights.begin(), weights.end() );
}

void Seldon::Network::transpose()
Expand Down

0 comments on commit 1dd9ab4

Please sign in to comment.