<commit_before>import {GLM} from "gl-matrix";
import {Func3} from "../Base/Delegates";
import {InvalidStringException} from "../Exceptions";
interface IVectorParseDescription {
  needNormalize: boolean;
  needNegate: boolean;
  coefficient: number;
  elements: number[];
}

class VectorBase {

  public rawElements: GLM.IArray;
  private magnitudeSquaredCache: number = -1;
  private magnitudeCache: number = -1;

  public get magnitude() {
    if (this.magnitudeCache < 0) {
      this.magnitudeCache = Math.sqrt(this.sqrMagnitude);
    }
    return this.magnitudeCache;
  }

  public get ElementCount(): number {
    return 0;
  }

  public get sqrMagnitude(): number {
    if (this.magnitudeSquaredCache < 0) {
      let sum = 0;
      let r = this.rawElements;
      for (let i = 0; i < this.ElementCount; i++) {
        sum += r[i] * r[i];
      }
      this.magnitudeSquaredCache = sum;
    }
    return this.magnitudeSquaredCache;
  }

  protected static elementEqual(v1: VectorBase, v2: VectorBase): boolean {
    if (v1.ElementCount !== v2.ElementCount) {
      return false;
    }
    for (let i = 0; i < v1.ElementCount; i++) {
      if (v1.rawElements[i] !== v2.rawElements[i]) {
        return false;
      }
    }
    return true;
  }

  protected static fromGenerationFunction<T extends VectorBase>(v1: T, v2: T, gen: Func3<number, T, T, number>): GLM.IArray {
    let f = new Float32Array(v1.ElementCount);
    for (let i = 0; i < f.length; i++) {
      f[i] = gen(i, v1, v2);
    }
    return f;
  }

  protected static __parse(str: string): IVectorParseDescription {
    const checkRegex = /(-?)([\d,E\+\-\.]+)?(n)?\(([-\d,E\+\.\s]+)\)/g;
    const matches = checkRegex.exec(str);
    if (matches) {
      if (!matches[4]) { // When (x,x,x,x) was not specifed
        throw new InvalidStringException(`The specified string '${str}' is not containing braced vector.`);
      }
      return {
        needNormalize: matches[3] === "n",
        needNegate: matches[1] === "-",
        coefficient:parseFloat(matches[2]),
        elements: VectorBase.__parseRawVector(matches[4])
      };
    } else {
      // Assume this is simplified format.
      return {
        needNormalize: false,
        needNegate: false,
        elements: VectorBase.__parseRawVector(str),
        coefficient: undefined
      };
    }
  }

  private static __parseRawVector(str: string): number[] {
    const splitted = str.split(",");
    const result = new Array(splitted.length);
    for (let i = 0; i < splitted.length; i++) {
      result[i] = parseFloat(splitted[i]);
    }
    return result;
  }
}

export default VectorBase;
<commit_msg>fix: adapt to lint<commit_after>import {GLM} from "gl-matrix";
import {Func3} from "../Base/Delegates";
import {InvalidStringException} from "../Exceptions";
interface IVectorParseDescription {
  needNormalize: boolean;
  needNegate: boolean;
  coefficient: number;
  elements: number[];
}

class VectorBase {

  public rawElements: GLM.IArray;
  private magnitudeSquaredCache: number = -1;
  private magnitudeCache: number = -1;

  public get magnitude() {
    if (this.magnitudeCache < 0) {
      this.magnitudeCache = Math.sqrt(this.sqrMagnitude);
    }
    return this.magnitudeCache;
  }

  public get ElementCount(): number {
    return 0;
  }

  public get sqrMagnitude(): number {
    if (this.magnitudeSquaredCache < 0) {
      let sum = 0;
      let r = this.rawElements;
      for (let i = 0; i < this.ElementCount; i++) {
        sum += r[i] * r[i];
      }
      this.magnitudeSquaredCache = sum;
    }
    return this.magnitudeSquaredCache;
  }

  protected static elementEqual(v1: VectorBase, v2: VectorBase): boolean {
    if (v1.ElementCount !== v2.ElementCount) {
      return false;
    }
    for (let i = 0; i < v1.ElementCount; i++) {
      if (v1.rawElements[i] !== v2.rawElements[i]) {
        return false;
      }
    }
    return true;
  }

  protected static fromGenerationFunction<T extends VectorBase>(v1: T, v2: T, gen: Func3<number, T, T, number>): GLM.IArray {
    let f = new Float32Array(v1.ElementCount);
    for (let i = 0; i < f.length; i++) {
      f[i] = gen(i, v1, v2);
    }
    return f;
  }

  protected static __parse(str: string): IVectorParseDescription {
    const checkRegex = /(-?)([\d,E\+\-\.]+)?(n)?\(([-\d,E\+\.\s]+)\)/g;
    const matches = checkRegex.exec(str);
    if (matches) {
      if (!matches[4]) { // When (x,x,x,x) was not specifed
        throw new InvalidStringException(`The specified string '${str}' is not containing braced vector.`);
      }
      return {
        needNormalize: matches[3] === "n",
        needNegate: matches[1] === "-",
        coefficient: parseFloat(matches[2]),
        elements: VectorBase.__parseRawVector(matches[4])
      };
    } else {
      // Assume this is simplified format.
      return {
        needNormalize: false,
        needNegate: false,
        elements: VectorBase.__parseRawVector(str),
        coefficient: undefined
      };
    }
  }

  private static __parseRawVector(str: string): number[] {
    const splitted = str.split(",");
    const result = new Array(splitted.length);
    for (let i = 0; i < splitted.length; i++) {
      result[i] = parseFloat(splitted[i]);
    }
    return result;
  }
}

export default VectorBase;

{'fraction_non_alphanumeric': 0.14364118092354278, 'fraction_numerical': 0.011733535200605601, 'mean_word_length': 2.745570517363572, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 7, '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': '1051444', 'n_tokens_mistral': 1778, 'n_tokens_neox': 1729, 'n_words': 436}