From 3ccf514ac5db4ee1c1a7d4bc931b31d35a5c2c1c Mon Sep 17 00:00:00 2001 From: Thomas Steinreiter Date: Wed, 16 Nov 2016 17:55:37 +0100 Subject: [PATCH] use more algorithms --- bhtree_mpi/src/datastructures/Body.cpp | 5 ++--- bhtree_mpi/src/datastructures/Box.cpp | 15 ++++++--------- bhtree_mpi/src/datastructures/Node.cpp | 22 +++++++--------------- bhtree_mpi/src/datastructures/Tree.cpp | 7 ++----- 4 files changed, 17 insertions(+), 32 deletions(-) diff --git a/bhtree_mpi/src/datastructures/Body.cpp b/bhtree_mpi/src/datastructures/Body.cpp index 9aa9ede..20b09e9 100644 --- a/bhtree_mpi/src/datastructures/Body.cpp +++ b/bhtree_mpi/src/datastructures/Body.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -56,9 +57,7 @@ namespace nbody { distance2 += (from.position[0] - to.position[0]) * (from.position[0] - to.position[0]); distance2 += (from.position[1] - to.position[1]) * (from.position[1] - to.position[1]); distance2 += (from.position[2] - to.position[2]) * (from.position[2] - to.position[2]); - if (fabs(distance2) < std::numeric_limits::epsilon()) { //TODO(steinret) max? - distance2 = std::numeric_limits::epsilon(); - } + distance2 = std::max(distance2, std::numeric_limits::epsilon()); double distance = sqrt(distance2); double mdist = -1.0 * ((from.mass * to.mass) / distance2); for (int i = 0; i < 3; i++) { diff --git a/bhtree_mpi/src/datastructures/Box.cpp b/bhtree_mpi/src/datastructures/Box.cpp index 474a3a0..4f99ce1 100644 --- a/bhtree_mpi/src/datastructures/Box.cpp +++ b/bhtree_mpi/src/datastructures/Box.cpp @@ -63,16 +63,13 @@ namespace nbody { } //copy bodies within box - std::vector copyBodies(Box box, std::vector bodies) { //TODO(steinret) std::copy_if ?? + std::vector copyBodies(Box box, std::vector bodies) { std::vector result; - - for (auto& body : bodies) { - if (body.position[0] >= box.min[0] && body.position[0] <= box.max[0] && - body.position[1] >= box.min[1] && body.position[1] <= box.max[1] && - body.position[2] >= box.min[2] && body.position[2] <= box.max[2]) { - result.push_back(body); - } - } + std::copy_if(std::begin(bodies), std::end(bodies), std::back_inserter(result), [&](const Body& body) { + return body.position[0] >= box.min[0] && body.position[0] <= box.max[0] && + body.position[1] >= box.min[1] && body.position[1] <= box.max[1] && + body.position[2] >= box.min[2] && body.position[2] <= box.max[2]; + }); return result; } diff --git a/bhtree_mpi/src/datastructures/Node.cpp b/bhtree_mpi/src/datastructures/Node.cpp index ccde996..ce147c9 100644 --- a/bhtree_mpi/src/datastructures/Node.cpp +++ b/bhtree_mpi/src/datastructures/Node.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "Node.hpp" namespace nbody { @@ -88,11 +89,9 @@ namespace nbody { return false; } } - for (auto& body : bodies) { //TODO(steinret) std::all_of ?? - if (!isContained(body, this->bb)) { - std::cerr << "bb out of bounds\n"; - return false; - } + if (!std::all_of(std::begin(bodies), std::end(bodies), [&](const Body& b) {return isContained(b, bb); })) { + std::cerr << "bb out of bounds\n"; + return false; } if (!this->leaf) { Node* current = this->next; @@ -186,15 +185,8 @@ namespace nbody { } //get local bodies - void Node::extractLocalBodiesTo(std::vector& bodies) { //TODO(steinret): std::copy_if - std::vector result; - auto it= std::begin(bodies); - - while (it != std::end(bodies)) { - if (!it->refinement) { - bodies.push_back(*it); - } - it = this->bodies.erase(it); - } + void Node::extractLocalBodiesTo(std::vector& result) { + std::copy_if(std::begin(this->bodies), std::end(this->bodies), std::back_inserter(result), [](const Body& b) {return !b.refinement; }); + this->bodies.clear(); } } diff --git a/bhtree_mpi/src/datastructures/Tree.cpp b/bhtree_mpi/src/datastructures/Tree.cpp index 2e1d5c1..e4bf505 100644 --- a/bhtree_mpi/src/datastructures/Tree.cpp +++ b/bhtree_mpi/src/datastructures/Tree.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "Tree.hpp" #include "Node.hpp" #include "Simulation.hpp" @@ -96,11 +97,7 @@ namespace nbody { while (this->nodes->next != this->nodes) { if (this->nodes->next->leaf) { - for (auto& b : nodes->next->bodies) { //TODO(steinret) copy_if - if (!b.refinement) { - result.push_back(b); - } - } + std::copy_if(std::begin(nodes->next->bodies), std::end(nodes->next->bodies), std::back_inserter(result), [](const Body& b) {return !b.refinement; }); } Node* h = this->nodes->next; this->nodes->next->unlink(); -- GitLab