Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
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 
38 namespace 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.
42 template <typename NumericType = double>
43 class MemoryRate : public DimensionalScalar<Unit::MemoryRate, NumericType> {
44 public:
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 
151 private:
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 
158 template <typename NumericType>
159 inline constexpr bool operator==(
160  const MemoryRate<NumericType>& left, const MemoryRate<NumericType>& right) noexcept {
161  return left.Value() == right.Value();
162 }
163 
164 template <typename NumericType>
165 inline constexpr bool operator!=(
166  const MemoryRate<NumericType>& left, const MemoryRate<NumericType>& right) noexcept {
167  return left.Value() != right.Value();
168 }
169 
170 template <typename NumericType>
171 inline constexpr bool operator<(
172  const MemoryRate<NumericType>& left, const MemoryRate<NumericType>& right) noexcept {
173  return left.Value() < right.Value();
174 }
175 
176 template <typename NumericType>
177 inline constexpr bool operator>(
178  const MemoryRate<NumericType>& left, const MemoryRate<NumericType>& right) noexcept {
179  return left.Value() > right.Value();
180 }
181 
182 template <typename NumericType>
183 inline constexpr bool operator<=(
184  const MemoryRate<NumericType>& left, const MemoryRate<NumericType>& right) noexcept {
185  return left.Value() <= right.Value();
186 }
187 
188 template <typename NumericType>
189 inline constexpr bool operator>=(
190  const MemoryRate<NumericType>& left, const MemoryRate<NumericType>& right) noexcept {
191  return left.Value() >= right.Value();
192 }
193 
194 template <typename NumericType>
195 inline std::ostream& operator<<(std::ostream& stream, const MemoryRate<NumericType>& memory_rate) {
196  stream << memory_rate.Print();
197  return stream;
198 }
199 
200 template <typename NumericType>
202  const NumericType number, const MemoryRate<NumericType>& memory_rate) {
203  return memory_rate * number;
204 }
205 
206 template <typename NumericType>
207 inline constexpr Time<NumericType>::Time(
208  const Memory<NumericType>& memory, const MemoryRate<NumericType>& memory_rate)
209  : Time<NumericType>(memory.Value() / memory_rate.Value()) {}
210 
211 template <typename NumericType>
212 inline constexpr Frequency<NumericType>::Frequency(
213  const MemoryRate<NumericType>& memory_rate, const Memory<NumericType>& memory)
214  : Frequency<NumericType>(memory_rate.Value() / memory.Value()) {}
215 
216 template <typename NumericType>
217 inline constexpr Memory<NumericType>::Memory(
218  const MemoryRate<NumericType>& memory_rate, const Time<NumericType>& time)
219  : Memory<NumericType>(memory_rate.Value() * time.Value()) {}
220 
221 template <typename NumericType>
222 inline constexpr Memory<NumericType>::Memory(
223  const MemoryRate<NumericType>& memory_rate, const Frequency<NumericType>& frequency)
224  : Memory<NumericType>(memory_rate.Value() / frequency.Value()) {}
225 
226 template <typename NumericType>
227 inline constexpr MemoryRate<NumericType> Frequency<NumericType>::operator*(
228  const Memory<NumericType>& memory) const {
229  return MemoryRate<NumericType>{memory, *this};
230 }
231 
232 template <typename NumericType>
233 inline constexpr MemoryRate<NumericType> Memory<NumericType>::operator*(
234  const Frequency<NumericType>& frequency) const {
235  return MemoryRate<NumericType>{*this, frequency};
236 }
237 
238 template <typename NumericType>
239 inline constexpr MemoryRate<NumericType> Memory<NumericType>::operator/(
240  const Time<NumericType>& time) const {
241  return MemoryRate<NumericType>{*this, time};
242 }
243 
244 template <typename NumericType>
245 inline constexpr Time<NumericType> Memory<NumericType>::operator/(
246  const MemoryRate<NumericType>& memory_rate) const {
247  return Time<NumericType>{*this, memory_rate};
248 }
249 
250 } // namespace PhQ
251 
252 namespace std {
253 
254 template <typename NumericType>
255 struct 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...
double value
Value of this physical quantity expressed in its standard unit of measure.
constexpr double Value() const noexcept
Value of this physical quantity expressed in its standard unit of measure.
static constexpr Unit::MemoryRate 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
Computer memory rate. Can represent the time rate of change of memory or a memory transfer speed; see...
Definition: MemoryRate.hpp:43
static constexpr MemoryRate< NumericType > Create(const NumericType value)
Statically creates a memory rate with a given value expressed in a given memory rate unit.
Definition: MemoryRate.hpp:98
constexpr void operator*=(const NumericType number) noexcept
Definition: MemoryRate.hpp:143
constexpr MemoryRate< NumericType > operator-(const MemoryRate< NumericType > &memory_rate) const
Definition: MemoryRate.hpp:107
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...
Definition: MemoryRate.hpp:60
constexpr Memory< NumericType > operator*(const Time< NumericType > &time) const
Definition: MemoryRate.hpp:115
~MemoryRate() noexcept=default
Destructor. Destroys this memory rate.
constexpr MemoryRate< NumericType > & operator=(const MemoryRate< OtherNumericType > &other)
Copy assignment operator. Assigns this memory rate by copying another one.
Definition: MemoryRate.hpp:82
constexpr MemoryRate(const NumericType value)
Constructor. Constructs a memory rate quantity with a given value expressed in the standard memory ra...
Definition: MemoryRate.hpp:154
constexpr MemoryRate< NumericType > operator*(const NumericType number) const
Definition: MemoryRate.hpp:111
constexpr Memory< NumericType > operator/(const Frequency< NumericType > &frequency) const
Definition: MemoryRate.hpp:123
constexpr MemoryRate(MemoryRate< NumericType > &&other) noexcept=default
Move constructor. Constructs a memory rate by moving another one.
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.
Definition: MemoryRate.hpp:50
static constexpr MemoryRate< NumericType > Zero()
Statically creates a memory rate of zero.
Definition: MemoryRate.hpp:91
constexpr MemoryRate< NumericType > operator/(const NumericType number) const
Definition: MemoryRate.hpp:119
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...
Definition: MemoryRate.hpp:55
constexpr MemoryRate< NumericType > operator+(const MemoryRate< NumericType > &memory_rate) const
Definition: MemoryRate.hpp:103
constexpr MemoryRate< NumericType > & operator=(const MemoryRate< NumericType > &other)=default
Copy assignment operator. Assigns this memory rate by copying another one.
constexpr void operator+=(const MemoryRate< NumericType > &memory_rate) noexcept
Definition: MemoryRate.hpp:135
constexpr MemoryRate< NumericType > & operator=(MemoryRate< NumericType > &&other) noexcept=default
Move assignment operator. Assigns this memory rate by moving another one.
constexpr Frequency< NumericType > operator/(const Memory< NumericType > &memory) const
Definition: MemoryRate.hpp:127
constexpr NumericType operator/(const MemoryRate< NumericType > &memory_rate) const noexcept
Definition: MemoryRate.hpp:131
constexpr void operator-=(const MemoryRate< NumericType > &memory_rate) noexcept
Definition: MemoryRate.hpp:139
constexpr void operator/=(const NumericType number) noexcept
Definition: MemoryRate.hpp:147
Computer memory. For the time rate of change of computer memory, see PhQ::MemoryRate; see also PhQ::T...
Definition: Memory.hpp:52
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.
Frequency
Frequency units.
Definition: Frequency.hpp:53
Memory
Computer memory units.
Definition: Memory.hpp:53
MemoryRate
Computer memory rate unit. Can represent the time rate of change of memory or a memory transfer speed...
Definition: MemoryRate.hpp:54
Time
Time units.
Definition: Time.hpp:53
Namespace that encompasses all of the Physical Quantities library's content.
constexpr MemoryRate< NumericType > operator*(const NumericType number, const MemoryRate< NumericType > &memory_rate)
Definition: MemoryRate.hpp:201
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
constexpr Dyad< NumericType > operator/(const Dyad< NumericType > &dyad, const OtherNumericType number)
Definition: Dyad.hpp:696
constexpr bool operator!=(const Acceleration< NumericType > &left, const Acceleration< NumericType > &right) noexcept
std::ostream & operator<<(std::ostream &stream, const Acceleration< NumericType > &acceleration)