<commit_before>#[cfg(test)]
extern crate test;

use std::{fmt, rt};
use std::rand::{task_rng, Rng};
use std::sync::{Arc, TaskPool, RWLock, Semaphore};

#[cfg(test)]
use self::test::Bencher;

const LIVE_CELL: char = '@';
const DEAD_CELL: char = '.';

#[deriving(PartialEq, Eq, Clone)]
pub struct Board {
  board: Vec<bool>,
  survive: Vec<uint>,
  born: Vec<uint>,
  rows: uint,
  cols: uint
}

pub struct WorkerPool {
  pool: TaskPool<()>,
  size: uint
}

impl WorkerPool {
  pub fn new(size: uint) -> WorkerPool {
    WorkerPool {
      pool: TaskPool::new(size, || proc(_) {}),
      size: size
    }
  }

  pub fn new_with_default_size() -> WorkerPool {
    WorkerPool::new(rt::default_sched_threads())
  }
}

struct FutureBoard {
  board: Vec<Vec<bool>>,
  tasks_done: uint,
}

impl FutureBoard {
  fn cells(&self) -> Vec<bool> {
    self.board.as_slice().concat_vec()
  }
}

struct BoardAdvancer {
  board: Board,
  next_board: RWLock<FutureBoard>,
  done: Semaphore
}

impl BoardAdvancer {
  fn new(board: &Board, num_tasks: uint) -> BoardAdvancer {
    BoardAdvancer {
      board: board.clone(),
      next_board: RWLock::new(FutureBoard {
        board: Vec::from_elem(num_tasks, vec![]),
        tasks_done: 0
      }),
      done: Semaphore::new(0)
    }
  }

  fn advance(board: &Board, workers: &mut WorkerPool) -> Vec<bool> {
    let shared_board = Arc::new(BoardAdvancer::new(board, workers.size));
    let length = board.len();
    let all_tasks: Vec<uint> = range(0, length).collect();
    let tasks: Vec<&[uint]> = all_tasks.as_slice().chunks((length + workers.size - 1) / workers.size).collect();
    let task_count = tasks.clone().iter().len();

    for (i, task) in tasks.iter().enumerate() {
      let task_board = shared_board.clone();
      let task = task.to_vec();

      workers.pool.execute(proc(_) {
        let task_values = task.iter().map(|&idx|
          task_board.board.successor_cell(idx)
        ).collect::<Vec<bool>>();
        let mut task_results = task_board.next_board.write();
        task_results.board[i] = task_values;
        task_results.tasks_done += 1;<commit_msg>works with new TaskPool from nightly<commit_after>#[cfg(test)]
extern crate test;

use std::{fmt, rt};
use std::rand::{task_rng, Rng};
use std::sync::{Arc, TaskPool, RWLock, Semaphore};

#[cfg(test)]
use self::test::Bencher;

const LIVE_CELL: char = '@';
const DEAD_CELL: char = '.';

#[deriving(PartialEq, Eq, Clone)]
pub struct Board {
  board: Vec<bool>,
  survive: Vec<uint>,
  born: Vec<uint>,
  rows: uint,
  cols: uint
}

pub struct WorkerPool {
  pool: TaskPool,
  size: uint
}

impl WorkerPool {
  pub fn new(size: uint) -> WorkerPool {
    WorkerPool {
      pool: TaskPool::new(size),
      size: size
    }
  }

  pub fn new_with_default_size() -> WorkerPool {
    WorkerPool::new(rt::default_sched_threads())
  }
}

struct FutureBoard {
  board: Vec<Vec<bool>>,
  tasks_done: uint,
}

impl FutureBoard {
  fn cells(&self) -> Vec<bool> {
    self.board.as_slice().concat_vec()
  }
}

struct BoardAdvancer {
  board: Board,
  next_board: RWLock<FutureBoard>,
  done: Semaphore
}

impl BoardAdvancer {
  fn new(board: &Board, num_tasks: uint) -> BoardAdvancer {
    BoardAdvancer {
      board: board.clone(),
      next_board: RWLock::new(FutureBoard {
        board: Vec::from_elem(num_tasks, vec![]),
        tasks_done: 0
      }),
      done: Semaphore::new(0)
    }
  }

  fn advance(board: &Board, workers: &mut WorkerPool) -> Vec<bool> {
    let shared_board = Arc::new(BoardAdvancer::new(board, workers.size));
    let length = board.len();
    let all_tasks: Vec<uint> = range(0, length).collect();
    let tasks: Vec<&[uint]> = all_tasks.as_slice().chunks((length + workers.size - 1) / workers.size).collect();
    let task_count = tasks.clone().iter().len();

    for (i, task) in tasks.iter().enumerate() {
      let task_board = shared_board.clone();
      let task = task.to_vec();

      workers.pool.execute(proc() {
        let task_values = task.iter().map(|&idx|
          task_board.board.successor_cell(idx)
        ).collect::<Vec<bool>>();
        let mut task_results = task_board.next_board.write();
        task_results.board[i] = task_values;
        task_results.tasks_done += 1;
{'fraction_non_alphanumeric': 0.16087880935506732, 'fraction_numerical': 0.0023623907394283014, 'mean_word_length': 3.442812172088143, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 38, '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': '1010435', 'n_tokens_mistral': 1509, 'n_tokens_neox': 1515, 'n_words': 321}