1
0
mirror of https://github.com/TomHarte/CLK.git synced 2025-01-14 13:33:42 +00:00

Enhances with constexpr.

This commit is contained in:
Thomas Harte 2019-12-24 18:53:50 -05:00
parent 99415217dc
commit adc2b77833

View File

@ -21,40 +21,40 @@ namespace Disk {
class HeadPosition { class HeadPosition {
public: public:
/// Creates an instance decribing position @c value at a resolution of @c scale ticks per track. /// Creates an instance decribing position @c value at a resolution of @c scale ticks per track.
HeadPosition(int value, int scale) : position_(value * (4/scale)) {} constexpr HeadPosition(int value, int scale) : position_(value * (4/scale)) {}
explicit HeadPosition(int value) : HeadPosition(value, 1) {} constexpr explicit HeadPosition(int value) : HeadPosition(value, 1) {}
HeadPosition() : HeadPosition(0) {} constexpr HeadPosition() : HeadPosition(0) {}
/// @returns the whole number part of the position. /// @returns the whole number part of the position.
int as_int() const { return position_ >> 2; } constexpr int as_int() const { return position_ >> 2; }
/// @returns n where n/2 is the head position. /// @returns n where n/2 is the head position.
int as_half() const { return position_ >> 1; } constexpr int as_half() const { return position_ >> 1; }
/// @returns n where n/4 is the head position. /// @returns n where n/4 is the head position.
int as_quarter() const { return position_; } constexpr int as_quarter() const { return position_; }
/// @returns the head position at maximal but unspecified precision. /// @returns the head position at maximal but unspecified precision.
int as_largest() const { return as_quarter(); } constexpr int as_largest() const { return as_quarter(); }
HeadPosition &operator +=(const HeadPosition &rhs) { HeadPosition &operator +=(const HeadPosition &rhs) {
position_ += rhs.position_; position_ += rhs.position_;
return *this; return *this;
} }
bool operator ==(const HeadPosition &rhs) const { constexpr bool operator ==(const HeadPosition &rhs) const {
return position_ == rhs.position_; return position_ == rhs.position_;
} }
bool operator !=(const HeadPosition &rhs) const { constexpr bool operator !=(const HeadPosition &rhs) const {
return position_ != rhs.position_; return position_ != rhs.position_;
} }
bool operator <(const HeadPosition &rhs) const { constexpr bool operator <(const HeadPosition &rhs) const {
return position_ < rhs.position_; return position_ < rhs.position_;
} }
bool operator <=(const HeadPosition &rhs) const { constexpr bool operator <=(const HeadPosition &rhs) const {
return position_ <= rhs.position_; return position_ <= rhs.position_;
} }
bool operator >(const HeadPosition &rhs) const { constexpr bool operator >(const HeadPosition &rhs) const {
return position_ > rhs.position_; return position_ > rhs.position_;
} }
bool operator >=(const HeadPosition &rhs) const { constexpr bool operator >=(const HeadPosition &rhs) const {
return position_ >= rhs.position_; return position_ >= rhs.position_;
} }
@ -79,7 +79,7 @@ class Track {
int head; int head;
HeadPosition position; HeadPosition position;
bool operator < (const Address &rhs) const { constexpr bool operator < (const Address &rhs) const {
int largest_position = position.as_largest(); int largest_position = position.as_largest();
int rhs_largest_position = rhs.position.as_largest(); int rhs_largest_position = rhs.position.as_largest();
return std::tie(head, largest_position) < std::tie(rhs.head, rhs_largest_position); return std::tie(head, largest_position) < std::tie(rhs.head, rhs_largest_position);