<commit_before>import { QR } from './interfaces';
import {qrRange, range, isInteger} from './util';
import { Vector3 } from 'three';

export default class Grid<T> {
    private data: T[][] = []
    private halfWidth = this._width / 2
    private halfHeight = this._height / 2

    get length(): number {
        return this._width * this._height
    }

    get width(): number {
        return this._width
    }

    get height(): number {
        return this._height
    }

    constructor(private _width: number, private _height: number) {
        if (_width % 2 != 0 || _height % 2 != 0) {
            throw new Error("With and height of grid must be divisible by 2")
        }
        this.data = []
    }

    forEachQR(f: (q: number, r: number, existingItem?: T)=>void) {
        const {_width, _height} = this

        for (var i = -this.halfWidth; i < this.halfWidth; i++) {
            for (var j = -this.halfHeight; j < this.halfHeight; j++) {
                const q = i - j / 2 + ((-_height / 2 + j) % 2) / 2
                const r = j

                f(q, r, this.get(q, r))
            }
        }

        return this
    }

    /**
     * Iterates over the grid using the indices (i,j), where i = [0..width-1] and j = [0..height-1].
     * (0, 0) corresponds to the upper left corner, (width-1, height-1) to the bottom right corner.
     */
    forEachIJ(f: (i: number, j: number, q: number, r: number, item?: T)=>void) {
        const {_width, _height} = this

        for (var i = -this.halfWidth; i < this.halfWidth; i++) {
            for (var j = -this.halfHeight; j < this.halfHeight; j++) {
                const q = i - j / 2 + ((-_height / 2 + j) % 2) / 2
                const r = j

                f(i + this.halfWidth, j + this.halfHeight, q, r, this.get(q, r))
            }
        }

        return this
    }

    initQR(f: (q: number, r: number, existingItem?: T) => T) {
        return this.forEachQR((q,r,item) => this.add(q, r, f(q, r, item)))
    }

    mapQR<R>(f: (q: number, r: number, existingItem?: T) => R) {
        const mapped = new Grid<R>(this._width, this._height)
        this.forEachQR((q,r,item) => mapped.add(q, r, f(q, r, item)))
        return mapped
    }

    toArray(): T[] {
        const arr: T[] = new Array(this._width * this._height)
        var i = 0
<commit_msg>Grid.init added<commit_after>import { QR } from './interfaces';
import {qrRange, range, isInteger} from './util';
import { Vector3 } from 'three';

export default class Grid<T> {
    private data: T[][] = []
    private halfWidth = this._width / 2
    private halfHeight = this._height / 2

    get length(): number {
        return this._width * this._height
    }

    get width(): number {
        return this._width
    }

    get height(): number {
        return this._height
    }

    constructor(private _width: number, private _height: number) {
        if (_width % 2 != 0 || _height % 2 != 0) {
            throw new Error("With and height of grid must be divisible by 2")
        }
        this.data = []
    }

    forEachQR(f: (q: number, r: number, existingItem?: T)=>void) {
        const {_width, _height} = this

        for (var i = -this.halfWidth; i < this.halfWidth; i++) {
            for (var j = -this.halfHeight; j < this.halfHeight; j++) {
                const q = i - j / 2 + ((-_height / 2 + j) % 2) / 2
                const r = j

                f(q, r, this.get(q, r))
            }
        }

        return this
    }

    /**
     * Iterates over the grid using the indices (i,j), where i = [0..width-1] and j = [0..height-1].
     * (0, 0) corresponds to the upper left corner, (width-1, height-1) to the bottom right corner.
     */
    forEachIJ(f: (i: number, j: number, q: number, r: number, item?: T)=>void) {
        const {_width, _height} = this

        for (var i = -this.halfWidth; i < this.halfWidth; i++) {
            for (var j = -this.halfHeight; j < this.halfHeight; j++) {
                const q = i - j / 2 + ((-_height / 2 + j) % 2) / 2
                const r = j

                f(i + this.halfWidth, j + this.halfHeight, q, r, this.get(q, r))
            }
        }

        return this
    }

    init(items: T[], getQR: (item: T) => QR) {
        items.forEach(item => {
            const qr = getQR(item)
            this.add(qr.q, qr.r, item)
        })
    }

    initQR(f: (q: number, r: number, existingItem?: T) => T) {
        return this.forEachQR((q,r,item) => this.add(q, r, f(q, r, item)))
    }

    mapQR<R>(f: (q: number, r: number, existingItem?: T) => R) {
        const mapped = new Grid<R>(this._width, this._height)
        this.forEachQR((q,r,item) => mapped.add(q, r, f(q, r, item)))
        return mapped
    }

    toArray(): T[] {
        const arr: T[] = new Array(this._width * this._height)
        var i = 0

{'fraction_non_alphanumeric': 0.1547692944340989, 'fraction_numerical': 0.010345541071798055, 'mean_word_length': 1.8072009291521487, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1294079', 'n_tokens_mistral': 1680, 'n_tokens_neox': 1575, 'n_words': 500}