forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayOrderedPairsValidator.cpp
57 lines (51 loc) · 1.85 KB
/
ArrayOrderedPairsValidator.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
// 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 "MantidKernel/ArrayOrderedPairsValidator.h"
#include <cstdint>
#include <memory>
#include <sstream>
namespace Mantid {
namespace Kernel {
/**
* Create a clone of the current ArrayOrderedPairsValidator.
* @return The cloned object.
*/
template <typename TYPE> IValidator_sptr ArrayOrderedPairsValidator<TYPE>::clone() const {
return std::make_shared<ArrayOrderedPairsValidator<TYPE>>(*this);
}
/**
* Function that actually does the work of checking the validity of the
* array elements.
* @param value :: The array to be checked.
* @return An error message giving the values of wrong entries.
*/
template <typename TYPE>
std::string ArrayOrderedPairsValidator<TYPE>::checkValidity(const std::vector<TYPE> &value) const {
std::stringstream error;
error << "";
// Check the number of entries is even
if (value.size() % 2 != 0) {
error << "Array has an odd number of entries (" << std::to_string(value.size()) << ").";
} else {
// Check that each pair is ordered.
for (auto it = value.begin(); it != value.end(); it += 2) {
if (*it > *(it + 1)) {
error << "Pair (" << *it << ", " << *(it + 1) << ") is not ordered.\n";
}
}
}
return error.str();
}
// Required explicit instantiations
template class ArrayOrderedPairsValidator<double>;
template class ArrayOrderedPairsValidator<int32_t>;
template class ArrayOrderedPairsValidator<int64_t>;
#if defined(_WIN32) || defined(__clang__) && defined(__APPLE__)
template class ArrayOrderedPairsValidator<long>;
#endif
} // namespace Kernel
} // namespace Mantid