Skip to content

Commit a367c7c

Browse files
committed
refactor: remove goto statements and modernize FiffTime handling
- Remove all goto statements from 12 library files (124 total): mne_cov_matrix.cpp(1), mne_meas_data.cpp(2), inv_ecd_set.cpp(2), mne_sss_data.cpp(8), mne_msh_display_surface.cpp(8), mne_proj_op.cpp(9), mne_surface.cpp(10), inv_guess_data.cpp(16), mne_raw_info.cpp(18), mne_ctf_comp_data_set.cpp(18), mne_raw_data.cpp(32), mne_source_space.cpp(61) - Replace goto-label patterns with direct return/cleanup statements - parksmcclellan.cpp intentionally kept (Remez algorithm) - Replace FiffTime** new/delete with value-type FiffTime& overloading in get_meas_info (eliminates raw pointer ownership) - Fold start_time fallback logic (MEAS_DATE > id->time > {0,0}) into get_meas_info itself - Update test files for new get_meas_info API - Update optimization-requirements.md with new subsections
1 parent 5b2ea45 commit a367c7c

File tree

16 files changed

+572
-423
lines changed

16 files changed

+572
-423
lines changed

doc/dev-notes/optimization-requirements.md

Lines changed: 226 additions & 19 deletions
Large diffs are not rendered by default.

src/libraries/inv/dipole_fit/inv_ecd_set.cpp

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,9 @@ bool InvEcdSet::save_dipoles_bdip(const QString& fileName)
239239
one_out.khi2 = swap_float(one.khi2);
240240
if (fwrite(&one_out,sizeof(bdipEcdRec),1,out) != 1) {
241241
qCritical("Failed to write a dipole");
242-
goto bad;
242+
fclose(out);
243+
QFile::remove(fileName);
244+
return false;
243245
}
244246
nsave++;
245247
}
@@ -251,14 +253,6 @@ bool InvEcdSet::save_dipoles_bdip(const QString& fileName)
251253
}
252254
qInfo("Save %d dipoles in bdip format to %s\n",nsave,fileName.toUtf8().data());
253255
return true;
254-
255-
bad : {
256-
if (out) {
257-
fclose(out);
258-
QFile::remove(fileName);
259-
}
260-
return false;
261-
}
262256
}
263257

264258
//=============================================================================================================
@@ -292,18 +286,10 @@ bool InvEcdSet::save_dipoles_dip(const QString& fileName) const
292286
if (fclose(out) != 0) {
293287
out = nullptr;
294288
qInfo("%s", fileName.toUtf8().constData());
295-
goto bad;
289+
return false;
296290
}
297291
qInfo("Save %d dipoles in dip format to %s\n",nsave,fileName.toUtf8().data());
298292
return true;
299-
300-
bad : {
301-
if (out) {
302-
fclose(out);
303-
QFile::remove(fileName);
304-
}
305-
return false;
306-
}
307293
}
308294

309295
//=============================================================================================================

src/libraries/inv/dipole_fit/inv_guess_data.cpp

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ InvGuessData::InvGuessData(const QString &guessname, const QString &guess_surfna
130130
*/
131131
std::vector<std::unique_ptr<MNESourceSpace>> sp;
132132
if (MNESourceSpace::read_source_spaces(guessname,sp) == FAIL)
133-
goto bad;
133+
return;
134134
if (static_cast<int>(sp.size()) != 1) {
135135
qCritical("Incorrect number of source spaces in guess file");
136-
goto bad;
136+
return;
137137
}
138138
qInfo("Read guesses from %s\n",guessname.toUtf8().constData());
139139
guesses = std::move(sp[0]);
@@ -148,25 +148,25 @@ InvGuessData::InvGuessData(const QString &guessname, const QString &guess_surfna
148148
if (f->bem_model) {
149149
qInfo("Using inner skull surface from the BEM (%s)...\n",f->bemname.toUtf8().constData());
150150
if ((inner_skull = f->bem_model->fwd_bem_find_surface(FIFFV_BEM_SURF_ID_BRAIN)) == nullptr)
151-
goto bad;
151+
return;
152152
}
153153
else if (!guess_surfname.isEmpty()) {
154154
qInfo("Reading inner skull surface from %s...\n",guess_surfname.toUtf8().data());
155155
if ((inner_skull = MNESurface::read_bem_surface(guess_surfname,FIFFV_BEM_SURF_ID_BRAIN,true)) == nullptr)
156-
goto bad;
156+
return;
157157
free_inner_skull = true;
158158
}
159159
guesses.reset((MNESourceSpace*)FwdBemModel::make_guesses(inner_skull,guessrad,r0,grid,exclude,mindist).release());
160160
if (!guesses)
161-
goto bad;
161+
return;
162162
if (free_inner_skull)
163163
delete inner_skull;
164164
}
165165
{
166166
std::vector<std::unique_ptr<MNESourceSpace>> guesses_vec;
167167
guesses_vec.push_back(std::move(guesses));
168168
if (MNESourceSpace::transform_source_spaces_to(f->coord_frame,*f->mri_head_t,guesses_vec) != OK)
169-
goto bad;
169+
return;
170170
guesses = std::move(guesses_vec[0]);
171171
}
172172
qInfo("Guess locations are now in %s coordinates.\n",FiffCoordTrans::frame_name(f->coord_frame).toUtf8().constData());
@@ -194,7 +194,7 @@ InvGuessData::InvGuessData(const QString &guessname, const QString &guess_surfna
194194
for (k = 0; k < this->nguess; k++) {
195195
this->guess_fwd[k].reset(InvDipoleFitData::dipole_forward_one(f,Eigen::Vector3f(this->rr.row(k).transpose()),nullptr));
196196
if (!this->guess_fwd[k])
197-
goto bad;
197+
return;
198198
#ifdef DEBUG
199199
sing = this->guess_fwd[k]->sing;
200200
qInfo("%f %f %f\n",sing[0],sing[1],sing[2]);
@@ -206,11 +206,6 @@ InvGuessData::InvGuessData(const QString &guessname, const QString &guess_surfna
206206

207207
return;
208208
// return res;
209-
210-
bad : {
211-
return;
212-
// return nullptr;
213-
}
214209
}
215210

216211
//=============================================================================================================
@@ -227,10 +222,10 @@ InvGuessData::InvGuessData(const QString &guessname, const QString &guess_surfna
227222
*/
228223
std::vector<std::unique_ptr<MNESourceSpace>> sp;
229224
if (MNESourceSpace::read_source_spaces(guessname,sp) == FIFF_FAIL)
230-
goto bad;
225+
return;
231226
if (static_cast<int>(sp.size()) != 1) {
232227
qCritical("Incorrect number of source spaces in guess file");
233-
goto bad;
228+
return;
234229
}
235230
qInfo("Read guesses from %s\n",guessname.toUtf8().constData());
236231
guesses = std::move(sp[0]);
@@ -245,17 +240,17 @@ InvGuessData::InvGuessData(const QString &guessname, const QString &guess_surfna
245240
if (f->bem_model) {
246241
qInfo("Using inner skull surface from the BEM (%s)...\n",f->bemname.toUtf8().constData());
247242
if ((inner_skull = f->bem_model->fwd_bem_find_surface(FIFFV_BEM_SURF_ID_BRAIN)) == nullptr)
248-
goto bad;
243+
return;
249244
}
250245
else if (!guess_surfname.isEmpty()) {
251246
qInfo("Reading inner skull surface from %s...\n",guess_surfname.toUtf8().data());
252247
if ((inner_skull = MNESurface::read_bem_surface(guess_surfname,FIFFV_BEM_SURF_ID_BRAIN,true)) == nullptr)
253-
goto bad;
248+
return;
254249
free_inner_skull = true;
255250
}
256251
guesses.reset((MNESourceSpace*)FwdBemModel::make_guesses(inner_skull,guessrad,r0,grid,exclude,mindist).release());
257252
if (!guesses)
258-
goto bad;
253+
return;
259254
if (free_inner_skull)
260255
delete inner_skull;
261256
}
@@ -264,7 +259,7 @@ InvGuessData::InvGuessData(const QString &guessname, const QString &guess_surfna
264259
*/
265260
if (guesses->nuse == 0) {
266261
qCritical("No active guess locations remaining.");
267-
goto bad;
262+
return;
268263
}
269264
if (guess_save_name) {
270265
qCritical("###################DEBUG writing source spaces not yet implemented.");
@@ -279,7 +274,7 @@ InvGuessData::InvGuessData(const QString &guessname, const QString &guess_surfna
279274
std::vector<std::unique_ptr<MNESourceSpace>> guesses_vec;
280275
guesses_vec.push_back(std::move(guesses));
281276
if (MNESourceSpace::transform_source_spaces_to(f->coord_frame,*f->mri_head_t,guesses_vec) != OK)
282-
goto bad;
277+
return;
283278
guesses = std::move(guesses_vec[0]);
284279
}
285280
qInfo("Guess locations are now in %s coordinates.\n",FiffCoordTrans::frame_name(f->coord_frame).toUtf8().constData());
@@ -298,13 +293,9 @@ InvGuessData::InvGuessData(const QString &guessname, const QString &guess_surfna
298293
* Compute the guesses using the sphere model for speed
299294
*/
300295
if (!this->compute_guess_fields(f))
301-
goto bad;
296+
return;
302297

303298
return;
304-
305-
bad : {
306-
return;
307-
}
308299
}
309300

310301
//=============================================================================================================

src/libraries/mne/mne_cov_matrix.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,11 @@ int MNECovMatrix::decompose_eigen_small(float p_small, int use_rank)
559559

560560
lambda.resize(ncov);
561561
eigen.resize(ncov,ncov);
562-
if (mne_decompose_eigen (cov,lambda,eigen,ncov) != 0)
563-
goto bad;
562+
if (mne_decompose_eigen (cov,lambda,eigen,ncov) != 0) {
563+
lambda.resize(0);
564+
eigen.resize(0,0);
565+
return FAIL;
566+
}
564567
nzero = ncov - rank;
565568
for (k = 0; k < nzero; k++)
566569
lambda[k] = 0.0;
@@ -589,12 +592,6 @@ int MNECovMatrix::decompose_eigen_small(float p_small, int use_rank)
589592
}
590593
}
591594
return add_inv();
592-
593-
bad : {
594-
lambda.resize(0);
595-
eigen.resize(0,0);
596-
return FAIL;
597-
}
598595
}
599596

600597
//=============================================================================================================

0 commit comments

Comments
 (0)