Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use std containers and std::make_unique/shared #2764

Merged
merged 4 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
16 changes: 0 additions & 16 deletions check_syntax
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,6 @@ for f in $(find cmd core src -type f -name '*.h' -o -name '*.cpp' | $grep -v '_m
# remove quoted strings:
cat .check_syntax2.tmp | perl -pe 's/(")(\\"|.)*?"//g' > .check_syntax.tmp

# detect any instances of std::vector:
res="$res"$(
cat .check_syntax.tmp | \
# match for the parts we're interested in and output just the bits that match:
$grep -Po '(?<!::)std::vector\b'
)


# detect any instances of std::make_shared:
res="$res"$(
cat .check_syntax.tmp | \
# match for the parts we're interested in and output just the bits that match:
$grep -Po '(?<!::)std::make_shared\b'
)

# detect any instances of "using namespace std;":
res="$res"$(
Expand Down Expand Up @@ -126,8 +112,6 @@ else
echo "" >> $LOG
echo "Please check the following syntax requirements:
- Add MEMALIGN() or NOMEMALIGN macro to any class declarations identified above;
- Replace all occurrences of std::vector<> with MR::vector<> (or just vector<>);
- Avoid use of std::make_shared();
- Replace all instances of std::abs() with MR::abs() (or just abs());
- Replace all instances of %zu in print() calls with PRI_SIZET." >> $LOG
exit 1
Expand Down
2 changes: 1 addition & 1 deletion cmd/afdconnectivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ value_type AFDConnectivity::get(const std::string &path) {
if (all_fixels) {

// All fixels contribute to the result
for (vector<AFDConnFixel>::const_iterator i = fixels.begin(); i != fixels.end(); ++i) {
for (std::vector<AFDConnFixel>::const_iterator i = fixels.begin(); i != fixels.end(); ++i) {
if (i->is_selected())
sum_volumes += i->get_FOD();
}
Expand Down
16 changes: 8 additions & 8 deletions cmd/amp2response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ Eigen::Matrix<default_type, 3, 3> gen_rotation_matrix(const Eigen::Vector3d &dir
return R;
}

vector<size_t> all_volumes(const size_t num) {
vector<size_t> result(num);
std::vector<size_t> all_volumes(const size_t num) {
std::vector<size_t> result(num);
for (size_t i = 0; i != num; ++i)
result[i] = i;
return result;
Expand All @@ -112,7 +112,7 @@ class Accumulator {
public:
class Shared {
public:
Shared(int lmax, const vector<size_t> &volumes, const Eigen::MatrixXd &dirs)
Shared(int lmax, const std::vector<size_t> &volumes, const Eigen::MatrixXd &dirs)
: lmax(lmax),
dirs(dirs),
volumes(volumes),
Expand All @@ -122,7 +122,7 @@ class Accumulator {

const int lmax;
const Eigen::MatrixXd &dirs;
const vector<size_t> &volumes;
const std::vector<size_t> &volumes;
Eigen::MatrixXd M;
Eigen::VectorXd b;
size_t count;
Expand Down Expand Up @@ -196,8 +196,8 @@ void run() {
auto header = Header::open(argument[0]);

// May be dealing with multiple shells
vector<Eigen::MatrixXd> dirs_azel;
vector<vector<size_t>> volumes;
std::vector<Eigen::MatrixXd> dirs_azel;
std::vector<std::vector<size_t>> volumes;
std::unique_ptr<DWI::Shells> shells;

auto opt = get_options("directions");
Expand All @@ -207,7 +207,7 @@ void run() {
} else {
auto hit = header.keyval().find("directions");
if (hit != header.keyval().end()) {
vector<default_type> dir_vector;
std::vector<default_type> dir_vector;
for (auto line : split_lines(hit->second)) {
auto v = parse_floats(line);
dir_vector.insert(dir_vector.end(), v.begin(), v.end());
Expand All @@ -230,7 +230,7 @@ void run() {
}
}

vector<uint32_t> lmax;
std::vector<uint32_t> lmax;
uint32_t max_lmax = 0;
opt = get_options("lmax");
if (get_options("isotropic").size()) {
Expand Down
12 changes: 6 additions & 6 deletions cmd/amp2sh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ class Amp2SHCommon {
public:
template <class MatrixType>
Amp2SHCommon(const MatrixType &sh2amp,
const vector<size_t> &bzeros,
const vector<size_t> &dwis,
const std::vector<size_t> &bzeros,
const std::vector<size_t> &dwis,
bool normalise_to_bzero)
: sh2amp(sh2amp), amp2sh(Math::pinv(sh2amp)), bzeros(bzeros), dwis(dwis), normalise(normalise_to_bzero) {}

Eigen::MatrixXd sh2amp, amp2sh;
const vector<size_t> &bzeros;
const vector<size_t> &dwis;
const std::vector<size_t> &bzeros;
const std::vector<size_t> &dwis;
bool normalise;
};

Expand Down Expand Up @@ -175,7 +175,7 @@ void run() {
auto amp = Image<value_type>::open(argument[0]).with_direct_io(3);
Header header(amp);

vector<size_t> bzeros, dwis;
std::vector<size_t> bzeros, dwis;
Eigen::MatrixXd dirs;
auto opt = get_options("directions");
if (opt.size()) {
Expand All @@ -185,7 +185,7 @@ void run() {
} else {
auto hit = header.keyval().find("directions");
if (hit != header.keyval().end()) {
vector<default_type> dir_vector;
std::vector<default_type> dir_vector;
for (auto line : split_lines(hit->second)) {
auto v = parse_floats(line);
dir_vector.insert(dir_vector.end(), v.begin(), v.end());
Expand Down
18 changes: 9 additions & 9 deletions cmd/connectome2tck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ void run() {
Tractography::Properties properties;
Tractography::Reader<float> reader(argument[0], properties);

vector<vector<node_t>> assignments_lists;
std::vector<std::vector<node_t>> assignments_lists;
assignments_lists.reserve(to<size_t>(properties["count"]));
vector<NodePair> assignments_pairs;
std::vector<NodePair> assignments_pairs;
bool nonpair_found = false;
node_t max_node_index = 0;
{
Expand All @@ -172,7 +172,7 @@ void run() {
if (line.empty())
continue;
std::stringstream line_stream(line);
vector<node_t> nodes;
std::vector<node_t> nodes;
while (1) {
node_t n;
line_stream >> n;
Expand Down Expand Up @@ -216,7 +216,7 @@ void run() {
const bool keep_self = get_options("keep_self").size();

// Get the list of nodes of interest
vector<node_t> nodes;
std::vector<node_t> nodes;
opt = get_options("nodes");
bool manual_node_list = false;
if (opt.size()) {
Expand Down Expand Up @@ -256,8 +256,8 @@ void run() {
// Load the node image, get the centres of mass
// Generate exemplars - these can _only_ be done per edge, and requires a mutex per edge to multi-thread
auto image = Image<node_t>::open(opt[0][0]);
vector<Eigen::Vector3f> COMs(max_node_index + 1, Eigen::Vector3f(0.0f, 0.0f, 0.0f));
vector<size_t> volumes(max_node_index + 1, 0);
std::vector<Eigen::Vector3f> COMs(max_node_index + 1, Eigen::Vector3f(0.0f, 0.0f, 0.0f));
std::vector<size_t> volumes(max_node_index + 1, 0);
for (auto i = Loop()(image); i; ++i) {
const node_t index = image.value();
if (index) {
Expand Down Expand Up @@ -344,7 +344,7 @@ void run() {
} else {
// For each node in the list, write one file for an exemplar to every other node
ProgressBar progress("writing exemplars to files", nodes.size() * COMs.size());
for (vector<node_t>::const_iterator n = nodes.begin(); n != nodes.end(); ++n) {
for (std::vector<node_t>::const_iterator n = nodes.begin(); n != nodes.end(); ++n) {
for (size_t i = first_node; i != COMs.size(); ++i) {
generator.write(*n,
i,
Expand All @@ -356,7 +356,7 @@ void run() {
}
} else if (file_format == 1) { // One file per node
ProgressBar progress("writing exemplars to files", nodes.size());
for (vector<node_t>::const_iterator n = nodes.begin(); n != nodes.end(); ++n) {
for (std::vector<node_t>::const_iterator n = nodes.begin(); n != nodes.end(); ++n) {
generator.write(
*n, prefix + str(*n) + ".tck", weights_prefix.size() ? (weights_prefix + str(*n) + ".csv") : "");
++progress;
Expand Down Expand Up @@ -399,7 +399,7 @@ void run() {
INFO("A total of " + str(writer.file_count()) + " output track files will be generated (one for each edge)");
break;
case 1: // One file per node
for (vector<node_t>::const_iterator i = nodes.begin(); i != nodes.end(); ++i)
for (std::vector<node_t>::const_iterator i = nodes.begin(); i != nodes.end(); ++i)
writer.add(*i, prefix + str(*i) + ".tck", weights_prefix.size() ? (weights_prefix + str(*i) + ".csv") : "");
INFO("A total of " + str(writer.file_count()) + " output track files will be generated (one for each node)");
break;
Expand Down
4 changes: 2 additions & 2 deletions cmd/connectomestats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ void run() {

// Before validating the contrast matrix, we first need to see if there are any
// additional design matrix columns coming from edge-wise subject data
vector<CohortDataImport> extra_columns;
std::vector<CohortDataImport> extra_columns;
bool nans_in_columns = false;
auto opt = get_options("column");
for (size_t i = 0; i != opt.size(); ++i) {
Expand All @@ -236,7 +236,7 @@ void run() {
CONSOLE("Number of variance groups: " + str(num_vgs));

// Load hypotheses
const vector<Hypothesis> hypotheses = Math::Stats::GLM::load_hypotheses(argument[3]);
const std::vector<Hypothesis> hypotheses = Math::Stats::GLM::load_hypotheses(argument[3]);
const index_type num_hypotheses = hypotheses.size();
if (hypotheses[0].cols() != num_factors)
throw Exception(
Expand Down
4 changes: 2 additions & 2 deletions cmd/dcmedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ inline uint16_t read_hex(const std::string &m) {
}

void run() {
vector<Tag> tags;
vector<uint16_t> VRs;
std::vector<Tag> tags;
std::vector<uint16_t> VRs;

auto opt = get_options("anonymise");
if (opt.size()) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/dcminfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void run() {
if (opt.size()) {
std::istringstream hex;

vector<Tag> tags(opt.size());
std::vector<Tag> tags(opt.size());
for (size_t n = 0; n < opt.size(); ++n) {
tags[n].group = read_hex(opt[n][0]);
tags[n].element = read_hex(opt[n][1]);
Expand Down
14 changes: 7 additions & 7 deletions cmd/dirflip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Shared {
best_signs(directions.rows(), 1),
best_eddy(std::numeric_limits<value_type>::max()) {}

bool update(value_type eddy, const vector<int> &signs) {
bool update(value_type eddy, const std::vector<int> &signs) {
std::lock_guard<std::mutex> lock(mutex);
if (eddy < best_eddy) {
best_eddy = eddy;
Expand All @@ -74,7 +74,7 @@ class Shared {
return num_permutations < target_num_permutations;
}

value_type eddy(size_t i, size_t j, const vector<int> &signs) const {
value_type eddy(size_t i, size_t j, const std::vector<int> &signs) const {
vector3_type a = {directions(i, 0), directions(i, 1), directions(i, 2)};
vector3_type b = {directions(j, 0), directions(j, 1), directions(j, 2)};
if (signs[i] < 0)
Expand All @@ -84,15 +84,15 @@ class Shared {
return 1.0 / (a - b).norm();
}

vector<int> get_init_signs() const { return vector<int>(directions.rows(), 1); }
const vector<int> &get_best_signs() const { return best_signs; }
std::vector<int> get_init_signs() const { return std::vector<int>(directions.rows(), 1); }
const std::vector<int> &get_best_signs() const { return best_signs; }

protected:
const Eigen::MatrixXd &directions;
const size_t target_num_permutations;
size_t num_permutations;
ProgressBar progress;
vector<int> best_signs;
std::vector<int> best_signs;
value_type best_eddy;
std::mutex mutex;
};
Expand Down Expand Up @@ -121,7 +121,7 @@ class Processor {

protected:
Shared &shared;
vector<int> signs;
std::vector<int> signs;
Math::RNG rng;
std::uniform_int_distribution<int> uniform;
};
Expand All @@ -131,7 +131,7 @@ void run() {

size_t num_permutations = get_option_value<size_t>("permutations", default_permutations);

vector<int> signs;
std::vector<int> signs;
{
Shared eddy_shared(directions, num_permutations);
Thread::run(Thread::multi(Processor(eddy_shared)), "eval thread");
Expand Down
16 changes: 8 additions & 8 deletions cmd/dirmerge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void usage() {

using value_type = double;
using Direction = Eigen::Matrix<value_type, 3, 1>;
using DirectionSet = vector<Direction>;
using DirectionSet = std::vector<Direction>;

struct OutDir {
Direction d;
Expand All @@ -68,8 +68,8 @@ void run() {
value_type unipolar_weight = App::get_option_value("unipolar_weight", 0.2);
value_type bipolar_weight = 1.0 - unipolar_weight;

vector<vector<DirectionSet>> dirs;
vector<value_type> bvalue((argument.size() - 2) / (1 + num_subsets));
std::vector<std::vector<DirectionSet>> dirs;
std::vector<value_type> bvalue((argument.size() - 2) / (1 + num_subsets));
INFO("expecting " + str(bvalue.size()) + " b-values");
if (bvalue.size() * (1 + num_subsets) + 2 != argument.size())
throw Exception("inconsistent number of arguments");
Expand All @@ -78,7 +78,7 @@ void run() {
size_t current = 1, nb = 0;
while (current < argument.size() - 1) {
bvalue[nb] = to<value_type>(argument[current++]);
vector<DirectionSet> d;
std::vector<DirectionSet> d;
for (size_t i = 0; i < num_subsets; ++i) {
auto m = DWI::Directions::load_cartesian(argument[current++]);
DirectionSet set;
Expand All @@ -87,7 +87,7 @@ void run() {
d.push_back(set);
}
INFO("found b = " + str(bvalue[nb]) + ", " + str([&] {
vector<size_t> s;
std::vector<size_t> s;
for (auto &n : d)
s.push_back(n.size());
return s;
Expand All @@ -112,7 +112,7 @@ void run() {
std::mt19937 rng(rd());
size_t first = std::uniform_int_distribution<size_t>(0, dirs[0][0].size() - 1)(rng);

vector<OutDir> merged;
std::vector<OutDir> merged;

auto push = [&](size_t b, size_t p, size_t n) {
merged.push_back({Direction(dirs[b][p][n][0], dirs[b][p][n][1], dirs[b][p][n][2]), b, p});
Expand Down Expand Up @@ -149,7 +149,7 @@ void run() {
return best;
};

vector<float> fraction;
std::vector<float> fraction;
for (auto &d : dirs) {
size_t n = 0;
for (auto &m : d)
Expand All @@ -159,7 +159,7 @@ void run() {

push(0, 0, first);

vector<size_t> counts(bvalue.size(), 0);
std::vector<size_t> counts(bvalue.size(), 0);
++counts[0];

auto num_for_b = [&](size_t b) {
Expand Down
12 changes: 6 additions & 6 deletions cmd/dirorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ void usage() {

using value_type = double;

vector<size_t> optimise(const Eigen::MatrixXd &directions, const size_t first_volume) {
vector<size_t> indices(1, first_volume);
vector<size_t> remaining;
std::vector<size_t> optimise(const Eigen::MatrixXd &directions, const size_t first_volume) {
std::vector<size_t> indices(1, first_volume);
std::vector<size_t> remaining;
for (size_t n = 0; n < size_t(directions.rows()); ++n)
if (n != indices[0])
remaining.push_back(n);
Expand Down Expand Up @@ -79,7 +79,7 @@ vector<size_t> optimise(const Eigen::MatrixXd &directions, const size_t first_vo
return indices;
}

value_type calc_cost(const Eigen::MatrixXd &directions, const vector<size_t> &order) {
value_type calc_cost(const Eigen::MatrixXd &directions, const std::vector<size_t> &order) {
const size_t start = Math::SH::NforL(2);
if (size_t(directions.rows()) <= start)
return value_type(0);
Expand Down Expand Up @@ -112,10 +112,10 @@ void run() {
}

value_type min_cost = std::numeric_limits<value_type>::infinity();
vector<size_t> best_order;
std::vector<size_t> best_order;
ProgressBar progress("Determining best reordering", directions.rows());
for (size_t first_volume = 0; first_volume != last_candidate_first_volume; ++first_volume) {
const vector<size_t> order = optimise(directions, first_volume);
const std::vector<size_t> order = optimise(directions, first_volume);
const value_type cost = calc_cost(directions, order);
if (cost < min_cost) {
min_cost = cost;
Expand Down
Loading
Loading