xo-unit: refactor: bpu_store replaces if-then tree for bu abbrevs
This commit is contained in:
parent
7ee9eca0dd
commit
867132c84c
6 changed files with 265 additions and 183 deletions
|
|
@ -4,10 +4,15 @@
|
||||||
|
|
||||||
#include "dimension.hpp"
|
#include "dimension.hpp"
|
||||||
#include "basis_unit_abbrev.hpp"
|
#include "basis_unit_abbrev.hpp"
|
||||||
|
//#include "bpu_store.hpp"
|
||||||
#include "xo/ratio/ratio.hpp"
|
#include "xo/ratio/ratio.hpp"
|
||||||
|
|
||||||
namespace xo {
|
namespace xo {
|
||||||
namespace qty {
|
namespace qty {
|
||||||
|
using bu_abbrev_type = flatstring<16>;
|
||||||
|
using scalefactor_ratio_type = xo::ratio::ratio<std::int64_t>;
|
||||||
|
using scalefactor2x_ratio_type = xo::ratio::ratio<__int128>;
|
||||||
|
|
||||||
/** @class basis_unit
|
/** @class basis_unit
|
||||||
* @brief A dimensionless multiple of a single natively-specified basis dimension
|
* @brief A dimensionless multiple of a single natively-specified basis dimension
|
||||||
*
|
*
|
||||||
|
|
@ -31,12 +36,13 @@ namespace xo {
|
||||||
constexpr const scalefactor_ratio_type & scalefactor() const { return scalefactor_; }
|
constexpr const scalefactor_ratio_type & scalefactor() const { return scalefactor_; }
|
||||||
///@}
|
///@}
|
||||||
|
|
||||||
constexpr bu_abbrev_type abbrev() const {
|
#ifdef OBSOLETE // use bu_abbrev(bu)
|
||||||
return abbrev::basis_unit2_abbrev(native_dim_,
|
constexpr bu_abbrev_type abbrev() const {
|
||||||
scalefactor_);
|
return basis_unit2_abbrev(native_dim_, scalefactor_);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
public: /* need public members so that a basis_unit instance can be a non-type template parameter (a structural type) */
|
public: /* public so instance can be a non-type template parameter (a structural type) */
|
||||||
/** @defgroup basis-unit-instance-vars **/
|
/** @defgroup basis-unit-instance-vars **/
|
||||||
///@{
|
///@{
|
||||||
/** @brief identifies a native unit, e.g. time (in seconds) **/
|
/** @brief identifies a native unit, e.g. time (in seconds) **/
|
||||||
|
|
@ -172,7 +178,6 @@ namespace xo {
|
||||||
} /*namespace bu*/
|
} /*namespace bu*/
|
||||||
} /*namespace detail*/
|
} /*namespace detail*/
|
||||||
|
|
||||||
|
|
||||||
namespace units {
|
namespace units {
|
||||||
/** for runtime work, would like to be able to promptly find special abbreviation
|
/** for runtime work, would like to be able to promptly find special abbreviation
|
||||||
* keyed by (native_dim, scalefactor).
|
* keyed by (native_dim, scalefactor).
|
||||||
|
|
|
||||||
|
|
@ -11,10 +11,14 @@
|
||||||
|
|
||||||
namespace xo {
|
namespace xo {
|
||||||
namespace qty {
|
namespace qty {
|
||||||
|
#ifdef OBSOLETE
|
||||||
using bu_abbrev_type = flatstring<16>;
|
using bu_abbrev_type = flatstring<16>;
|
||||||
using scalefactor_ratio_type = xo::ratio::ratio<std::int64_t>;
|
using scalefactor_ratio_type = xo::ratio::ratio<std::int64_t>;
|
||||||
|
using scalefactor2x_ratio_type = xo::ratio::ratio<__int128>;
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace abbrev {
|
namespace abbrev {
|
||||||
|
#ifdef OBSOLETE
|
||||||
static
|
static
|
||||||
constexpr bu_abbrev_type
|
constexpr bu_abbrev_type
|
||||||
fallback_unit_abbrev(const scalefactor_ratio_type & scalefactor,
|
fallback_unit_abbrev(const scalefactor_ratio_type & scalefactor,
|
||||||
|
|
@ -237,6 +241,7 @@ namespace xo {
|
||||||
|
|
||||||
return bu_abbrev_type();
|
return bu_abbrev_type();
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
} /*namespace abbrev*/
|
} /*namespace abbrev*/
|
||||||
} /*namespace qty*/
|
} /*namespace qty*/
|
||||||
} /*namespace xo*/
|
} /*namespace xo*/
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,10 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "basis_unit.hpp"
|
#include "basis_unit.hpp"
|
||||||
|
#include "bpu_store.hpp"
|
||||||
|
|
||||||
namespace xo {
|
namespace xo {
|
||||||
namespace qty {
|
namespace qty {
|
||||||
using bpu_abbrev_type = flatstring<24>;
|
|
||||||
|
|
||||||
using power_ratio_type = xo::ratio::ratio<std::int64_t>;
|
|
||||||
|
|
||||||
namespace abbrev {
|
namespace abbrev {
|
||||||
using power_abbrev_type = flatstring<16>;
|
using power_abbrev_type = flatstring<16>;
|
||||||
|
|
||||||
|
|
@ -43,7 +40,7 @@ namespace xo {
|
||||||
{
|
{
|
||||||
return (bpu_abbrev_type::from_flatstring
|
return (bpu_abbrev_type::from_flatstring
|
||||||
(flatstring_concat
|
(flatstring_concat
|
||||||
(basis_unit2_abbrev(native_dim, scalefactor),
|
(bu_abbrev(basis_unit(native_dim, scalefactor)),
|
||||||
flatstring_from_exponent(power.num(), power.den()))));
|
flatstring_from_exponent(power.num(), power.den()))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,137 +2,56 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "bpu.hpp"
|
//#include "bpu.hpp"
|
||||||
#include <vector>
|
#include "basis_unit.hpp"
|
||||||
|
#include "xo/ratio/ratio.hpp"
|
||||||
|
#include <array>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
namespace xo {
|
namespace xo {
|
||||||
namespace qty {
|
namespace qty {
|
||||||
/** @class basis_unit2_store
|
using bpu_abbrev_type = flatstring<24>;
|
||||||
* @brief Store known basis units for runtime
|
|
||||||
**/
|
|
||||||
template <typename Tag>
|
|
||||||
struct basis_unit2_store {
|
|
||||||
basis_unit2_store() : bu_abbrev_vv_(static_cast<std::size_t>(dim::n_dim)) {
|
|
||||||
this->bu_establish_abbrev_for<dim::mass, 1, 1000000000>();
|
|
||||||
this->bu_establish_abbrev_for<dim::mass, 1, 1000000>();
|
|
||||||
this->bu_establish_abbrev_for<dim::mass, 1, 1000>();
|
|
||||||
this->bu_establish_abbrev_for<dim::mass, 1, 1>();
|
|
||||||
this->bu_establish_abbrev_for<dim::mass, 1000, 1>();
|
|
||||||
this->bu_establish_abbrev_for<dim::mass, 1000000, 1>();
|
|
||||||
this->bu_establish_abbrev_for<dim::mass, 1000000000, 1>();
|
|
||||||
|
|
||||||
this->bu_establish_abbrev_for<dim::distance, 1, 1000000000>();
|
using power_ratio_type = xo::ratio::ratio<std::int64_t>;
|
||||||
this->bu_establish_abbrev_for<dim::distance, 1, 1000000>();
|
|
||||||
this->bu_establish_abbrev_for<dim::distance, 1, 1000>();
|
|
||||||
this->bu_establish_abbrev_for<dim::distance, 1, 1>();
|
|
||||||
this->bu_establish_abbrev_for<dim::distance, 1000, 1>();
|
|
||||||
|
|
||||||
this->bu_establish_abbrev_for<dim::time, 1, 1000000000>();
|
struct bu_dim_store {
|
||||||
this->bu_establish_abbrev_for<dim::time, 1, 1000000>();
|
/** max number of basis-units per dimension **/
|
||||||
this->bu_establish_abbrev_for<dim::time, 1, 1000>();
|
static constexpr std::size_t max_bu_per_dim = 25;
|
||||||
this->bu_establish_abbrev_for<dim::time, 1, 1>();
|
|
||||||
this->bu_establish_abbrev_for<dim::time, 60, 1>();
|
|
||||||
this->bu_establish_abbrev_for<dim::time, 3600, 1>();
|
|
||||||
this->bu_establish_abbrev_for<dim::time, 24*3600, 1>();
|
|
||||||
this->bu_establish_abbrev_for<dim::time, 250*24*3600, 1>();
|
|
||||||
this->bu_establish_abbrev_for<dim::time, 360*24*3600, 1>();
|
|
||||||
this->bu_establish_abbrev_for<dim::time, 365*24*3600, 1>();
|
|
||||||
|
|
||||||
this->bu_establish_abbrev_for<dim::currency, 1, 1>();
|
using entry_type = std::pair<scalefactor2x_ratio_type, bu_abbrev_type>;
|
||||||
|
|
||||||
this->bu_establish_abbrev_for<dim::price, 1, 1>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* e.g.
|
/* e.g.
|
||||||
* [(1/1000000000, "nm"), (1/1000000, "um"), (1/1000, "mm"), (1/1, "m"), (1000/1, "km")]
|
* [(1/1000000000, "nm"), (1/1000000, "um"), (1/1000, "mm"), (1/1, "m"), (1000/1, "km")]
|
||||||
*/
|
*/
|
||||||
using native_scale_v = std::vector<std::pair<scalefactor_ratio_type, bu_abbrev_type>>;
|
using native_scale_v = std::array<entry_type, max_bu_per_dim>;
|
||||||
|
|
||||||
/** @brief get basis-unit abbreviation at runtime **/
|
public:
|
||||||
bu_abbrev_type bu_abbrev(dim basis_dim,
|
constexpr bu_dim_store() = default;
|
||||||
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_lub_ix(basis_dim, scalefactor, bu_abbrev_v);
|
constexpr bool empty() const { return n_bu_ == 0; }
|
||||||
|
constexpr std::size_t size() const { return n_bu_; }
|
||||||
|
|
||||||
if ((i_abbrev < bu_abbrev_v.size())
|
constexpr const entry_type & operator[](std::size_t i) const { return bu_abbrev_v_[i]; }
|
||||||
&& (bu_abbrev_v[i_abbrev].first == scalefactor))
|
|
||||||
{
|
|
||||||
return bu_abbrev_v[i_abbrev].second;
|
|
||||||
} else {
|
|
||||||
return units::bu_fallback_abbrev(basis_dim, scalefactor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @brief get basis-power-unit abbreviation at runtime **/
|
|
||||||
bpu_abbrev_type bpu_abbrev(dim basis_dim,
|
|
||||||
const scalefactor_ratio_type & scalefactor,
|
|
||||||
const power_ratio_type & power)
|
|
||||||
{
|
|
||||||
return abbrev::bpu_abbrev(basis_dim,
|
|
||||||
scalefactor,
|
|
||||||
power);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <dim BasisDim, std::int64_t InnerScaleNum, std::int64_t InnerScaleDen>
|
|
||||||
void bu_establish_abbrev_for() {
|
|
||||||
this->bu_establish_abbrev
|
|
||||||
(basis_unit(BasisDim,
|
|
||||||
scalefactor_ratio_type(InnerScaleNum, InnerScaleDen)),
|
|
||||||
abbrev::basis_unit2_abbrev(BasisDim, scalefactor_ratio_type(InnerScaleNum, InnerScaleDen)));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @brief establish abbreviation @p abbrev for basis unit @p bu
|
|
||||||
**/
|
|
||||||
void bu_establish_abbrev(const basis_unit & bu,
|
|
||||||
const bu_abbrev_type & abbrev) {
|
|
||||||
|
|
||||||
auto & bu_abbrev_v = bu_abbrev_vv_[static_cast<std::size_t>(bu.native_dim())];
|
|
||||||
|
|
||||||
std::int32_t i_abbrev = 0;
|
|
||||||
|
|
||||||
if (!bu_abbrev_v.empty()) {
|
|
||||||
i_abbrev = bu_abbrev_lub_ix(bu.native_dim(),
|
|
||||||
bu.scalefactor(),
|
|
||||||
bu_abbrev_v);
|
|
||||||
}
|
|
||||||
|
|
||||||
auto entry = std::make_pair(bu.scalefactor(), abbrev);
|
|
||||||
|
|
||||||
if ((i_abbrev < bu_abbrev_v.size())
|
|
||||||
&& (bu_abbrev_v[i_abbrev].first == bu.scalefactor()))
|
|
||||||
{
|
|
||||||
bu_abbrev_v[i_abbrev] = entry;
|
|
||||||
} else {
|
|
||||||
bu_abbrev_v.insert(bu_abbrev_v.begin() + i_abbrev, entry);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
/** @brief get least-upper-bound index position in bu_abbrev_v[]
|
/** @brief get least-upper-bound index position in bu_abbrev_v[]
|
||||||
*
|
*
|
||||||
* return value in [0, n] where n = bu_abbrev_v.size()
|
* return value in [0, n] where n = .size()
|
||||||
**/
|
**/
|
||||||
static std::size_t bu_abbrev_lub_ix(dim basis_dim,
|
constexpr std::size_t abbrev_lub_ix(const scalefactor_ratio_type & scalefactor) const
|
||||||
const scalefactor_ratio_type & scalefactor,
|
|
||||||
const native_scale_v & bu_abbrev_v)
|
|
||||||
{
|
{
|
||||||
std::size_t n = bu_abbrev_v.size();
|
if (n_bu_ == 0)
|
||||||
|
|
||||||
if (n == 0)
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
std::size_t lo = 0;
|
std::size_t lo = 0;
|
||||||
std::size_t hi = n-1;
|
std::size_t hi = n_bu_-1;
|
||||||
|
|
||||||
if (scalefactor <= bu_abbrev_v[lo].first)
|
if (scalefactor <= bu_abbrev_v_[lo].first)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
auto cmp = (scalefactor <=> bu_abbrev_v[hi].first);
|
auto cmp = (scalefactor <=> bu_abbrev_v_[hi].first);
|
||||||
|
|
||||||
if (cmp > 0)
|
if (cmp > 0)
|
||||||
return n;
|
return n_bu_;
|
||||||
|
|
||||||
if (cmp == 0)
|
if (cmp == 0)
|
||||||
return hi;
|
return hi;
|
||||||
|
|
@ -144,7 +63,7 @@ namespace xo {
|
||||||
|
|
||||||
std::size_t mid = lo + (hi - lo)/2;
|
std::size_t mid = lo + (hi - lo)/2;
|
||||||
|
|
||||||
if (scalefactor > bu_abbrev_v[mid].first)
|
if (scalefactor > bu_abbrev_v_[mid].first)
|
||||||
lo = mid;
|
lo = mid;
|
||||||
else
|
else
|
||||||
hi = mid;
|
hi = mid;
|
||||||
|
|
@ -153,10 +72,167 @@ namespace xo {
|
||||||
return hi;
|
return hi;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
constexpr void insert_aux(std::size_t ix,
|
||||||
/* bu_abbrev_v[dim] holds known units for native unit dim */
|
const entry_type & entry)
|
||||||
std::vector<native_scale_v> bu_abbrev_vv_;
|
{
|
||||||
|
|
||||||
|
if (n_bu_ >= max_bu_per_dim)
|
||||||
|
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];
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
|
||||||
|
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:
|
||||||
|
std::size_t n_bu_ = 0;
|
||||||
|
std::array<entry_type, max_bu_per_dim> bu_abbrev_v_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** @class basis_unit2_store
|
||||||
|
* @brief Store known basis units for runtime
|
||||||
|
**/
|
||||||
|
//template <typename Tag>
|
||||||
|
struct basis_unit2_store {
|
||||||
|
constexpr basis_unit2_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"));
|
||||||
|
//this->bu_establish_abbrev_for<dim::price, 1, 1>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @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;
|
||||||
|
} else {
|
||||||
|
return units::bu_fallback_abbrev(basis_dim, scalefactor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef OBSOLETE
|
||||||
|
/** @brief get basis-power-unit abbreviation at runtime **/
|
||||||
|
bpu_abbrev_type bpu_abbrev(dim basis_dim,
|
||||||
|
const scalefactor_ratio_type & scalefactor,
|
||||||
|
const power_ratio_type & power)
|
||||||
|
{
|
||||||
|
return abbrev::bpu_abbrev(basis_dim,
|
||||||
|
scalefactor,
|
||||||
|
power);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef OBSOLETE
|
||||||
|
template <dim BasisDim, std::int64_t InnerScaleNum, std::int64_t InnerScaleDen>
|
||||||
|
void bu_establish_abbrev_for() {
|
||||||
|
this->bu_establish_abbrev
|
||||||
|
(basis_unit(BasisDim,
|
||||||
|
scalefactor_ratio_type(InnerScaleNum, InnerScaleDen)),
|
||||||
|
abbrev::basis_unit2_abbrev(BasisDim, scalefactor_ratio_type(InnerScaleNum, InnerScaleDen)));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
public:
|
||||||
|
/** **/
|
||||||
|
std::array<bu_dim_store, n_dim> bu_abbrev_vv_;
|
||||||
|
};
|
||||||
|
|
||||||
|
static constexpr basis_unit2_store bu_abbrev_store = basis_unit2_store();
|
||||||
|
|
||||||
|
constexpr bu_abbrev_type
|
||||||
|
bu_abbrev(const basis_unit & bu)
|
||||||
|
{
|
||||||
|
return bu_abbrev_store.bu_abbrev(bu.native_dim(), bu.scalefactor());
|
||||||
|
}
|
||||||
} /*namespace qty*/
|
} /*namespace qty*/
|
||||||
} /*namespace xo*/
|
} /*namespace xo*/
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,14 @@
|
||||||
/* @file bu.test.cpp */
|
/* @file bu.test.cpp */
|
||||||
|
|
||||||
#include "xo/unit/basis_unit.hpp"
|
#include "xo/unit/basis_unit.hpp"
|
||||||
|
#include "xo/unit/bpu_store.hpp"
|
||||||
#include "xo/indentlog/scope.hpp"
|
#include "xo/indentlog/scope.hpp"
|
||||||
//#include "xo/indentlog/print/tag.hpp"
|
//#include "xo/indentlog/print/tag.hpp"
|
||||||
#include <catch2/catch.hpp>
|
#include <catch2/catch.hpp>
|
||||||
|
|
||||||
namespace xo {
|
namespace xo {
|
||||||
using xo::qty::scalefactor_ratio_type;
|
using xo::qty::scalefactor_ratio_type;
|
||||||
using xo::qty::abbrev::basis_unit2_abbrev;
|
using xo::qty::bu_abbrev;
|
||||||
using xo::qty::basis_unit;
|
using xo::qty::basis_unit;
|
||||||
using xo::qty::bu_abbrev_type;
|
using xo::qty::bu_abbrev_type;
|
||||||
using xo::qty::native_unit2_v;
|
using xo::qty::native_unit2_v;
|
||||||
|
|
@ -21,11 +22,11 @@ namespace xo {
|
||||||
* we will need this for quantity<Repr, Int, natural_unit<Int>>
|
* we will need this for quantity<Repr, Int, natural_unit<Int>>
|
||||||
*/
|
*/
|
||||||
template <basis_unit bu>
|
template <basis_unit bu>
|
||||||
constexpr bu_abbrev_type bu_mpl_abbrev = bu.abbrev();
|
constexpr bu_abbrev_type bu_mpl_abbrev = bu_abbrev(bu);
|
||||||
|
|
||||||
TEST_CASE("basis_unit", "[basis_unit]") {
|
TEST_CASE("basis_unit", "[basis_unit]") {
|
||||||
static_assert(bu_mpl_abbrev<bu::gram> == bu::gram.abbrev());
|
static_assert(bu_mpl_abbrev<bu::gram> == bu_abbrev(bu::gram));
|
||||||
REQUIRE(bu_mpl_abbrev<bu::gram> == bu::gram.abbrev());
|
REQUIRE(bu_mpl_abbrev<bu::gram> == bu_abbrev(bu::gram));
|
||||||
} /*TEST_CASE(basis_unit)*/
|
} /*TEST_CASE(basis_unit)*/
|
||||||
|
|
||||||
TEST_CASE("basis_unit1", "[basis_unit]") {
|
TEST_CASE("basis_unit1", "[basis_unit]") {
|
||||||
|
|
@ -39,9 +40,9 @@ namespace xo {
|
||||||
static_assert(native_unit2_v[static_cast<int>(dim::currency)].native_dim() == dim::currency);
|
static_assert(native_unit2_v[static_cast<int>(dim::currency)].native_dim() == dim::currency);
|
||||||
static_assert(native_unit2_v[static_cast<int>(dim::price)].native_dim() == dim::price);
|
static_assert(native_unit2_v[static_cast<int>(dim::price)].native_dim() == dim::price);
|
||||||
|
|
||||||
log && log(xtag("mass*10^3", basis_unit2_abbrev(dim::mass, scalefactor_ratio_type(1000, 1))));
|
log && log(xtag("mass*10^3", bu_abbrev(bu::kilogram)));
|
||||||
|
|
||||||
static_assert(basis_unit2_abbrev(dim::mass, scalefactor_ratio_type(1000, 1))
|
static_assert(bu_abbrev(bu::kilogram)
|
||||||
== bu_abbrev_type::from_chars("kg"));
|
== bu_abbrev_type::from_chars("kg"));
|
||||||
|
|
||||||
log && log("---------------------");
|
log && log("---------------------");
|
||||||
|
|
@ -50,97 +51,97 @@ namespace xo {
|
||||||
|
|
||||||
# define REQUIRE_x2(x) static_assert(x); REQUIRE(x)
|
# define REQUIRE_x2(x) static_assert(x); REQUIRE(x)
|
||||||
|
|
||||||
log && log(xtag("mass", basis_unit2_abbrev(dim::distance, scalefactor_ratio_type(1, 1))));
|
log && log(xtag("mass", bu_abbrev(bu::meter)));
|
||||||
|
|
||||||
REQUIRE_x2(bu::picogram.abbrev() == bu_abbrev_type::from_chars("pg"));
|
REQUIRE_x2(bu_abbrev(bu::picogram) == bu_abbrev_type::from_chars("pg"));
|
||||||
REQUIRE_x2(bu::nanogram.abbrev()
|
REQUIRE_x2(bu_abbrev(bu::nanogram)
|
||||||
== bu_abbrev_type::from_chars("ng"));
|
== bu_abbrev_type::from_chars("ng"));
|
||||||
REQUIRE_x2(bu::microgram.abbrev()
|
REQUIRE_x2(bu_abbrev(bu::microgram)
|
||||||
== bu_abbrev_type::from_chars("ug"));
|
== bu_abbrev_type::from_chars("ug"));
|
||||||
REQUIRE_x2(bu::milligram.abbrev()
|
REQUIRE_x2(bu_abbrev(bu::milligram)
|
||||||
== bu_abbrev_type::from_chars("mg"));
|
== bu_abbrev_type::from_chars("mg"));
|
||||||
REQUIRE_x2(bu::gram.abbrev()
|
REQUIRE_x2(bu_abbrev(bu::gram)
|
||||||
== bu_abbrev_type::from_chars("g"));
|
== bu_abbrev_type::from_chars("g"));
|
||||||
REQUIRE_x2(bu::kilogram.abbrev()
|
REQUIRE_x2(bu_abbrev(bu::kilogram)
|
||||||
== bu_abbrev_type::from_chars("kg"));
|
== bu_abbrev_type::from_chars("kg"));
|
||||||
REQUIRE_x2(bu::tonne.abbrev()
|
REQUIRE_x2(bu_abbrev(bu::tonne)
|
||||||
== bu_abbrev_type::from_chars("t"));
|
== bu_abbrev_type::from_chars("t"));
|
||||||
REQUIRE_x2(bu::kilotonne.abbrev()
|
REQUIRE_x2(bu_abbrev(bu::kilotonne)
|
||||||
== bu_abbrev_type::from_chars("kt"));
|
== bu_abbrev_type::from_chars("kt"));
|
||||||
REQUIRE_x2(bu::megatonne.abbrev()
|
REQUIRE_x2(bu_abbrev(bu::megatonne)
|
||||||
== bu_abbrev_type::from_chars("Mt"));
|
== bu_abbrev_type::from_chars("Mt"));
|
||||||
REQUIRE_x2(bu::gigatonne.abbrev()
|
REQUIRE_x2(bu_abbrev(bu::gigatonne)
|
||||||
== bu_abbrev_type::from_chars("Gt"));
|
== bu_abbrev_type::from_chars("Gt"));
|
||||||
|
|
||||||
log && log(xtag("distance", basis_unit2_abbrev(dim::distance, scalefactor_ratio_type(1, 1))));
|
log && log(xtag("distance", bu_abbrev(bu::meter)));
|
||||||
|
|
||||||
REQUIRE_x2(bu::picometre.abbrev()
|
REQUIRE_x2(bu_abbrev(bu::picometre)
|
||||||
== bu_abbrev_type::from_chars("pm"));
|
== bu_abbrev_type::from_chars("pm"));
|
||||||
REQUIRE_x2(bu::nanometre.abbrev()
|
REQUIRE_x2(bu_abbrev(bu::nanometre)
|
||||||
== bu_abbrev_type::from_chars("nm"));
|
== bu_abbrev_type::from_chars("nm"));
|
||||||
REQUIRE_x2(bu::micrometre.abbrev()
|
REQUIRE_x2(bu_abbrev(bu::micrometre)
|
||||||
== bu_abbrev_type::from_chars("um"));
|
== bu_abbrev_type::from_chars("um"));
|
||||||
REQUIRE_x2(bu::millimetre.abbrev()
|
REQUIRE_x2(bu_abbrev(bu::millimetre)
|
||||||
== bu_abbrev_type::from_chars("mm"));
|
== bu_abbrev_type::from_chars("mm"));
|
||||||
REQUIRE_x2(bu::metre.abbrev()
|
REQUIRE_x2(bu_abbrev(bu::metre)
|
||||||
== bu_abbrev_type::from_chars("m"));
|
== bu_abbrev_type::from_chars("m"));
|
||||||
REQUIRE_x2(bu::kilometre.abbrev()
|
REQUIRE_x2(bu_abbrev(bu::kilometre)
|
||||||
== bu_abbrev_type::from_chars("km"));
|
== bu_abbrev_type::from_chars("km"));
|
||||||
REQUIRE_x2(bu::megametre.abbrev()
|
REQUIRE_x2(bu_abbrev(bu::megametre)
|
||||||
== bu_abbrev_type::from_chars("Mm"));
|
== bu_abbrev_type::from_chars("Mm"));
|
||||||
REQUIRE_x2(bu::gigametre.abbrev()
|
REQUIRE_x2(bu_abbrev(bu::gigametre)
|
||||||
== bu_abbrev_type::from_chars("Gm"));
|
== bu_abbrev_type::from_chars("Gm"));
|
||||||
|
|
||||||
REQUIRE_x2(bu::picometer.abbrev()
|
REQUIRE_x2(bu_abbrev(bu::picometer)
|
||||||
== bu_abbrev_type::from_chars("pm"));
|
== bu_abbrev_type::from_chars("pm"));
|
||||||
REQUIRE_x2(bu::nanometer.abbrev()
|
REQUIRE_x2(bu_abbrev(bu::nanometer)
|
||||||
== bu_abbrev_type::from_chars("nm"));
|
== bu_abbrev_type::from_chars("nm"));
|
||||||
REQUIRE_x2(bu::micrometer.abbrev()
|
REQUIRE_x2(bu_abbrev(bu::micrometer)
|
||||||
== bu_abbrev_type::from_chars("um"));
|
== bu_abbrev_type::from_chars("um"));
|
||||||
REQUIRE_x2(bu::millimeter.abbrev()
|
REQUIRE_x2(bu_abbrev(bu::millimeter)
|
||||||
== bu_abbrev_type::from_chars("mm"));
|
== bu_abbrev_type::from_chars("mm"));
|
||||||
REQUIRE_x2(bu::meter.abbrev()
|
REQUIRE_x2(bu_abbrev(bu::meter)
|
||||||
== bu_abbrev_type::from_chars("m"));
|
== bu_abbrev_type::from_chars("m"));
|
||||||
REQUIRE_x2(bu::kilometer.abbrev()
|
REQUIRE_x2(bu_abbrev(bu::kilometer)
|
||||||
== bu_abbrev_type::from_chars("km"));
|
== bu_abbrev_type::from_chars("km"));
|
||||||
REQUIRE_x2(bu::megameter.abbrev()
|
REQUIRE_x2(bu_abbrev(bu::megameter)
|
||||||
== bu_abbrev_type::from_chars("Mm"));
|
== bu_abbrev_type::from_chars("Mm"));
|
||||||
REQUIRE_x2(bu::gigameter.abbrev()
|
REQUIRE_x2(bu_abbrev(bu::gigameter)
|
||||||
== bu_abbrev_type::from_chars("Gm"));
|
== bu_abbrev_type::from_chars("Gm"));
|
||||||
|
|
||||||
REQUIRE_x2(bu::lightsecond.abbrev() == flatstring("lsec"));
|
REQUIRE_x2(bu_abbrev(bu::lightsecond) == flatstring("lsec"));
|
||||||
REQUIRE_x2(bu::astronomicalunit.abbrev() == flatstring("AU"));
|
REQUIRE_x2(bu_abbrev(bu::astronomicalunit) == flatstring("AU"));
|
||||||
|
|
||||||
REQUIRE_x2(bu::inch.abbrev() == flatstring("in"));
|
REQUIRE_x2(bu_abbrev(bu::inch) == flatstring("in"));
|
||||||
REQUIRE_x2(bu::foot.abbrev() == flatstring("ft"));
|
REQUIRE_x2(bu_abbrev(bu::foot) == flatstring("ft"));
|
||||||
REQUIRE_x2(bu::yard.abbrev() == flatstring("yd"));
|
REQUIRE_x2(bu_abbrev(bu::yard) == flatstring("yd"));
|
||||||
REQUIRE_x2(bu::mile.abbrev() == flatstring("mi"));
|
REQUIRE_x2(bu_abbrev(bu::mile) == flatstring("mi"));
|
||||||
|
|
||||||
log && log(xtag("time", basis_unit2_abbrev(dim::time, scalefactor_ratio_type(1, 1))));
|
log && log(xtag("time", bu_abbrev(bu::second)));
|
||||||
|
|
||||||
REQUIRE_x2(bu::picosecond.abbrev() == bu_abbrev_type::from_chars("ps"));
|
REQUIRE_x2(bu_abbrev(bu::picosecond) == bu_abbrev_type::from_chars("ps"));
|
||||||
REQUIRE_x2(bu::nanosecond.abbrev() == bu_abbrev_type::from_chars("ns"));
|
REQUIRE_x2(bu_abbrev(bu::nanosecond) == bu_abbrev_type::from_chars("ns"));
|
||||||
REQUIRE_x2(bu::microsecond.abbrev() == bu_abbrev_type::from_chars("us"));
|
REQUIRE_x2(bu_abbrev(bu::microsecond) == bu_abbrev_type::from_chars("us"));
|
||||||
REQUIRE_x2(bu::millisecond.abbrev() == bu_abbrev_type::from_chars("ms"));
|
REQUIRE_x2(bu_abbrev(bu::millisecond) == bu_abbrev_type::from_chars("ms"));
|
||||||
REQUIRE_x2(bu::second.abbrev() == bu_abbrev_type::from_chars("s"));
|
REQUIRE_x2(bu_abbrev(bu::second) == bu_abbrev_type::from_chars("s"));
|
||||||
REQUIRE_x2(bu::minute.abbrev() == bu_abbrev_type::from_chars("min"));
|
REQUIRE_x2(bu_abbrev(bu::minute) == bu_abbrev_type::from_chars("min"));
|
||||||
REQUIRE_x2(bu::hour.abbrev() == bu_abbrev_type::from_chars("hr"));
|
REQUIRE_x2(bu_abbrev(bu::hour) == bu_abbrev_type::from_chars("hr"));
|
||||||
REQUIRE_x2(bu::day.abbrev() == bu_abbrev_type::from_chars("dy"));
|
REQUIRE_x2(bu_abbrev(bu::day) == bu_abbrev_type::from_chars("dy"));
|
||||||
REQUIRE_x2(bu::week.abbrev() == bu_abbrev_type::from_chars("wk"));
|
REQUIRE_x2(bu_abbrev(bu::week) == bu_abbrev_type::from_chars("wk"));
|
||||||
REQUIRE_x2(bu::month.abbrev() == bu_abbrev_type::from_chars("mo"));
|
REQUIRE_x2(bu_abbrev(bu::month) == bu_abbrev_type::from_chars("mo"));
|
||||||
|
|
||||||
REQUIRE_x2(bu::year.abbrev() == bu_abbrev_type::from_chars("yr"));
|
REQUIRE_x2(bu_abbrev(bu::year) == bu_abbrev_type::from_chars("yr"));
|
||||||
REQUIRE_x2(bu::year250.abbrev() == bu_abbrev_type::from_chars("yr250"));
|
REQUIRE_x2(bu_abbrev(bu::year250) == bu_abbrev_type::from_chars("yr250"));
|
||||||
REQUIRE_x2(bu::year360.abbrev() == bu_abbrev_type::from_chars("yr360"));
|
REQUIRE_x2(bu_abbrev(bu::year360) == bu_abbrev_type::from_chars("yr360"));
|
||||||
REQUIRE_x2(bu::year365.abbrev() == bu_abbrev_type::from_chars("yr365"));
|
REQUIRE_x2(bu_abbrev(bu::year365) == bu_abbrev_type::from_chars("yr365"));
|
||||||
|
|
||||||
log && log(xtag("currency", basis_unit2_abbrev(dim::currency, scalefactor_ratio_type(1, 1))));
|
log && log(xtag("currency", bu_abbrev(bu::currency)));
|
||||||
|
|
||||||
REQUIRE_x2(bu::currency.abbrev() == flatstring("ccy"));
|
REQUIRE_x2(bu_abbrev(bu::currency) == flatstring("ccy"));
|
||||||
|
|
||||||
log && log(xtag("price", basis_unit2_abbrev(dim::price, scalefactor_ratio_type(1, 1))));
|
log && log(xtag("price", bu_abbrev(bu::price)));
|
||||||
|
|
||||||
REQUIRE_x2(bu::price.abbrev() == flatstring("px"));
|
REQUIRE_x2(bu_abbrev(bu::price) == flatstring("px"));
|
||||||
|
|
||||||
# undef REQUIRE_x2
|
# undef REQUIRE_x2
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,8 +12,6 @@ namespace xo {
|
||||||
namespace ut {
|
namespace ut {
|
||||||
/* compile-time tests */
|
/* compile-time tests */
|
||||||
|
|
||||||
namespace u = xo::qty::u;
|
|
||||||
|
|
||||||
using xo::qty::dim;
|
using xo::qty::dim;
|
||||||
using xo::qty::bu_abbrev_type;
|
using xo::qty::bu_abbrev_type;
|
||||||
using xo::qty::scalefactor_ratio_type;
|
using xo::qty::scalefactor_ratio_type;
|
||||||
|
|
@ -32,7 +30,7 @@ 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));
|
||||||
|
|
||||||
basis_unit2_store<class AnyTag> bu_store;
|
basis_unit2_store/*<class AnyTag>*/ bu_store;
|
||||||
|
|
||||||
log && log(xtag("mass*10^-9", bu_store.bu_abbrev(dim::mass, scalefactor_ratio_type( 1, 1000000000))));
|
log && log(xtag("mass*10^-9", bu_store.bu_abbrev(dim::mass, scalefactor_ratio_type( 1, 1000000000))));
|
||||||
log && log(xtag("mass*10^-6", bu_store.bu_abbrev(dim::mass, scalefactor_ratio_type( 1, 1000000))));
|
log && log(xtag("mass*10^-6", bu_store.bu_abbrev(dim::mass, scalefactor_ratio_type( 1, 1000000))));
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue