<commit_before>'use strict';
class SpatialHash{
    constructor(range, cellSize){
        var bounds = getBounds(range);
        this.cellSize = cellSize;
        if (range.width%cellSize !== 0 || range.height%cellSize !== 0)
            throw "Exception: width and height must both be divisible by cell size";
    
        this._horizontalCells = range.width/cellSize;
        this._verticalCells = range.height/cellSize;
        this.hash = { };

        this._hStart = Math.floor(bounds.left / cellSize);
        this._hEnd = Math.floor(bounds.right / cellSize)-1;
        this._vStart = Math.floor(bounds.top / cellSize);
        this._vEnd = Math.floor(bounds.bottom / cellSize)-1;

        var i, j, a;
        for (i = this._hStart; i <= this._hEnd; i++){
            a = { };
            for (j = this._vStart; j <= this._vEnd; j++)
                a[j] = [];
            this.hash[i] = a;
        }

        this.objectCount = 0;
        this.cellCount = this._horizontalCells * this._verticalCells;
        this._id = -9e15; //max number of objects
    }

    insert(item) {
        if (!item.range)
            throw "Exception: item has no range object";
        var bounds = getBounds(item.range);
        var hStart = Math.max(Math.floor(bounds.left / this.cellSize), this._hStart);
        var hEnd = Math.min(Math.floor(bounds.right / this.cellSize), this._hEnd);
        var vStart = Math.max(Math.floor(bounds.top / this.cellSize), this._vStart);
        var vEnd = Math.min(Math.floor(bounds.bottom / this.cellSize), this._vEnd);
   
        item.__b = {
            hStart: hStart,
            hEnd: hEnd,
            vStart: vStart,
            vEnd: vEnd,
            id: this._id++
        };

        var i, j;
        console.log(hStart);
        console.log(hEnd);
        console.log(vStart);
        console.log(vEnd);
        for (i = hStart; i <= hEnd; i++) {
            for (j = vStart; j <= vEnd; j++)
                this.hash[i][j].push(item);
        }

        if (this.objectCount++ >= 9e15)
            throw "Exception: more than 9E15 (900 000 000 000 000) objects";
        else if (this._id > 9e15 - 1)
            this._id = -9e15;
    }

    remove(item) {
        if (!item.__b) return;
        var hStart = item.__b.hStart;
        var hEnd = item.__b.hEnd;
        var vStart = item.__b.vStart;
        var vEnd = item.__b.vEnd;

        var i, j, k;
        for (i = hStart; i <= hEnd; i++) {
            for (j = vStart; j <= vEnd; j++) {
                k = this.hash[i][j].indexOf(item);
                if (k !== -1) this.hash[i][j].splice(k, 1);
            }
        }
        if (!(delete item.__b)) item.__b = undefined;
        this.objectCount--;
    }

    update(item) {
        this.remove(item);
        this.insert(item);
    }

    __srch(range, selector, callback, returnOnFirst) {
        var bounds = getBounds(range),
            cellSize = this.cellSize;

        // range might be larger than the hash's size itself
        var hStart = Math.max(Math.floor(bounds.left / cellSize), this._hStart);
        var hEnd = Math.min(Math.floor(bounds.right / cellSize), this._hEnd);
        var vStart = Math.max(Math.floor(bounds.top / cellSize), this._vStart);
        var vEnd = Math.min(Math.floor(bounds.bottom / cellSize), this._vEnd);

        var i , j, k, l, m, o = [], p = [];
        for (i = hStart; i <= hEnd; i++) {
            for (j = vStart; j <= vEnd; j++) {
                k = this.hash[i][j];
                l = k.length;
                for (m = 0; m < l; m++)
                    if (intersects(k[m].range, range) && p.indexOf(k[m].__b.id) === -1) {
                        p.push(k[m].__b.id);
                        if (selector) if (!selector(k[m])) continue;
                        if (callback) callback(k[m]);
                        if (returnOnFirst) return true;
                        o.push(k[m]);
                    }
            }
        }
        if (returnOnFirst) return false;
        return o;
    };

    any(range) {
        return this.__srch(range, null, null, true);
    }

    query(range, selector) {
        return this.__srch(range, selector, null, false);
    }

    find(range, callback) {
        return this.__srch(range, null, callback, false);
    }

}


function intersects(a, b) {
    return a.x <= b.x + b.width
        && a.x + a.width >= b.x
        && a.y <= b.y + b.height
        && a.y + a.height >= b.y;
}

function getBounds(range) {
    return {
        left: range.x,
        right: range.x + range.width,
        top: range.y,
        bottom: range.y + range.height
    };
}
<commit_msg>fixed bug with negative starting coordinates<commit_after>'use strict';
class SpatialHash{
    constructor(range, cellSize){
        //var bounds = getBounds(range);
        this.cellSize = cellSize;
        if (range.width%cellSize !== 0 || range.height%cellSize !== 0)
            throw "Exception: width and height must both be divisible by cell size";
    
        this._horizontalCells = range.width/cellSize;
        this._verticalCells = range.height/cellSize;
        this.hash = [];

        // this._hStart = 0;
        // this._hEnd = this._horizontalCells-1;
        // this._vStart = 0;
        // this._vEnd = this._verticalCells-1;
        this.range= range;

        var i, j, a;
        for (i = 0; i <= this._horizontalCells-1; i++){
            a = [];
            for (j = 0; j <= this._verticalCells-1; j++)
                a.push([]);
            this.hash.push(a);
        }

        this.objectCount = 0;
        this.cellCount = this._horizontalCells * this._verticalCells;
        this._id = -9e15; //max number of objects
    }

    insert(item) {
        if (!item.range)
            throw "Exception: item has no range object";
        var bounds = getBounds(item.range);
        var hStart = Math.max(~~((bounds.left-this.range.x) / this.cellSize), 0);
        var hEnd = Math.min(~~((bounds.right-this.range.x) / this.cellSize), this._horizontalCells-1);
        var vStart = Math.max(~~((bounds.top-this.range.y) / this.cellSize), 0);
        var vEnd = Math.min(~~((bounds.bottom-this.range.y) / this.cellSize), this._verticalCells-1);
   
        item.__b = {
            hStart: hStart,
            hEnd: hEnd,
            vStart: vStart,
            vEnd: vEnd,
            id: this._id++
        };

        var i, j;
        console.log(hStart);
        console.log(hEnd);
        console.log(vStart);
        console.log(vEnd);
        for (i = hStart; i <= hEnd; i++) {
            for (j = vStart; j <= vEnd; j++)
                this.hash[i][j].push(item);
        }

        if (this.objectCount++ >= 9e15)
            throw "Exception: more than 9E15 (900 000 000 000 000) objects";
        else if (this._id > 9e15 - 1)
            this._id = -9e15;
    }

    remove(item) {
        if (!item.__b) return;
        var hStart = item.__b.hStart;
        var hEnd = item.__b.hEnd;
        var vStart = item.__b.vStart;
        var vEnd = item.__b.vEnd;

        var i, j, k;
        for (i = hStart; i <= hEnd; i++) {
            for (j = vStart; j <= vEnd; j++) {
                k = this.hash[i][j].indexOf(item);
                if (k !== -1) this.hash[i][j].splice(k, 1);
            }
        }
        if (!(delete item.__b)) item.__b = undefined;
        this.objectCount--;
    }

    update(item) {
        this.remove(item);
        this.insert(item);
    }

    __srch(range, selector, callback, returnOnFirst) {
        var bounds = getBounds(range),
            cellSize = this.cellSize;

        // range might be larger than the hash's size itself
        var hStart = Math.max(~~((bounds.left-this.range.x) / this.cellSize), 0);
        var hEnd = Math.min(~~((bounds.right-this.range.x) / this.cellSize), this._horizontalCells-1);
        var vStart = Math.max(~~((bounds.top-this.range.y) / this.cellSize), 0);
        var vEnd = Math.min(~~((bounds.bottom-this.range.y) / this.cellSize), this._verticalCells-1);
   

        var i , j, k, l, m, o = [], p = [];
        for (i = hStart; i <= hEnd; i++) {
            for (j = vStart; j <= vEnd; j++) {
                k = this.hash[i][j];
                l = k.length;
                for (m = 0; m < l; m++)
                    if (intersects(k[m].range, range) && p.indexOf(k[m].__b.id) === -1) {
                        p.push(k[m].__b.id);
                        if (selector) if (!selector(k[m])) continue;
                        if (callback) callback(k[m]);
                        if (returnOnFirst) return true;
                        o.push(k[m]);
                    }
            }
        }
        if (returnOnFirst) return false;
        return o;
    };

    any(range) {
        return this.__srch(range, null, null, true);
    }

    query(range, selector) {
        return this.__srch(range, selector, null, false);
    }

    find(range, callback) {
        return this.__srch(range, null, callback, false);
    }
    print(){
        var i, j;
        for (i = 0; i <= this._horizontalCells-1; i++){
            console.log(this.hash[i]);
        }
    }  
}



function intersects(a, b) {
    return a.x <= b.x + b.width
        && a.x + a.width >= b.x
        && a.y <= b.y + b.height
        && a.y + a.height >= b.y;
}

function getBounds(range) {
    return {
        left: range.x,
        right: range.x + range.width,
        top: range.y,
        bottom: range.y + range.height
    };
}

{'fraction_non_alphanumeric': 0.15006345177664976, 'fraction_numerical': 0.01015228426395939, 'mean_word_length': 1.6081081081081081, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 11, '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': '2024906', 'n_tokens_mistral': 3168, 'n_tokens_neox': 3016, 'n_words': 715}