<commit_before>#include <cmath>
#include <istream>
#include <ostream>
#include <sstream>
#include <time.h>

#include "PosixTimespec.hpp"

const unsigned int PosixTimespec::nanoseconds_per_second = 1000000000;

//==============================================================================
// Initializes to 0s 0ns
//==============================================================================
PosixTimespec::PosixTimespec()
{
    tp.tv_sec  = 0;
    tp.tv_nsec = 0;
}

//==============================================================================
// Saves the provided timespec internally
//==============================================================================
PosixTimespec::PosixTimespec(const timespec& tp) :
    tp(tp)
{
}

//==============================================================================
// Converts to timespec before saving
//==============================================================================
PosixTimespec::PosixTimespec(double tp_sec)
{
    fromDouble(tp_sec);
}

//==============================================================================
// Initializes to tv_sec tv_nsec
//==============================================================================
PosixTimespec::PosixTimespec(time_t tv_sec, long tv_nsec)
{
    tp.tv_sec = tv_sec;
    tp.tv_nsec = tv_nsec;
}

//==============================================================================
// Does nothing
//==============================================================================
PosixTimespec::~PosixTimespec()
{
}

//==============================================================================
// Returns double-precision floating point representation of timespec
//==============================================================================
double PosixTimespec::toDouble() const
{
    return static_cast<double>(this->tp.tv_sec) +
        (static_cast<double>(this->tp.tv_nsec) /
         static_cast<double>(nanoseconds_per_second));
}

//==============================================================================
// Sets timespec based on provided double-precision floating point number
//==============================================================================
bool PosixTimespec::fromDouble(double tp_dbl)
{
    // This class can't represent values less than zero
    if (tp_dbl < 0.0)
    {
        return false;
    }

    double fractional_part = 0.0;
    this->tp.tv_sec  = std::modf(tp_dbl, &fractional_part);
    this->tp.tv_nsec = fractional_part *
        static_cast<double>(nanoseconds_per_second);

    return true;
}

//==============================================================================
// Does what you would expect
//==============================================================================
PosixTimespec& PosixTimespec::operator=(double tp_dbl)
{
    fromDouble(tp_dbl);

    return *this;
}

//==============================================================================
// Does what you would expect
//==============================================================================
PosixTimespec& PosixTimespec::operator=(const timespec& tp)
{
    this->tp.tv_sec  = tp.tv_sec;
    this->tp.tv_nsec = tp.tv_nsec;

    return *this;
}

//==============================================================================
// Does what you would expect
//==============================================================================
PosixTimespec& PosixTimespec::operator+=(double tp_dbl)
{
    return *this += PosixTimespec(tp_dbl);
}

//==============================================================================
// Performs integer addition so no data loss occurs; tv_nsec overflows and adds
// to tv_sec as one would expect
//==============================================================================
PosixTimespec& PosixTimespec::operator+=(const timespec& tp)
{
    this->tp.tv_sec += tp.tv_sec;
    this->tp.tv_nsec += tp.tv_nsec;

    if (this->tp.tv_nsec >= nanoseconds_per_second)
    {
        this->tp.tv_nsec -= nanoseconds_per_second;
        this->tp.tv_sec += 1;
    }

    return *this;
}

//==============================================================================
// Does what you would expect
//==============================================================================
PosixTimespec& PosixTimespec::operator+=(const PosixTimespec& tp)
{
    timespec tp_temp;
    tp.getTimespec(tp_temp);

    return operator+=(tp_temp);
}

//==============================================================================
// Does what you would expect
//==============================================================================
PosixTimespec& PosixTimespec::operator-=(double tp_dbl)
{
    return *this -= PosixTimespec(tp_dbl);
}

//==============================================================================
// Does what you would expect
//==============================================================================
PosixTimespec& PosixTimespec::operator-=(const timespec& tp)
{
    this->tp.tv_sec -= tp.tv_sec;

    if (tp.tv_nsec > this->tp.tv_nsec)
    {
        this->tp.tv_sec -= 1;
        this->tp.tv_nsec = nanoseconds_per_second -
            (tp.tv_nsec - this->tp.tv_nsec);
    }
    else
    {
        this->tp.tv_nsec -= tp.tv_nsec;
    }

    return *this;
}

//==============================================================================
// Does what you would expect
//==============================================================================
PosixTimespec& PosixTimespec::operator-=(const PosixTimespec& tp)
{
    timespec tp_temp;
    tp.getTimespec(tp_temp);

    return operator-=(tp_temp);
}

//==============================================================================
// Does what you would expect
//==============================================================================
PosixTimespec operator+(PosixTimespec lhs, const PosixTimespec& rhs)
{
    lhs += rhs;
    return lhs;
}

//==============================================================================
// Does what you would expect
//==============================================================================
PosixTimespec operator+(PosixTimespec lhs, const timespec& rhs)
{
    lhs += rhs;
    return lhs;
}

//==============================================================================
// Does what you would expect
//==============================================================================
PosixTimespec operator+(timespec lhs, const PosixTimespec& rhs)<commit_msg>One more of these to get rid of<commit_after>#include <cmath>
#include <istream>
#include <ostream>
#include <sstream>
#include <time.h>

#include "PosixTimespec.hpp"

const unsigned int PosixTimespec::nanoseconds_per_second = 1000000000;

//==============================================================================
// Initializes to 0s 0ns
//==============================================================================
PosixTimespec::PosixTimespec()
{
    tp.tv_sec  = 0;
    tp.tv_nsec = 0;
}

//==============================================================================
// Saves the provided timespec internally
//==============================================================================
PosixTimespec::PosixTimespec(const timespec& tp) :
    tp(tp)
{
}

//==============================================================================
// Converts to timespec before saving
//==============================================================================
PosixTimespec::PosixTimespec(double tp_sec)
{
    fromDouble(tp_sec);
}

//==============================================================================
// Initializes to tv_sec tv_nsec
//==============================================================================
PosixTimespec::PosixTimespec(time_t tv_sec, long tv_nsec)
{
    tp.tv_sec = tv_sec;
    tp.tv_nsec = tv_nsec;
}

//==============================================================================
// Does nothing
//==============================================================================
PosixTimespec::~PosixTimespec()
{
}

//==============================================================================
// Returns double-precision floating point representation of timespec
//==============================================================================
double PosixTimespec::toDouble() const
{
    return static_cast<double>(this->tp.tv_sec) +
        (static_cast<double>(this->tp.tv_nsec) /
         static_cast<double>(nanoseconds_per_second));
}

//==============================================================================
// Sets timespec based on provided double-precision floating point number
//==============================================================================
bool PosixTimespec::fromDouble(double tp_dbl)
{
    // This class can't represent values less than zero
    if (tp_dbl < 0.0)
    {
        return false;
    }

    double fractional_part = 0.0;
    this->tp.tv_sec  = std::modf(tp_dbl, &fractional_part);
    this->tp.tv_nsec = fractional_part *
        static_cast<double>(nanoseconds_per_second);

    return true;
}

//==============================================================================
// Does what you would expect
//==============================================================================
PosixTimespec& PosixTimespec::operator=(double tp_dbl)
{
    fromDouble(tp_dbl);

    return *this;
}

//==============================================================================
// Does what you would expect
//==============================================================================
PosixTimespec& PosixTimespec::operator=(const timespec& tp)
{
    this->tp.tv_sec  = tp.tv_sec;
    this->tp.tv_nsec = tp.tv_nsec;

    return *this;
}

//==============================================================================
// Does what you would expect
//==============================================================================
PosixTimespec& PosixTimespec::operator+=(double tp_dbl)
{
    return *this += PosixTimespec(tp_dbl);
}

//==============================================================================
// Performs integer addition so no data loss occurs; tv_nsec overflows and adds
// to tv_sec as one would expect
//==============================================================================
PosixTimespec& PosixTimespec::operator+=(const timespec& tp)
{
    this->tp.tv_sec += tp.tv_sec;
    this->tp.tv_nsec += tp.tv_nsec;

    if (this->tp.tv_nsec >= nanoseconds_per_second)
    {
        this->tp.tv_nsec -= nanoseconds_per_second;
        this->tp.tv_sec += 1;
    }

    return *this;
}

//==============================================================================
// Does what you would expect
//==============================================================================
PosixTimespec& PosixTimespec::operator+=(const PosixTimespec& tp)
{
    timespec tp_temp;
    tp.getTimespec(tp_temp);

    return *this += tp_temp;
}

//==============================================================================
// Does what you would expect
//==============================================================================
PosixTimespec& PosixTimespec::operator-=(double tp_dbl)
{
    return *this -= PosixTimespec(tp_dbl);
}

//==============================================================================
// Does what you would expect
//==============================================================================
PosixTimespec& PosixTimespec::operator-=(const timespec& tp)
{
    this->tp.tv_sec -= tp.tv_sec;

    if (tp.tv_nsec > this->tp.tv_nsec)
    {
        this->tp.tv_sec -= 1;
        this->tp.tv_nsec = nanoseconds_per_second -
            (tp.tv_nsec - this->tp.tv_nsec);
    }
    else
    {
        this->tp.tv_nsec -= tp.tv_nsec;
    }

    return *this;
}

//==============================================================================
// Does what you would expect
//==============================================================================
PosixTimespec& PosixTimespec::operator-=(const PosixTimespec& tp)
{
    timespec tp_temp;
    tp.getTimespec(tp_temp);

    return *this -= tp_temp;
}

//==============================================================================
// Does what you would expect
//==============================================================================
PosixTimespec operator+(PosixTimespec lhs, const PosixTimespec& rhs)
{
    lhs += rhs;
    return lhs;
}

//==============================================================================
// Does what you would expect
//==============================================================================
PosixTimespec operator+(PosixTimespec lhs, const timespec& rhs)
{
    lhs += rhs;
    return lhs;
}

//==============================================================================
// Does what you would expect
//==============================================================================
PosixTimespec operator+(timespec lhs, const PosixTimespec& rhs)
{'fraction_non_alphanumeric': 0.5087773329226979, 'fraction_numerical': 0.003079765937788728, 'mean_word_length': 6.224137931034483, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '1153290', 'n_tokens_mistral': 3236, 'n_tokens_neox': 2906, 'n_words': 578}