现在我有了这段代码:
if (minimizator_weighted) {
while (dataFile >> t >> e >> sigma) { // read data file value by value
/* some code */
}
}
else {
while (dataFile >> t >> e) { // all the same but will not read standard deviation
/* almost the same code */
}
}
正如您所看到的,if
和else
流之间的唯一区别是while
循环的条件。我想知道是否有可能优化该片段并重新使用代码?如果我能写出某种东西会很棒:
while ((minimizator_weighted) ? (dataFile >> t >> e >> sigma) : (dataFile >> t >> e)) { ... }
但我不确定这个技巧是否正确......你能告诉我一些优化吗?谢谢!
修改 这是完整的代码段
if (minimizator_weighted) {
while (dataFile >> t >> e >> sigma) { // read data file value by value
data_set::pt point;
point.t = t;
point.e = e;
point.c_vis = 0.0;
point.c_invis = 0.0;
if (std::abs(sigma) <= GSL_SQRT_DBL_MIN) // check for division-by-zero error
sigma = 1.0;
point.sigma = sigma;
set.curve.push_back(point); // store point
data_numPoints++; // collect some stats
set.curveAvg += e;
}
}
else {
while (dataFile >> t >> e) { // all the same but will not read standard deviation
data_set::pt point;
point.t = t;
point.e = e;
point.c_vis = 0.0;
point.c_invis = 0.0;
set.curve.push_back(point);
data_numPoints++;
set.curveAvg += e;
}
}
答案 0 :(得分:1)
添加间接级别
bool read_data_line1(istream& dataFile, T& t, E& e, Sig& sigma)
{ return dataFile >> t >> e >> sigma; }
bool read_data_line2(istream& dataFile, T& t, E& e, Sig&)
{ return dataFile >> t >> e; }
auto read_data_line_func = minimizator_weighted ? read_data_line1 : read_data_line2;
while(read_data_line_func(dataFile, t, e, sigma))
{
data_set::pt point;
point.t = t;
point.e = e;
point.c_vis = 0.0;
point.c_invis = 0.0;
if (minimizator_weighted)
{
if (std::abs(sigma) <= GSL_SQRT_DBL_MIN) // check for division-by-zero error
sigma = 1.0;
point.sigma = sigma;
}
set.curve.push_back(point); // store point
data_numPoints++; // collect some stats
set.curveAvg += e;
}
答案 1 :(得分:1)
我想我会这样说:
boolean[] flag = {false};
//New thread to show some repeated animation
new Thread(new Runnnable{ run() {
while(true){
someImageView.animate()....setListener(.. onComplete(){ flag[0] = true; } ..).start();
}
}).start()
//Wait for flag to be true to carry on in this thread
while(!flag[0]){
Thread.sleep(100);
}
答案 2 :(得分:1)
您的架构向我们展示了3件错误:
sigma
是某些对象的未使用变量minimizator_weighted
)minimizator_weighted
这些是基本的设计缺陷,不仅在这里花费你,而且在整个程序中。该修复程序将要求重新构建。让我告诉你如何做到这一点,你可以决定是继续解决问题还是修复架构。
首先,您需要 2 基本类型,1 st 将与您已定义的data_set::pt
匹配而不需要{{1然后我们用sigma
扩展它以添加data_set::pt_weighted
:
sigma
现在我们将编写提取运算符,再次从struct pt {
double t;
double e;
double c_vis;
double c_invis;
};
struct pt_weighted : pt {
double sigma;
};
的提取运算符开始并将其扩展为data_set::pt
:
data_set::pt_weighted
从这里开始,您需要开始使用模板。首先,您需要将istream& operator>> (istream& lhs, data_set::pt& rhs) {
rhs.c_vis = 0.0;
rhs.c_invis = 0.0;
return lhs >> rhs.t >> rhs.e;
}
istream& operator>> (istream& lhs, data_set::pt_weighted& rhs) {
lhs >> static_cast<data_set::pt&>(rhs) >> rhs.sigma;
// check for division-by-zero error
if(std::abs(rhs.sigma) <= GSL_SQRT_DBL_MIN) rhs.sigma = 1.0;
return lhs;
}
模板化为set.curve
或data_set::pt
的容器,然后您的功能需要更改为:
data_set::pt_weighted
如果您在运行时之前无法建立template <typename T>
void foo() {
for(T point; dataFile >> point;) {
set.curve.push_back(point); // store point
data_numPoints++; // collect some stats
set.curveAvg += point.e;
}
}
,则需要致电minimizator_weighted
,如:
foo