<commit_before>import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Observable';

interface Cell {
  x: number;
  y: number;
  alive: boolean;
}

interface Rules {
  toSurvive: Array<number>;
  toComeAlive: Array<number>;
}

@Injectable()
export class LifeService {

  private _state: BehaviorSubject<Array<Cell>>;
  private _generations: BehaviorSubject<number>;
  private rules: Rules;
  private cols: number;
  private rows: number;
  private universe = [];

  constructor() {
    this._state = new BehaviorSubject([]);
    this._generations = new BehaviorSubject(0);
  }

  get state(): Observable<Array<Cell>> {
    return this._state.asObservable();
  }

  get generations(): Observable<number> {
    return this._generations.asObservable();
  }

  initUniverse(cols: number, rows: number): void {
    this.cols = cols;
    this.rows = rows;
    this.universe = [];

    for (let j = 0; j < rows; j++) {
      for (let i = 0; i < cols; i++) {
        this.universe.push({ x: i, y: j, alive: false });
      }
    }

    this._state.next(this.universe);
  }

  setRules(toSurvive: Array<number>, toComeAlive: Array<number>): void {
    this.rules = { toSurvive, toComeAlive };
  }

  loadState(state: Array<number>): void {
    this.universe.forEach((cell, index) => cell.alive = state[index]);
    this._state.next(this.universe);
    this._generations.next(0);
  }

  nextGeneration() {
    this.universe = this.universe.map(this.evolve, this);
    this._state.next(this.universe);
    this._generations.next(this._generations.getValue() + 1);
  }

  private isAlive(x: number, y: number): boolean {
    return this.universe[y * this.cols + x].alive;
  }

  private evolve(cell: Cell) {
    const x = cell.x;
    const y = cell.y;
    let alive = cell.alive;
    let aliveNeighbors = 0;
<commit_msg>Added new interfaces, implement loadState<commit_after>import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Observable';

interface Cell {
  x: number;
  y: number;
  alive: boolean;
}

export interface Rules {
  toSurvive: Array<number>;
  toComeAlive: Array<number>;
}

export interface Block {
  start: { x: number, y: number };
  cells: Array<Array<number>>;
}

export interface State {
  rules: Rules;
  blocks: Array<Block>;
}

@Injectable()
export class LifeService {

  private _state: Subject<Array<Cell>>;
  private _generations: BehaviorSubject<number>;
  private rules: Rules;
  private cols: number;
  private rows: number;
  private universe = [];

  constructor() {
    this._state = new Subject();
    this._generations = new BehaviorSubject(0);
  }

  get state(): Observable<Array<Cell>> {
    return this._state.asObservable();
  }

  get generations(): Observable<number> {
    return this._generations.asObservable();
  }

  initUniverse(cols: number, rows: number): void {
    this.cols = cols;
    this.rows = rows;
    this.universe = [];

    for (let j = 0; j < rows; j++) {
      for (let i = 0; i < cols; i++) {
        this.universe.push({ x: i, y: j, alive: false });
      }
    }

    this._state.next(this.universe);
  }

  setRules(rules: Rules): void {
    this.rules = rules;
  }

  loadState(state: State): void {
    this.universe.forEach(cell => cell.alive = false);

    state.blocks.forEach(block => {
      const x = block.start.x;
      let y = block.start.y;
      let index = y * this.cols + x;
      block.cells.forEach(cellLine => {
        cellLine.forEach(cell => this.universe[index++].alive = cell);
        index = ++y * this.cols + x;
      });
    });

    this._state.next(this.universe);
    this._generations.next(0);
  }

  nextGeneration() {
    this.universe = this.universe.map(this.evolve, this);
    this._state.next(this.universe);
    this._generations.next(this._generations.getValue() + 1);
  }

  private isAlive(x: number, y: number): boolean {
    return this.universe[y * this.cols + x].alive;
  }

  private evolve(cell: Cell) {
    const x = cell.x;
    const y = cell.y;
    let alive = cell.alive;
    let aliveNeighbors = 0;

{'fraction_non_alphanumeric': 0.145234133586879, 'fraction_numerical': 0.0028523888756833847, 'mean_word_length': 3.229145728643216, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 30, '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': '6504234', 'n_tokens_mistral': 1440, 'n_tokens_neox': 1395, 'n_words': 332}