Skip to content

Commit f53efda

Browse files
committed
app: adds more random distribution parameter checks
1 parent e4d9f99 commit f53efda

File tree

1 file changed

+114
-42
lines changed

1 file changed

+114
-42
lines changed

app/gui/project-external-source-editor.cpp

Lines changed: 114 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ static status display_allocate_external_source(application& app,
3737
project_editor& ed,
3838
source_type part) noexcept
3939
{
40-
4140
switch (part) {
4241
case source_type::constant:
4342
return try_allocate_external_source(
@@ -167,8 +166,8 @@ bool show_random_distribution_input(
167166
src.ints[1] = 100;
168167
}
169168

170-
int a = src.ints[0];
171-
int b = src.ints[1];
169+
auto a = src.ints[0];
170+
auto b = src.ints[1];
172171

173172
if (ImGui::InputInt("a", &a)) {
174173
up++;
@@ -197,7 +196,6 @@ bool show_random_distribution_input(
197196

198197
auto a = src.reals[0];
199198
auto b = src.reals[1];
200-
201199
if (ImGui::InputDouble("a", &a)) {
202200
++up;
203201
if (a < b)
@@ -216,150 +214,224 @@ bool show_random_distribution_input(
216214
} break;
217215

218216
case distribution_type::bernouilli:
219-
if (old_current != current_item) {
217+
if (old_current != current_item)
220218
src.reals[0] = 0.5;
221-
}
222-
if (ImGui::InputDouble("p", &src.reals[0]))
219+
220+
if (auto p = src.reals[0];
221+
ImGui::InputDouble("p", &p) and 0.0 <= p and p <= 1.0) {
222+
src.reals[0] = p;
223223
++up;
224+
}
224225
break;
225226

226227
case distribution_type::binomial:
227228
if (old_current != current_item) {
228229
src.reals[0] = 0.5;
229230
src.ints[0] = 1;
230231
}
231-
if (ImGui::InputDouble("p", &src.reals[0]))
232+
233+
if (auto p = src.reals[0];
234+
ImGui::InputDouble("p", &p) and 0.0 <= p and p <= 1.0) {
235+
src.reals[0] = p;
232236
++up;
233-
if (ImGui::InputInt("t", &src.ints[0]))
237+
}
238+
239+
if (auto t = src.ints[0]; ImGui::InputInt("t", &t) and t >= 0) {
240+
src.ints[0] = t;
234241
++up;
242+
}
235243
break;
236244

237245
case distribution_type::negative_binomial:
238246
if (old_current != current_item) {
239247
src.reals[0] = 0.5;
240248
src.ints[0] = 1;
241249
}
242-
if (ImGui::InputDouble("p", &src.reals[0]))
250+
251+
if (auto p = src.reals[0];
252+
ImGui::InputDouble("p", &p) and 0.0 < p and p <= 1.0) {
253+
src.reals[0] = p;
243254
++up;
244-
if (ImGui::InputInt("t", &src.ints[0]))
255+
}
256+
257+
if (auto t = src.ints[0]; ImGui::InputInt("t", &t) and 0 < t) {
258+
src.ints[0] = t;
245259
++up;
260+
}
246261
break;
247262

248263
case distribution_type::geometric:
249-
if (old_current != current_item) {
264+
if (old_current != current_item)
250265
src.reals[0] = 0.5;
251-
}
252-
if (ImGui::InputDouble("p", &src.reals[0]))
266+
267+
if (auto p = src.reals[0];
268+
ImGui::InputDouble("p", &p) and 0.0 <= p and p < 1.0) {
269+
src.reals[0] = p;
253270
++up;
271+
}
254272
break;
255273

256274
case distribution_type::poisson:
257-
if (old_current != current_item) {
275+
if (old_current != current_item)
258276
src.reals[0] = 0.5;
259-
}
260-
if (ImGui::InputDouble("mean", &src.reals[0]))
277+
278+
if (auto m = src.reals[0]; ImGui::InputDouble("mean", &m) and 0.0 < m) {
279+
src.reals[0] = m;
261280
++up;
281+
}
262282
break;
263283

264284
case distribution_type::exponential:
265-
if (old_current != current_item) {
285+
if (old_current != current_item)
266286
src.reals[0] = 1.0;
267-
}
268-
if (ImGui::InputDouble("lambda", &src.reals[0]))
287+
288+
if (auto l = src.reals[0];
289+
ImGui::InputDouble("lambda", &l) and 0.0 < l) {
290+
src.reals[0] = l;
269291
++up;
292+
}
270293
break;
271294

272295
case distribution_type::gamma:
273296
if (old_current != current_item) {
274297
src.reals[0] = 1.0;
275298
src.reals[1] = 1.0;
276299
}
277-
if (ImGui::InputDouble("alpha", &src.reals[0]))
300+
301+
if (auto a = src.reals[0];
302+
ImGui::InputDouble("alpha", &a) and 0.0 < a) {
303+
src.reals[0] = a;
278304
++up;
279-
if (ImGui::InputDouble("beta", &src.reals[1]))
305+
}
306+
307+
if (auto b = src.reals[1]; ImGui::InputDouble("beta", &b) and 0.0 < b) {
308+
src.reals[1] = b;
280309
++up;
310+
}
281311
break;
282312

283313
case distribution_type::weibull:
284314
if (old_current != current_item) {
285315
src.reals[0] = 1.0;
286316
src.reals[1] = 1.0;
287317
}
288-
if (ImGui::InputDouble("a", &src.reals[0]))
318+
319+
if (auto a = src.reals[0]; ImGui::InputDouble("a", &a) and 0.0 < a) {
320+
src.reals[0] = a;
289321
++up;
290-
if (ImGui::InputDouble("b", &src.reals[1]))
322+
}
323+
324+
if (auto b = src.reals[1]; ImGui::InputDouble("b", &b) and 0.0 < b) {
325+
src.reals[1] = b;
291326
++up;
327+
}
292328
break;
293329

294330
case distribution_type::exterme_value:
295331
if (old_current != current_item) {
296332
src.reals[0] = 1.0;
297333
src.reals[1] = 0.0;
298334
}
299-
if (ImGui::InputDouble("a", &src.reals[0]))
335+
336+
if (auto a = src.reals[0]; ImGui::InputDouble("a", &a)) {
337+
src.reals[0] = a;
300338
++up;
301-
if (ImGui::InputDouble("b", &src.reals[1]))
339+
}
340+
341+
if (auto b = src.reals[1]; ImGui::InputDouble("b", &b) and 0.0 < b) {
342+
src.reals[1] = b;
302343
++up;
344+
}
303345
break;
304346

305347
case distribution_type::normal:
306348
if (old_current != current_item) {
307349
src.reals[0] = 0.0;
308350
src.reals[1] = 1.0;
309351
}
310-
if (ImGui::InputDouble("mean", &src.reals[0]))
352+
353+
if (auto m = src.reals[0]; ImGui::InputDouble("mean", &m)) {
354+
src.reals[0] = m;
311355
++up;
312-
if (ImGui::InputDouble("stddev", &src.reals[1]))
356+
}
357+
358+
if (auto s = src.reals[1];
359+
ImGui::InputDouble("stddev", &s) and 0.0 < s) {
360+
src.reals[1] = s;
313361
++up;
362+
}
314363
break;
315364

316365
case distribution_type::lognormal:
317366
if (old_current != current_item) {
318367
src.reals[0] = 0.0;
319368
src.reals[1] = 1.0;
320369
}
321-
if (ImGui::InputDouble("m", &src.reals[0]))
370+
371+
if (auto m = src.reals[0]; ImGui::InputDouble("mean", &m)) {
372+
src.reals[0] = m;
322373
++up;
323-
if (ImGui::InputDouble("s", &src.reals[1]))
374+
}
375+
376+
if (auto s = src.reals[1];
377+
ImGui::InputDouble("stddev", &s) and 0.0 < s) {
378+
src.reals[1] = s;
324379
++up;
380+
}
325381
break;
326382

327383
case distribution_type::chi_squared:
328-
if (old_current != current_item) {
384+
if (old_current != current_item)
329385
src.reals[0] = 1.0;
330-
}
331-
if (ImGui::InputDouble("n", &src.reals[0]))
386+
387+
if (auto n = src.reals[0]; ImGui::InputDouble("n", &n) and 0 < n) {
388+
src.reals[0] = n;
332389
++up;
390+
}
333391
break;
334392

335393
case distribution_type::cauchy:
336394
if (old_current != current_item) {
337-
src.reals[0] = 1.0;
338-
src.reals[1] = 0.0;
395+
src.reals[0] = 0.0;
396+
src.reals[1] = 1.0;
339397
}
340-
if (ImGui::InputDouble("a", &src.reals[0]))
398+
399+
if (auto a = src.reals[0]; ImGui::InputDouble("a", &a)) {
400+
src.reals[0] = a;
341401
++up;
342-
if (ImGui::InputDouble("b", &src.reals[1]))
402+
}
403+
404+
if (auto b = src.reals[1]; ImGui::InputDouble("b", &b) and 0.0 < b) {
405+
src.reals[1] = b;
343406
++up;
407+
}
344408
break;
345409

346410
case distribution_type::fisher_f:
347411
if (old_current != current_item) {
348412
src.reals[0] = 1.0;
349413
src.reals[1] = 1.0;
350414
}
351-
if (ImGui::InputDouble("m", &src.reals[0]))
415+
416+
if (auto m = src.reals[0]; ImGui::InputDouble("m", &m) and 0 < m) {
417+
src.reals[0] = m;
352418
++up;
353-
if (ImGui::InputDouble("s", &src.reals[1]))
419+
}
420+
421+
if (auto n = src.reals[1]; ImGui::InputDouble("s", &n) and 0 < n) {
422+
src.reals[1] = n;
354423
++up;
424+
}
355425
break;
356426

357427
case distribution_type::student_t:
358-
if (old_current != current_item) {
428+
if (old_current != current_item)
359429
src.reals[0] = 1.0;
360-
}
361-
if (ImGui::InputDouble("n", &src.reals[0]))
430+
431+
if (auto n = src.reals[0]; ImGui::InputDouble("n", &n) and 0 < n) {
432+
src.reals[0] = n;
362433
++up;
434+
}
363435
break;
364436
}
365437

0 commit comments

Comments
 (0)