diff options
| author | Maxime Coste <mawww@kakoune.org> | 2024-12-04 22:18:51 +1100 |
|---|---|---|
| committer | Maxime Coste <mawww@kakoune.org> | 2024-12-04 22:18:51 +1100 |
| commit | dac922e24c48d297de063786be3065554409cc71 (patch) | |
| tree | 05d28520b15da9a7da4c0a7e7ef44e25e1dd6245 /src | |
| parent | dd1ea05bbcb989441ec5516d3b84562afa27d61a (diff) | |
Use default comparison impl for LineAndColumn and StronglyTypedNumber
This is a bit simpler and should leave more leeway to the optimizer
to detect it can compare the whole struct.
Diffstat (limited to 'src')
| -rw-r--r-- | src/coord.hh | 20 | ||||
| -rw-r--r-- | src/units.hh | 31 |
2 files changed, 15 insertions, 36 deletions
diff --git a/src/coord.hh b/src/coord.hh index 8420dcee..30c4ce74 100644 --- a/src/coord.hh +++ b/src/coord.hh @@ -41,18 +41,8 @@ struct LineAndColumn return *static_cast<EffectiveType*>(this); } - [[gnu::always_inline]] - constexpr friend auto operator<=>(const EffectiveType& lhs, const EffectiveType& rhs) - { - return (lhs.line != rhs.line) ? lhs.line <=> rhs.line - : lhs.column <=> rhs.column; - } - - [[gnu::always_inline]] - constexpr friend bool operator==(const EffectiveType& lhs, const EffectiveType& rhs) - { - return lhs.line == rhs.line and lhs.column == rhs.column; - } + constexpr friend auto operator<=>(const LineAndColumn& lhs, const LineAndColumn& rhs) = default; + constexpr friend bool operator==(const LineAndColumn& lhs, const LineAndColumn& rhs) = default; friend constexpr size_t hash_value(const EffectiveType& val) { @@ -65,6 +55,9 @@ struct BufferCoord : LineAndColumn<BufferCoord, LineCount, ByteCount> [[gnu::always_inline]] constexpr BufferCoord(LineCount line = 0, ByteCount column = 0) : LineAndColumn{line, column} {} + + constexpr friend auto operator<=>(const BufferCoord& lhs, const BufferCoord& rhs) = default; + constexpr friend bool operator==(const BufferCoord& lhs, const BufferCoord& rhs) = default; }; struct DisplayCoord : LineAndColumn<DisplayCoord, LineCount, ColumnCount> @@ -73,6 +66,9 @@ struct DisplayCoord : LineAndColumn<DisplayCoord, LineCount, ColumnCount> constexpr DisplayCoord(LineCount line = 0, ColumnCount column = 0) : LineAndColumn{line, column} {} + constexpr friend auto operator<=>(const DisplayCoord& lhs, const DisplayCoord& rhs) = default; + constexpr friend bool operator==(const DisplayCoord& lhs, const DisplayCoord& rhs) = default; + static constexpr const char* option_type_name = "coord"; }; diff --git a/src/units.hh b/src/units.hh index ff4ed882..9d2511b9 100644 --- a/src/units.hh +++ b/src/units.hh @@ -16,7 +16,7 @@ public: StronglyTypedNumber() = default; [[gnu::always_inline]] - explicit constexpr StronglyTypedNumber(ValueType value) + constexpr StronglyTypedNumber(ValueType value) : m_value(value) { static_assert(std::is_base_of<StronglyTypedNumber, RealType>::value, @@ -82,13 +82,8 @@ public: RealType& operator%=(RealType other) { m_value %= other.m_value; return static_cast<RealType&>(*this); } - [[gnu::always_inline]] - constexpr friend bool operator==(RealType lhs, RealType rhs) - { return lhs.m_value == rhs.m_value; } - - [[gnu::always_inline]] - constexpr friend auto operator<=>(RealType lhs, RealType rhs) - { return lhs.m_value <=> rhs.m_value; } + constexpr friend bool operator==(StronglyTypedNumber lhs, StronglyTypedNumber rhs) = default; + constexpr friend auto operator<=>(StronglyTypedNumber lhs, StronglyTypedNumber rhs) = default; [[gnu::always_inline]] constexpr bool operator!() const @@ -110,10 +105,7 @@ protected: struct LineCount : public StronglyTypedNumber<LineCount, int> { - LineCount() = default; - - [[gnu::always_inline]] - constexpr LineCount(int value) : StronglyTypedNumber<LineCount>(value) {} + using StronglyTypedNumber::StronglyTypedNumber; }; [[gnu::always_inline]] @@ -124,10 +116,7 @@ inline constexpr LineCount operator"" _line(unsigned long long int value) struct ByteCount : public StronglyTypedNumber<ByteCount, int> { - ByteCount() = default; - - [[gnu::always_inline]] - constexpr ByteCount(int value) : StronglyTypedNumber<ByteCount>(value) {} + using StronglyTypedNumber::StronglyTypedNumber; }; [[gnu::always_inline]] @@ -142,10 +131,7 @@ Byte* operator+(Byte* ptr, ByteCount count) { return ptr + (int)count; } struct CharCount : public StronglyTypedNumber<CharCount, int> { - CharCount() = default; - - [[gnu::always_inline]] - constexpr CharCount(int value) : StronglyTypedNumber<CharCount>(value) {} + using StronglyTypedNumber::StronglyTypedNumber; }; [[gnu::always_inline]] @@ -156,10 +142,7 @@ inline constexpr CharCount operator"" _char(unsigned long long int value) struct ColumnCount : public StronglyTypedNumber<ColumnCount, int> { - ColumnCount() = default; - - [[gnu::always_inline]] - constexpr ColumnCount(int value) : StronglyTypedNumber<ColumnCount>(value) {} + using StronglyTypedNumber::StronglyTypedNumber; }; [[gnu::always_inline]] |
