<commit_before>/*
 (c) 2013, <NAME>
 RBush, a JavaScript library for high-performance 2D spatial indexing of points and rectangles.
 https://github.com/mourner/rbush
*/

(function () { 'use strict';

function rbush(maxEntries, format) {
    // jshint newcap: false, validthis: true, evil: true

    if (!(this instanceof rbush)) {
        // allow constructing RBush trees without "new"
        return new rbush(maxEntries, format);
    }

    if (!maxEntries) {
        // maxEntries is required because it's the most important performance decision when using RBush
        // and depens on the type of data and search queries you perform
        throw new Error("Provide a maxEntries argument to rbush constructor");
    }

    this._maxEntries = Math.max(4, maxEntries);
    this._minFill = Math.max(2, Math.floor(this._maxEntries * 0.4));

    // data format (minX, minY, maxX, maxY accessors),
    // uses eval-type function compilation instead of accepting functions to simplify customization;
    // performance is not affected since this happens only once

    format = format || ['[0]', '[1]', '[2]', '[3]'];

    this._sortMinX = this._createSort(format[0]);
    this._sortMinY = this._createSort(format[1]);
    this._sortMaxX = this._createSort(format[2]);
    this._sortMaxY = this._createSort(format[3]);

    this._sortNodeMinX = this._createSort('.bbox[0]');
    this._sortNodeMinY = this._createSort('.bbox[1]');
    this._sortNodeMaxX = this._createSort('.bbox[2]');
    this._sortNodeMaxY = this._createSort('.bbox[3]');

    this._toBBox = new Function('a', 'return [a' + format.join(', a') + '];');
}

rbush.prototype = {

    search: function (bbox) {
        var result = [];

        // recursively search for items in a given bbox, collecting matches in the result array
        this._search(bbox, this.data, result);

        return result;
    },

    load: function (data) {
        // recursively build the tree with the given data from stratch using OMT algorithm
        this.data = this._build(data.slice(), 0);

        // recursively calculate all bboxes
        this._calcBBoxes(this.data, true);

        return this;
    },

    insert: function (item) {
        // an index of what tree levels overflowed during this single insert
        this._overflowLevels = {};

        this._insert(item);

        return this;
    },

    toJSON: function () {
        // TODO cleanup nodes from area, enlargement, sqDist properties
        return this.data;
    },

    fromJSON: function (data) {
        this.data = data;
        return this;
    },

    _search: function (bbox, node, result) {

        if (!this._intersects(bbox, node.bbox)) { return; }

        for (var i = 0, len = node.children.length, child; i < len; i++) {
            child = node.children[i];

            if (!node.leaf) {
                this._search(bbox, child, result);
            } else if (this._intersects(bbox, this._toBBox(child))) {
                result.push(child);
            }
        }
    },
<commit_msg>handle insertion to an empty tree<commit_after>/*
 (c) 2013, <NAME>
 RBush, a JavaScript library for high-performance 2D spatial indexing of points and rectangles.
 https://github.com/mourner/rbush
*/

(function () { 'use strict';

function rbush(maxEntries, format) {
    // jshint newcap: false, validthis: true, evil: true

    if (!(this instanceof rbush)) {
        // allow constructing RBush trees without "new"
        return new rbush(maxEntries, format);
    }

    if (!maxEntries) {
        // maxEntries is required because it's the most important performance decision when using RBush
        // and depens on the type of data and search queries you perform
        throw new Error("Provide a maxEntries argument to rbush constructor");
    }

    this._maxEntries = Math.max(4, maxEntries);
    this._minFill = Math.max(2, Math.floor(this._maxEntries * 0.4));

    // data format (minX, minY, maxX, maxY accessors),
    // uses eval-type function compilation instead of accepting functions to simplify customization;
    // performance is not affected since this happens only once

    format = format || ['[0]', '[1]', '[2]', '[3]'];

    this._sortMinX = this._createSort(format[0]);
    this._sortMinY = this._createSort(format[1]);
    this._sortMaxX = this._createSort(format[2]);
    this._sortMaxY = this._createSort(format[3]);

    this._sortNodeMinX = this._createSort('.bbox[0]');
    this._sortNodeMinY = this._createSort('.bbox[1]');
    this._sortNodeMaxX = this._createSort('.bbox[2]');
    this._sortNodeMaxY = this._createSort('.bbox[3]');

    this._toBBox = new Function('a', 'return [a' + format.join(', a') + '];');
}

rbush.prototype = {

    search: function (bbox) {
        var result = [];

        // recursively search for items in a given bbox, collecting matches in the result array
        this._search(bbox, this.data, result);

        return result;
    },

    load: function (data) {
        // recursively build the tree with the given data from stratch using OMT algorithm
        this.data = this._build(data.slice(), 0);

        // recursively calculate all bboxes
        this._calcBBoxes(this.data, true);

        return this;
    },

    insert: function (item) {
        if (!this.data) {
            this.data = {
                children: [item],
                leaf: true,
                bbox: this._toBBox(item)
            };
        } else {
            // an index of what tree levels overflowed during this single insert
            this._overflowLevels = {};
            this._insert(item);
        }

        return this;
    },

    toJSON: function () {
        // TODO cleanup nodes from area, enlargement, sqDist properties
        return this.data;
    },

    fromJSON: function (data) {
        this.data = data;
        return this;
    },

    _search: function (bbox, node, result) {

        if (!this._intersects(bbox, node.bbox)) { return; }

        for (var i = 0, len = node.children.length, child; i < len; i++) {
            child = node.children[i];

            if (!node.leaf) {
                this._search(bbox, child, result);
            } else if (this._intersects(bbox, this._toBBox(child))) {
                result.push(child);
            }
        }
    },

{'fraction_non_alphanumeric': 0.12599300921512552, 'fraction_numerical': 0.0073085478233238, 'mean_word_length': 2.4082295614510016, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '1802882', 'n_tokens_mistral': 1904, 'n_tokens_neox': 1810, 'n_words': 543}