38 return aLhs.
value < aRhs;
66 void Add2RLookupResults( std::pair<RESISTANCE&, RESISTANCE&> aResults, std::function<
double(
double )> aValueFunc,
69 considerSolution( aValueFunc( aResults.first.value ), aResults.first, aResultFunc );
70 considerSolution( aValueFunc( aResults.second.value ), aResults.second, aResultFunc );
79 throw std::logic_error(
"Empty solution collector" );
105 std::vector<double> parts = aRes.
parts;
106 std::sort( parts.begin(), parts.end() );
112 for(
double v : parts )
130 if( candUnique != bestUnique )
131 return candUnique < bestUnique;
133 if( aCand.
parts.size() != aBest.
parts.size() )
134 return aCand.
parts.size() < aBest.
parts.size();
149static std::string
maybeEmbrace(
const std::string& aText,
char aRequiredSymbol )
151 bool shouldEmbrace =
false;
156 for(
char c : aText )
162 else if( c == aRequiredSymbol && parenLevel == 0 )
163 shouldEmbrace =
true;
168 return '(' + aText +
')';
185 return aR1 * aR2 / ( aR1 + aR2 );
191 std::vector<double> parts = aR1.
parts;
192 parts.insert( parts.end(), aR2.
parts.begin(), aR2.
parts.end() );
199 std::vector<double> parts = aR1.
parts;
200 parts.insert( parts.end(), aR2.
parts.begin(), aR2.
parts.end() );
207 std::vector<double> parts = aR1.
parts;
208 parts.insert( parts.end(), aR2.
parts.begin(), aR2.
parts.end() );
215 std::vector<double> parts = aR1.
parts;
216 parts.insert( parts.end(), aR2.
parts.begin(), aR2.
parts.end() );
230 if( aValue < 1000.0 )
232 result = std::to_string(
static_cast<int>( aValue ) );
248 int valueAsInt =
static_cast<int>( aValue );
249 result = std::to_string( valueAsInt );
253 double mantissa = aValue - valueAsInt;
256 result += std::to_string( lround( mantissa * 10 ) );
290 if( std::isnan( aValue ) )
294 auto it = std::lower_bound( series.begin(), series.end(), aValue -
epsilon );
328 std::vector<RESISTANCE> result_list;
332 double multiplier = curr_decade / aList[0];
334 for(
const uint16_t listvalue : aList )
336 double value = multiplier * listvalue;
337 result_list.emplace_back( value,
strValue( value ) );
350 for(
size_t i = 0; i < series.size(); i++ )
361 for(
size_t i1 = 0; i1 <
m_buffer_1R.size(); i1++ )
363 for(
size_t i2 = i1; i2 <
m_buffer_1R.size(); i2++ )
376 if( std::isnan( aTarget ) )
397 auto valueFunc = [](
double aFoundValue )
418 auto valueFunc = [&](
double aFoundValue )
422 auto resultFunc = [&](
RESISTANCE& aFoundRes )
431 auto valueFunc = [&](
double aFoundValue )
435 auto resultFunc = [&](
RESISTANCE& aFoundRes )
455 auto valueFunc = [&](
double aFoundValue )
459 auto resultFunc = [&](
RESISTANCE& aFoundRes )
468 auto valueFunc = [&](
double aFoundValue )
472 auto resultFunc = [&](
RESISTANCE& aFoundRes )
487 auto valueFunc = [&](
double aFoundValue )
491 auto resultFunc = [&](
RESISTANCE& aFoundRes )
497 valueFunc, resultFunc );
502 auto valueFunc = [&](
double aFoundValue )
506 auto resultFunc = [&](
RESISTANCE& aFoundRes )
511 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.
RESISTANCE GetBest()
Return the best collected combination.
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.
void considerSolution(double aValue, RESISTANCE &aFound, std::function< RESISTANCE(RESISTANCE &)> &aResultFunc)
SolutionCollector(double aTarget)
std::optional< RESISTANCE > m_best_solution
static bool betterCandidate(const RESISTANCE &aCand, const RESISTANCE &aBest)
static int uniqueCount(const RESISTANCE &aRes)
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
std::vector< double > parts