<commit_before>// Copyright (C) 2017 by <NAME>, Virginia Tech Intellectual
// Properties, Inc., University of Heidelberg, and University of
// of Connecticut School of Medicine.
// All rights reserved.

// Copyright (C) 2016 by <NAME>, Virginia Tech Intellectual
// Properties, Inc., University of Heidelberg, and The University
// of Manchester.
// All rights reserved.

#include <cmath>

#include "CRootFinder.h"
#include "math/CMathEnum.h"

CRootFinder::CRootFinder():
  mRelativeTolerance(1e-6),
  mTimeLeft(std::numeric_limits< C_FLOAT64 >::quiet_NaN()),
  mTimeRight(std::numeric_limits< C_FLOAT64 >::quiet_NaN()),
  mTimeCurrent(std::numeric_limits< C_FLOAT64 >::quiet_NaN()),
  mRootsLeft(),
  mRootsRight(),
  mRootsCurrent(),
  mToggledRootsLeft(),
  mToggledRootsCurrent(),
  mToggledRootsLeftValid(false),
  mRootMask(),
  mRootMasking(CRootFinder::NONE),
  mRootError(std::numeric_limits< C_FLOAT64 >::quiet_NaN()),
  mpBrentRootValueCalculator(NULL),
  mpRootValueCalculator(NULL)
{
  mpBrentRootValueCalculator = new CBrent::EvalTemplate< CRootFinder >(this, & CRootFinder::brentRootValue);
}

CRootFinder::CRootFinder(const CRootFinder & src)
  :
  mRelativeTolerance(src.mRelativeTolerance),
  mTimeLeft(src.mTimeLeft),
  mTimeRight(src.mTimeRight),
  mTimeCurrent(src.mTimeCurrent),
  mRootsLeft(src.mRootsLeft),
  mRootsRight(src.mRootsRight),
  mRootsCurrent(src.mRootsCurrent),
  mToggledRootsLeft(src.mToggledRootsLeft),
  mToggledRootsCurrent(src.mToggledRootsCurrent),
  mToggledRootsLeftValid(src.mToggledRootsLeftValid),
  mRootMask(),
  mRootMasking(src.mRootMasking),
  mRootError(src.mRootError),
  mpBrentRootValueCalculator(NULL),
  mpRootValueCalculator(src.mpRootValueCalculator)
{
  mRootMask.initialize(src.mRootMask);
  mpBrentRootValueCalculator = new CBrent::EvalTemplate< CRootFinder >(this, & CRootFinder::brentRootValue);
}

// virtual
CRootFinder::~CRootFinder()
{
  pdelete(mpBrentRootValueCalculator);
}

void CRootFinder::initialize(CRootFinder::Eval * pRootValueCalculator,
                             const C_FLOAT64 & relativeTolerance,
                             const CVectorCore< C_INT > & rootMask)
{
  size_t numRoots = rootMask.size();

  mRootMask.initialize(rootMask);
  mRootMasking = NONE;

  mpRootValueCalculator = pRootValueCalculator;
  mRelativeTolerance = relativeTolerance;

  mRootsLeft.resize(numRoots);
  mRootsRight.resize(numRoots);
  mRootsCurrent.resize(numRoots);

  mToggledRootsLeft.resize(numRoots);
  mToggledRootsLeft = CMath::NoToggle;

  mToggledRootsCurrent.resize(numRoots);
  mToggledRootsCurrent = CMath::NoToggle;

  mToggledRootsLeftValid = false;
  mRootError = std::numeric_limits< C_FLOAT64 >::quiet_NaN();

  restart();
}

CRootFinder::ReturnStatus CRootFinder::checkRoots(const C_FLOAT64 & timeLeft,
    const C_FLOAT64 & timeRight,
    const CRootFinder::RootMasking & rootMasking)
{
  // Default return values
  ReturnStatus Status = NotFound;
  mRootMasking = rootMasking;

  // Sanity check
  if (timeLeft >= timeRight)
    {
      return InvalidInterval;
    }

  // Check whether we are restarting
  if (std::isnan(mTimeLeft) &&
      std::isnan(mTimeRight) &&
      timeLeft == mTimeCurrent)
    {
      mTimeCurrent = std::numeric_limits< C_FLOAT64 >::quiet_NaN();
      calculateCurrentRoots(timeLeft);

      // If the newly calculated roots differ only in the sign for the previously toggled roots we
      // can resume
      C_FLOAT64 * pLeftRoot = mRootsLeft.begin();
      C_FLOAT64 * pLeftRootEnd = mRootsLeft.end();
      C_FLOAT64 * pRoot = mRootsCurrent.begin();
      C_INT * pRootFound = mToggledRootsCurrent.begin();
      bool Reset = false;

      for (; pLeftRoot != pLeftRootEnd; ++pLeftRoot, ++pRoot, ++pRootFound)
        if (*pRootFound != CMath::NoToggle &&
            *pRoot == -1.0 * *pLeftRoot)
          {
            *pLeftRoot = -1.0 * *pRoot;
          }
        else if (*pRoot != *pLeftRoot)
          {
            *pLeftRoot = *pRoot;
            Reset = true;
          }

      mTimeLeft = mTimeCurrent;

      if (Reset)
        {<commit_msg>- fix compilation on windows (necessary since we #define isnan to be _isnan on windows)<commit_after>// Copyright (C) 2017 by <NAME>, Virginia Tech Intellectual
// Properties, Inc., University of Heidelberg, and University of
// of Connecticut School of Medicine.
// All rights reserved.

// Copyright (C) 2016 by <NAME>, Virginia Tech Intellectual
// Properties, Inc., University of Heidelberg, and The University
// of Manchester.
// All rights reserved.

#include <cmath>

#include "CRootFinder.h"
#include "math/CMathEnum.h"

CRootFinder::CRootFinder():
  mRelativeTolerance(1e-6),
  mTimeLeft(std::numeric_limits< C_FLOAT64 >::quiet_NaN()),
  mTimeRight(std::numeric_limits< C_FLOAT64 >::quiet_NaN()),
  mTimeCurrent(std::numeric_limits< C_FLOAT64 >::quiet_NaN()),
  mRootsLeft(),
  mRootsRight(),
  mRootsCurrent(),
  mToggledRootsLeft(),
  mToggledRootsCurrent(),
  mToggledRootsLeftValid(false),
  mRootMask(),
  mRootMasking(CRootFinder::NONE),
  mRootError(std::numeric_limits< C_FLOAT64 >::quiet_NaN()),
  mpBrentRootValueCalculator(NULL),
  mpRootValueCalculator(NULL)
{
  mpBrentRootValueCalculator = new CBrent::EvalTemplate< CRootFinder >(this, & CRootFinder::brentRootValue);
}

CRootFinder::CRootFinder(const CRootFinder & src)
  :
  mRelativeTolerance(src.mRelativeTolerance),
  mTimeLeft(src.mTimeLeft),
  mTimeRight(src.mTimeRight),
  mTimeCurrent(src.mTimeCurrent),
  mRootsLeft(src.mRootsLeft),
  mRootsRight(src.mRootsRight),
  mRootsCurrent(src.mRootsCurrent),
  mToggledRootsLeft(src.mToggledRootsLeft),
  mToggledRootsCurrent(src.mToggledRootsCurrent),
  mToggledRootsLeftValid(src.mToggledRootsLeftValid),
  mRootMask(),
  mRootMasking(src.mRootMasking),
  mRootError(src.mRootError),
  mpBrentRootValueCalculator(NULL),
  mpRootValueCalculator(src.mpRootValueCalculator)
{
  mRootMask.initialize(src.mRootMask);
  mpBrentRootValueCalculator = new CBrent::EvalTemplate< CRootFinder >(this, & CRootFinder::brentRootValue);
}

// virtual
CRootFinder::~CRootFinder()
{
  pdelete(mpBrentRootValueCalculator);
}

void CRootFinder::initialize(CRootFinder::Eval * pRootValueCalculator,
                             const C_FLOAT64 & relativeTolerance,
                             const CVectorCore< C_INT > & rootMask)
{
  size_t numRoots = rootMask.size();

  mRootMask.initialize(rootMask);
  mRootMasking = NONE;

  mpRootValueCalculator = pRootValueCalculator;
  mRelativeTolerance = relativeTolerance;

  mRootsLeft.resize(numRoots);
  mRootsRight.resize(numRoots);
  mRootsCurrent.resize(numRoots);

  mToggledRootsLeft.resize(numRoots);
  mToggledRootsLeft = CMath::NoToggle;

  mToggledRootsCurrent.resize(numRoots);
  mToggledRootsCurrent = CMath::NoToggle;

  mToggledRootsLeftValid = false;
  mRootError = std::numeric_limits< C_FLOAT64 >::quiet_NaN();

  restart();
}

CRootFinder::ReturnStatus CRootFinder::checkRoots(const C_FLOAT64 & timeLeft,
    const C_FLOAT64 & timeRight,
    const CRootFinder::RootMasking & rootMasking)
{
  // Default return values
  ReturnStatus Status = NotFound;
  mRootMasking = rootMasking;

  // Sanity check
  if (timeLeft >= timeRight)
    {
      return InvalidInterval;
    }

  // Check whether we are restarting
  if (isnan(mTimeLeft) &&
      isnan(mTimeRight) &&
      timeLeft == mTimeCurrent)
    {
      mTimeCurrent = std::numeric_limits< C_FLOAT64 >::quiet_NaN();
      calculateCurrentRoots(timeLeft);

      // If the newly calculated roots differ only in the sign for the previously toggled roots we
      // can resume
      C_FLOAT64 * pLeftRoot = mRootsLeft.begin();
      C_FLOAT64 * pLeftRootEnd = mRootsLeft.end();
      C_FLOAT64 * pRoot = mRootsCurrent.begin();
      C_INT * pRootFound = mToggledRootsCurrent.begin();
      bool Reset = false;

      for (; pLeftRoot != pLeftRootEnd; ++pLeftRoot, ++pRoot, ++pRootFound)
        if (*pRootFound != CMath::NoToggle &&
            *pRoot == -1.0 * *pLeftRoot)
          {
            *pLeftRoot = -1.0 * *pRoot;
          }
        else if (*pRoot != *pLeftRoot)
          {
            *pLeftRoot = *pRoot;
            Reset = true;
          }

      mTimeLeft = mTimeCurrent;

      if (Reset)
        {
{'fraction_non_alphanumeric': 0.11354242645271091, 'fraction_numerical': 0.009238998298079261, 'mean_word_length': 4.047239263803681, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, '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': '3082932', 'n_tokens_mistral': 2865, 'n_tokens_neox': 2772, 'n_words': 506}