From f19bbcfa14a125a3463df972d4040ea70eebb94c Mon Sep 17 00:00:00 2001
From: "armindamon.riess" <armindamon.riess@uzh.ch>
Date: Wed, 7 Dec 2022 13:10:20 +0100
Subject: [PATCH] use std::vector for localParticles, update

---
 lib/node.cpp | 14 ++++++++++++--
 lib/node.hpp |  9 +++++++--
 lib/tree.cpp |  9 ++++++---
 lib/tree.hpp |  2 +-
 4 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/lib/node.cpp b/lib/node.cpp
index b7ce061..7fea2fa 100644
--- a/lib/node.cpp
+++ b/lib/node.cpp
@@ -1,5 +1,6 @@
 #include "node.hpp"
 
+#include <vector>
 #include <cmath>
 
 /* 
@@ -17,7 +18,8 @@ Node::Node() {
 }
  */
 
-Node::Node(Node* root, Node* parent, double* masses, double* particles, unsigned nParticles, unsigned depth, double size, double center[3], unsigned* localParticles, unsigned nLocalParticles) {
+Node::Node(Node* root, Node* parent, double* masses, double* particles, unsigned nParticles, unsigned depth, 
+        double size, double center[3], std::vector<unsigned> localParticles, unsigned nLocalParticles) {
     parent_ = parent;
     particles_ = particles;
     nParticles_ = nParticles;
@@ -44,7 +46,6 @@ Node::~Node() {
     for (unsigned i=0; i<8; ++i) {
         delete children_[i];
     }
-    delete[] localParticles_;
 }
 
 Node& Node::operator=(const Node& other) {
@@ -62,6 +63,15 @@ Node& Node::operator=(const Node& other) {
     return *this;
 }
 
+void Node::update() {
+    // update this node
+    // TODO
+    // update children
+    for (unsigned i=0; i<8; ++i) {
+        if (children_[i] != nullptr) children_[i]->update();
+    }
+}
+
 double* Node::calculateForce(unsigned particle) {
     // calculate theta
     double px = particles_[3*particle + 0];
diff --git a/lib/node.hpp b/lib/node.hpp
index 0905c74..ebb859a 100644
--- a/lib/node.hpp
+++ b/lib/node.hpp
@@ -1,19 +1,24 @@
 #ifndef NODE_HPP
 #define NODE_HPP
 
+#include <vector>
+
 class Node {
 public:
     // default constructor creates a leaf node
     Node() = default;
     // whoever creates the node is responsible for deciding which particles are in which octant and for allocating the localParticles array 
     // node will write to localParticles array
-    Node(Node* root, Node* parent, double* masses, double* particles, unsigned nParticles, unsigned depth, double size, double center[3], unsigned* localParticles, unsigned nLocalParticles);
+    Node(Node* root, Node* parent, double* masses, double* particles, unsigned nParticles, unsigned depth, 
+        double size, double center[3], std::vector<unsigned> localParticles, unsigned nLocalParticles);
     // destuctor
     ~Node();
     // copy constructor
     Node(const Node&) = default;
     // copy assignment
     Node& operator=(const Node&);
+    // update tree: visit each node, check if it needs to be split or merged (for example if a particle has left the region)
+    void update();
     // calculate force on a particle recursively
     double* calculateForce(unsigned particle);
 private:
@@ -26,7 +31,7 @@ private:
     unsigned depth_;
     double size_;
     double center_[3];
-    unsigned* localParticles_;
+    std::vector<unsigned> localParticles_;
     unsigned nLocalParticles_;
     double mass_;
     const double theta0_ = 0.4;
diff --git a/lib/tree.cpp b/lib/tree.cpp
index 15a4f9b..08a3824 100644
--- a/lib/tree.cpp
+++ b/lib/tree.cpp
@@ -1,6 +1,8 @@
 #include "tree.hpp"
 #include "node.hpp"
 
+#include <vector>
+
 Tree::Tree(double* masses, double* particles, double* forces, unsigned nParticles, double size, double* center) {
     particles_ = particles;
     forces_ = forces;
@@ -9,11 +11,12 @@ Tree::Tree(double* masses, double* particles, double* forces, unsigned nParticle
 
     for (unsigned i=0; i < 3; ++i) center_[i] = center[i];
 
-    unsigned* localParticles_ = new unsigned[nParticles_];
+    std::vector<unsigned> localParticles_(nParticles_);
     for (unsigned i=0; i < nParticles_; ++i) localParticles_[i] = i;
     
     root_ = new Node();
-    *root_ = Node(root_, root_, masses_, particles_, nParticles_, 0, size_, center_, localParticles_, nParticles_);
+    *root_ = Node(root_, root_, masses_, particles_, nParticles_, 0, 
+                size_, center_, localParticles_, nParticles_);
 }
 
 Tree::~Tree() {
@@ -26,5 +29,5 @@ void Tree::calculateForce(unsigned particle) {
 }
 
 void Tree::update() {
-    // TODO
+    root_->update();
 }
\ No newline at end of file
diff --git a/lib/tree.hpp b/lib/tree.hpp
index deca6eb..5b55b41 100644
--- a/lib/tree.hpp
+++ b/lib/tree.hpp
@@ -12,7 +12,7 @@ public:
     ~Tree();
     // calculate force on a particle
     void calculateForce(unsigned particle);
-    // update tree: visit each node, check if it needs to be split or merged (for example if a particle has left the region)
+    // update tree
     void update();
 private:
     Node* root_;
-- 
GitLab