From 1dd9ab48cd92d6ceea710588af3b70c72f4c5951 Mon Sep 17 00:00:00 2001 From: Moritz Sallermann Date: Fri, 8 Mar 2024 13:59:54 +0000 Subject: [PATCH] Network: the set_weights and set_neighbours functions, now, also work with std::span --- include/network.hpp | 8 +++----- src/network.cpp | 23 ++++++++++------------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/include/network.hpp b/include/network.hpp index 3597b8b..63c4d63 100644 --- a/include/network.hpp +++ b/include/network.hpp @@ -17,20 +17,18 @@ class Network std::span get_neighbours( std::size_t agent_idx ) const; std::span get_neighbours( std::size_t agent_idx ); - std::span get_weights( std::size_t agent_idx ) const; std::span 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 & weights ); + void set_weights( std::size_t agent_idx, std::span weights ); void set_neighbours_and_weights( - std::size_t agent_idx, const std::vector & buffer_neighbours, const WeightT & weight ); + std::size_t agent_idx, std::span buffer_neighbours, const WeightT & weight ); void set_neighbours_and_weights( - std::size_t agent_idx, const std::vector & buffer_neighbours, - const std::vector & buffer_weights ); + std::size_t agent_idx, std::span buffer_neighbours, std::span buffer_weights ); void push_back_neighbour_and_weight( size_t i, size_t j, WeightT w ); diff --git a/src/network.cpp b/src/network.cpp index dd46202..a2aa294 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -1,6 +1,7 @@ #include "network.hpp" #include "connectivity.hpp" #include +#include #include #include @@ -37,20 +38,16 @@ std::span Seldon::Network::get_neighbours( std::size_t agent_idx ) } void Seldon::Network::set_neighbours_and_weights( - std::size_t agent_idx, const std::vector & buffer_neighbours, const WeightT & weight ) + std::size_t agent_idx, std::span 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 & buffer_neighbours, - const std::vector & buffer_weights ) + std::size_t agent_idx, std::span buffer_neighbours, + std::span buffer_weights ) { if( buffer_neighbours.size() != buffer_weights.size() ) [[unlikely]] @@ -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 ) @@ -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 & weights ) +void Seldon::Network::set_weights( std::size_t agent_idx, std::span 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()