/
SortTableWorkspace.cpp
83 lines (68 loc) · 3.34 KB
/
SortTableWorkspace.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidDataHandling/SortTableWorkspace.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidKernel/ArrayProperty.h"
namespace Mantid::DataHandling {
using Mantid::API::WorkspaceProperty;
using Mantid::Kernel::Direction;
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(SortTableWorkspace)
//----------------------------------------------------------------------------------------------
/// Algorithm's version for identification. @see Algorithm::version
int SortTableWorkspace::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string SortTableWorkspace::category() const { return "Utility\\Sorting"; }
/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
const std::string SortTableWorkspace::summary() const { return "Sort a TableWorkspace."; }
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void SortTableWorkspace::init() {
declareProperty(std::make_unique<WorkspaceProperty<API::ITableWorkspace>>("InputWorkspace", "", Direction::Input),
"An input workspace.");
declareProperty(std::make_unique<WorkspaceProperty<API::ITableWorkspace>>("OutputWorkspace", "", Direction::Output),
"An output workspace.");
declareProperty(std::make_unique<Kernel::ArrayProperty<std::string>>("Columns"), "Column names to sort by.");
declareProperty(std::make_unique<Kernel::ArrayProperty<int>>("Ascending"),
"List of bools for each column: true for ascending order, "
"false for descending. "
"If contains a single value it applies to all columns.");
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void SortTableWorkspace::exec() {
API::ITableWorkspace_sptr ws = getProperty("InputWorkspace");
std::vector<std::string> columns = getProperty("Columns");
std::vector<int> ascending = getProperty("Ascending");
if (columns.empty()) {
throw std::invalid_argument("No column names given.");
}
// by default sort all columns in ascending order
if (ascending.empty()) {
ascending.emplace_back(1);
}
// if "Ascending" contains a single value - it's common for all columns.
if (ascending.size() == 1) {
int commonValue = ascending.front();
ascending.resize(columns.size(), commonValue);
} else if (ascending.size() != columns.size()) {
throw std::invalid_argument("Number of sorting options is different form number of columns.");
}
std::vector<std::pair<std::string, bool>> criteria(columns.size());
auto col = columns.begin();
auto asc = ascending.begin();
for (auto crt = criteria.begin(); crt != criteria.end(); ++crt, ++col, ++asc) {
crt->first = *col;
crt->second = (*asc) != 0;
}
API::ITableWorkspace_sptr outputWS(ws->clone());
outputWS->sort(criteria);
setProperty("OutputWorkspace", outputWS);
}
} // namespace Mantid::DataHandling