Skip to content

Commit

Permalink
Embrace structured bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyelewis committed May 30, 2021
1 parent 74ee058 commit 1e2e0b7
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 68 deletions.
13 changes: 7 additions & 6 deletions source/ct_cluster/cath/cluster/cath_cluster_mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <fstream>
#include <functional>

#include <boost/range/adaptor/indexed.hpp>

#include "cath/cluster/clustmap_options.hpp"
#include "cath/cluster/detail/mapping_job.hpp"
#include "cath/cluster/file/cluster_membership_file.hpp"
Expand All @@ -45,6 +47,7 @@ using namespace ::cath::clust::detail;
using namespace ::cath::common;
using namespace ::cath::opts;

using ::boost::adaptors::indexed;
using ::std::filesystem::path;
using ::std::ifstream;
using ::std::istream;
Expand Down Expand Up @@ -130,12 +133,10 @@ void cath::clust::perform_map_clusters(const clustmap_input_spec &prm_input_sp
path_vec{ { prm_output_spec.get_output_to_file().value_or( out_list.get_flag() ) } }
);

// \TODO Come C++17 and structured bindings, use here
for (const size_t &job_idx : indices( jobs.size() ) ) {
const mapping_job &job = jobs[ job_idx ];
const auto &job_batch_id = job.get_batch_id();
const auto &job_new_clustmemb_file = job.get_new_cluster_membership_file();
const auto &job_old_clustmemb_file = job.get_old_cluster_membership_file();
for ( const auto &[ job_idx, job ] : jobs | indexed() ) {
const auto &job_batch_id = job.get_batch_id();
const auto &job_new_clustmemb_file = job.get_new_cluster_membership_file();
const auto &job_old_clustmemb_file = job.get_old_cluster_membership_file();

id_of_str_bidirnl seq_ider;

Expand Down
43 changes: 21 additions & 22 deletions source/ct_cluster/cath/cluster/cluster_info_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,28 @@ using ::std::vector;

namespace {

/// \brief The cluster_info_test_suite_fixture to assist in testing cluster_info
struct cluster_info_test_suite_fixture {
protected:
~cluster_info_test_suite_fixture() noexcept = default;

/// \brief Type alias for a pair of string and seq_seg_run_opt
using str_seq_seg_run_opt_pair = pair < string, seq_seg_run_opt >;

/// \brief Type alias for a vector of str_seq_seg_run_opt_pair
using str_seq_seg_run_opt_pair_vec = vector< str_seq_seg_run_opt_pair >;

/// \brief Make a cluster_info from the specified str_seq_seg_run_opt_pair_vec
cluster_info make_cluster_info(const str_seq_seg_run_opt_pair_vec &prm_data ///< The data from which to build the cluster_info
) {
cluster_info result{};

// \TODO Come C++17 and structured bindings, use here
for (const pair<string, seq_seg_run_opt> &datum : prm_data) {
result.add_entry( datum.first, datum.second );
}
return result;
/// \brief The cluster_info_test_suite_fixture to assist in testing cluster_info
struct cluster_info_test_suite_fixture {
protected:
~cluster_info_test_suite_fixture() noexcept = default;

/// \brief Type alias for a pair of string and seq_seg_run_opt
using str_seq_seg_run_opt_pair = pair<string, seq_seg_run_opt>;

/// \brief Type alias for a vector of str_seq_seg_run_opt_pair
using str_seq_seg_run_opt_pair_vec = vector<str_seq_seg_run_opt_pair>;

/// \brief Make a cluster_info from the specified str_seq_seg_run_opt_pair_vec
cluster_info make_cluster_info( const str_seq_seg_run_opt_pair_vec &prm_data ///< The data from which to build the cluster_info
) {
cluster_info result{};

for ( const auto &[ name, segments ] : prm_data ) {
result.add_entry( name, segments );
}
};
return result;
}
};

} // namespace

Expand Down
15 changes: 8 additions & 7 deletions source/ct_cluster/cath/cluster/map/map_clusters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ map_results cath::clust::map_clusters(const old_cluster_data_opt &prm_old_cluste
const domain_cluster_ids &new_dom_clust_ids = get_domain_cluster_ids_of_seq_id( prm_new_clusters, seq_id );

/// Loop over the old entries on the sequence
// \TODO Come C++17 and structured bindings, use here
for (const domain_cluster_id &old_dom_clust_id : old_dom_cluster_ids) {
const seq_seg_run_opt &old_segments_opt = old_dom_clust_id.segments;
for ( const auto &[ old_segments_opt, old_dom_clust_id ] : old_dom_cluster_ids ) {

// If the entry doesn't have segments then...
if ( ! old_segments_opt ) {
Expand All @@ -151,7 +149,7 @@ map_results cath::clust::map_clusters(const old_cluster_data_opt &prm_old_cluste
<< " "
<< prm_old_clusters->get_id_of_seq_name().get_name_of_id( seq_id )
<< " "
<< get_name_of_cluster_of_id( *prm_old_clusters, old_dom_clust_id.cluster_id )
<< get_name_of_cluster_of_id( *prm_old_clusters, old_dom_clust_id )
<< " 100 "
<< prm_old_clusters->get_id_of_seq_name().get_name_of_id( seq_id )
<< " "
Expand All @@ -167,7 +165,7 @@ map_results cath::clust::map_clusters(const old_cluster_data_opt &prm_old_cluste

if constexpr ( IS_IN_DEBUG_MODE ) {
// Check the old cluster ID is consistent
if ( old_cluster_idx != old_dom_clust_id.cluster_id ) {
if ( old_cluster_idx != old_dom_clust_id ) {
BOOST_THROW_EXCEPTION(
out_of_range_exception( "Internal inconsistency detected in old cluster IDs" ) );
}
Expand All @@ -187,8 +185,11 @@ map_results cath::clust::map_clusters(const old_cluster_data_opt &prm_old_cluste
}

// Make a closure for calculating the domain overlap with *old_segments_opt
//
// TODO: Come C++20, drop this silly reference (needed in C++17 to allow a structured binding to be captured by a lambda)
const auto &old_segments_opt_ref = old_segments_opt;
const auto get_dom_ol_fn = [&] (const domain_cluster_id &x) {
return fraction_overlap_over_longer( *old_segments_opt, *x.segments );
return fraction_overlap_over_longer( *old_segments_opt_ref, *x.segments );
};

// Find the new entry that maps to the old domain best (and its overlap)
Expand All @@ -209,7 +210,7 @@ map_results cath::clust::map_clusters(const old_cluster_data_opt &prm_old_cluste
<< prm_old_clusters->get_id_of_seq_name().get_name_of_id( seq_id )
<< get_segments_suffix_string( old_segments_opt )
<< " "
<< get_name_of_cluster_of_id( *prm_old_clusters, old_dom_clust_id.cluster_id )
<< get_name_of_cluster_of_id( *prm_old_clusters, old_dom_clust_id )
<< " "
<< ( 100.0 * best_ol )
<< " "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,8 @@ size_size_doub_tpl_vec cath::common::order_spanning_tree_from_start(const size_s
vector<size_set> edge_indcs( prm_spanning_tree.size() + 1 );
const auto non_starting_edge_indices = indices( prm_spanning_tree.size() )
| filtered( [&] (const size_t &x) { return x != prm_index; } );
// \TODO Come C++17 and structured bindings, use here
for (const size_t &non_starting_edge_index : non_starting_edge_indices) {
const auto &the_edge = prm_spanning_tree[ non_starting_edge_index ];
const size_t &node_a = get<0>( the_edge );
const size_t &node_b = get<1>( the_edge );
const auto &[ node_a, node_b, val ] = prm_spanning_tree[ non_starting_edge_index ];
edge_indcs[ node_a ].insert( non_starting_edge_index );
edge_indcs[ node_b ].insert( non_starting_edge_index );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ size_t residue_name_align_map::get_index_of_residue_name_string(const string &pr
/// \brief Get a list of all the residue names (ie the keys)
str_vec residue_name_align_map::get_residue_name_strings() const {
str_vec residue_names;
residue_names.reserve(index_of_residue_name.size());
/// TODO: Come C++17, use a structured binding to make the names clearer here
for (const auto &residue_name_index_pair : index_of_residue_name) {
residue_names.push_back(residue_name_index_pair.first);
residue_names.reserve( index_of_residue_name.size() );
for ( const auto &[ residue_name, index ] : index_of_residue_name ) {
residue_names.push_back( residue_name );
}
return residue_names;
}
Expand Down
10 changes: 2 additions & 8 deletions source/ct_uni/cath/display/viewer/chimera_viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,17 +293,11 @@ void cath::detail::write_chimera_global_alignment(ostream &p
}
prm_os << join(
core_res_ids_of_entry_name
// \TODO Come C++17 and structured bindings, use here
| transformed( [] (const pair<const coreness, str_res_id_vec_map> &core_data) {
const coreness &is_core = core_data.first;
const str_res_id_vec_map &res_ids_of_entry_name = core_data.second;
const auto &[ is_core, res_ids_of_entry_name ] = core_data;

// \TODO Come C++17 and structured bindings, use here
str_vec selection_strings;
for (const pair<const string, residue_id_vec> &entry_name_and_res_ids : res_ids_of_entry_name) {
const string &entry_name = entry_name_and_res_ids.first;
const residue_id_vec &res_ids = entry_name_and_res_ids.second;

for ( const auto &[ entry_name, res_ids ] : res_ids_of_entry_name ) {
const size_t num_res_ids = res_ids.size();
const size_t num_res_batches = num_batches( num_res_ids, chimera_viewer::RESIDUE_BATCH_SIZE, broken_batch_tol::PERMIT );
for (const size_t &batch_ctr : indices( num_res_batches ) ) {
Expand Down
10 changes: 2 additions & 8 deletions source/ct_uni/cath/display/viewer/pymol_viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,17 +279,11 @@ void cath::detail::write_pymol_global_alignment(ostream &prm
}
prm_os << join(
core_res_ids_of_entry_name
// \TODO Come C++17 and structured bindings, use here
| transformed( [] (const pair<const coreness, str_res_id_vec_map> &core_data) {
const coreness &is_core = core_data.first;
const str_res_id_vec_map &res_ids_of_entry_name = core_data.second;
const auto &[ is_core, res_ids_of_entry_name ] = core_data;

// \TODO Come C++17 and structured bindings, use here
str_vec selection_strings;
for (const pair<const string, residue_id_vec> &entry_name_and_res_ids : res_ids_of_entry_name) {
const string &entry_name = entry_name_and_res_ids.first;
const residue_id_vec &res_ids = entry_name_and_res_ids.second;

for ( const auto &[ entry_name, res_ids ] : res_ids_of_entry_name ) {
const size_t num_res_ids = res_ids.size();
const size_t num_res_batches = num_batches( num_res_ids, pymol_viewer::RESIDUE_BATCH_SIZE, broken_batch_tol::PERMIT );
for (const size_t &batch_ctr : indices( num_res_batches ) ) {
Expand Down
7 changes: 3 additions & 4 deletions source/ct_uni/cath/ssap/ssap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -823,11 +823,10 @@ protein cath::read_protein_data_from_ssap_options_files(const data_dirs_spec
prm_protein_name
);

/// TODO: Come C++17, use a structured binding to make the names clearer here
for (const auto &filename_and_data_file : filename_of_data_file) {
const string file_str = to_lower_copy( lexical_cast<string>( filename_and_data_file.first ) );
for ( const auto &[ data_file, filename ] : filename_of_data_file ) {
const string file_str = to_lower_copy( lexical_cast<string>( data_file ) );
const string right_padded_file_str = string( max_data_file_str_length() - file_str.length(), ' ' );
::spdlog::debug( "Loading {}{} from {}", file_str, right_padded_file_str, filename_and_data_file.second.string() );
::spdlog::debug( "Loading {}{} from {}", file_str, right_padded_file_str, filename.string() );
}

// Create a protein object from the name, wolf file and sec file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ gsl_matrix_wrp cath::geom::detail::cross_covariance_matrix(const coord_list &prm
gsl_matrix_wrp result{ 3, 3 };
gsl_matrix_set_zero( result.get_ptr() );

/// \TODO Come C++17 and structure bindings, use here
for (const auto &coord_pair : combine( prm_coords_a, prm_coords_b ) ) {
const coord &coord_a = coord_pair.get<0>();
const coord &coord_b = coord_pair.get<1>();

for ( const auto &[ coord_a, coord_b ] : combine( prm_coords_a, prm_coords_b ) ) {
gsl_matrix_wrp_increment( result, 0, 0, coord_a.get_x() * coord_b.get_x() );
gsl_matrix_wrp_increment( result, 0, 1, coord_a.get_x() * coord_b.get_y() );
gsl_matrix_wrp_increment( result, 0, 2, coord_a.get_x() * coord_b.get_z() );
Expand Down

0 comments on commit 1e2e0b7

Please sign in to comment.