Newer
Older
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
#include "MantidKernel/ArrayOrderedPairsValidator.h"
#include <boost/make_shared.hpp>
#include <sstream>
namespace Mantid {
namespace Kernel {
/**
* Create a clone of the current ArrayBoundedValidator.
* @return The cloned object.
*/
template <typename TYPE>
IValidator_sptr ArrayOrderedPairsValidator<TYPE>::clone() const {
return boost::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
} // Kernel
} // Mantid