c++_api/inc/opals/Array.hpp
1 #pragma once
2 
3 #include <opals/fwd.hpp>
4 #include <algorithm>
5 #include <array>
6 #include <initializer_list>
7 
8 namespace opals
9 {
10  /// A template class holding a C - array of constant size.
11  /**
12  \tparam T the element type
13  \tparam N the constant size, must be >= 1
14 
15  \author wk
16  \date 03.02.2011
17 
18  Array may be initialized with a static initializer list:
19  opals::Array<double,3> arr = { 1., 2., 3. };
20  Array fulfills almost all the requirements of an STL reversible container.
21  The complete implementation is exposed and hence, opals users may instantiate Array<T,N> with arbitrary template arguments.
22  The class inherits most of its functionality from std::array.
23  \internal
24  opals::Array extends std::array.
25  As extensions to std::array, assignment of a C-array is supported:
26  double da[] = { 1., 2. };
27  arr = da;
28  In contrast to opals::Vector and opals::List,
29  its data member is a plain-old-C-array
30  and hence, the whole implementation can be exposed to DLL users, together with
31  global template comparison operators.
32  As further extensions to the interface of std::array,
33  opals::Array provides operator<<, operator>>,
34  and function toInternal(),
35  which returns a std::array<U>, where U is the internal type
36  corresponding to T (see InternalType).
37  In contrast to std::array, Array may not be instantiated for N==0.
38 
39  Unfortunately, we cannot simply use the following template alias instead of a type on its own:
40  template<class T, std::size_t N>
41  using Array = std::array<T, N>;
42  The reason is that boost::lexical_cast would not work any longer.
43  */
44  template<class T, std::size_t N>
45  class Array : public std::array<T, N>
46  {
47  public:
48  Array() = default;
49  Array(std::initializer_list<T> init)
50  {
51  *this = init;
52  }
53 
54  /// \name assignment with type conversion
55  ///@{
56  Array& operator=(const Array&) = default;
57 
58  template <typename T2>
59  Array& operator=(const Array<T2, N>& that)
60  {
61  std::copy(that.begin(), that.end(), this->begin());
62  return *this;
63  }
64 
65  template <typename T2>
66  Array& operator=(const T2(&that)[N])
67  {
68  std::copy(that, &that[N], this->begin());
69  return *this;
70  }
71 
72  template <typename T2>
73  Array& operator=(const std::array<T2, N>& that)
74  {
75  std::copy(that.begin(), that.end(), this->begin());
76  return *this;
77  }
78 
79  template <typename T2>
80  Array& operator=(std::initializer_list<T2> that)
81  {
82  std::copy(that.begin(), that.begin() + std::min(this->size(), that.size()), this->begin());
83  return *this;
84  }
85  };
86 
87  /// \relatesalso Array
88  template<class T, std::size_t N>
89  bool operator== (const Array<T, N>& l, const Array<T, N>& r) {
90  return static_cast<const std::array<T, N>&>(l) == static_cast<const std::array<T, N>&>(r);
91  }
92 
93  /// \relatesalso Array
94  template<class T, std::size_t N>
95  bool operator< (const Array<T, N>& l, const Array<T, N>& r) {
96  return static_cast<const std::array<T, N>&>(l) < static_cast<const std::array<T, N>&>(r);
97  }
98 
99  /// \relatesalso Array
100  template<class T, std::size_t N>
101  bool operator!= (const Array<T, N>& l, const Array<T, N>& r) {
102  return static_cast<const std::array<T, N>&>(l) != static_cast<const std::array<T, N>&>(r);
103  }
104 
105  /// \relatesalso Array
106  template<class T, std::size_t N>
107  bool operator> (const Array<T, N>& l, const Array<T, N>& r) {
108  return static_cast<const std::array<T, N>&>(l) > static_cast<const std::array<T, N>&>(r);
109  }
110 
111  /// \relatesalso Array
112  template<class T, std::size_t N>
113  bool operator<= (const Array<T, N>& l, const Array<T, N>& r) {
114  return static_cast<const std::array<T, N>&>(l) <= static_cast<const std::array<T, N>&>(r);
115  }
116 
117  /// \relatesalso Array
118  template<class T, std::size_t N>
119  bool operator>= (const Array<T, N>& l, const Array<T, N>& r) {
120  return static_cast<const std::array<T, N>&>(l) >= static_cast<const std::array<T, N>&>(r);
121  }
122 
123  /// \relatesalso Array
124  template<class T, std::size_t N>
125  void swap(Array<T, N>& l, Array<T, N>& r) {
126  swap( static_cast<std::array<T, N>&>(l), static_cast<std::array<T, N>&>(r) );
127  }
128 }
129 
A template class holding a C - array of constant size.
Definition: c++_api/inc/opals/Array.hpp:45
Contains the public interface of OPALS.
Definition: AbsValueOrQuantile.hpp:8