<commit_before>/*
 *  vptree.h
 *  Implementation of a vantage-point tree.
 *
 *  Created by <NAME>.
 *  Copyright 2012, Delft University of Technology. All rights reserved.
 *
 */


#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <stdio.h>
#include <queue>
#include <limits>


#ifndef VPTREE_H
#define VPTREE_H

class DataPoint
{
    int _D;
    int _ind;
    double* _x;
    double _len;

public:
    DataPoint() {
        _D = 1;
        _ind = -1;
        _x = NULL;
        _len = 0;
    }
    DataPoint(int D, int ind, double* x) {
        _D = D;
        _ind = ind;
        _x = (double*) malloc(_D * sizeof(double));
        double len_sq = 0.0;
        for(int d = 0; d < _D; d++){
            _x[d] = x[d];
            len_sq + x[d] * x[d];
        }
        _len = sqrt(len_sq);
    }
    DataPoint(const DataPoint& other) {                     // this makes a deep copy -- should not free anything
        if(this != &other) {
            _D = other.dimensionality();
            _ind = other.index();
            _x = (double*) malloc(_D * sizeof(double));
            _len = other.len();
            for(int d = 0; d < _D; d++) _x[d] = other.x(d);
        }
    }
    ~DataPoint() { if(_x != NULL) free(_x); }
    DataPoint& operator= (const DataPoint& other) {         // asignment should free old object
        if(this != &other) {
            if(_x != NULL) free(_x);
            _D = other.dimensionality();
            _ind = other.index();
            _len = other.len();
            _x = (double*) malloc(_D * sizeof(double));
            for(int d = 0; d < _D; d++) _x[d] = other.x(d);
        }
        return *this;
    }
    int index() const { return _ind; }
    int dimensionality() const { return _D; }
    double x(int d) const { return _x[d]; }
    double len() const { return _len; }
};


double euclidean_distance(const DataPoint &t1, const DataPoint &t2) {
    double dd = .0;
    for(int d = 0; d < t1.dimensionality(); d++) dd += (t1.x(d) - t2.x(d)) * (t1.x(d) - t2.x(d));
    return dd;
}

double cosine_distance(const DataPoint &t1, const DataPoint &t2) {
    double dd = .0;
    if (t1.len() == 0 || t2.len() == 0){
        return 2.0;
    }
    for(int d = 0; d < t1.dimensionality(); d++){
        dd += t1.x(d) * t2.x(d);
    }
    return 2.0 - dd / (t1.len() * t2.len());
}

template<typename T, double (*distance)( const T&, const T& )>
class VpTree
{
public:

    // Default constructor
    VpTree() : _root(0) {}

    // Destructor
    ~VpTree() {
        delete _root;
    }

    // Function to create a new VpTree from data
    void create(const std::vector<T>& items) {
        delete _root;
        _items = items;
        _root = buildFromPoints(0, items.size());
    }
<commit_msg>Fix wrong cosine distance offset<commit_after>/*
 *  vptree.h
 *  Implementation of a vantage-point tree.
 *
 *  Created by <NAME>.
 *  Copyright 2012, Delft University of Technology. All rights reserved.
 *
 */


#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <stdio.h>
#include <queue>
#include <limits>


#ifndef VPTREE_H
#define VPTREE_H

class DataPoint
{
    int _D;
    int _ind;
    double* _x;
    double _len;

public:
    DataPoint() {
        _D = 1;
        _ind = -1;
        _x = NULL;
        _len = 0;
    }
    DataPoint(int D, int ind, double* x) {
        _D = D;
        _ind = ind;
        _x = (double*) malloc(_D * sizeof(double));
        double len_sq = 0.0;
        for(int d = 0; d < _D; d++){
            _x[d] = x[d];
            len_sq + x[d] * x[d];
        }
        _len = sqrt(len_sq);
    }
    DataPoint(const DataPoint& other) {                     // this makes a deep copy -- should not free anything
        if(this != &other) {
            _D = other.dimensionality();
            _ind = other.index();
            _x = (double*) malloc(_D * sizeof(double));
            _len = other.len();
            for(int d = 0; d < _D; d++) _x[d] = other.x(d);
        }
    }
    ~DataPoint() { if(_x != NULL) free(_x); }
    DataPoint& operator= (const DataPoint& other) {         // asignment should free old object
        if(this != &other) {
            if(_x != NULL) free(_x);
            _D = other.dimensionality();
            _ind = other.index();
            _len = other.len();
            _x = (double*) malloc(_D * sizeof(double));
            for(int d = 0; d < _D; d++) _x[d] = other.x(d);
        }
        return *this;
    }
    int index() const { return _ind; }
    int dimensionality() const { return _D; }
    double x(int d) const { return _x[d]; }
    double len() const { return _len; }
};


double euclidean_distance(const DataPoint &t1, const DataPoint &t2) {
    double dd = .0;
    for(int d = 0; d < t1.dimensionality(); d++) dd += (t1.x(d) - t2.x(d)) * (t1.x(d) - t2.x(d));
    return dd;
}

double cosine_distance(const DataPoint &t1, const DataPoint &t2) {
    double dd = .0;
    if (t1.len() == 0 || t2.len() == 0){
        return 2.0;
    }
    for(int d = 0; d < t1.dimensionality(); d++){
        dd += t1.x(d) * t2.x(d);
    }
    return 1.0 - dd / (t1.len() * t2.len());
}

template<typename T, double (*distance)( const T&, const T& )>
class VpTree
{
public:

    // Default constructor
    VpTree() : _root(0) {}

    // Destructor
    ~VpTree() {
        delete _root;
    }

    // Function to create a new VpTree from data
    void create(const std::vector<T>& items) {
        delete _root;
        _items = items;
        _root = buildFromPoints(0, items.size());
    }

{'fraction_non_alphanumeric': 0.1563235028044147, 'fraction_numerical': 0.014474398407816174, 'mean_word_length': 1.904887020493957, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 21, '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': '1524271', 'n_tokens_mistral': 2026, 'n_tokens_neox': 1949, 'n_words': 542}