units
Use physical dimensions at compile-time or run-time.
Classes | Public Types | Public Member Functions | Static Public Member Functions | Private Types | Static Private Member Functions | Private Attributes | Friends | List of all members
vnix::units::basic_dim< DBO > Class Template Reference

Rational exponent for each basis-element of dimension. More...

#include <dim.hpp>

Inheritance diagram for vnix::units::basic_dim< DBO >:
[legend]
Collaboration diagram for vnix::units::basic_dim< DBO >:
[legend]

Classes

struct  divd
 Function object used for dividing every exponent by a single factor. More...
 
struct  mult
 Function object used for multiplying every exponent by a single factor. More...
 

Public Types

enum  { NUM_BASES = DBO::num_offs, NUM_BITS = NUM_BASES * rat::BITS }
 
using rat = rational< 4, 2 >
 Type of rational for dimensioned values. More...
 
using off = DBO
 Type of offset for each base-element. More...
 
using word = typename int_types< NUM_BITS >::US
 Type of unsigned word in which dimensions are encoded. More...
 

Public Member Functions

constexpr basic_dim ()
 
constexpr basic_dim (word u)
 Initialize from dim that has been encoded into a word. More...
 
template<typename... T>
constexpr basic_dim (T...ts)
 Initialize exponents, one for each base quantity. More...
 
constexpr basic_dim (std::array< rat, NUM_BASES > const &a)
 
constexpr word encode () const
 Encode data for this dim into a word. More...
 
constexpr rat exp (DBO off) const
 Rational exponent at specified offset. More...
 
constexpr rat operator[] (DBO off) const
 Rational exponent at specified offset. More...
 
constexpr void set (DBO off, rat r)
 Set rational exponent at specified offset. More...
 
template<typename F >
constexpr basic_dim combine (basic_dim const &d, F f) const
 Combine each exponent with corresponding other exponent via function. More...
 
template<typename F >
constexpr basic_dim transform (F f) const
 Transform each exponent according to a function. More...
 
constexpr basic_dim operator+ (basic_dim const &a) const
 Add corresponding exponents. More...
 
constexpr basic_dim operator- (basic_dim const &s) const
 Subtract corresponding exponents. More...
 
constexpr basic_dim operator* (rat f) const
 Multiply exponents by rational factor. More...
 
constexpr basic_dim operator/ (rat f) const
 Divide exponents by rational factor. More...
 
constexpr basic_dim operator/ (rat::stype f) const
 Divide exponents by rational factor. More...
 
constexpr bool operator== (basic_dim const &d) const
 
constexpr bool operator!= (basic_dim const &d) const
 

Static Public Member Functions

static constexpr rat add (rat x, rat y)
 Function used to add corresponding exponents. More...
 
static constexpr rat sbtrct (rat x, rat y)
 Function used to subtract corresponding exponents. More...
 
static std::ostream & print_unit (std::ostream &s, char const *u, rat e)
 Print to output stream the symbolic contribution from a given unit. More...
 

Private Types

enum  { MAX_SHIFT = (NUM_BASES - 1) * rat::BITS }
 

Static Private Member Functions

template<typename T >
static constexpr word encode (T t)
 Encode exponent associated with highest DBO at highest bit-offsets. More...
 
template<typename T , typename... U>
static constexpr word encode (T t, U...us)
 Encode exponents, with higher DBO at higher bit-offsets. More...
 
static constexpr word encode (std::array< rat, NUM_BASES > const &a)
 

Private Attributes

word e_
 Storage for exponents. More...
 

Friends

std::ostream & operator<< (std::ostream &s, basic_dim d)
 Print to to output stream. More...
 

Detailed Description

template<typename DBO>
class vnix::units::basic_dim< DBO >

Rational exponent for each basis-element of dimension.

The template-type parameter DBO is of type dim_base_offset, which is automatically generated from YAML and ERB before the header-only library is installed.

Template Parameters
DBOType of offset of basis-element of dimension.

Definition at line 24 of file dim.hpp.

Member Typedef Documentation

template<typename DBO>
using vnix::units::basic_dim< DBO >::off = DBO

Type of offset for each base-element.

Definition at line 27 of file dim.hpp.

template<typename DBO>
using vnix::units::basic_dim< DBO >::rat = rational<4, 2>

Type of rational for dimensioned values.

Definition at line 26 of file dim.hpp.

template<typename DBO>
using vnix::units::basic_dim< DBO >::word = typename int_types<NUM_BITS>::US

Type of unsigned word in which dimensions are encoded.

Definition at line 35 of file dim.hpp.

Member Enumeration Documentation

template<typename DBO>
anonymous enum
Enumerator
NUM_BASES 

Number of bases for dimension.

NUM_BITS 

Number of bits in instance of dim.

Definition at line 29 of file dim.hpp.

29  {
30  NUM_BASES = DBO::num_offs,
32  };
Total number of bits.
Definition: encoding.hpp:21
Number of bits in instance of dim.
Definition: dim.hpp:31
Number of bases for dimension.
Definition: dim.hpp:30
template<typename DBO>
anonymous enum
private
Enumerator
MAX_SHIFT 

Maximum number of bits to shift an encoding of a rational exponent.

Definition at line 41 of file dim.hpp.

41  {
43  MAX_SHIFT = (NUM_BASES - 1) * rat::BITS
44  };
Maximum number of bits to shift an encoding of a rational exponent.
Definition: dim.hpp:43
Total number of bits.
Definition: encoding.hpp:21
Number of bases for dimension.
Definition: dim.hpp:30

Constructor & Destructor Documentation

template<typename DBO>
constexpr vnix::units::basic_dim< DBO >::basic_dim ( )
inline

Definition at line 82 of file dim.hpp.

82 : e_(0) {}
word e_
Storage for exponents.
Definition: dim.hpp:38

Here is the caller graph for this function:

template<typename DBO>
constexpr vnix::units::basic_dim< DBO >::basic_dim ( word  u)
inlineexplicit

Initialize from dim that has been encoded into a word.

By default, initialize null (dimensionless) dim.

Parameters
uEncoded data for dim.

Definition at line 87 of file dim.hpp.

87 : e_(u) {}
word e_
Storage for exponents.
Definition: dim.hpp:38

Here is the caller graph for this function:

template<typename DBO>
template<typename... T>
constexpr vnix::units::basic_dim< DBO >::basic_dim ( T...  ts)
inline

Initialize exponents, one for each base quantity.

Template Parameters
TList of types, one for each initializer in list for rat_array. Each type should be rat.
Parameters
tsList of initializers for rat_array.

Definition at line 96 of file dim.hpp.

96  : e_(encode(ts...)) {
97  static_assert(sizeof...(ts) == NUM_BASES, "illegal number of bases");
98  }
constexpr word encode() const
Encode data for this dim into a word.
Definition: dim.hpp:104
Number of bases for dimension.
Definition: dim.hpp:30
word e_
Storage for exponents.
Definition: dim.hpp:38
template<typename DBO>
constexpr vnix::units::basic_dim< DBO >::basic_dim ( std::array< rat, NUM_BASES > const &  a)
inline

Definition at line 101 of file dim.hpp.

101 : e_(encode(a)) {}
constexpr word encode() const
Encode data for this dim into a word.
Definition: dim.hpp:104
word e_
Storage for exponents.
Definition: dim.hpp:38

Here is the caller graph for this function:

Member Function Documentation

template<typename DBO>
static constexpr rat vnix::units::basic_dim< DBO >::add ( rat  x,
rat  y 
)
inlinestatic

Function used to add corresponding exponents.

Definition at line 156 of file dim.hpp.

156 { return x + y; }

Here is the caller graph for this function:

template<typename DBO>
template<typename F >
constexpr basic_dim vnix::units::basic_dim< DBO >::combine ( basic_dim< DBO > const &  d,
f 
) const
inline

Combine each exponent with corresponding other exponent via function.

Template Parameters
FType of function for combining two rationals to make third.
Parameters
dOther rational exponents.
fFunction operating on pair of rationals.
Returns
New rational exponents.

Definition at line 133 of file dim.hpp.

133  {
134  basic_dim r(word(0));
135  for (auto b : off::array) {
136  auto const os = DBO(b);
137  r.set(os, f(exp(os), d.exp(os)));
138  }
139  return r;
140  }
typename int_types< NUM_BITS >::US word
Type of unsigned word in which dimensions are encoded.
Definition: dim.hpp:35
constexpr rat exp(DBO off) const
Rational exponent at specified offset.
Definition: dim.hpp:108
constexpr basic_dim()
Definition: dim.hpp:82
template<typename DBO>
template<typename T >
static constexpr word vnix::units::basic_dim< DBO >::encode ( t)
inlinestaticprivate

Encode exponent associated with highest DBO at highest bit-offsets.

Template Parameters
TType that is convertible rat.
tExponent to be encoded.

Definition at line 49 of file dim.hpp.

49  {
50  return rat::encode(t) << MAX_SHIFT;
51  }
static constexpr utype encode(rational r)
Encoding from rational number.
Definition: rational.hpp:113
Maximum number of bits to shift an encoding of a rational exponent.
Definition: dim.hpp:43
template<typename DBO>
template<typename T , typename... U>
static constexpr word vnix::units::basic_dim< DBO >::encode ( t,
U...  us 
)
inlinestaticprivate

Encode exponents, with higher DBO at higher bit-offsets.

Template Parameters
TType that is convertible to rat.
UList of types, each convertible to rat.
Parameters
tExponent to be encoded at lowest bit-offsets.
usExponents to be encoded at higher bit-offsets.

Definition at line 60 of file dim.hpp.

60  {
61  constexpr auto N = sizeof...(us);
62  static_assert(N < NUM_BASES, "too many exponents");
63  constexpr auto SHIFT = MAX_SHIFT - N * rat::BITS;
64  constexpr word MASK = bit_range<word>(0, rat::BITS - 1) << SHIFT;
65  word const e = rat::encode(t) << SHIFT;
66  return (e & MASK) | (encode(us...) & ~MASK);
67  }
static constexpr utype encode(rational r)
Encoding from rational number.
Definition: rational.hpp:113
Maximum number of bits to shift an encoding of a rational exponent.
Definition: dim.hpp:43
constexpr word encode() const
Encode data for this dim into a word.
Definition: dim.hpp:104
constexpr auto N
Constant-expression symbol for N.
Definition: units.hpp:2008
typename int_types< NUM_BITS >::US word
Type of unsigned word in which dimensions are encoded.
Definition: dim.hpp:35
Total number of bits.
Definition: encoding.hpp:21
Number of bases for dimension.
Definition: dim.hpp:30
template<typename DBO>
static constexpr word vnix::units::basic_dim< DBO >::encode ( std::array< rat, NUM_BASES > const &  a)
inlinestaticprivate

Definition at line 70 of file dim.hpp.

70  {
71  word es = 0;
72  for (uint_fast8_t i = 0; i < NUM_BASES; ++i) {
73  auto const SHIFT = i * rat::BITS;
74  word const MASK = bit_range<word>(0, rat::BITS - 1) << SHIFT;
75  word const e = rat::encode(a[i]) << SHIFT;
76  es |= (e & MASK);
77  }
78  return es;
79  }
static constexpr utype encode(rational r)
Encoding from rational number.
Definition: rational.hpp:113
typename int_types< NUM_BITS >::US word
Type of unsigned word in which dimensions are encoded.
Definition: dim.hpp:35
Total number of bits.
Definition: encoding.hpp:21
Number of bases for dimension.
Definition: dim.hpp:30
template<typename DBO>
constexpr word vnix::units::basic_dim< DBO >::encode ( ) const
inline

Encode data for this dim into a word.

Definition at line 104 of file dim.hpp.

104 { return e_; }
word e_
Storage for exponents.
Definition: dim.hpp:38

Here is the caller graph for this function:

template<typename DBO>
constexpr rat vnix::units::basic_dim< DBO >::exp ( DBO  off) const
inline

Rational exponent at specified offset.

Parameters
offOffset of exponent.

Definition at line 108 of file dim.hpp.

108  {
109  constexpr auto mask = bit_range<word>(0, rat::BITS - 1);
110  return rat::decode((e_ >> (off * rat::BITS)) & mask);
111  }
static constexpr rational decode(utype u)
Rational number from encoding.
Definition: rational.hpp:116
Total number of bits.
Definition: encoding.hpp:21
DBO off
Type of offset for each base-element.
Definition: dim.hpp:27
word e_
Storage for exponents.
Definition: dim.hpp:38

Here is the caller graph for this function:

template<typename DBO>
constexpr bool vnix::units::basic_dim< DBO >::operator!= ( basic_dim< DBO > const &  d) const
inline

Definition at line 229 of file dim.hpp.

229 { return e_ != d.e_; }
word e_
Storage for exponents.
Definition: dim.hpp:38

Here is the caller graph for this function:

template<typename DBO>
constexpr basic_dim vnix::units::basic_dim< DBO >::operator* ( rat  f) const
inline

Multiply exponents by rational factor.

This is called when a physical quantity is raised to a power.

Parameters
fFactor.
Returns
Products.

Definition at line 211 of file dim.hpp.

211 { return transform(mult(f)); }
constexpr basic_dim transform(F f) const
Transform each exponent according to a function.
Definition: dim.hpp:146

Here is the caller graph for this function:

template<typename DBO>
constexpr basic_dim vnix::units::basic_dim< DBO >::operator+ ( basic_dim< DBO > const &  a) const
inline

Add corresponding exponents.

This is called when two physical quantities are multiplied. Passing lambda in constexpr function requires C++17.

Parameters
aAddends.
Returns
Sums.

Definition at line 194 of file dim.hpp.

194  {
195  return combine(a, add);
196  }
constexpr basic_dim combine(basic_dim const &d, F f) const
Combine each exponent with corresponding other exponent via function.
Definition: dim.hpp:133
static constexpr rat add(rat x, rat y)
Function used to add corresponding exponents.
Definition: dim.hpp:156

Here is the caller graph for this function:

template<typename DBO>
constexpr basic_dim vnix::units::basic_dim< DBO >::operator- ( basic_dim< DBO > const &  s) const
inline

Subtract corresponding exponents.

This is called when one physical quantity is divided by another. Passing lambda in constexpr function requires C++17.

Parameters
sSubtrahends.
Returns
Differences.

Definition at line 203 of file dim.hpp.

203  {
204  return combine(s, sbtrct);
205  }
static constexpr rat sbtrct(rat x, rat y)
Function used to subtract corresponding exponents.
Definition: dim.hpp:159
constexpr basic_dim combine(basic_dim const &d, F f) const
Combine each exponent with corresponding other exponent via function.
Definition: dim.hpp:133
constexpr auto s
Constant-expression symbol for s.
Definition: units.hpp:1825

Here is the caller graph for this function:

template<typename DBO>
constexpr basic_dim vnix::units::basic_dim< DBO >::operator/ ( rat  f) const
inline

Divide exponents by rational factor.

This is called when a physical quantity is raised to a power.

Parameters
fFactor.
Returns
Products.

Definition at line 217 of file dim.hpp.

217 { return transform(divd(f)); }
constexpr basic_dim transform(F f) const
Transform each exponent according to a function.
Definition: dim.hpp:146

Here is the caller graph for this function:

template<typename DBO>
constexpr basic_dim vnix::units::basic_dim< DBO >::operator/ ( rat::stype  f) const
inline

Divide exponents by rational factor.

This is called when a physical quantity is raised to a power.

Parameters
fFactor.
Returns
Products.

Definition at line 223 of file dim.hpp.

223  {
224  return transform(divd(f));
225  }
constexpr basic_dim transform(F f) const
Transform each exponent according to a function.
Definition: dim.hpp:146

Here is the caller graph for this function:

template<typename DBO>
constexpr bool vnix::units::basic_dim< DBO >::operator== ( basic_dim< DBO > const &  d) const
inline

Definition at line 227 of file dim.hpp.

227 { return e_ == d.e_; }
word e_
Storage for exponents.
Definition: dim.hpp:38

Here is the caller graph for this function:

template<typename DBO>
constexpr rat vnix::units::basic_dim< DBO >::operator[] ( DBO  off) const
inline

Rational exponent at specified offset.

Parameters
offOffset of exponent.

Definition at line 115 of file dim.hpp.

115 { return exp(off); }
constexpr rat exp(DBO off) const
Rational exponent at specified offset.
Definition: dim.hpp:108
DBO off
Type of offset for each base-element.
Definition: dim.hpp:27

Here is the caller graph for this function:

template<typename DBO>
static std::ostream& vnix::units::basic_dim< DBO >::print_unit ( std::ostream &  s,
char const *  u,
rat  e 
)
inlinestatic

Print to output stream the symbolic contribution from a given unit.

Parameters
sOutput stream.
uAbbreviation for unit.
eExponent of unit.

Definition at line 235 of file dim.hpp.

235  {
236  if (e.to_bool()) {
237  s << " " << u;
238  if (e != rat(1)) {
239  s << "^";
240  if (e.d() != 1) { s << "["; }
241  s << e;
242  if (e.d() != 1) { s << "]"; }
243  }
244  }
245  return s;
246  }
rational< 4, 2 > rat
Type of rational for dimensioned values.
Definition: dim.hpp:26
constexpr auto s
Constant-expression symbol for s.
Definition: units.hpp:1825
template<typename DBO>
static constexpr rat vnix::units::basic_dim< DBO >::sbtrct ( rat  x,
rat  y 
)
inlinestatic

Function used to subtract corresponding exponents.

Definition at line 159 of file dim.hpp.

159 { return x - y; }

Here is the caller graph for this function:

template<typename DBO>
constexpr void vnix::units::basic_dim< DBO >::set ( DBO  off,
rat  r 
)
inline

Set rational exponent at specified offset.

Parameters
offOffset of exponent.
rRational exponent.

Definition at line 120 of file dim.hpp.

120  {
121  unsigned const bit_off = off * rat::BITS;
122  word const expon = rat::encode(r) << bit_off;
123  auto const mask = bit_range<word>(0, rat::BITS - 1) << bit_off;
124  e_ = (e_ & ~mask) | (expon & mask);
125  }
static constexpr utype encode(rational r)
Encoding from rational number.
Definition: rational.hpp:113
typename int_types< NUM_BITS >::US word
Type of unsigned word in which dimensions are encoded.
Definition: dim.hpp:35
Total number of bits.
Definition: encoding.hpp:21
DBO off
Type of offset for each base-element.
Definition: dim.hpp:27
word e_
Storage for exponents.
Definition: dim.hpp:38
template<typename DBO>
template<typename F >
constexpr basic_dim vnix::units::basic_dim< DBO >::transform ( f) const
inline

Transform each exponent according to a function.

Template Parameters
FType of function.
Parameters
fUnary function operating on rational.
Returns
New exponents.

Definition at line 146 of file dim.hpp.

146  {
147  basic_dim r(word(0));
148  for (auto b : off::array) {
149  auto const os = DBO(b);
150  r.set(os, f(exp(os)));
151  }
152  return r;
153  }
typename int_types< NUM_BITS >::US word
Type of unsigned word in which dimensions are encoded.
Definition: dim.hpp:35
constexpr rat exp(DBO off) const
Rational exponent at specified offset.
Definition: dim.hpp:108
constexpr basic_dim()
Definition: dim.hpp:82

Friends And Related Function Documentation

template<typename DBO>
std::ostream& operator<< ( std::ostream &  s,
basic_dim< DBO >  d 
)
friend

Print to to output stream.

Parameters
sReference to stream.
dDimension to print.

Definition at line 251 of file dim.hpp.

251  {
252  for (auto i : DBO::array) { print_unit(s, DBO::sym[i], d[i]); }
253  return s;
254  }
constexpr auto s
Constant-expression symbol for s.
Definition: units.hpp:1825
static std::ostream & print_unit(std::ostream &s, char const *u, rat e)
Print to output stream the symbolic contribution from a given unit.
Definition: dim.hpp:235

Member Data Documentation

template<typename DBO>
word vnix::units::basic_dim< DBO >::e_
private

Storage for exponents.

Definition at line 38 of file dim.hpp.


The documentation for this class was generated from the following file: