Mantid
Loading...
Searching...
No Matches
ExtractSubtrees.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8#include <algorithm>
9#include <boost/iterator/transform_iterator.hpp>
10#include <tuple>
11
13
15 RandomAccessConstRowIterator regionEnd, int subtreeRootDepth) const
17 return std::find_if(std::next(subtreeBegin), regionEnd, [subtreeRootDepth](Row const &row) -> bool {
18 return row.location().depth() == subtreeRootDepth;
19 });
20}
21
23 RandomAccessConstRowIterator subtreeEnd) const {
24 auto subtree = std::vector<Row>();
25 subtree.reserve(std::distance(subtreeBegin, subtreeEnd));
26 std::transform(subtreeBegin, subtreeEnd, std::back_inserter(subtree), [&subtreeRootLocation](Row const &row) -> Row {
27 return Row(row.location().relativeTo(subtreeRootLocation), row.cells());
28 });
29 return subtree;
30}
31
32std::vector<Subtree> ExtractSubtrees::makeSubtreesFromRows(std::vector<Row> const &rows, int subtreeRootDepth) const {
33 auto subtrees = std::vector<Subtree>();
34 auto current = rows.cbegin();
35 while (current != rows.cend()) {
36 auto subtreeRootLocation = (*current).location();
37 auto subtreeBegin = current;
38 auto subtreeEnd = findEndOfSubtree(subtreeBegin, rows.cend(), subtreeRootDepth);
39 subtrees.emplace_back(makeSubtreeFromRows(subtreeRootLocation, subtreeBegin, subtreeEnd));
40 current = subtreeEnd;
41 }
42 return subtrees;
43}
44
45RowLocation rowToRowLocation(Row const &row) { return row.location(); }
46
47auto ExtractSubtrees::operator()(std::vector<Row> region) const -> boost::optional<std::vector<Subtree>> {
48 std::sort(region.begin(), region.end());
49 if (!region.empty()) {
50 auto subtreeRootDepth = rowToRowLocation(region[0]).depth();
51 auto rowLocationBegin = boost::make_transform_iterator(region.cbegin(), &rowToRowLocation);
52 auto rowLocationEnd = boost::make_transform_iterator(region.cend(), &rowToRowLocation);
53
54 if (allSubtreeRootsShareAParentAndAllSubtreeNodesAreConnected(subtreeRootDepth, rowLocationBegin, rowLocationEnd))
55 return makeSubtreesFromRows(region, subtreeRootDepth);
56 else
57 return boost::none;
58 } else {
59 return std::vector<Subtree>();
60 }
61}
62
63} // namespace MantidQt::MantidWidgets::Batch
Subtree makeSubtreeFromRows(RowLocation subtreeRootLocation, RandomAccessConstRowIterator subtreeBegin, RandomAccessConstRowIterator subtreeEnd) const
boost::optional< std::vector< Subtree > > operator()(std::vector< Row > region) const
std::vector< Row >::const_iterator RandomAccessConstRowIterator
RandomAccessConstRowIterator findEndOfSubtree(RandomAccessConstRowIterator subtreeBegin, RandomAccessConstRowIterator regionEnd, int subtreeRootDepth) const
std::vector< Subtree > makeSubtreesFromRows(std::vector< Row > const &rows, int subtreeRootDepth) const
RowLocation relativeTo(RowLocation const &ancestor) const
Definition: RowLocation.cpp:82
RowLocation const & location() const
Definition: Row.cpp:14
std::vector< Cell > const & cells() const
Definition: Row.cpp:16
bool allSubtreeRootsShareAParentAndAllSubtreeNodesAreConnected(int subtreeRootDepth, RowLocationConstIterator sortedRegionBegin, RowLocationConstIterator sortedRegionEnd)
Definition: Subtree.h:58
std::vector< Row > Subtree
Definition: Subtree.h:18
RowLocation rowToRowLocation(Row const &row)