<commit_before>/**
 * Created by <NAME> on 27.12.2016.
 */

import {IPersistable} from '../index';
import {Range, all, parse, RangeLike} from '../range';
import {SelectAble, resolve as idtypes_resolve, IDType} from '../idtype';
import {IVector} from '../vector';
import {ITable, IQueryArgs} from './ITable';
import MultiTableVector from './internal/MultiTableVector';
import {IValueType, IValueTypeDesc} from '../datatype';
import {IInternalAccess} from './internal';

/**
 * base class for different Table implementations, views, transposed,...
 * @internal
 */
export abstract class ATable extends SelectAble implements IInternalAccess {
  constructor(protected root: ITable & IInternalAccess) {
    super();
  }

  get dim() {
    return this.size();
  }

  get nrow() {
    return this.dim[0];
  }

  get ncol() {
    return this.dim[1];
  }

  abstract size(): number[];

  view(range: RangeLike = all()): ITable {
    return new TableView(this.root, parse(range));
  }

  abstract colData<T>(column: string, range?: RangeLike): Promise<T[]>;

  abstract queryView(name: string, args: IQueryArgs): ITable;

  async idView(idRange: RangeLike = all()): Promise<ITable> {
    return this.view((await this.ids()).indexOf(parse(idRange)));
  }

  reduce<T, D extends IValueTypeDesc>(f: (row: IValueType[]) => T, thisArgument?: any, valuetype?: D, idtype?: IDType): IVector<T,D> {
    return new MultiTableVector(this.root, f, thisArgument, valuetype, idtype);
  }

  restore(persisted: any): IPersistable {
    if (persisted && persisted.f) {
      /* tslint:disable:no-eval */
      return this.reduce(eval(persisted.f), this, persisted.valuetype, persisted.idtype ? idtypes_resolve(persisted.idtype) : undefined);
      /* tslint:enable:no-eval */
    } else if (persisted && persisted.range) { //some view onto it
      return this.view(parse(persisted.range));
    } else {
      return <IPersistable>(<any>this);
    }
  }
}

export default ATable;

// circular dependency thus not extractable

/**
 * @internal
 */
export class TableView extends ATable implements ITable {
  constructor(root: ITable & IInternalAccess, private range: Range) {
    super(root);
    this.range = range;
  }

  get desc() {
    return this.root.desc;
  }

  persist() {
    return {
      root: this.root.persist(),
      range: this.range.toString()
    };
  }

  restore(persisted: any) {
    let r: ITable = this;
    if (persisted && persisted.range) { //some view onto it
      r = r.view(parse(persisted.range));
    }
    return r;
  }

  size() {
    return this.range.size(this.root.dim);
  }

  at(i: number, j: number) {
    const inverted = this.range.invert([i, j], this.root.dim);
    return this.root.at(inverted[0], inverted[1]);
  }

  col(i: number) {
    const inverted = this.range.invert([0, i], this.root.dim);
    return this.root.col(inverted[1]);
  }

  cols(range: RangeLike = all()) {
    return this.root.cols(this.range.swap().preMultiply(parse(range), this.root.dim));
  }

  data(range: RangeLike = all()) {
    return this.root.data(this.range.preMultiply(parse(range), this.root.dim));
  }

  colData<T>(column: string, range: RangeLike = all()) {
    return this.dataForColumn(column, range);
  }

  dataOfColumn<T>(column: string, range: RangeLike = all()) {
    // since we directly accessing the column by name there is no need for the column part of the range
    const rowRange = this.range.dim(0).preMultiply(parse(range).dim(0), this.root.dim[0]);
    return this.root.dataOfColumn<T>(column, new Range([rowRange]));
  }

  objects(range: RangeLike = all()) {
    return this.root.objects(this.range.preMultiply(parse(range), this.root.dim));
  }

  rows(range: RangeLike = all()) {
    return this.root.rows(this.range.preMultiply(parse(range), this.root.dim));
  }

  rowIds(range: RangeLike = all()) {<commit_msg>fix compile error<commit_after>/**
 * Created by <NAME> on 27.12.2016.
 */

import {IPersistable} from '../index';
import {Range, all, parse, RangeLike} from '../range';
import {SelectAble, resolve as idtypes_resolve, IDType} from '../idtype';
import {IVector} from '../vector';
import {ITable, IQueryArgs} from './ITable';
import MultiTableVector from './internal/MultiTableVector';
import {IValueType, IValueTypeDesc} from '../datatype';
import {IInternalAccess} from './internal';

/**
 * base class for different Table implementations, views, transposed,...
 * @internal
 */
export abstract class ATable extends SelectAble implements IInternalAccess {
  constructor(protected root: ITable & IInternalAccess) {
    super();
  }

  get dim() {
    return this.size();
  }

  get nrow() {
    return this.dim[0];
  }

  get ncol() {
    return this.dim[1];
  }

  abstract size(): number[];

  view(range: RangeLike = all()): ITable {
    return new TableView(this.root, parse(range));
  }

  abstract colData<T>(column: string, range?: RangeLike): Promise<T[]>;

  abstract queryView(name: string, args: IQueryArgs): ITable;

  async idView(idRange: RangeLike = all()): Promise<ITable> {
    return this.view((await this.ids()).indexOf(parse(idRange)));
  }

  reduce<T, D extends IValueTypeDesc>(f: (row: IValueType[]) => T, thisArgument?: any, valuetype?: D, idtype?: IDType): IVector<T,D> {
    return new MultiTableVector(this.root, f, thisArgument, valuetype, idtype);
  }

  restore(persisted: any): IPersistable {
    if (persisted && persisted.f) {
      /* tslint:disable:no-eval */
      return this.reduce(eval(persisted.f), this, persisted.valuetype, persisted.idtype ? idtypes_resolve(persisted.idtype) : undefined);
      /* tslint:enable:no-eval */
    } else if (persisted && persisted.range) { //some view onto it
      return this.view(parse(persisted.range));
    } else {
      return <IPersistable>(<any>this);
    }
  }
}

export default ATable;

// circular dependency thus not extractable

/**
 * @internal
 */
export class TableView extends ATable implements ITable {
  constructor(root: ITable & IInternalAccess, private range: Range) {
    super(root);
    this.range = range;
  }

  get desc() {
    return this.root.desc;
  }

  persist() {
    return {
      root: this.root.persist(),
      range: this.range.toString()
    };
  }

  restore(persisted: any) {
    let r: ITable = this;
    if (persisted && persisted.range) { //some view onto it
      r = r.view(parse(persisted.range));
    }
    return r;
  }

  size() {
    return this.range.size(this.root.dim);
  }

  at(i: number, j: number) {
    const inverted = this.range.invert([i, j], this.root.dim);
    return this.root.at(inverted[0], inverted[1]);
  }

  col(i: number) {
    const inverted = this.range.invert([0, i], this.root.dim);
    return this.root.col(inverted[1]);
  }

  cols(range: RangeLike = all()) {
    return this.root.cols(this.range.swap().preMultiply(parse(range), this.root.dim));
  }

  data(range: RangeLike = all()) {
    return this.root.data(this.range.preMultiply(parse(range), this.root.dim));
  }

  colData<T>(column: string, range: RangeLike = all()) {
    return this.dataOfColumn(column, range);
  }

  dataOfColumn<T>(column: string, range: RangeLike = all()) {
    // since we directly accessing the column by name there is no need for the column part of the range
    const rowRange = this.range.dim(0).preMultiply(parse(range).dim(0), this.root.dim[0]);
    return this.root.dataOfColumn<T>(column, new Range([rowRange]));
  }

  objects(range: RangeLike = all()) {
    return this.root.objects(this.range.preMultiply(parse(range), this.root.dim));
  }

  rows(range: RangeLike = all()) {
    return this.root.rows(this.range.preMultiply(parse(range), this.root.dim));
  }

  rowIds(range: RangeLike = all()) {
{'fraction_non_alphanumeric': 0.15684744440109247, 'fraction_numerical': 0.004421901417609572, 'mean_word_length': 3.629741119807345, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, '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': '2599362', 'n_tokens_mistral': 2593, 'n_tokens_neox': 2488, 'n_words': 610}