xo-unit: refactor bu_store ns + expand docs
This commit is contained in:
parent
83aceebe33
commit
8844390090
3 changed files with 247 additions and 215 deletions
|
|
@ -7,7 +7,7 @@ Basis Unit Store
|
||||||
|
|
||||||
#include <xo/unit/bu_store.hpp>
|
#include <xo/unit/bu_store.hpp>
|
||||||
|
|
||||||
namespace bu = xo::qty::bu;
|
namespace bu = xo::qty::detail::bu;
|
||||||
|
|
||||||
A :code:`xo::qty::bu_store` is a small, constexpr, key-value store associating
|
A :code:`xo::qty::bu_store` is a small, constexpr, key-value store associating
|
||||||
abbreviations with basis units. To satisfy the constexpr requirement,
|
abbreviations with basis units. To satisfy the constexpr requirement,
|
||||||
|
|
@ -54,7 +54,28 @@ This class supports the implementation of ``natural_unit::abbrev()``.
|
||||||
|
|
||||||
Application code is not expected to interact directly with it.
|
Application code is not expected to interact directly with it.
|
||||||
|
|
||||||
.. doxygenclass:: xo::qty::bu_store
|
.. doxygenclass:: xo::qty::detail::bu_store
|
||||||
|
|
||||||
|
For example, this would be possible:
|
||||||
|
|
||||||
|
.. code-block:: cpp
|
||||||
|
|
||||||
|
#include <xo/unit/bu_store.hpp>
|
||||||
|
|
||||||
|
namespace bu = using xo::qty::detail::bu;
|
||||||
|
using xo::qty::detail::bu_store;
|
||||||
|
using xo::qty::dim;
|
||||||
|
using xo::flatstring;
|
||||||
|
|
||||||
|
constexpr bu_store store;
|
||||||
|
static_assert(store.bu_abbrev(bu::minute) == flatstring("min"));
|
||||||
|
static_assert(store.bu_abbrev(bu::microgram) == flatstring("ug"));
|
||||||
|
|
||||||
|
|
||||||
|
.. doxygenclass:: xo::qty::detail::bu_dim_store
|
||||||
|
.. doxygengroup:: bu-dim-store-type-traits
|
||||||
|
.. doxygengroup:: bu-dim-store-instance-vars
|
||||||
|
|
||||||
|
|
||||||
Constants
|
Constants
|
||||||
---------
|
---------
|
||||||
|
|
|
||||||
|
|
@ -14,201 +14,211 @@ namespace xo {
|
||||||
|
|
||||||
using power_ratio_type = xo::ratio::ratio<std::int64_t>;
|
using power_ratio_type = xo::ratio::ratio<std::int64_t>;
|
||||||
|
|
||||||
struct bu_dim_store {
|
namespace detail {
|
||||||
/** max number of basis-units per dimension **/
|
/** @class bu_dim_store
|
||||||
static constexpr std::size_t max_bu_per_dim = 25;
|
* @brief store basis-unit abbreviations for a particular dimension
|
||||||
|
|
||||||
using entry_type = std::pair<scalefactor2x_ratio_type, bu_abbrev_type>;
|
|
||||||
|
|
||||||
/* e.g.
|
|
||||||
* [(1/1000000000, "nm"), (1/1000000, "um"), (1/1000, "mm"), (1/1, "m"), (1000/1, "km")]
|
|
||||||
*/
|
|
||||||
using native_scale_v = std::array<entry_type, max_bu_per_dim>;
|
|
||||||
|
|
||||||
public:
|
|
||||||
constexpr bu_dim_store() = default;
|
|
||||||
|
|
||||||
constexpr bool empty() const { return n_bu_ == 0; }
|
|
||||||
constexpr std::size_t size() const { return n_bu_; }
|
|
||||||
|
|
||||||
constexpr const entry_type & operator[](std::size_t i) const { return bu_abbrev_v_[i]; }
|
|
||||||
|
|
||||||
/** @brief get least-upper-bound index position in bu_abbrev_v[]
|
|
||||||
*
|
|
||||||
* return value in [0, n] where n = .size()
|
|
||||||
**/
|
**/
|
||||||
constexpr std::size_t abbrev_lub_ix(const scalefactor_ratio_type & scalefactor) const
|
struct bu_dim_store {
|
||||||
{
|
/** max number of basis-units per dimension **/
|
||||||
if (n_bu_ == 0)
|
static constexpr std::size_t max_bu_per_dim = 25;
|
||||||
return 0;
|
|
||||||
|
|
||||||
std::size_t lo = 0;
|
/** @defgroup bu-dim-store-type-traits bu-dim-store type traits **/
|
||||||
std::size_t hi = n_bu_-1;
|
///@{
|
||||||
|
using entry_type = std::pair<scalefactor2x_ratio_type, bu_abbrev_type>;
|
||||||
|
|
||||||
if (scalefactor <= bu_abbrev_v_[lo].first)
|
/* e.g.
|
||||||
return 0;
|
* [(1/1000000000, "nm"), (1/1000000, "um"), (1/1000, "mm"), (1/1, "m"), (1000/1, "km")]
|
||||||
|
*/
|
||||||
|
using native_scale_v = std::array<entry_type, max_bu_per_dim>;
|
||||||
|
///@}
|
||||||
|
|
||||||
auto cmp = (scalefactor <=> bu_abbrev_v_[hi].first);
|
public:
|
||||||
|
constexpr bu_dim_store() = default;
|
||||||
|
|
||||||
if (cmp > 0)
|
constexpr bool empty() const { return n_bu_ == 0; }
|
||||||
return n_bu_;
|
constexpr std::size_t size() const { return n_bu_; }
|
||||||
|
|
||||||
|
constexpr const entry_type & operator[](std::size_t i) const { return bu_abbrev_v_[i]; }
|
||||||
|
|
||||||
|
/** @brief get least-upper-bound index position in bu_abbrev_v[]
|
||||||
|
*
|
||||||
|
* return value in [0, n] where n = .size()
|
||||||
|
**/
|
||||||
|
constexpr std::size_t abbrev_lub_ix(const scalefactor_ratio_type & scalefactor) const
|
||||||
|
{
|
||||||
|
if (n_bu_ == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
std::size_t lo = 0;
|
||||||
|
std::size_t hi = n_bu_-1;
|
||||||
|
|
||||||
|
if (scalefactor <= bu_abbrev_v_[lo].first)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
auto cmp = (scalefactor <=> bu_abbrev_v_[hi].first);
|
||||||
|
|
||||||
|
if (cmp > 0)
|
||||||
|
return n_bu_;
|
||||||
|
|
||||||
|
if (cmp == 0)
|
||||||
|
return hi;
|
||||||
|
|
||||||
|
while (hi-lo > 1) {
|
||||||
|
/* inv:
|
||||||
|
* bu_abbrev_v[lo].first < scalefactor <= bu_abbrev_v[hi].first
|
||||||
|
*/
|
||||||
|
|
||||||
|
std::size_t mid = lo + (hi - lo)/2;
|
||||||
|
|
||||||
|
if (scalefactor > bu_abbrev_v_[mid].first)
|
||||||
|
lo = mid;
|
||||||
|
else
|
||||||
|
hi = mid;
|
||||||
|
}
|
||||||
|
|
||||||
if (cmp == 0)
|
|
||||||
return hi;
|
return hi;
|
||||||
|
|
||||||
while (hi-lo > 1) {
|
|
||||||
/* inv:
|
|
||||||
* bu_abbrev_v[lo].first < scalefactor <= bu_abbrev_v[hi].first
|
|
||||||
*/
|
|
||||||
|
|
||||||
std::size_t mid = lo + (hi - lo)/2;
|
|
||||||
|
|
||||||
if (scalefactor > bu_abbrev_v_[mid].first)
|
|
||||||
lo = mid;
|
|
||||||
else
|
|
||||||
hi = mid;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return hi;
|
constexpr void insert_aux(std::size_t ix,
|
||||||
}
|
const entry_type & entry)
|
||||||
|
{
|
||||||
|
|
||||||
constexpr void insert_aux(std::size_t ix,
|
if (n_bu_ >= max_bu_per_dim)
|
||||||
const entry_type & entry)
|
return;
|
||||||
{
|
|
||||||
|
|
||||||
if (n_bu_ >= max_bu_per_dim)
|
++n_bu_;
|
||||||
return;
|
|
||||||
|
|
||||||
++n_bu_;
|
for (std::size_t dest_ix = n_bu_; dest_ix > ix; --dest_ix)
|
||||||
|
bu_abbrev_v_[dest_ix] = bu_abbrev_v_[dest_ix - 1];
|
||||||
|
|
||||||
for (std::size_t dest_ix = n_bu_; dest_ix > ix; --dest_ix)
|
bu_abbrev_v_[ix] = entry;
|
||||||
bu_abbrev_v_[dest_ix] = bu_abbrev_v_[dest_ix - 1];
|
}
|
||||||
|
|
||||||
bu_abbrev_v_[ix] = entry;
|
/** @brief establish abbreviation @p abbrev for basis unit @p bu
|
||||||
}
|
**/
|
||||||
|
constexpr void bu_establish_abbrev(const scalefactor_ratio_type & scalefactor,
|
||||||
|
const bu_abbrev_type & abbrev)
|
||||||
|
{
|
||||||
|
|
||||||
/** @brief establish abbreviation @p abbrev for basis unit @p bu
|
std::int32_t i_abbrev = this->abbrev_lub_ix(scalefactor);
|
||||||
|
|
||||||
|
auto entry = std::make_pair(scalefactor, abbrev);
|
||||||
|
|
||||||
|
if ((i_abbrev < bu_abbrev_v_.size())
|
||||||
|
&& (bu_abbrev_v_[i_abbrev].first == scalefactor))
|
||||||
|
{
|
||||||
|
bu_abbrev_v_[i_abbrev] = entry;
|
||||||
|
} else {
|
||||||
|
this->insert_aux(i_abbrev, entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
/** @defgroup bu-dim-store-instance-vars bu-dim-store instance vars **/
|
||||||
|
///@{
|
||||||
|
std::size_t n_bu_ = 0;
|
||||||
|
std::array<entry_type, max_bu_per_dim> bu_abbrev_v_;
|
||||||
|
///@}
|
||||||
|
}; /*bu_dim_store*/
|
||||||
|
|
||||||
|
/** @class bu_store
|
||||||
|
* @brief associate basis units with abbreviations
|
||||||
**/
|
**/
|
||||||
constexpr void bu_establish_abbrev(const scalefactor_ratio_type & scalefactor,
|
struct bu_store {
|
||||||
const bu_abbrev_type & abbrev)
|
constexpr bu_store() {
|
||||||
{
|
// ----- mass -----
|
||||||
|
|
||||||
std::int32_t i_abbrev = this->abbrev_lub_ix(scalefactor);
|
this->bu_establish_abbrev(detail::bu::picogram, bu_abbrev_type::from_chars("pg"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::nanogram, bu_abbrev_type::from_chars("ng"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::microgram, bu_abbrev_type::from_chars("ug"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::milligram, bu_abbrev_type::from_chars("mg"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::gram, bu_abbrev_type::from_chars("g"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::kilogram, bu_abbrev_type::from_chars("kg"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::tonne, bu_abbrev_type::from_chars("t"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::kilotonne, bu_abbrev_type::from_chars("kt"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::megatonne, bu_abbrev_type::from_chars("Mt"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::gigatonne, bu_abbrev_type::from_chars("Gt"));
|
||||||
|
|
||||||
auto entry = std::make_pair(scalefactor, abbrev);
|
// ----- distance -----
|
||||||
|
|
||||||
if ((i_abbrev < bu_abbrev_v_.size())
|
this->bu_establish_abbrev(detail::bu::picometer, bu_abbrev_type::from_chars("pm"));
|
||||||
&& (bu_abbrev_v_[i_abbrev].first == scalefactor))
|
this->bu_establish_abbrev(detail::bu::nanometer, bu_abbrev_type::from_chars("nm"));
|
||||||
{
|
this->bu_establish_abbrev(detail::bu::micrometer, bu_abbrev_type::from_chars("um"));
|
||||||
bu_abbrev_v_[i_abbrev] = entry;
|
this->bu_establish_abbrev(detail::bu::millimeter, bu_abbrev_type::from_chars("mm"));
|
||||||
} else {
|
this->bu_establish_abbrev(detail::bu::meter, bu_abbrev_type::from_chars("m"));
|
||||||
this->insert_aux(i_abbrev, entry);
|
this->bu_establish_abbrev(detail::bu::kilometer, bu_abbrev_type::from_chars("km"));
|
||||||
}
|
this->bu_establish_abbrev(detail::bu::megameter, bu_abbrev_type::from_chars("Mm"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::gigameter, bu_abbrev_type::from_chars("Gm"));
|
||||||
|
|
||||||
|
this->bu_establish_abbrev(detail::bu::lightsecond, bu_abbrev_type::from_chars("lsec"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::astronomicalunit, bu_abbrev_type::from_chars("AU"));
|
||||||
|
|
||||||
|
this->bu_establish_abbrev(detail::bu::inch, bu_abbrev_type::from_chars("in"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::foot, bu_abbrev_type::from_chars("ft"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::yard, bu_abbrev_type::from_chars("yd"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::mile, bu_abbrev_type::from_chars("mi"));
|
||||||
|
|
||||||
|
// ----- time -----
|
||||||
|
|
||||||
|
this->bu_establish_abbrev(detail::bu::picosecond, bu_abbrev_type::from_chars("ps"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::nanosecond, bu_abbrev_type::from_chars("ns"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::microsecond, bu_abbrev_type::from_chars("us"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::millisecond, bu_abbrev_type::from_chars("ms"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::second, bu_abbrev_type::from_chars("s"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::minute, bu_abbrev_type::from_chars("min"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::hour, bu_abbrev_type::from_chars("hr"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::day, bu_abbrev_type::from_chars("dy"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::week, bu_abbrev_type::from_chars("wk"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::month, bu_abbrev_type::from_chars("mo"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::year250, bu_abbrev_type::from_chars("yr250"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::year, bu_abbrev_type::from_chars("yr"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::year360, bu_abbrev_type::from_chars("yr360"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::year365, bu_abbrev_type::from_chars("yr365"));
|
||||||
|
|
||||||
|
// ----- misc (currency, price) -----
|
||||||
|
|
||||||
|
this->bu_establish_abbrev(detail::bu::currency, bu_abbrev_type::from_chars("ccy"));
|
||||||
|
this->bu_establish_abbrev(detail::bu::price, bu_abbrev_type::from_chars("px"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
static constexpr bu_abbrev_type
|
||||||
std::size_t n_bu_ = 0;
|
bu_fallback_abbrev(dim basis_dim,
|
||||||
std::array<entry_type, max_bu_per_dim> bu_abbrev_v_;
|
const scalefactor_ratio_type & scalefactor)
|
||||||
};
|
|
||||||
|
|
||||||
/** @class bu_store
|
|
||||||
* @brief associate basis units with abbreviations
|
|
||||||
**/
|
|
||||||
struct bu_store {
|
|
||||||
constexpr bu_store() {
|
|
||||||
// ----- mass -----
|
|
||||||
|
|
||||||
this->bu_establish_abbrev(detail::bu::picogram, bu_abbrev_type::from_chars("pg"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::nanogram, bu_abbrev_type::from_chars("ng"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::microgram, bu_abbrev_type::from_chars("ug"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::milligram, bu_abbrev_type::from_chars("mg"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::gram, bu_abbrev_type::from_chars("g"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::kilogram, bu_abbrev_type::from_chars("kg"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::tonne, bu_abbrev_type::from_chars("t"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::kilotonne, bu_abbrev_type::from_chars("kt"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::megatonne, bu_abbrev_type::from_chars("Mt"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::gigatonne, bu_abbrev_type::from_chars("Gt"));
|
|
||||||
|
|
||||||
// ----- distance -----
|
|
||||||
|
|
||||||
this->bu_establish_abbrev(detail::bu::picometer, bu_abbrev_type::from_chars("pm"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::nanometer, bu_abbrev_type::from_chars("nm"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::micrometer, bu_abbrev_type::from_chars("um"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::millimeter, bu_abbrev_type::from_chars("mm"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::meter, bu_abbrev_type::from_chars("m"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::kilometer, bu_abbrev_type::from_chars("km"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::megameter, bu_abbrev_type::from_chars("Mm"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::gigameter, bu_abbrev_type::from_chars("Gm"));
|
|
||||||
|
|
||||||
this->bu_establish_abbrev(detail::bu::lightsecond, bu_abbrev_type::from_chars("lsec"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::astronomicalunit, bu_abbrev_type::from_chars("AU"));
|
|
||||||
|
|
||||||
this->bu_establish_abbrev(detail::bu::inch, bu_abbrev_type::from_chars("in"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::foot, bu_abbrev_type::from_chars("ft"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::yard, bu_abbrev_type::from_chars("yd"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::mile, bu_abbrev_type::from_chars("mi"));
|
|
||||||
|
|
||||||
// ----- time -----
|
|
||||||
|
|
||||||
this->bu_establish_abbrev(detail::bu::picosecond, bu_abbrev_type::from_chars("ps"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::nanosecond, bu_abbrev_type::from_chars("ns"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::microsecond, bu_abbrev_type::from_chars("us"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::millisecond, bu_abbrev_type::from_chars("ms"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::second, bu_abbrev_type::from_chars("s"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::minute, bu_abbrev_type::from_chars("min"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::hour, bu_abbrev_type::from_chars("hr"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::day, bu_abbrev_type::from_chars("dy"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::week, bu_abbrev_type::from_chars("wk"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::month, bu_abbrev_type::from_chars("mo"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::year250, bu_abbrev_type::from_chars("yr250"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::year, bu_abbrev_type::from_chars("yr"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::year360, bu_abbrev_type::from_chars("yr360"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::year365, bu_abbrev_type::from_chars("yr365"));
|
|
||||||
|
|
||||||
// ----- misc (currency, price) -----
|
|
||||||
|
|
||||||
this->bu_establish_abbrev(detail::bu::currency, bu_abbrev_type::from_chars("ccy"));
|
|
||||||
this->bu_establish_abbrev(detail::bu::price, bu_abbrev_type::from_chars("px"));
|
|
||||||
}
|
|
||||||
|
|
||||||
static constexpr bu_abbrev_type
|
|
||||||
bu_fallback_abbrev(dim basis_dim,
|
|
||||||
const scalefactor_ratio_type & scalefactor)
|
|
||||||
{
|
|
||||||
return (bu_abbrev_type::from_flatstring
|
|
||||||
(flatstring_concat
|
|
||||||
(scalefactor.to_str<bu_abbrev_type::fixed_capacity>(),
|
|
||||||
native_unit2_v[static_cast<std::uint32_t>(basis_dim)].abbrev_str())));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @brief get basis-unit abbreviation at runtime **/
|
|
||||||
constexpr bu_abbrev_type bu_abbrev(dim basis_dim,
|
|
||||||
const scalefactor_ratio_type & scalefactor) const
|
|
||||||
{
|
|
||||||
const auto & bu_abbrev_v = bu_abbrev_vv_[static_cast<std::size_t>(basis_dim)];
|
|
||||||
|
|
||||||
std::size_t i_abbrev = bu_abbrev_v.abbrev_lub_ix(scalefactor);
|
|
||||||
|
|
||||||
if ((i_abbrev < bu_abbrev_v.size())
|
|
||||||
&& (bu_abbrev_v[i_abbrev].first == scalefactor))
|
|
||||||
{
|
{
|
||||||
return bu_abbrev_v[i_abbrev].second;
|
return (bu_abbrev_type::from_flatstring
|
||||||
} else {
|
(flatstring_concat
|
||||||
return bu_fallback_abbrev(basis_dim, scalefactor);
|
(scalefactor.to_str<bu_abbrev_type::fixed_capacity>(),
|
||||||
|
native_unit2_v[static_cast<std::uint32_t>(basis_dim)].abbrev_str())));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @brief get basis-unit abbreviation at runtime **/
|
||||||
|
constexpr bu_abbrev_type bu_abbrev(const basis_unit & bu) const
|
||||||
|
{
|
||||||
|
const auto & bu_abbrev_v = bu_abbrev_vv_[static_cast<std::size_t>(bu.native_dim())];
|
||||||
|
|
||||||
|
std::size_t i_abbrev = bu_abbrev_v.abbrev_lub_ix(bu.scalefactor());
|
||||||
|
|
||||||
|
if ((i_abbrev < bu_abbrev_v.size())
|
||||||
|
&& (bu_abbrev_v[i_abbrev].first == bu.scalefactor()))
|
||||||
|
{
|
||||||
|
return bu_abbrev_v[i_abbrev].second;
|
||||||
|
} else {
|
||||||
|
return bu_fallback_abbrev(bu.native_dim(), bu.scalefactor());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr void bu_establish_abbrev(const basis_unit & bu,
|
||||||
|
const bu_abbrev_type & abbrev) {
|
||||||
|
auto & dim_store = bu_abbrev_vv_[static_cast<std::size_t>(bu.native_dim_)];
|
||||||
|
|
||||||
|
dim_store.bu_establish_abbrev(bu.scalefactor_, abbrev);
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr void bu_establish_abbrev(const basis_unit & bu,
|
public:
|
||||||
const bu_abbrev_type & abbrev) {
|
/** **/
|
||||||
auto & dim_store = bu_abbrev_vv_[static_cast<std::size_t>(bu.native_dim_)];
|
std::array<bu_dim_store, n_dim> bu_abbrev_vv_;
|
||||||
|
};
|
||||||
dim_store.bu_establish_abbrev(bu.scalefactor_, abbrev);
|
} /*namespace detail*/
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
|
||||||
/** **/
|
|
||||||
std::array<bu_dim_store, n_dim> bu_abbrev_vv_;
|
|
||||||
};
|
|
||||||
|
|
||||||
/** @brief global abbreviation store.
|
/** @brief global abbreviation store.
|
||||||
*
|
*
|
||||||
|
|
@ -216,13 +226,13 @@ namespace xo {
|
||||||
* Extending the contents of this store at runtime is not supported,
|
* Extending the contents of this store at runtime is not supported,
|
||||||
* in favor of preserving constexpr abbreviations.
|
* in favor of preserving constexpr abbreviations.
|
||||||
**/
|
**/
|
||||||
static constexpr bu_store bu_abbrev_store = bu_store();
|
static constexpr detail::bu_store bu_abbrev_store;
|
||||||
|
|
||||||
/** @brief get abbreviation for basis-unit @p bu **/
|
/** @brief get abbreviation for basis-unit @p bu **/
|
||||||
constexpr bu_abbrev_type
|
constexpr bu_abbrev_type
|
||||||
bu_abbrev(const basis_unit & bu)
|
bu_abbrev(const basis_unit & bu)
|
||||||
{
|
{
|
||||||
return bu_abbrev_store.bu_abbrev(bu.native_dim(), bu.scalefactor());
|
return bu_abbrev_store.bu_abbrev(bu);
|
||||||
}
|
}
|
||||||
} /*namespace qty*/
|
} /*namespace qty*/
|
||||||
} /*namespace xo*/
|
} /*namespace xo*/
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ namespace xo {
|
||||||
namespace ut {
|
namespace ut {
|
||||||
/* compile-time tests */
|
/* compile-time tests */
|
||||||
|
|
||||||
|
namespace bu = xo::qty::detail::bu;
|
||||||
using xo::qty::bu_abbrev_store;
|
using xo::qty::bu_abbrev_store;
|
||||||
using xo::qty::bu_abbrev_type;
|
using xo::qty::bu_abbrev_type;
|
||||||
using xo::qty::scalefactor_ratio_type;
|
using xo::qty::scalefactor_ratio_type;
|
||||||
|
|
@ -30,55 +31,55 @@ namespace xo {
|
||||||
scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.basis_unit2_store"));
|
scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.basis_unit2_store"));
|
||||||
//log && log("(A)", xtag("foo", foo));
|
//log && log("(A)", xtag("foo", foo));
|
||||||
|
|
||||||
log && log(xtag("mass*10^-9", bu_abbrev_store.bu_abbrev(dim::mass, scalefactor_ratio_type( 1, 1000000000))));
|
log && log(xtag("mass*10^-9", bu_abbrev_store.bu_abbrev(bu::picogram)));
|
||||||
log && log(xtag("mass*10^-6", bu_abbrev_store.bu_abbrev(dim::mass, scalefactor_ratio_type( 1, 1000000))));
|
log && log(xtag("mass*10^-6", bu_abbrev_store.bu_abbrev(bu::microgram)));
|
||||||
log && log(xtag("mass*10^-3", bu_abbrev_store.bu_abbrev(dim::mass, scalefactor_ratio_type( 1, 1000))));
|
log && log(xtag("mass*10^-3", bu_abbrev_store.bu_abbrev(bu::milligram)));
|
||||||
log && log(xtag("mass", bu_abbrev_store.bu_abbrev(dim::mass, scalefactor_ratio_type( 1, 1))));
|
log && log(xtag("mass", bu_abbrev_store.bu_abbrev(bu::gram)));
|
||||||
log && log(xtag("mass*10^3", bu_abbrev_store.bu_abbrev(dim::mass, scalefactor_ratio_type( 1000, 1))));
|
log && log(xtag("mass*10^3", bu_abbrev_store.bu_abbrev(bu::kilogram)));
|
||||||
log && log(xtag("mass*10^6", bu_abbrev_store.bu_abbrev(dim::mass, scalefactor_ratio_type( 1000000, 1))));
|
log && log(xtag("mass*10^6", bu_abbrev_store.bu_abbrev(bu::tonne)));
|
||||||
log && log(xtag("mass*10^9", bu_abbrev_store.bu_abbrev(dim::mass, scalefactor_ratio_type( 1000000000, 1))));
|
log && log(xtag("mass*10^9", bu_abbrev_store.bu_abbrev(bu::megatonne)));
|
||||||
|
|
||||||
log && log(xtag("distance*10^-9", bu_abbrev_store.bu_abbrev(dim::distance, scalefactor_ratio_type( 1, 1000000000))));
|
log && log(xtag("distance*10^-9", bu_abbrev_store.bu_abbrev(bu::nanometer)));
|
||||||
log && log(xtag("distance*10^-6", bu_abbrev_store.bu_abbrev(dim::distance, scalefactor_ratio_type( 1, 1000000))));
|
log && log(xtag("distance*10^-6", bu_abbrev_store.bu_abbrev(bu::micrometer)));
|
||||||
log && log(xtag("distance*10^-3", bu_abbrev_store.bu_abbrev(dim::distance, scalefactor_ratio_type( 1, 1000))));
|
log && log(xtag("distance*10^-3", bu_abbrev_store.bu_abbrev(bu::millimeter)));
|
||||||
log && log(xtag("distance", bu_abbrev_store.bu_abbrev(dim::distance, scalefactor_ratio_type( 1, 1))));
|
log && log(xtag("distance", bu_abbrev_store.bu_abbrev(bu::meter)));
|
||||||
log && log(xtag("distance*10^3", bu_abbrev_store.bu_abbrev(dim::distance, scalefactor_ratio_type( 1000, 1))));
|
log && log(xtag("distance*10^3", bu_abbrev_store.bu_abbrev(bu::kilometer)));
|
||||||
|
|
||||||
log && log(xtag("time*10^-9", bu_abbrev_store.bu_abbrev(dim::time, scalefactor_ratio_type( 1, 1000000000))));
|
log && log(xtag("time*10^-9", bu_abbrev_store.bu_abbrev(bu::nanosecond)));
|
||||||
log && log(xtag("time*10^-6", bu_abbrev_store.bu_abbrev(dim::time, scalefactor_ratio_type( 1, 1000000))));
|
log && log(xtag("time*10^-6", bu_abbrev_store.bu_abbrev(bu::microsecond)));
|
||||||
log && log(xtag("time*10^-3", bu_abbrev_store.bu_abbrev(dim::time, scalefactor_ratio_type( 1, 1000))));
|
log && log(xtag("time*10^-3", bu_abbrev_store.bu_abbrev(bu::millisecond)));
|
||||||
log && log(xtag("time", bu_abbrev_store.bu_abbrev(dim::time, scalefactor_ratio_type( 1, 1))));
|
log && log(xtag("time", bu_abbrev_store.bu_abbrev(bu::second)));
|
||||||
log && log(xtag("time*60", bu_abbrev_store.bu_abbrev(dim::time, scalefactor_ratio_type( 60, 1))));
|
log && log(xtag("time*60", bu_abbrev_store.bu_abbrev(bu::minute)));
|
||||||
log && log(xtag("time*3600", bu_abbrev_store.bu_abbrev(dim::time, scalefactor_ratio_type( 3600, 1))));
|
log && log(xtag("time*3600", bu_abbrev_store.bu_abbrev(bu::hour)));
|
||||||
log && log(xtag("time*24*3600", bu_abbrev_store.bu_abbrev(dim::time, scalefactor_ratio_type( 24*3600, 1))));
|
log && log(xtag("time*24*3600", bu_abbrev_store.bu_abbrev(bu::day)));
|
||||||
log && log(xtag("time*250*24*3600", bu_abbrev_store.bu_abbrev(dim::time, scalefactor_ratio_type(250*24*3600, 1))));
|
log && log(xtag("time*250*24*3600", bu_abbrev_store.bu_abbrev(bu::year250)));
|
||||||
log && log(xtag("time*360*24*3600", bu_abbrev_store.bu_abbrev(dim::time, scalefactor_ratio_type(360*24*3600, 1))));
|
log && log(xtag("time*360*24*3600", bu_abbrev_store.bu_abbrev(bu::year360)));
|
||||||
log && log(xtag("time*365*24*3600", bu_abbrev_store.bu_abbrev(dim::time, scalefactor_ratio_type(365*24*3600, 1))));
|
log && log(xtag("time*365*24*3600", bu_abbrev_store.bu_abbrev(bu::year365)));
|
||||||
|
|
||||||
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(dim::mass, scalefactor_ratio_type( 1, 1000000000)).c_str(), "ng") == 0);
|
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(bu::nanogram), "ng") == 0);
|
||||||
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(dim::mass, scalefactor_ratio_type( 1, 1000000)).c_str(), "ug") == 0);
|
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(bu::microgram), "ug") == 0);
|
||||||
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(dim::mass, scalefactor_ratio_type( 1, 1000)).c_str(), "mg") == 0);
|
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(bu::milligram), "mg") == 0);
|
||||||
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(dim::mass, scalefactor_ratio_type( 1, 1)).c_str(), "g") == 0);
|
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(bu::gram), "g") == 0);
|
||||||
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(dim::mass, scalefactor_ratio_type( 1000, 1)).c_str(), "kg") == 0);
|
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(bu::kilogram), "kg") == 0);
|
||||||
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(dim::mass, scalefactor_ratio_type( 1000000, 1)).c_str(), "t") == 0);
|
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(bu::tonne), "t") == 0);
|
||||||
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(dim::mass, scalefactor_ratio_type( 1000000000, 1)).c_str(), "kt") == 0);
|
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(bu::kilotonne), "kt") == 0);
|
||||||
|
|
||||||
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(dim::distance, scalefactor_ratio_type( 1, 1000000000)).c_str(), "nm") == 0);
|
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(bu::nanometer), "nm") == 0);
|
||||||
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(dim::distance, scalefactor_ratio_type( 1, 1000000)).c_str(), "um") == 0);
|
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(bu::micrometer), "um") == 0);
|
||||||
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(dim::distance, scalefactor_ratio_type( 1, 1000)).c_str(), "mm") == 0);
|
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(bu::millimeter), "mm") == 0);
|
||||||
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(dim::distance, scalefactor_ratio_type( 1, 1)).c_str(), "m") == 0);
|
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(bu::meter), "m") == 0);
|
||||||
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(dim::distance, scalefactor_ratio_type( 1000, 1)).c_str(), "km") == 0);
|
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(bu::kilometer), "km") == 0);
|
||||||
|
|
||||||
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(dim::time, scalefactor_ratio_type( 1, 1000000000)).c_str(), "ns") == 0);
|
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(bu::nanosecond), "ns") == 0);
|
||||||
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(dim::time, scalefactor_ratio_type( 1, 1000000)).c_str(), "us") == 0);
|
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(bu::microsecond), "us") == 0);
|
||||||
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(dim::time, scalefactor_ratio_type( 1, 1000)).c_str(), "ms") == 0);
|
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(bu::millisecond), "ms") == 0);
|
||||||
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(dim::time, scalefactor_ratio_type( 1, 1)).c_str(), "s") == 0);
|
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(bu::second), "s") == 0);
|
||||||
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(dim::time, scalefactor_ratio_type( 60, 1)).c_str(), "min") == 0);
|
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(bu::minute), "min") == 0);
|
||||||
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(dim::time, scalefactor_ratio_type( 3600, 1)).c_str(), "hr") == 0);
|
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(bu::hour), "hr") == 0);
|
||||||
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(dim::time, scalefactor_ratio_type( 24*3600, 1)).c_str(), "dy") == 0);
|
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(bu::day), "dy") == 0);
|
||||||
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(dim::time, scalefactor_ratio_type(250*24*3600, 1)).c_str(), "yr250") == 0);
|
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(bu::year250), "yr250") == 0);
|
||||||
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(dim::time, scalefactor_ratio_type(360*24*3600, 1)).c_str(), "yr360") == 0);
|
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(bu::year360), "yr360") == 0);
|
||||||
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(dim::time, scalefactor_ratio_type(365*24*3600, 1)).c_str(), "yr365") == 0);
|
REQUIRE(::strcmp(bu_abbrev_store.bu_abbrev(bu::year365), "yr365") == 0);
|
||||||
|
|
||||||
} /*TEST_CASE(basis_unit2_store)*/
|
} /*TEST_CASE(basis_unit2_store)*/
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue