<commit_before>
#ifndef SYSTEM_H_
#define SYSTEM_H_

#include <random>
#include <vector>
#include <list>
#include <map>
#include <algorithm>

#include "phys/object.h"
#include "phys/neutron.h"
#include "phys/toolbar.h"

#define SHOW(X) \
  std::cout << __FILE__ << ":" << __LINE__ << ": "#X" = " << X << "\n"

namespace phys {

class System {
 public:
  System(int w, int h)
    : width_(w),
      height_(h),
      neutron_weight_(1),
      prevpow_(0),
      normal1_(Neutron::kNomSpeed, Neutron::kNomSpeed / 8.0),
      blank_(Object::Rect {0, 0, 1, 1}) {
    //fraction of window width for toolbar
    int tb_w = 130;
    toolbar_.Init(Object::Rect {width_ - tb_w,0,tb_w,height_}, sdl::Color::gray());
    AddObject(&toolbar_);
  };

  ~System() { };

  bool InToolbar(Object* o) {
    SDL_Rect a = o->rect();
    SDL_Rect b = toolbar_.rect();
    return SDL_HasIntersection(&a, &b);
  };

  bool InToolbar(int x, int y) {
    SDL_Rect a = {x, y, 1, 1};
    SDL_Rect b = toolbar_.rect();
    return SDL_HasIntersection(&a, &b);
  };

  Toolbar& toolbar() {return toolbar_; };

  void AddObject(Object* o) {
    objs_.push_back(o);
  };

  void RemoveObject(Object *o)
  {
    std::vector<Object*>::iterator it;
    for(unsigned int i=0; i < objs_.size(); i++)
      {
	if ( objs_[i] == o )
	  {
	    it = objs_.erase(objs_.begin()+i);
	  }
      }
  };

  void AddNeutrons(Neutron::Pop ns) {
    neutrons_.insert(neutrons_.end(), ns.begin(), ns.end());
  };

  double period() const {
    return period_;
  }

  void Tick(double deltat) {
    double dtmax = 0.03;
    if (deltat > 1.1*dtmax) {
      // roulette off neutrons if we have too many
      double pkill = (deltat - dtmax) / deltat;
      int nkill = 0;
      int i = 0;
      while (i < neutrons_.size()) {
        double r = uniform01_(rand_gen_);
        if (r < pkill) {
          nkill++;
          neutrons_[i] = neutrons_[neutrons_.size() - 1];
          neutrons_.pop_back();
        } else {
          i++;
        }
      }<commit_msg>safety check to prevent divide-by-zero<commit_after>
#ifndef SYSTEM_H_
#define SYSTEM_H_

#include <random>
#include <vector>
#include <list>
#include <map>
#include <algorithm>

#include "phys/object.h"
#include "phys/neutron.h"
#include "phys/toolbar.h"

#define SHOW(X) \
  std::cout << __FILE__ << ":" << __LINE__ << ": "#X" = " << X << "\n"

namespace phys {

class System {
 public:
  System(int w, int h)
    : width_(w),
      height_(h),
      neutron_weight_(1),
      prevpow_(0),
      normal1_(Neutron::kNomSpeed, Neutron::kNomSpeed / 8.0),
      blank_(Object::Rect {0, 0, 1, 1}) {
    //fraction of window width for toolbar
    int tb_w = 130;
    toolbar_.Init(Object::Rect {width_ - tb_w,0,tb_w,height_}, sdl::Color::gray());
    AddObject(&toolbar_);
  };

  ~System() { };

  bool InToolbar(Object* o) {
    SDL_Rect a = o->rect();
    SDL_Rect b = toolbar_.rect();
    return SDL_HasIntersection(&a, &b);
  };

  bool InToolbar(int x, int y) {
    SDL_Rect a = {x, y, 1, 1};
    SDL_Rect b = toolbar_.rect();
    return SDL_HasIntersection(&a, &b);
  };

  Toolbar& toolbar() {return toolbar_; };

  void AddObject(Object* o) {
    objs_.push_back(o);
  };

  void RemoveObject(Object *o)
  {
    std::vector<Object*>::iterator it;
    for(unsigned int i=0; i < objs_.size(); i++)
      {
	if ( objs_[i] == o )
	  {
	    it = objs_.erase(objs_.begin()+i);
	  }
      }
  };

  void AddNeutrons(Neutron::Pop ns) {
    neutrons_.insert(neutrons_.end(), ns.begin(), ns.end());
  };

  double period() const {
    return period_;
  }

  void Tick(double deltat) {
    double dtmax = 0.03;
    if (neutrons_.size() > 0 && deltat > 1.1*dtmax) {
      // roulette off neutrons if we have too many
      double pkill = (deltat - dtmax) / deltat;
      int nkill = 0;
      int i = 0;
      while (i < neutrons_.size()) {
        double r = uniform01_(rand_gen_);
        if (r < pkill) {
          nkill++;
          neutrons_[i] = neutrons_[neutrons_.size() - 1];
          neutrons_.pop_back();
        } else {
          i++;
        }
      }
{'fraction_non_alphanumeric': 0.16818293582493238, 'fraction_numerical': 0.013031718711581017, 'mean_word_length': 2.5809859154929575, 'pattern_counts': {'":': 4, '<': 45, '<?xml version=': 0, '>': 20, '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': '1247466', 'n_tokens_mistral': 1583, 'n_tokens_neox': 1493, 'n_words': 352}