#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include "Tree.hpp"
#include "TreeNode.hpp"

namespace nbody {
	Tree::Tree() {
		this->nodes = new TreeNode();
	}

	Tree::~Tree() {
		this->clean();
		delete this->nodes;
	}

	void Tree::clean() {
		while (this->nodes->next != this->nodes) {
			TreeNode* node = this->nodes->next;

			node->unlink();
			delete node;
		}
		delete this->nodes;
		this->nodes = new TreeNode();
	}

	vector<Body> Tree::dubinskiParse(string filename) {
		vector<Body> result;
		string line;
		ifstream infile(filename.c_str(), ifstream::in);
		double mass, px, py, pz, vx, vy, vz;

		while (infile >> mass >> px >> py >> pz >> vx >> vy >> vz) {
			//cout << mass << " " << px << " " << py << " " << pz << " " << vx << " " << vy << " " << vz << " " << endl;
			Body b(px, py, pz, vx, vy, vz, mass);

			result.push_back(b);
		}
		infile.close();
		return result;
	}
}
