Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Loading...
Searching...
No Matches
MemoryRate.hpp
Go to the documentation of this file.
1// Copyright © 2020-2024 Alexandre Coderre-Chabot
2//
3// This file is part of Physical Quantities (PhQ), a C++ library of physical quantities, physical
4// models, and units of measure for scientific computing.
5//
6// Physical Quantities is hosted at:
7// https://github.com/acodcha/phq
8//
9// Physical Quantities is licensed under the MIT License:
10// https://mit-license.org
11//
12// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
13// associated documentation files (the "Software"), to deal in the Software without restriction,
14// including without limitation the rights to use, copy, modify, merge, publish, distribute,
15// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
16// furnished to do so, subject to the following conditions:
17// - The above copyright notice and this permission notice shall be included in all copies or
18// substantial portions of the Software.
19// - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
20// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
22// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM
23// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25#ifndef PHQ_MEMORY_RATE_HPP
26#define PHQ_MEMORY_RATE_HPP
27
28#include <cstddef>
29#include <functional>
30#include <ostream>
31
32#include "DimensionalScalar.hpp"
33#include "Frequency.hpp"
34#include "Memory.hpp"
35#include "Time.hpp"
36#include "Unit/MemoryRate.hpp"
37
38namespace PhQ {
39
40/// \brief Computer memory rate. Can represent the time rate of change of memory or a memory
41/// transfer speed; see PhQ::Memory, PhQ::Time, and PhQ::Frequency.
42template <typename NumericType = double>
43class MemoryRate : public DimensionalScalar<Unit::MemoryRate, NumericType> {
44public:
45 /// \brief Default constructor. Constructs a memory rate with an uninitialized value.
46 MemoryRate() = default;
47
48 /// \brief Constructor. Constructs a memory rate with a given value expressed in a given memory
49 /// rate unit.
50 MemoryRate(const NumericType value, const Unit::MemoryRate unit)
51 : DimensionalScalar<Unit::MemoryRate, NumericType>(value, unit) {}
52
53 /// \brief Constructor. Constructs a memory rate from a given memory and time duration using the
54 /// definition of memory rate.
55 constexpr MemoryRate(const Memory<NumericType>& memory, const Time<NumericType>& time)
56 : MemoryRate<NumericType>(memory.Value() / time.Value()) {}
57
58 /// \brief Constructor. Constructs a memory rate from a given memory and frequency using the
59 /// definition of memory rate.
60 constexpr MemoryRate(const Memory<NumericType>& memory, const Frequency<NumericType>& frequency)
61 : MemoryRate<NumericType>(memory.Value() * frequency.Value()) {}
62
63 /// \brief Destructor. Destroys this memory rate.
64 ~MemoryRate() noexcept = default;
65
66 /// \brief Copy constructor. Constructs a memory rate by copying another one.
67 constexpr MemoryRate(const MemoryRate<NumericType>& other) = default;
68
69 /// \brief Copy constructor. Constructs a memory rate by copying another one.
70 template <typename OtherNumericType>
71 explicit constexpr MemoryRate(const MemoryRate<OtherNumericType>& other)
72 : MemoryRate(static_cast<NumericType>(other.Value())) {}
73
74 /// \brief Move constructor. Constructs a memory rate by moving another one.
75 constexpr MemoryRate(MemoryRate<NumericType>&& other) noexcept = default;
76
77 /// \brief Copy assignment operator. Assigns this memory rate by copying another one.
78 constexpr MemoryRate<NumericType>& operator=(const MemoryRate<NumericType>& other) = default;
79
80 /// \brief Copy assignment operator. Assigns this memory rate by copying another one.
81 template <typename OtherNumericType>
83 this->value = static_cast<NumericType>(other.Value());
84 return *this;
85 }
86
87 /// \brief Move assignment operator. Assigns this memory rate by moving another one.
88 constexpr MemoryRate<NumericType>& operator=(MemoryRate<NumericType>&& other) noexcept = default;
89
90 /// \brief Statically creates a memory rate of zero.
91 [[nodiscard]] static constexpr MemoryRate<NumericType> Zero() {
92 return MemoryRate<NumericType>{static_cast<NumericType>(0)};
93 }
94
95 /// \brief Statically creates a memory rate with a given value expressed in a given memory rate
96 /// unit.
97 template <Unit::MemoryRate Unit>
98 [[nodiscard]] static constexpr MemoryRate<NumericType> Create(const NumericType value) {
100 ConvertStatically<Unit::MemoryRate, Unit, Standard<Unit::MemoryRate>>(value)};
101 }
102
103 constexpr MemoryRate<NumericType> operator+(const MemoryRate<NumericType>& memory_rate) const {
104 return MemoryRate<NumericType>{this->value + memory_rate.value};
105 }
106
107 constexpr MemoryRate<NumericType> operator-(const MemoryRate<NumericType>& memory_rate) const {
108 return MemoryRate<NumericType>{this->value - memory_rate.value};
109 }
110
111 constexpr MemoryRate<NumericType> operator*(const NumericType number) const {
112 return MemoryRate<NumericType>{this->value * number};
113 }
114
115 constexpr Memory<NumericType> operator*(const Time<NumericType>& time) const {
116 return Memory<NumericType>{*this, time};
117 }
118
119 constexpr MemoryRate<NumericType> operator/(const NumericType number) const {
120 return MemoryRate<NumericType>{this->value / number};
121 }
122
123 constexpr Memory<NumericType> operator/(const Frequency<NumericType>& frequency) const {
124 return Memory<NumericType>{*this, frequency};
125 }
126
127 constexpr Frequency<NumericType> operator/(const Memory<NumericType>& memory) const {
128 return Frequency<NumericType>{*this, memory};
129 }
130
131 constexpr NumericType operator/(const MemoryRate<NumericType>& memory_rate) const noexcept {
132 return this->value / memory_rate.value;
133 }
134
135 constexpr void operator+=(const MemoryRate<NumericType>& memory_rate) noexcept {
136 this->value += memory_rate.value;
137 }
138
139 constexpr void operator-=(const MemoryRate<NumericType>& memory_rate) noexcept {
140 this->value -= memory_rate.value;
141 }
142
143 constexpr void operator*=(const NumericType number) noexcept {
144 this->value *= number;
145 }
146
147 constexpr void operator/=(const NumericType number) noexcept {
148 this->value /= number;
149 }
150
151private:
152 /// \brief Constructor. Constructs a memory rate quantity with a given value expressed in the
153 /// standard memory rate unit.
154 explicit constexpr MemoryRate(const NumericType value)
155 : DimensionalScalar<Unit::MemoryRate, NumericType>(value) {}
156};
157
158template <typename NumericType>
159inline constexpr bool operator==(
160 const MemoryRate<NumericType>& left, const MemoryRate<NumericType>& right) noexcept {
161 return left.Value() == right.Value();
162}
163
164template <typename NumericType>
165inline constexpr bool operator!=(
166 const MemoryRate<NumericType>& left, const MemoryRate<NumericType>& right) noexcept {
167 return left.Value() != right.Value();
168}
169
170template <typename NumericType>
171inline constexpr bool operator<(
172 const MemoryRate<NumericType>& left, const MemoryRate<NumericType>& right) noexcept {
173 return left.Value() < right.Value();
174}
175
176template <typename NumericType>
177inline constexpr bool operator>(
178 const MemoryRate<NumericType>& left, const MemoryRate<NumericType>& right) noexcept {
179 return left.Value() > right.Value();
180}
181
182template <typename NumericType>
183inline constexpr bool operator<=(
184 const MemoryRate<NumericType>& left, const MemoryRate<NumericType>& right) noexcept {
185 return left.Value() <= right.Value();
186}
187
188template <typename NumericType>
189inline constexpr bool operator>=(
190 const MemoryRate<NumericType>& left, const MemoryRate<NumericType>& right) noexcept {
191 return left.Value() >= right.Value();
192}
193
194template <typename NumericType>
195inline std::ostream& operator<<(std::ostream& stream, const MemoryRate<NumericType>& memory_rate) {
196 stream << memory_rate.Print();
197 return stream;
198}
199
200template <typename NumericType>
202 const NumericType number, const MemoryRate<NumericType>& memory_rate) {
203 return memory_rate * number;
204}
205
206template <typename NumericType>
207inline constexpr Time<NumericType>::Time(
208 const Memory<NumericType>& memory, const MemoryRate<NumericType>& memory_rate)
209 : Time<NumericType>(memory.Value() / memory_rate.Value()) {}
210
211template <typename NumericType>
213 const MemoryRate<NumericType>& memory_rate, const Memory<NumericType>& memory)
214 : Frequency<NumericType>(memory_rate.Value() / memory.Value()) {}
215
216template <typename NumericType>
218 const MemoryRate<NumericType>& memory_rate, const Time<NumericType>& time)
219 : Memory<NumericType>(memory_rate.Value() * time.Value()) {}
220
221template <typename NumericType>
223 const MemoryRate<NumericType>& memory_rate, const Frequency<NumericType>& frequency)
224 : Memory<NumericType>(memory_rate.Value() / frequency.Value()) {}
225
226template <typename NumericType>
228 const Memory<NumericType>& memory) const {
229 return MemoryRate<NumericType>{memory, *this};
230}
231
232template <typename NumericType>
234 const Frequency<NumericType>& frequency) const {
235 return MemoryRate<NumericType>{*this, frequency};
236}
237
238template <typename NumericType>
240 const Time<NumericType>& time) const {
241 return MemoryRate<NumericType>{*this, time};
242}
243
244template <typename NumericType>
246 const MemoryRate<NumericType>& memory_rate) const {
247 return Time<NumericType>{*this, memory_rate};
248}
249
250} // namespace PhQ
251
252namespace std {
253
254template <typename NumericType>
255struct hash<PhQ::MemoryRate<NumericType>> {
256 inline size_t operator()(const PhQ::MemoryRate<NumericType>& memory_rate) const {
257 return hash<NumericType>()(memory_rate.Value());
258 }
259};
260
261} // namespace std
262
263#endif // PHQ_MEMORY_RATE_HPP
Abstract base class that represents any dimensional scalar physical quantity. Such a physical quantit...
NumericType value
Value of this physical quantity expressed in its standard unit of measure.
constexpr NumericType Value() const noexcept
Value of this physical quantity expressed in its standard unit of measure.
static constexpr UnitType Unit()
Standard unit of measure for this physical quantity. This physical quantity's value is stored interna...
std::string Print() const
Prints this physical quantity as a string. This physical quantity's value is expressed in its standar...
Frequency. Inverse of a time duration. See also PhQ::Time.
Definition Frequency.hpp:40
constexpr Frequency< NumericType > operator*(const NumericType number) const
Frequency()=default
Default constructor. Constructs a frequency with an uninitialized value.
Computer memory rate. Can represent the time rate of change of memory or a memory transfer speed; see...
constexpr MemoryRate< NumericType > operator*(const NumericType number) const
constexpr MemoryRate< NumericType > & operator=(MemoryRate< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this memory rate by moving another one.
constexpr void operator*=(const NumericType number) noexcept
constexpr MemoryRate(const Memory< NumericType > &memory, const Frequency< NumericType > &frequency)
Constructor. Constructs a memory rate from a given memory and frequency using the definition of memor...
~MemoryRate() noexcept=default
Destructor. Destroys this memory rate.
constexpr MemoryRate(const NumericType value)
Constructor. Constructs a memory rate quantity with a given value expressed in the standard memory ra...
static constexpr MemoryRate< NumericType > Zero()
Statically creates a memory rate of zero.
constexpr MemoryRate< NumericType > & operator=(const MemoryRate< OtherNumericType > &other)
Copy assignment operator. Assigns this memory rate by copying another one.
constexpr Frequency< NumericType > operator/(const Memory< NumericType > &memory) const
constexpr MemoryRate(MemoryRate< NumericType > &&other) noexcept=default
Move constructor. Constructs a memory rate by moving another one.
constexpr MemoryRate< NumericType > operator+(const MemoryRate< NumericType > &memory_rate) const
MemoryRate()=default
Default constructor. Constructs a memory rate with an uninitialized value.
MemoryRate(const NumericType value, const Unit::MemoryRate unit)
Constructor. Constructs a memory rate with a given value expressed in a given memory rate unit.
constexpr MemoryRate< NumericType > operator/(const NumericType number) const
constexpr MemoryRate(const Memory< NumericType > &memory, const Time< NumericType > &time)
Constructor. Constructs a memory rate from a given memory and time duration using the definition of m...
constexpr MemoryRate< NumericType > operator-(const MemoryRate< NumericType > &memory_rate) const
constexpr void operator+=(const MemoryRate< NumericType > &memory_rate) noexcept
constexpr Memory< NumericType > operator/(const Frequency< NumericType > &frequency) const
static constexpr MemoryRate< NumericType > Create(const NumericType value)
Statically creates a memory rate with a given value expressed in a given memory rate unit.
constexpr MemoryRate< NumericType > & operator=(const MemoryRate< NumericType > &other)=default
Copy assignment operator. Assigns this memory rate by copying another one.
constexpr NumericType operator/(const MemoryRate< NumericType > &memory_rate) const noexcept
constexpr void operator-=(const MemoryRate< NumericType > &memory_rate) noexcept
constexpr void operator/=(const NumericType number) noexcept
constexpr Memory< NumericType > operator*(const Time< NumericType > &time) const
Computer memory. For the time rate of change of computer memory, see PhQ::MemoryRate; see also PhQ::T...
Definition Memory.hpp:52
constexpr Memory< NumericType > operator*(const NumericType number) const
Definition Memory.hpp:119
constexpr Memory< NumericType > operator/(const NumericType number) const
Definition Memory.hpp:125
Memory()=default
Default constructor. Constructs a memory quantity with an uninitialized value.
Time. Can represent either a point in time, a time duration, or a period. For the inverse of time,...
Definition Time.hpp:172
Time()=default
Default constructor. Constructs a time quantity with an uninitialized value.
MemoryRate
Computer memory rate unit. Can represent the time rate of change of memory or a memory transfer speed...
Namespace that encompasses all of the Physical Quantities library's content.
std::ostream & operator<<(std::ostream &stream, const Acceleration< NumericType > &acceleration)
constexpr bool operator<(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr bool operator<=(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr bool operator>(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr Acceleration< NumericType > operator*(const NumericType number, const Acceleration< NumericType > &acceleration)
constexpr bool operator==(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr bool operator>=(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
constexpr bool operator!=(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept