forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Iterable.h
112 lines (91 loc) · 3.14 KB
/
Iterable.h
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2016 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 +
#pragma once
#include "MantidHistogramData/DllConfig.h"
namespace Mantid {
namespace HistogramData {
namespace detail {
/** Iterable
This class is an implementation detail of class like HistogramData::BinEdges
and HistogramData::Points. By inheriting from it, a type becomes iterable,
i.e., provides index and iterator based access.
*/
template <class T> class Iterable {
public:
/** Returns a const reference to the element at specified location pos. No
* bounds checking is performed.
*
* Note: There is no non-const version of this since it requires expensive cow
* access. */
const double &operator[](size_t pos) const {
return static_cast<const T *>(this)->data()[pos];
}
std::vector<double>::iterator begin() {
return static_cast<T *>(this)->mutableData().begin();
}
std::vector<double>::iterator end() {
return static_cast<T *>(this)->mutableData().end();
}
std::vector<double>::const_iterator begin() const {
return static_cast<const T *>(this)->data().begin();
}
std::vector<double>::const_iterator end() const {
return static_cast<const T *>(this)->data().end();
}
std::vector<double>::const_iterator cbegin() const {
return static_cast<const T *>(this)->data().cbegin();
}
std::vector<double>::const_iterator cend() const {
return static_cast<const T *>(this)->data().cend();
}
/** Returns a const reference to the first element.
*
* Note: There is no non-const version of this since it requires expensive cow
* access. */
const double &front() const {
return static_cast<const T *>(this)->data().front();
}
/** Returns a const reference to the last element.
*
* Note: There is no non-const version of this since it requires expensive cow
* access. */
const double &back() const {
return static_cast<const T *>(this)->data().back();
}
// expose typedefs for the iterator types in the underlying container
using iterator = std::vector<double>::iterator;
using const_iterator = std::vector<double>::const_iterator;
protected:
~Iterable() = default;
};
template <class T>
auto begin(Iterable<T> &container) -> decltype(container.begin()) {
return container.begin();
}
template <class T>
auto end(Iterable<T> &container) -> decltype(container.end()) {
return container.end();
}
template <class T>
auto begin(const Iterable<T> &container) -> decltype(container.begin()) {
return container.begin();
}
template <class T>
auto end(const Iterable<T> &container) -> decltype(container.end()) {
return container.end();
}
template <class T>
auto cbegin(const Iterable<T> &container) -> decltype(container.cbegin()) {
return container.cbegin();
}
template <class T>
auto cend(const Iterable<T> &container) -> decltype(container.cend()) {
return container.cend();
}
} // namespace detail
} // namespace HistogramData
} // namespace Mantid