<commit_before>#include "Kdtree.h"
#include <cstdio>
#include <algorithm>
#include <numeric>
#include <cmath>

namespace P_RVD{
	// Eww global. Need this for the sort function
	// A pointer to the class itself, allows the sort function to determine the splitting axis to sort by
	static KDtree *myself = NULL;

	KDtree::KDtree()
	{
		myself = this;
		m_id = 0;
	}

	KDtree::~KDtree()
	{
		// Delete all the ndoes
		std::vector <KDNode*> to_visit;

		to_visit.push_back(m_root);

		while (to_visit.size()) {
			std::vector <KDNode*> next_search;

			while (to_visit.size()) {
				KDNode *cur = to_visit.back();
				to_visit.pop_back();

				if (cur->left)
					next_search.push_back(cur->left);

				if (cur->right)
					next_search.push_back(cur->right);

				delete cur;
			}

			to_visit = next_search;
		}

		m_root = NULL;
	}

	void KDtree::Create_kdtree(std::vector<Kd_tree_point> &pts, int max_levels)
	{
		m_pts = &pts;
		m_levels = max_levels;

		m_root = new KDNode();
		m_root->id = m_id++;
		m_root->index.resize(pts.size());

		//init the index
		for (unsigned int i = 0; i < pts.size(); ++i)
		{
			m_root->index[i] = i;
		}

		std::vector<KDNode*> to_visit;
		to_visit.push_back(m_root);

		while (to_visit.size())
		{
			std::vector<KDNode*> next_search;
			while (to_visit.size())
			{
				KDNode *node = to_visit.back();
				to_visit.pop_back();;

				if (node->level < max_levels)
				{
					//if current node's index size > 1
					//it has more than 1 point in this space
					//So split it
					if (node->index.size() > 1)
					{
						KDNode *left = new KDNode();
						KDNode *right = new KDNode();

						left->id = m_id++;
						right->id = m_id++;

						Split(node, left, right);

						//Claer current index
						{
							std::vector<int> dummy;
							node->index.swap(dummy);
						}

						node->left = left;
						node->right = right;

						node->_left = left->id;
						node->_right = right->id;

						if (left->index.size())
							next_search.push_back(left);
						
						if (right->index.size())
							next_search.push_back(right);
					}
				}
			}
			to_visit = next_search;
		}
	}

	/*
		compare the data of 2 points with the index
	*/
	bool KDtree::SortPoints(const int a, const int b)
	{
		std::vector<Kd_tree_point> &pts = *myself->m_pts;

		return pts[a].coords[myself->m_current_axis] < pts[b].coords[myself->m_current_axis];
	}
<commit_msg>modify for knn<commit_after>#include "Kdtree.h"
#include <cstdio>
#include <algorithm>
#include <numeric>
#include <cmath>

namespace P_RVD{
	// Eww global. Need this for the sort function
	// A pointer to the class itself, allows the sort function to determine the splitting axis to sort by
	static KDtree *myself = NULL;

	KDtree::KDtree()
	{
		myself = this;
		m_id = 0;
	}

	KDtree::~KDtree()
	{
		// Delete all the ndoes
		std::vector <KDNode*> to_visit;

		to_visit.push_back(m_root);

		while (to_visit.size()) {
			std::vector <KDNode*> next_search;

			while (to_visit.size()) {
				KDNode *cur = to_visit.back();
				to_visit.pop_back();

				if (cur->left)
					next_search.push_back(cur->left);

				if (cur->right)
					next_search.push_back(cur->right);

				delete cur;
			}

			to_visit = next_search;
		}

		m_root = NULL;
	}

	void KDtree::Create_kdtree(std::vector<Kd_tree_point> &pts, int max_levels)
	{
		m_pts = &pts;
		m_levels = max_levels;

		m_root = new KDNode();
		m_root->id = m_id++;
		m_root->index.resize(pts.size());

		//init the index
		for (unsigned int i = 0; i < pts.size(); ++i)
		{
			m_root->index[i] = i;
		}

		std::vector<KDNode*> to_visit;
		to_visit.push_back(m_root);

		while (to_visit.size())
		{
			std::vector<KDNode*> next_search;
			while (to_visit.size())
			{
				KDNode *node = to_visit.back();
				to_visit.pop_back();;

				if (node->level < max_levels)
				{
					//if current node's index size > 1
					//it has more than 1 point in this space
					//So split it
					if (node->index.size() > 1)
					{
						KDNode *left = new KDNode();
						KDNode *right = new KDNode();

						left->id = m_id++;
						right->id = m_id++;

						Split(node, left, right);

						//Claer current index
						{
							//std::vector<int> dummy;
							//node->index.swap(dummy);
						}

						node->left = left;
						node->right = right;

						node->_left = left->id;
						node->_right = right->id;

						if (left->index.size())
							next_search.push_back(left);
						
						if (right->index.size())
							next_search.push_back(right);
					}
				}
			}
			to_visit = next_search;
		}
	}

	/*
		compare the data of 2 points with the index
	*/
	bool KDtree::SortPoints(const int a, const int b)
	{
		std::vector<Kd_tree_point> &pts = *myself->m_pts;

		return pts[a].coords[myself->m_current_axis] < pts[b].coords[myself->m_current_axis];
	}

{'fraction_non_alphanumeric': 0.15095128580388878, 'fraction_numerical': 0.0025088856366297305, 'mean_word_length': 2.6771714066102996, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3183017', 'n_tokens_mistral': 2299, 'n_tokens_neox': 1788, 'n_words': 408}