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 "basis_unit_abbrev.hpp"
|
||||
//#include "bpu_store.hpp"
|
||||
#include "xo/ratio/ratio.hpp"
|
||||
|
||||
namespace xo {
|
||||
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
|
||||
* @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 bu_abbrev_type abbrev() const {
|
||||
return abbrev::basis_unit2_abbrev(native_dim_,
|
||||
scalefactor_);
|
||||
#ifdef OBSOLETE // use bu_abbrev(bu)
|
||||
constexpr bu_abbrev_type abbrev() const {
|
||||
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 **/
|
||||
///@{
|
||||
/** @brief identifies a native unit, e.g. time (in seconds) **/
|
||||
|
|
@ -172,7 +178,6 @@ namespace xo {
|
|||
} /*namespace bu*/
|
||||
} /*namespace detail*/
|
||||
|
||||
|
||||
namespace units {
|
||||
/** for runtime work, would like to be able to promptly find special abbreviation
|
||||
* keyed by (native_dim, scalefactor).
|
||||
|
|
|
|||
|
|
@ -11,10 +11,14 @@
|
|||
|
||||
namespace xo {
|
||||
namespace qty {
|
||||
#ifdef OBSOLETE
|
||||
using bu_abbrev_type = flatstring<16>;
|
||||
using scalefactor_ratio_type = xo::ratio::ratio<std::int64_t>;
|
||||
using scalefactor2x_ratio_type = xo::ratio::ratio<__int128>;
|
||||
#endif
|
||||
|
||||
namespace abbrev {
|
||||
#ifdef OBSOLETE
|
||||
static
|
||||
constexpr bu_abbrev_type
|
||||
fallback_unit_abbrev(const scalefactor_ratio_type & scalefactor,
|
||||
|
|
@ -237,6 +241,7 @@ namespace xo {
|
|||
|
||||
return bu_abbrev_type();
|
||||
}
|
||||
#endif
|
||||
} /*namespace abbrev*/
|
||||
} /*namespace qty*/
|
||||
} /*namespace xo*/
|
||||
|
|
|
|||
|
|
@ -6,13 +6,10 @@
|
|||
#pragma once
|
||||
|
||||
#include "basis_unit.hpp"
|
||||
#include "bpu_store.hpp"
|
||||
|
||||
namespace xo {
|
||||
namespace qty {
|
||||
using bpu_abbrev_type = flatstring<24>;
|
||||
|
||||
using power_ratio_type = xo::ratio::ratio<std::int64_t>;
|
||||
|
||||
namespace abbrev {
|
||||
using power_abbrev_type = flatstring<16>;
|
||||
|
||||
|
|
@ -43,7 +40,7 @@ namespace xo {
|
|||
{
|
||||
return (bpu_abbrev_type::from_flatstring
|
||||
(flatstring_concat
|
||||
(basis_unit2_abbrev(native_dim, scalefactor),
|
||||
(bu_abbrev(basis_unit(native_dim, scalefactor)),
|
||||
flatstring_from_exponent(power.num(), power.den()))));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,137 +2,56 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "bpu.hpp"
|
||||
#include <vector>
|
||||
//#include "bpu.hpp"
|
||||
#include "basis_unit.hpp"
|
||||
#include "xo/ratio/ratio.hpp"
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
|
||||
namespace xo {
|
||||
namespace qty {
|
||||
/** @class basis_unit2_store
|
||||
* @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>();
|
||||
using bpu_abbrev_type = flatstring<24>;
|
||||
|
||||
this->bu_establish_abbrev_for<dim::distance, 1, 1000000000>();
|
||||
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>();
|
||||
using power_ratio_type = xo::ratio::ratio<std::int64_t>;
|
||||
|
||||
this->bu_establish_abbrev_for<dim::time, 1, 1000000000>();
|
||||
this->bu_establish_abbrev_for<dim::time, 1, 1000000>();
|
||||
this->bu_establish_abbrev_for<dim::time, 1, 1000>();
|
||||
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>();
|
||||
struct bu_dim_store {
|
||||
/** max number of basis-units per dimension **/
|
||||
static constexpr std::size_t max_bu_per_dim = 25;
|
||||
|
||||
this->bu_establish_abbrev_for<dim::currency, 1, 1>();
|
||||
|
||||
this->bu_establish_abbrev_for<dim::price, 1, 1>();
|
||||
}
|
||||
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::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 **/
|
||||
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)];
|
||||
public:
|
||||
constexpr bu_dim_store() = default;
|
||||
|
||||
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())
|
||||
&& (bu_abbrev_v[i_abbrev].first == scalefactor))
|
||||
{
|
||||
return bu_abbrev_v[i_abbrev].second;
|
||||
} else {
|
||||
return units::bu_fallback_abbrev(basis_dim, scalefactor);
|
||||
}
|
||||
}
|
||||
constexpr const entry_type & operator[](std::size_t i) const { return bu_abbrev_v_[i]; }
|
||||
|
||||
/** @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[]
|
||||
*
|
||||
* 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,
|
||||
const scalefactor_ratio_type & scalefactor,
|
||||
const native_scale_v & bu_abbrev_v)
|
||||
constexpr std::size_t abbrev_lub_ix(const scalefactor_ratio_type & scalefactor) const
|
||||
{
|
||||
std::size_t n = bu_abbrev_v.size();
|
||||
|
||||
if (n == 0)
|
||||
if (n_bu_ == 0)
|
||||
return 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;
|
||||
|
||||
auto cmp = (scalefactor <=> bu_abbrev_v[hi].first);
|
||||
auto cmp = (scalefactor <=> bu_abbrev_v_[hi].first);
|
||||
|
||||
if (cmp > 0)
|
||||
return n;
|
||||
return n_bu_;
|
||||
|
||||
if (cmp == 0)
|
||||
return hi;
|
||||
|
|
@ -144,7 +63,7 @@ namespace xo {
|
|||
|
||||
std::size_t mid = lo + (hi - lo)/2;
|
||||
|
||||
if (scalefactor > bu_abbrev_v[mid].first)
|
||||
if (scalefactor > bu_abbrev_v_[mid].first)
|
||||
lo = mid;
|
||||
else
|
||||
hi = mid;
|
||||
|
|
@ -153,10 +72,167 @@ namespace xo {
|
|||
return hi;
|
||||
}
|
||||
|
||||
private:
|
||||
/* bu_abbrev_v[dim] holds known units for native unit dim */
|
||||
std::vector<native_scale_v> bu_abbrev_vv_;
|
||||
constexpr void insert_aux(std::size_t ix,
|
||||
const entry_type & entry)
|
||||
{
|
||||
|
||||
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 xo*/
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue