2016-07-10 12:54:39 +00:00
|
|
|
//
|
|
|
|
// Storage.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 10/07/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef Storage_hpp
|
|
|
|
#define Storage_hpp
|
|
|
|
|
2016-07-29 11:31:02 +00:00
|
|
|
#include "../NumberTheory/Factors.hpp"
|
|
|
|
|
2016-07-10 12:54:39 +00:00
|
|
|
namespace Storage {
|
|
|
|
|
2016-08-01 10:04:55 +00:00
|
|
|
/*!
|
|
|
|
Contains either an absolute time or a time interval, described as a quotient, in terms of a
|
|
|
|
clock rate to which the time is relative and its length in cycles based on that clock rate.
|
|
|
|
*/
|
2016-07-10 12:54:39 +00:00
|
|
|
struct Time {
|
|
|
|
unsigned int length, clock_rate;
|
2016-08-03 10:59:45 +00:00
|
|
|
Time() : length(0), clock_rate(1) {}
|
2016-07-29 11:31:02 +00:00
|
|
|
|
2016-08-01 10:04:55 +00:00
|
|
|
/*!
|
|
|
|
Reduces this @c Time to its simplest form — eliminates all common factors from @c length
|
|
|
|
and @c clock_rate.
|
|
|
|
*/
|
2016-08-05 01:36:39 +00:00
|
|
|
inline Time &simplify()
|
2016-07-29 11:31:02 +00:00
|
|
|
{
|
|
|
|
unsigned int common_divisor = NumberTheory::greatest_common_divisor(length, clock_rate);
|
|
|
|
length /= common_divisor;
|
|
|
|
clock_rate /= common_divisor;
|
2016-08-05 01:36:39 +00:00
|
|
|
return *this;
|
2016-07-29 11:31:02 +00:00
|
|
|
}
|
2016-07-31 17:32:30 +00:00
|
|
|
|
2016-08-01 10:04:55 +00:00
|
|
|
/*!
|
2016-08-03 10:59:45 +00:00
|
|
|
@returns the floating point conversion of this @c Time. This will often be less precise.
|
2016-08-01 10:04:55 +00:00
|
|
|
*/
|
2016-07-31 17:32:30 +00:00
|
|
|
inline float get_float()
|
|
|
|
{
|
|
|
|
return (float)length / (float)clock_rate;
|
|
|
|
}
|
2016-08-03 10:59:45 +00:00
|
|
|
|
2016-08-05 01:36:39 +00:00
|
|
|
inline bool operator < (Time other)
|
2016-08-03 10:59:45 +00:00
|
|
|
{
|
|
|
|
return other.clock_rate * length < clock_rate * other.length;
|
|
|
|
}
|
|
|
|
|
2016-08-05 01:36:39 +00:00
|
|
|
inline Time operator + (Time other)
|
2016-08-03 10:59:45 +00:00
|
|
|
{
|
|
|
|
Time result;
|
|
|
|
result.clock_rate = NumberTheory::least_common_multiple(clock_rate, other.clock_rate);
|
2016-08-03 11:26:05 +00:00
|
|
|
result.length = length * (result.clock_rate / clock_rate) + other.length * (result.clock_rate / other.clock_rate);
|
2016-08-03 10:59:45 +00:00
|
|
|
return result;
|
|
|
|
}
|
2016-08-03 11:26:05 +00:00
|
|
|
|
2016-08-05 01:36:39 +00:00
|
|
|
inline Time &operator += (Time other)
|
2016-08-03 11:49:00 +00:00
|
|
|
{
|
|
|
|
unsigned int combined_clock_rate = NumberTheory::least_common_multiple(clock_rate, other.clock_rate);
|
|
|
|
length = length * (combined_clock_rate / clock_rate) + other.length * (combined_clock_rate / other.clock_rate);
|
|
|
|
clock_rate = combined_clock_rate;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-08-05 01:36:39 +00:00
|
|
|
inline Time operator - (Time other)
|
2016-08-03 11:33:26 +00:00
|
|
|
{
|
|
|
|
Time result;
|
|
|
|
result.clock_rate = NumberTheory::least_common_multiple(clock_rate, other.clock_rate);
|
|
|
|
result.length = length * (result.clock_rate / clock_rate) - other.length * (result.clock_rate / other.clock_rate);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-08-05 01:36:39 +00:00
|
|
|
inline Time operator -= (Time other)
|
|
|
|
{
|
|
|
|
unsigned int combined_clock_rate = NumberTheory::least_common_multiple(clock_rate, other.clock_rate);
|
|
|
|
length = length * (combined_clock_rate / clock_rate) - other.length * (combined_clock_rate / other.clock_rate);
|
|
|
|
clock_rate = combined_clock_rate;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Time operator * (Time other)
|
|
|
|
{
|
|
|
|
Time result;
|
|
|
|
result.clock_rate = clock_rate * other.clock_rate;
|
|
|
|
result.length = length * other.length;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Time &operator *= (Time other)
|
|
|
|
{
|
|
|
|
length *= other.length;
|
|
|
|
clock_rate *= other.clock_rate;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Time operator / (Time other)
|
|
|
|
{
|
|
|
|
Time result;
|
|
|
|
result.clock_rate = clock_rate * other.length;
|
|
|
|
result.length = length * other.clock_rate;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Time &operator /= (Time other)
|
|
|
|
{
|
|
|
|
length *= other.clock_rate;
|
|
|
|
clock_rate *= other.length;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-08-03 11:26:05 +00:00
|
|
|
inline void set_zero()
|
|
|
|
{
|
|
|
|
length = 0;
|
|
|
|
clock_rate = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void set_one()
|
|
|
|
{
|
|
|
|
length = 1;
|
|
|
|
clock_rate = 1;
|
|
|
|
}
|
2016-07-10 12:54:39 +00:00
|
|
|
};
|
|
|
|
|
2016-08-03 10:59:45 +00:00
|
|
|
|
2016-07-10 12:54:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* Storage_h */
|