Debian Codegraph @

intl/icu/source/i18n/units_converter.h

raw
1 // © 2020 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3
4 #include "unicode/utypes.h"
5
6 #if !UCONFIG_NO_FORMATTING
7 #ifndef __UNITS_CONVERTER_H__
8 #define __UNITS_CONVERTER_H__
9
10 #include "cmemory.h"
11 #include "measunit_impl.h"
12 #include "unicode/errorcode.h"
13 #include "unicode/stringpiece.h"
14 #include "unicode/uobject.h"
15 #include "units_converter.h"
16 #include "units_data.h"
17
18 U_NAMESPACE_BEGIN
19 namespace units {
20
21 /* Internal Structure */
22
23 // Constants corresponding to unitConstants in CLDR's units.xml.
24 enum Constants {
25 CONSTANT_FT2M, // ft_to_m
26 CONSTANT_PI, // PI
27 CONSTANT_GRAVITY, // Gravity of earth (9.80665 m/s^2), "g".
28 CONSTANT_G, // Newtonian constant of gravitation, "G".
29 CONSTANT_GAL_IMP2M3, // Gallon imp to m3
30 CONSTANT_LB2KG, // Pound to Kilogram
31 CONSTANT_GLUCOSE_MOLAR_MASS,
32 CONSTANT_ITEM_PER_MOLE,
33 CONSTANT_METERS_PER_AU,
34 CONSTANT_SEC_PER_JULIAN_YEAR,
35 CONSTANT_SPEED_OF_LIGHT_METERS_PER_SECOND,
36 CONSTANT_SHO_TO_M3, // https://en.wikipedia.org/wiki/Japanese_units_of_measurement
37 CONSTANT_TSUBO_TO_M2, // https://en.wikipedia.org/wiki/Japanese_units_of_measurement
38 CONSTANT_SHAKU_TO_M, // https://en.wikipedia.org/wiki/Japanese_units_of_measurement
39 CONSTANT_AMU, // Atomic Mass Unit https://www.nist.gov/pml/special-publication-811/nist-guide-si-chapter-5-units-outside-si#table7
40
41 // Must be the last element.
42 CONSTANTS_COUNT
43 };
44
45 // These values are a hard-coded subset of unitConstants in the units
46 // resources file. A unit test checks that all constants in the resource
47 // file are at least recognised by the code. Derived constants' values or
48 // hard-coded derivations are not checked.
49 // In ICU4J, these constants live in UnitConverter.Factor.getConversionRate().
50 static const double constantsValues[CONSTANTS_COUNT] = {
51 0.3048, // CONSTANT_FT2M
52 411557987.0 / 131002976.0, // CONSTANT_PI
53 9.80665, // CONSTANT_GRAVITY
54 6.67408E-11, // CONSTANT_G
55 0.00454609, // CONSTANT_GAL_IMP2M3
56 0.45359237, // CONSTANT_LB2KG
57 180.1557, // CONSTANT_GLUCOSE_MOLAR_MASS
58 6.02214076E+23, // CONSTANT_ITEM_PER_MOLE
59 149597870700, // CONSTANT_METERS_PER_AU
60 31557600, // CONSTANT_SEC_PER_JULIAN_YEAR
61 299792458, // CONSTANT_SPEED_OF_LIGHT_METERS_PER_SECOND
62 2401.0 / (1331.0 * 1000.0),
63 400.0 / 121.0,
64 4.0 / 121.0,
65 1.66053878283E-27, // CONSTANT_AMU
66 };
67
68 typedef enum Signum {
69 NEGATIVE = -1,
70 POSITIVE = 1,
71 } Signum;
72
73 /* Represents a conversion factor */
74 struct U_I18N_API Factor {
75 double factorNum = 1;
76 double factorDen = 1;
77 double offset = 0;
78 bool reciprocal = false;
79
80 // Exponents for the symbolic constants
81 int32_t constantExponents[CONSTANTS_COUNT] = {};
82
83 void multiplyBy(const Factor &rhs);
84 void divideBy(const Factor &rhs);
85 void divideBy(const uint64_t constant);
86
87 // Apply the power to the factor.
88 void power(int32_t power);
89
90 // Apply SI or binary prefix to the Factor.
91 void applyPrefix(UMeasurePrefix unitPrefix);
92
93 // Does an in-place substitution of the "symbolic constants" based on
94 // constantExponents (resetting the exponents).
95 //
96 // In ICU4J, see UnitConverter.Factor.getConversionRate().
97 void substituteConstants();
98 };
99
100 struct U_I18N_API ConversionInfo {
101 double conversionRate;
102 double offset;
103 bool reciprocal;
104 };
105
106 /*
107 * Adds a single factor element to the `Factor`. e.g "ft3m", "2.333" or "cup2m3". But not "cup2m3^3".
108 */
109 void U_I18N_API addSingleFactorConstant(StringPiece baseStr, int32_t power, Signum sigNum,
110 Factor &factor, UErrorCode &status);
111
112 /**
113 * Represents the conversion rate between `source` and `target`.
114 * TODO ICU-22683: COnsider moving the handling of special mappings (e.g. beaufort) to a separate
115 * struct.
116 */
117 struct U_I18N_API ConversionRate : public UMemory {
118 const MeasureUnitImpl source;
119 const MeasureUnitImpl target;
120 CharString specialSource;
121 CharString specialTarget;
122 double factorNum = 1;
123 double factorDen = 1;
124 double sourceOffset = 0;
125 double targetOffset = 0;
126 bool reciprocal = false;
127
128 ConversionRate(MeasureUnitImpl &&source, MeasureUnitImpl &&target)
129 : source(std::move(source)), target(std::move(target)), specialSource(), specialTarget() {}
130 };
131
132 enum Convertibility {
133 RECIPROCAL,
134 CONVERTIBLE,
135 UNCONVERTIBLE,
136 };
137
138 MeasureUnitImpl U_I18N_API extractCompoundBaseUnit(const MeasureUnitImpl &source,
139 const ConversionRates &conversionRates,
140 UErrorCode &status);
141
142 /**
143 * Check if the convertibility between `source` and `target`.
144 * For example:
145 * `meter` and `foot` are `CONVERTIBLE`.
146 * `meter-per-second` and `second-per-meter` are `RECIPROCAL`.
147 * `meter` and `pound` are `UNCONVERTIBLE`.
148 *
149 * NOTE:
150 * Only works with SINGLE and COMPOUND units. If one of the units is a
151 * MIXED unit, an error will occur. For more information, see UMeasureUnitComplexity.
152 */
153 Convertibility U_I18N_API extractConvertibility(const MeasureUnitImpl &source,
154 const MeasureUnitImpl &target,
155 const ConversionRates &conversionRates,
156 UErrorCode &status);
157
158 /**
159 * Converts from a source `MeasureUnit` to a target `MeasureUnit`.
160 *
161 * NOTE:
162 * Only works with SINGLE and COMPOUND units. If one of the units is a
163 * MIXED unit, an error will occur. For more information, see UMeasureUnitComplexity.
164 */
165 class U_I18N_API UnitsConverter : public UMemory {
166 public:
167 /**
168 * Constructor of `UnitConverter`.
169 * NOTE:
170 * - source and target must be under the same category
171 * - e.g. meter to mile --> both of them are length units.
172 * NOTE:
173 * This constructor creates an instance of `ConversionRates` internally.
174 *
175 * @param sourceIdentifier represents the source unit identifier.
176 * @param targetIdentifier represents the target unit identifier.
177 * @param status
178 */
179 UnitsConverter(StringPiece sourceIdentifier, StringPiece targetIdentifier, UErrorCode &status);
180
181 /**
182 * Constructor of `UnitConverter`.
183 * NOTE:
184 * - source and target must be under the same category
185 * - e.g. meter to mile --> both of them are length units.
186 *
187 * @param source represents the source unit.
188 * @param target represents the target unit.
189 * @param ratesInfo Contains all the needed conversion rates.
190 * @param status
191 */
192 UnitsConverter(const MeasureUnitImpl &source, const MeasureUnitImpl &target,
193 const ConversionRates &ratesInfo, UErrorCode &status);
194
195 /**
196 * Compares two single units and returns 1 if the first one is greater, -1 if the second
197 * one is greater and 0 if they are equal.
198 *
199 * NOTE:
200 * Compares only single units that are convertible.
201 */
202 static int32_t compareTwoUnits(const MeasureUnitImpl &firstUnit, const MeasureUnitImpl &SecondUnit,
203 const ConversionRates &ratesInfo, UErrorCode &status);
204
205 /**
206 * Convert a measurement expressed in the source unit to a measurement
207 * expressed in the target unit.
208 *
209 * @param inputValue the value to be converted.
210 * @return the converted value.
211 */
212 double convert(double inputValue) const;
213
214 /**
215 * The inverse of convert(): convert a measurement expressed in the target
216 * unit to a measurement expressed in the source unit.
217 *
218 * @param inputValue the value to be converted.
219 * @return the converted value.
220 */
221 double convertInverse(double inputValue) const;
222
223 ConversionInfo getConversionInfo() const;
224
225 private:
226 ConversionRate conversionRate_;
227
228 /**
229 * Initialises the object.
230 */
231 void init(const ConversionRates &ratesInfo, UErrorCode &status);
232
233 /**
234 * Convert from what should be discrete scale values for a particular unit like beaufort
235 * to a corresponding value in the base unit (which can have any decimal value, like meters/sec).
236 * This can handle different scales, specified by minBaseForScaleValues[].
237 */
238 double scaleToBase(double scaleValue, double minBaseForScaleValues[], int scaleMax) const;
239
240 /**
241 * Convert from a value in the base unit (which can have any decimal value, like meters/sec) to a corresponding
242 * discrete value in a scale (like beaufort), where each scale value represents a range of base values.
243 * This can handle different scales, specified by minBaseForScaleValues[].
244 */
245 double baseToScale(double baseValue, double minBaseForScaleValues[], int scaleMax) const;
246
247 };
248
249 } // namespace units
250 U_NAMESPACE_END
251
252 #endif //__UNITS_CONVERTER_H__
253
254 #endif /* #if !UCONFIG_NO_FORMATTING */