37 return aLhs.
value < aRhs;
63 std::function<
double(
double )> aValueFunc,
66 addSolution( aValueFunc( aResults.first.value ), &aResults.first, aResultFunc );
67 addSolution( aValueFunc( aResults.second.value ), &aResults.second, aResultFunc );
76 throw std::logic_error(
"Empty solution collector" );
113static std::string
maybeEmbrace(
const std::string& aText,
char aRequiredSymbol )
115 bool shouldEmbrace =
false;
120 for(
char c : aText )
126 else if( c == aRequiredSymbol && parenLevel == 0 )
127 shouldEmbrace =
true;
132 return '(' + aText +
')';
149 return aR1 * aR2 / ( aR1 + aR2 );
186 if( aValue < 1000.0 )
188 result = std::to_string(
static_cast<int>( aValue ) );
204 int valueAsInt =
static_cast<int>( aValue );
205 result = std::to_string( valueAsInt );
209 double mantissa = aValue - valueAsInt;
212 result += std::to_string( lround( mantissa * 10 ) );
246 if( std::isnan( aValue ) )
250 auto it = std::lower_bound( series.begin(), series.end(), aValue -
epsilon );
284 std::vector<RESISTANCE> result_list;
288 double multiplier = curr_decade / aList[0];
290 for(
const uint16_t listvalue : aList )
292 double value = multiplier * listvalue;
293 result_list.emplace_back( value,
strValue( value ) );
306 for(
size_t i = 0; i < series.size(); i++ )
317 for(
size_t i1 = 0; i1 <
m_buffer_1R.size(); i1++ )
319 for(
size_t i2 = i1; i2 <
m_buffer_1R.size(); i2++ )
332 if( std::isnan( aTarget ) )
354 auto valueFunc = [](
double aFoundValue )
375 auto valueFunc = [&](
double aFoundValue )
379 auto resultFunc = [&](
RESISTANCE& aFoundRes )
389 auto valueFunc = [&](
double aFoundValue )
393 auto resultFunc = [&](
RESISTANCE& aFoundRes )
414 auto valueFunc = [&](
double aFoundValue )
418 auto resultFunc = [&](
RESISTANCE& aFoundRes )
428 auto valueFunc = [&](
double aFoundValue )
432 auto resultFunc = [&](
RESISTANCE& aFoundRes )
448 auto valueFunc = [&](
double aFoundValue )
452 auto resultFunc = [&](
RESISTANCE& aFoundRes )
457 / ( r1.value + r2.value -
m_target ) ),
458 valueFunc, resultFunc );
463 auto valueFunc = [&](
double aFoundValue )
467 auto resultFunc = [&](
RESISTANCE& aFoundRes )
473 valueFunc, resultFunc );
Creates a vector of integers of the E12 series values.
Creates a vector of integers of the E1 series values.
Creates a vector of integers of the E24 series values.
Creates a vector of integers of the E3 series values.
Creates a vector of integers of the E6 series values.
A small class to help profiling.
void Show(std::ostream &aStream=std::cerr)
Print the elapsed time (in a suitable unit) to a stream.
std::array< std::optional< RESISTANCE >, NUMBER_OF_LEVELS > m_results
RESISTANCE calculate2RSolution()
Calculate the best combination consisting of exactly 2, 3 or 4 resistors.
std::vector< RESISTANCE > m_buffer_2R
RESISTANCE calculate3RSolution()
std::pair< RESISTANCE &, RESISTANCE & > findIn2RBuffer(double aTargetValue)
Find in 2R buffer two values nearest to the given value (one smaller and one larger).
std::vector< RESISTANCE > m_buffer_1R
std::vector< RESISTANCE > buildSeriesData(const ESERIES::ESERIES_VALUES &aList)
Add values from aList to m_e_series tables.
void Exclude(double aValue)
If any value of the selected E-series not available, it can be entered as an exclude value.
RESISTANCE calculate4RSolution()
void SetSeries(uint32_t aSeries)
Set E-series to be used in calculations.
std::vector< std::vector< RESISTANCE > > m_e_series
void prepare1RBuffer()
Build 1R buffer, which is selected E-series table with excluded values removed.
void Calculate()
Executes all the calculations.
void NewCalc(double aTargetValue)
Initialize next calculation, clear exclusion mask and erase results from previous calculation.
void prepare2RBuffer()
Build 2R buffer, which consists of all possible combinations of two resistors from 1R buffer (serial ...
std::vector< bool > m_exclude_mask
Helper class that collects solutions and keeps one with the best deviation.
void addSolution(double aValue, RESISTANCE *aFound, std::function< RESISTANCE(RESISTANCE &)> &aResultFunc)
Add single solution to the collector.
RESISTANCE GetBest()
Return the best collected combination, running the corresponding result_func.
std::function< RESISTANCE(RESISTANCE &)> m_best_result_func
void Add2RLookupResults(std::pair< RESISTANCE &, RESISTANCE & > aResults, std::function< double(double)> aValueFunc, std::function< RESISTANCE(RESISTANCE &)> aResultFunc)
Add two solutions, based on single 2R buffer lookup, to the collector.
SolutionCollector(double aTarget)
RESISTANCE * m_best_found_resistance
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
static std::string maybeEmbrace(const std::string &aText, char aRequiredSymbol)
If aText contains aRequiredSymbol as top-level (i.e.
static RESISTANCE parallelResistanceSimple(const RESISTANCE &aR1, const RESISTANCE &aR2)
static std::string strValue(double aValue)
static double parallelValue(double aR1, double aR2)
static double serialValue(double aR1, double aR2)
Functions calculating values and text representations of serial and parallel combinations.
static RESISTANCE serialResistanceSimple(const RESISTANCE &aR1, const RESISTANCE &aR2)
static RESISTANCE serialResistance(const RESISTANCE &aR1, const RESISTANCE &aR2)
bool operator<(const RESISTANCE &aLhs, double aRhs)
static RESISTANCE parallelResistance(const RESISTANCE &aR1, const RESISTANCE &aR2)
#define RES_EQUIV_CALC_FIRST_VALUE
#define RES_EQUIV_CALC_LAST_VALUE