<commit_before>import {NumberUtils} from './util/NumberUtils';
import {ArrayUtils} from './util/ArrayUtils';

/**
 * NDArray - Largely borrowed from https://github.com/mikolalysenko/ndarray
 * but rewritten in ES6 and not string generated.
 *
 * Base class for Matrix/Vector
 */
export class NDArray {
    constructor (data, shape, stride, offset) {
        this.data = typeof data === 'number' ? [data] : data;
        this.shape = shape || [this.data.length];
        let shapeLength = this.shape.length;
        if (!stride) {
            stride = new Array(shapeLength);
            let i = shapeLength,
                size = 1;
            while (i--) {
                stride[i] = size;
                size *= this.shape[i];
            }
        }
        this.stride = stride;
        if (!offset) {
            offset = 0;
            let i = shapeLength;
            while (i--) {
                if (this.stride[i] < 0) {
                    offset -= (this.shape[i] - 1) * this.stride[i];
                }
            }
        }
        this.offset = offset;
    }

    get data () {
        return this.data_;
    }

    set data (data) {
        this.data_ = data ? ArrayUtils.fill(data) : data;
    }

    /**
     * Get the size of the NDArray
     * @returns {number}
     */
    get size () {
        return this.size_;
    }

    /**
     * Get the shape of the NDArray
     * @returns {Array}
     */
    get shape () {
        return this.shape_;
    }

    /**
     * Set the shape of the NDArray
     * @param {Array} shape
     */
    set shape (shape) {
        if (!shape) {
            this.size_ = 0;
            this.shape_ = [];
        } else {
            this.shape_ = shape.map(NumberUtils.nullToZero);
            this.size_ = this.shape_.reduce((previous, shaped) => {
                return previous *= shaped;
            }, 1);
        }
    }

    /**
     * Get the stride of the NDArray
     * @returns {Array}
     */
    get stride () {
        return this.stride_;
    }

    /**
     * Set the stride of the NDArray
     * @param {Array} stride
     */
    set stride (stride) {
        if (!stride) {
            this.order_ = [];
            this.stride_ = [];
        } else {
            this.stride_ = stride.map(NumberUtils.nullToZero);
            switch (this.stride_.length) {
                case 1:
                    this.order_ = [0];
                    break;
                case 2:
                    this.order_ = Math.abs(this.stride_[0]) > Math.abs(this.stride_[1]) ? [1, 0] : [0, 1];
                    break;
                case 3:
                    let stride0 = Math.abs(this.stride_[0]),
                        stride1 = Math.abs(this.stride_[1]),
                        stride2 = Math.abs(this.stride_[2]);
                    if (stride0 > stride1) {
                        if (stride1 > stride2) {
                            this.order_ = [2, 1, 0];
                        } else if (stride0 > stride2) {
                            this.order_ = [1, 2, 0];
                        } else {
                            this.order_ = [1, 0, 2];
                        }
                    } else {
                        if (stride0 > stride2) {
                            this.order_ = [2, 0, 1];
                        } else if (stride2 > stride1) {
                            this.order_ = [0, 1, 2];
                        } else {
                            this.order_ = [0, 2, 1];
                        }
                    }
                    break;
                default:
                    this.order_ = this.stride_
                        .map((stride, index) => [Math.abs(stride), index])
                        .sort((a, b) => a[0] - b[0])
                        .map((term) => term[1]);
            }
        }
    }

    /**
     * Get the offset of the NDArray
     * @returns {number}
     */
    get offset () {
        return this.offset_;
    }

    /**
     * Set the offset of the NDArray
     * @param {number} offset
     */
    set offset (offset) {
        this.offset_ = NumberUtils.nullToZero(offset);
    }

    /**
     * Get the order of the NDArray
     * @returns {Array}
     */
    get order () {
        return this.order_;
    }

    /**
     * Get a particular index of the NDArray
     * @param indices
     * @returns {number}
     */<commit_msg>Using symbols to hide private properties<commit_after>import {NumberUtils} from './util/NumberUtils';
import {ArrayUtils} from './util/ArrayUtils';

/* jshint -W064 */
var data_ = Symbol('data'),
    shape_ = Symbol('shape'),
    stride_ = Symbol('stride'),
    size_ = Symbol('size'),
    offset_ = Symbol('offset'),
    order_ = Symbol('order');
/* jshint +W064 */

/**
 * NDArray - Largely borrowed from https://github.com/mikolalysenko/ndarray
 * but rewritten in ES6 and not string generated.
 *
 * Base class for Matrix/Vector
 */
export class NDArray {
    constructor (data, shape, stride, offset) {
        this.data = typeof data === 'number' ? [data] : data;
        this.shape = shape || [this.data.length];
        let shapeLength = this.shape.length;
        if (!stride) {
            stride = new Array(shapeLength);
            let i = shapeLength,
                size = 1;
            while (i--) {
                stride[i] = size;
                size *= this.shape[i];
            }
        }
        this.stride = stride;
        if (!offset) {
            offset = 0;
            let i = shapeLength;
            while (i--) {
                if (this.stride[i] < 0) {
                    offset -= (this.shape[i] - 1) * this.stride[i];
                }
            }
        }
        this.offset = offset;
    }

    get data () {
        return this[data_];
    }

    set data (data) {
        this[data_] = data ? ArrayUtils.fill(data) : data;
    }

    /**
     * Get the size of the NDArray
     * @returns {number}
     */
    get size () {
        return this[size_];
    }

    /**
     * Get the shape of the NDArray
     * @returns {Array}
     */
    get shape () {
        return this[shape_];
    }

    /**
     * Set the shape of the NDArray
     * @param {Array} shape
     */
    set shape (shape) {
        if (!shape) {
            this[size_] = 0;
            this[shape_] = [];
        } else {
            this[shape_] = shape.map(NumberUtils.nullToZero);
            this[size_] = this[shape_].reduce((previous, shaped) => {
                return previous *= shaped;
            }, 1);
        }
    }

    /**
     * Get the stride of the NDArray
     * @returns {Array}
     */
    get stride () {
        return this[stride_];
    }

    /**
     * Set the stride of the NDArray
     * @param {Array} stride
     */
    set stride (stride) {
        if (!stride) {
            this[order_] = [];
            this[stride_] = [];
        } else {
            this[stride_] = stride.map(NumberUtils.nullToZero);
            switch (this[stride_].length) {
                case 1:
                    this[order_] = [0];
                    break;
                case 2:
                    this[order_] = Math.abs(this[stride_][0]) > Math.abs(this[stride_][1]) ? [1, 0] : [0, 1];
                    break;
                case 3:
                    let stride0 = Math.abs(this[stride_][0]),
                        stride1 = Math.abs(this[stride_][1]),
                        stride2 = Math.abs(this[stride_][2]);
                    if (stride0 > stride1) {
                        if (stride1 > stride2) {
                            this[order_] = [2, 1, 0];
                        } else if (stride0 > stride2) {
                            this[order_] = [1, 2, 0];
                        } else {
                            this[order_] = [1, 0, 2];
                        }
                    } else {
                        if (stride0 > stride2) {
                            this[order_] = [2, 0, 1];
                        } else if (stride2 > stride1) {
                            this[order_] = [0, 1, 2];
                        } else {
                            this[order_] = [0, 2, 1];
                        }
                    }
                    break;
                default:
                    this[order_] = this[stride_]
                        .map((stride, index) => [Math.abs(stride), index])
                        .sort((a, b) => a[0] - b[0])
                        .map((term) => term[1]);
            }
        }
    }

    /**
     * Get the offset of the NDArray
     * @returns {number}
     */
    get offset () {
        return this[offset_];
    }

    /**
     * Set the offset of the NDArray
     * @param {number} offset
     */
    set offset (offset) {
        this[offset_] = NumberUtils.nullToZero(offset);
    }

    /**
     * Get the order of the NDArray
     * @returns {Array}
     */
    get order () {
        return this[order_];
    }

    /**
     * Get a particular index of the NDArray
     * @param indices
     * @returns {number}
     */
{'fraction_non_alphanumeric': 0.12817118905801897, 'fraction_numerical': 0.01257445400397088, 'mean_word_length': 1.1249121162409186, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, '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': '7007268', 'n_tokens_mistral': 2699, 'n_tokens_neox': 2534, 'n_words': 636}