Skip to content

Commit

Permalink
Migrate to std::optional for size_t
Browse files Browse the repository at this point in the history
  • Loading branch information
peterfpeterson committed Jan 17, 2025
1 parent 06064dc commit 0537c1f
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ std::vector<MantidQt::MantidWidgets::Batch::Cell> cellsFromRow(Row const &row, c
qRangeCellOrDefault(row.qRange(), row.qRangeOutput(), &RangeInQ::step, precisionStd),
MantidQt::MantidWidgets::Batch::Cell(optionalToString(row.scaleFactor(), precision)),
MantidQt::MantidWidgets::Batch::Cell(MantidWidgets::optionsToString(row.reductionOptions())),
MantidQt::MantidWidgets::Batch::Cell(optionalToString(row.lookupIndex(), precision))});
MantidQt::MantidWidgets::Batch::Cell(optionalToString(row.lookupIndex(), precisionStd))});
}
} // namespace

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ MantidWidgets::Batch::Cell qRangeCellOrDefault(RangeInQ const &qRangeInput, Rang
return result;
}

boost::optional<size_t> incrementIndex(const Row &row) {
std::optional<size_t> incrementIndex(const Row &row) {
auto lookupIndex = row.lookupIndex();
return lookupIndex.is_initialized() ? boost::optional<size_t>(lookupIndex.get() + 1) : boost::none;
return lookupIndex.has_value() ? std::optional<size_t>(lookupIndex.value() + 1) : std::nullopt;
}

std::vector<MantidQt::MantidWidgets::Batch::Cell> cellsFromRow(Row const &row, boost::optional<int> precision) {
Expand All @@ -57,7 +57,7 @@ std::vector<MantidQt::MantidWidgets::Batch::Cell> cellsFromRow(Row const &row, b
qRangeCellOrDefault(row.qRange(), row.qRangeOutput(), &RangeInQ::step, precisionStd),
MantidQt::MantidWidgets::Batch::Cell(optionalToString(row.scaleFactor(), precision)),
MantidQt::MantidWidgets::Batch::Cell(MantidWidgets::optionsToString(row.reductionOptions())),
MantidQt::MantidWidgets::Batch::Cell(optionalToString(lookupIndex, precision))});
MantidQt::MantidWidgets::Batch::Cell(optionalToString(lookupIndex, precisionStd))});
}
} // namespace

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ void Experiment::updateLookupRow(LookupRow lookupRow, double tolerance) {
m_lookupTable.updateLookupRow(std::move(lookupRow), tolerance);
}

boost::optional<size_t> Experiment::getLookupRowIndexFromRow(Row const &row, double tolerance) const {
std::optional<size_t> Experiment::getLookupRowIndexFromRow(Row const &row, double tolerance) const {
if (auto const lookupRow = m_lookupTable.findLookupRow(row, tolerance)) {
return m_lookupTable.getIndex(lookupRow.get());
}
return boost::none;
return std::nullopt;
}

bool operator!=(Experiment const &lhs, Experiment const &rhs) { return !operator==(lhs, rhs); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class MANTIDQT_ISISREFLECTOMETRY_DLL Experiment {

void updateLookupRow(LookupRow lookupRow, double tolerance);

boost::optional<size_t> getLookupRowIndexFromRow(Row const &row, double tolerance) const;
std::optional<size_t> getLookupRowIndexFromRow(Row const &row, double tolerance) const;

private:
AnalysisMode m_analysisMode;
Expand Down
6 changes: 3 additions & 3 deletions qt/scientific_interfaces/ISISReflectometry/Reduction/Row.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Row::Row(std::vector<std::string> runNumbers, double theta,
: Item(), m_runNumbers(std::move(runNumbers)), m_theta(theta), m_qRange(std::move(qRange)), m_qRangeOutput(),
m_scaleFactor(std::move(scaleFactor)), m_transmissionRuns(std::move(transmissionRuns)),
m_reducedWorkspaceNames(std::move(reducedWorkspaceNames)), m_reductionOptions(std::move(reductionOptions)),
m_lookupIndex(boost::none), m_parent(nullptr) {
m_lookupIndex(std::nullopt), m_parent(nullptr) {
std::sort(m_runNumbers.begin(), m_runNumbers.end());
}

Expand All @@ -45,7 +45,7 @@ ReductionOptionsMap const &Row::reductionOptions() const { return m_reductionOpt

ReductionWorkspaces const &Row::reducedWorkspaceNames() const { return m_reducedWorkspaceNames; }

boost::optional<size_t> const &Row::lookupIndex() const { return m_lookupIndex; }
const std::optional<size_t> &Row::lookupIndex() const { return m_lookupIndex; }

void Row::setOutputNames(std::vector<std::string> const &outputNames) {
if (outputNames.size() != 3)
Expand All @@ -56,7 +56,7 @@ void Row::setOutputNames(std::vector<std::string> const &outputNames) {

void Row::setOutputQRange(RangeInQ qRange) { m_qRangeOutput = std::move(qRange); }

void Row::setLookupIndex(boost::optional<size_t> lookupIndex) { m_lookupIndex = std::move(lookupIndex); }
void Row::setLookupIndex(std::optional<size_t> lookupIndex) { m_lookupIndex = std::move(lookupIndex); }

void Row::resetOutputs() {
m_reducedWorkspaceNames.resetOutputNames();
Expand Down
7 changes: 4 additions & 3 deletions qt/scientific_interfaces/ISISReflectometry/Reduction/Row.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <boost/optional.hpp>
#include <boost/range/algorithm/set_algorithm.hpp>
#include <boost/variant.hpp>
#include <optional>
#include <string>
#include <vector>

Expand Down Expand Up @@ -43,11 +44,11 @@ class MANTIDQT_ISISREFLECTOMETRY_DLL Row : public Item {
boost::optional<double> scaleFactor() const;
ReductionOptionsMap const &reductionOptions() const;
ReductionWorkspaces const &reducedWorkspaceNames() const;
boost::optional<size_t> const &lookupIndex() const;
std::optional<size_t> const &lookupIndex() const;

void setOutputNames(std::vector<std::string> const &outputNames) override;
void setOutputQRange(RangeInQ qRange);
void setLookupIndex(boost::optional<size_t> lookupIndex);
void setLookupIndex(std::optional<size_t> lookupIndex);
void resetOutputs() override;
bool hasOutputWorkspace(std::string const &wsName) const;
void renameOutputWorkspace(std::string const &oldName, std::string const &newName) override;
Expand Down Expand Up @@ -75,7 +76,7 @@ class MANTIDQT_ISISREFLECTOMETRY_DLL Row : public Item {
TransmissionRunPair m_transmissionRuns;
ReductionWorkspaces m_reducedWorkspaceNames;
ReductionOptionsMap m_reductionOptions;
boost::optional<size_t> m_lookupIndex;
std::optional<size_t> m_lookupIndex;
mutable IGroup *m_parent;
friend class Encoder;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,30 @@ class BatchLookupIndexTest : public CxxTest::TestSuite {
constexpr auto angle = 0.5;
auto row = ModelCreationHelper::makeRow(angle);
auto model = Batch(m_experiment, m_instrument, m_runsTable, m_slicing);
TS_ASSERT_EQUALS(row.lookupIndex(), boost::none);
TS_ASSERT_EQUALS(row.lookupIndex(), std::nullopt);
model.updateLookupIndex(row);
TS_ASSERT(row.lookupIndex().is_initialized());
TS_ASSERT_EQUALS(row.lookupIndex().get(), size_t(1));
TS_ASSERT(row.lookupIndex().has_value());
TS_ASSERT_EQUALS(row.lookupIndex().value(), size_t(1));
}

void test_update_lookup_index_single_row_wildcard() {
constexpr auto angle = 0.1;
auto row = ModelCreationHelper::makeRow(angle);
auto model = Batch(m_experiment, m_instrument, m_runsTable, m_slicing);
TS_ASSERT_EQUALS(row.lookupIndex(), boost::none);
TS_ASSERT_EQUALS(row.lookupIndex(), std::nullopt);
model.updateLookupIndex(row);
TS_ASSERT(row.lookupIndex().is_initialized());
TS_ASSERT_EQUALS(row.lookupIndex().get(), size_t(0));
TS_ASSERT(row.lookupIndex().has_value());
TS_ASSERT_EQUALS(row.lookupIndex().value(), size_t(0));
}

void test_update_lookup_index_single_row_no_match() {
constexpr auto angle = 0.1;
auto row = ModelCreationHelper::makeRow(angle);
auto model = Batch(ModelCreationHelper::makeEmptyExperiment(), m_instrument, m_runsTable, m_slicing);
TS_ASSERT_EQUALS(row.lookupIndex(), boost::none);
TS_ASSERT_EQUALS(row.lookupIndex(), std::nullopt);
model.updateLookupIndex(row);
TS_ASSERT(!row.lookupIndex().is_initialized());
TS_ASSERT_EQUALS(row.lookupIndex(), boost::none);
TS_ASSERT(!row.lookupIndex().has_value());
TS_ASSERT_EQUALS(row.lookupIndex(), std::nullopt);
}

void test_update_lookup_index_group_updates_all_rows() {
Expand All @@ -63,34 +63,34 @@ class BatchLookupIndexTest : public CxxTest::TestSuite {
group.appendRow(std::move(rowB));
group.appendRow(std::move(rowW));
auto model = Batch(m_experiment, m_instrument, m_runsTable, m_slicing);
TS_ASSERT_EQUALS(group.mutableRows()[0].get().lookupIndex(), boost::none);
TS_ASSERT_EQUALS(group.mutableRows()[1].get().lookupIndex(), boost::none);
TS_ASSERT_EQUALS(group.mutableRows()[2].get().lookupIndex(), boost::none);
TS_ASSERT_EQUALS(group.mutableRows()[0].get().lookupIndex(), std::nullopt);
TS_ASSERT_EQUALS(group.mutableRows()[1].get().lookupIndex(), std::nullopt);
TS_ASSERT_EQUALS(group.mutableRows()[2].get().lookupIndex(), std::nullopt);

model.updateLookupIndexesOfGroup(group);

TS_ASSERT(group.mutableRows()[0].get().lookupIndex().is_initialized());
TS_ASSERT_EQUALS(group.mutableRows()[0].get().lookupIndex().get(), size_t(1));
TS_ASSERT(group.mutableRows()[1].get().lookupIndex().is_initialized());
TS_ASSERT_EQUALS(group.mutableRows()[1].get().lookupIndex().get(), size_t(2));
TS_ASSERT(group.mutableRows()[2].get().lookupIndex().is_initialized());
TS_ASSERT_EQUALS(group.mutableRows()[2].get().lookupIndex().get(), size_t(0));
TS_ASSERT(group.mutableRows()[0].get().lookupIndex().has_value());
TS_ASSERT_EQUALS(group.mutableRows()[0].get().lookupIndex().value(), size_t(1));
TS_ASSERT(group.mutableRows()[1].get().lookupIndex().has_value());
TS_ASSERT_EQUALS(group.mutableRows()[1].get().lookupIndex().value(), size_t(2));
TS_ASSERT(group.mutableRows()[2].get().lookupIndex().has_value());
TS_ASSERT_EQUALS(group.mutableRows()[2].get().lookupIndex().value(), size_t(0));
}

void test_update_lookup_index_table_updates_all_groups() {
auto model = Batch(m_experiment, m_instrument, m_runsTable, m_slicing);

for (auto const &group : m_runsTable.reductionJobs().groups()) {
for (auto const &row : group.rows()) {
TS_ASSERT_EQUALS(row.get().lookupIndex(), boost::none);
TS_ASSERT_EQUALS(row.get().lookupIndex(), std::nullopt);
}
}

model.updateLookupIndexesOfTable();

for (auto const &group : m_runsTable.reductionJobs().groups()) {
for (auto const &row : group.rows()) {
TS_ASSERT(row.is_initialized());
TS_ASSERT(row.has_value());
if (row.get().theta() == 0.5) {
TS_ASSERT_EQUALS(row.get().lookupIndex(), size_t(1));
} else {
Expand All @@ -110,9 +110,9 @@ class BatchLookupIndexTest : public CxxTest::TestSuite {

auto model = Batch(exp, m_instrument, m_runsTable, m_slicing);

TS_ASSERT_EQUALS(group.rows()[0].get().lookupIndex(), boost::none);
TS_ASSERT_EQUALS(group.rows()[0].get().lookupIndex(), std::nullopt);
model.updateLookupIndex(group.mutableRows()[0].get());
TS_ASSERT(!group.rows()[0].get().lookupIndex().is_initialized());
TS_ASSERT(!group.rows()[0].get().lookupIndex().has_value());
TS_ASSERT_EQUALS(group.rows()[0].get().state(), State::ITEM_ERROR);
TS_ASSERT_EQUALS(group.rows()[0].get().message(), "Multiple matching Experiment Setting rows")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ class RowTest : public CxxTest::TestSuite {

void test_set_get_lookup_row_index() {
auto row = makeEmptyRow();
auto index = boost::optional<size_t>{1};
auto index = std::optional<size_t>{1};
row.setLookupIndex(index);
TS_ASSERT(row.lookupIndex().is_initialized());
TS_ASSERT_EQUALS(row.lookupIndex().get(), index.get());
TS_ASSERT(row.lookupIndex().has_value());
TS_ASSERT_EQUALS(row.lookupIndex().value(), index.value());
}

void test_set_get_no_lookup_row_index() {
auto row = makeEmptyRow();
auto index = boost::none;
std::optional<size_t> index = std::nullopt;
row.setLookupIndex(index);
TS_ASSERT(!row.lookupIndex().is_initialized());
TS_ASSERT_EQUALS(row.lookupIndex(), boost::none);
TS_ASSERT(!row.lookupIndex().has_value());
TS_ASSERT_EQUALS(row.lookupIndex(), std::nullopt);
}
};

0 comments on commit 0537c1f

Please sign in to comment.