Mantid
Loading...
Searching...
No Matches
RowLocation.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 +
9#include <boost/algorithm/string/predicate.hpp>
10#include <utility>
11
13// equivalent to
14// #include <boost/algorithm/cxx14/equal.hpp>
15// or just #include <algorithm> in c++14
16// available in boost 1.54+ - required for RHEL7.
17
19
20RowLocation::RowLocation(RowPath path) : m_path(std::move(path)) {}
21RowPath const &RowLocation::path() const { return m_path; }
22int RowLocation::rowRelativeToParent() const { return m_path.back(); }
23bool RowLocation::isRoot() const { return m_path.empty(); }
24bool RowLocation::isChildOf(RowLocation const &other) const {
25 if (!isRoot()) {
26 if (other.isRoot()) {
27 return depth() == 1;
28 } else {
29 auto const &otherPath = other.path();
30 if (depth() - other.depth() == 1)
31 return boost::algorithm::equal(m_path.cbegin(), m_path.cend() - 1, otherPath.cbegin(), otherPath.cend());
32 else
33 return false;
34 }
35 } else {
36 return false;
37 }
38}
39
41 assertOrThrow(!isRoot(), "RowLocation::parent: cannot get parent of root node location.");
42 return RowLocation(RowPath(m_path.begin(), m_path.cend() - 1));
43}
44
46 auto childPath = RowPath(m_path);
47 childPath.emplace_back(n);
48 return RowLocation(childPath);
49}
50
51int RowLocation::depth() const { return static_cast<int>(m_path.size()); }
52
53std::ostream &operator<<(std::ostream &os, RowLocation const &location) {
54 auto &path = location.path();
55 os << "[";
56 if (!path.empty()) {
57 auto it = path.cbegin();
58 for (; it < path.cend() - 1; ++it)
59 os << (*it) << ", ";
60 os << (*it);
61 }
62 os << "]";
63 return os;
64}
65
66bool RowLocation::operator==(RowLocation const &other) const { return this->path() == other.path(); }
67
68bool RowLocation::operator!=(RowLocation const &other) const { return !((*this) == other); }
69
70bool RowLocation::operator<(RowLocation const &other) const {
71 auto &lhsPath = this->path();
72 auto &rhsPath = other.path();
73 return boost::algorithm::lexicographical_compare(lhsPath, rhsPath);
74}
75
76bool RowLocation::operator<=(RowLocation const &other) const { return (*this) < other || (*this) == other; }
77
78bool RowLocation::operator>=(RowLocation const &other) const { return !((*this) < other); }
79
80bool RowLocation::operator>(RowLocation const &other) const { return !((*this) <= other); }
81
83 assertOrThrow((*this) == ancestor || (*this).isDescendantOf(ancestor),
84 "RowLocation::relativeTo: Tried to get position relative to "
85 "node which was not an ancestor");
86 return RowLocation(RowPath(m_path.cbegin() + ancestor.depth(), m_path.cend()));
87}
88
89bool RowLocation::isSiblingOf(RowLocation const &other) const {
90 if (!(isRoot() || other.isRoot())) {
91 auto const &otherPath = other.path();
92 if (depth() == other.depth())
93 return boost::algorithm::equal(m_path.cbegin(), m_path.cend() - 1, otherPath.cbegin(), otherPath.cend() - 1);
94 }
95 return false;
96}
97
98bool RowLocation::isChildOrSiblingOf(RowLocation const &other) const { return isChildOf(other) || isSiblingOf(other); }
99
100bool RowLocation::isDescendantOf(RowLocation const &ancestor) const {
101 if (!isRoot()) {
102 if (ancestor.isRoot()) {
103 return true;
104 } else {
105 auto const &ancestorPath = ancestor.path();
106 if (depth() > ancestor.depth())
107 return boost::algorithm::equal(m_path.cbegin(), m_path.cbegin() + ancestor.depth(), ancestorPath.cbegin(),
108 ancestorPath.cend());
109 }
110 }
111 return false;
112}
113
114bool pathsSameUntilDepth(int depth, RowLocation const &locationA, RowLocation const &locationB) {
115 auto &pathA = locationA.path();
116 auto &pathB = locationB.path();
117 assertOrThrow(depth <= std::min(locationA.depth(), locationB.depth()),
118 "pathsSameUntilDepth: Comparison depth must be less than or "
119 "equal to the depth of both locations");
120 return boost::algorithm::equal(pathA.cbegin(), pathA.cbegin() + depth, pathB.cbegin(), pathB.cbegin() + depth);
121}
122} // namespace MantidQt::MantidWidgets::Batch
void assertOrThrow(bool condition, std::string const &message)
See the developer documentation for Batch Widget at developer.mantidproject.org/BatchWidget/index....
Definition: AssertOrThrow.h:15
bool operator<=(RowLocation const &other) const
Definition: RowLocation.cpp:76
bool isChildOrSiblingOf(RowLocation const &other) const
Definition: RowLocation.cpp:98
bool operator<(RowLocation const &other) const
Definition: RowLocation.cpp:70
bool operator>=(RowLocation const &other) const
Definition: RowLocation.cpp:78
bool operator!=(RowLocation const &other) const
Definition: RowLocation.cpp:68
bool isChildOf(RowLocation const &other) const
Definition: RowLocation.cpp:24
bool operator>(RowLocation const &other) const
Definition: RowLocation.cpp:80
bool operator==(RowLocation const &other) const
Definition: RowLocation.cpp:66
bool isDescendantOf(RowLocation const &other) const
RowLocation relativeTo(RowLocation const &ancestor) const
Definition: RowLocation.cpp:82
bool isSiblingOf(RowLocation const &other) const
Definition: RowLocation.cpp:89
Test ranges to if they are equal.
std::vector< int > RowPath
Definition: RowLocation.h:20
EXPORT_OPT_MANTIDQT_COMMON std::ostream & operator<<(std::ostream &os, Cell const &cell)
Definition: Cell.cpp:83
EXPORT_OPT_MANTIDQT_COMMON bool pathsSameUntilDepth(int depth, RowLocation const &locationA, RowLocation const &locationB)
bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred)
Definition: equal.hpp:63
STL namespace.