Vector.hpp
1 #pragma once
2 
3 #include <opals/fwd.hpp>
4 
5 #include <cstddef>
6 #include <type_traits>
7 
8 namespace std {
9  struct random_access_iterator_tag;
10 }
11 
12 namespace opals
13 {
14  /// Mimics std::vector<bool>::reference
15  /** \author wk
16  \date 07.03.2020
17  */
19  {
20  struct Impl;
21  Impl* pimpl_;
22 
23  public:
25  ~BoolReference();
26  BoolReference& operator=(bool) noexcept;
27  BoolReference& operator=(const BoolReference&) noexcept;
28  operator bool() const noexcept;
29  void flip() noexcept;
30  };
31 
32  /// Mimics std::vector<T>
33  /** \tparam T the element type
34 
35  Symbols for Vector are exported to opals DLLs as required by their modules' interfaces,
36  meaning that DLL users are able to instantiate Vector<T> only with the corresponding template arguments.
37  The interface of opals::Vector differs slightly from std::vector:
38  no template constructors, no template functions are provided, but these are represented
39  as non-template overloads for Vector::iterator, Vector::const_iterator, Vector::reverse_iterator, and Vector::const_reverse_iterator.
40 
41  \author wk
42  \date 02.02.2011
43 
44  \internal
45  Conversion constructors and operators for std::vector<T> and its iterators are provided for internal use only.
46  If T is another interface class (e.g. opals::String),
47  then use function toInternal() to convert to std::vector<std::string>.
48  opals::Vector encapsulates STL's std::vector, whose implementation it hides from the
49  interface in order to avoid problems when using the interface / opals-DLLs
50  with different STL-implementations.
51  As opals data types are not required to be comparable,
52  no non-template member operators are provided in the interface.
53  \endinternal
54  */
55  template<class T>
56  class Vector
57  {
58  struct Impl;
59  Impl *pimpl_;
60 
61  public:
62  template<class ForX, class ForBool>
63  using ForXOrBool = typename std::conditional<!std::is_same<T, bool>::value, ForX, ForBool>::type;
64 
65  template<bool Const, bool Forward>
66  class OPALS_API Iterator;
67 
72 
73  using value_type = T;
74  using size_type = std::size_t;
75  using difference_type = std::ptrdiff_t;
76  using reference = ForXOrBool<T&, BoolReference>;
77  using const_reference = ForXOrBool<T const&, bool>;
78  using pointer = ForXOrBool<T*, iterator>;
79  using const_pointer = ForXOrBool<T const*, const_iterator>;
80 
81  /// \name construction / destruction
82  ///@{
83  Vector();
84  Vector(size_type n);
85  Vector(size_type n, const T& value);
86  Vector(const Vector& that);
87  Vector(Vector&& that);
88  Vector(iterator beg, iterator end);
92 
93  ~Vector();
94  ///@}
95 
96  /// \name assignment, swapping
97  ///@{
98  Vector& operator=(const Vector& that);
99  Vector& operator=(Vector&& that);
100  void assign(size_type count, const T& value);
101  void assign(iterator beg, iterator end);
102  void assign(const_iterator beg, const_iterator end);
103  void assign(reverse_iterator beg, reverse_iterator end);
104  void assign(const_reverse_iterator beg, const_reverse_iterator end);
105  void swap(Vector& that);
106  ///@}
107 
108  /// \name iterator traversal
109  /// forward/reverse, const/non-const
110  ///@{
111  iterator begin() noexcept;
112  iterator end() noexcept;
113  const_iterator begin() const noexcept;
114  const_iterator end() const noexcept;
115  const_iterator cbegin() const noexcept;
116  const_iterator cend() const noexcept;
117 
118  reverse_iterator rbegin() noexcept;
119  reverse_iterator rend() noexcept;
120  const_reverse_iterator rbegin() const noexcept;
121  const_reverse_iterator rend() const noexcept;
122  const_reverse_iterator crbegin() const noexcept;
123  const_reverse_iterator crend() const noexcept;
124  ///@}
125 
126  /// \name element access
127  ///@{
128  reference at(size_type pos);
129  const_reference at(size_type pos) const;
130 
131  reference operator[](size_type n);
132  const_reference operator[](size_type n) const;
133 
134  reference front();
135  reference back();
136  const_reference front() const;
137  const_reference back() const;
138  ///@}
139 
140  /// \name size
141  ///@{
142  size_type size() const noexcept;
143  size_type max_size() const noexcept;
144  size_type capacity() const noexcept;
145  bool empty() const noexcept;
146  void shrink_to_fit();
147  void clear() noexcept;
148  void resize(size_type n, T value = T());
149  void reserve(size_type n);
150  ///@}
151 
152  /// \name insertion
153  ///@{
154  void push_back(const T& val);
155 
156  iterator insert(const_iterator pos, const T& value);
157  iterator insert(const_iterator pos, size_type n, const T& value);
158  iterator insert(const_iterator pos, iterator beg, iterator end);
162  ///@}
163 
164  /// \name removal
165  ///@{
166  void pop_back();
167  iterator erase(const_iterator pos);
168  iterator erase(const_iterator beg, const_iterator end);
169  ///@}
170 
171  /// A random access iterator class for Vector
172  /** \tparam Const constant-ness
173  \tparam Forward forward/reverse traversal
174 
175  \author wk
176  \date 02.02.2011
177  */
178  template<bool Const, bool Forward>
179  class Iterator
180  {
181  class Impl;
182  Impl *pimpl_;
183 
184  public:
185  template<class U>
186  using ConstSel = typename std::conditional<Const, U const, U>::type;
187  template<class ForX, class ForBool>
188  using ForXOrBool = typename std::conditional<!std::is_same<T, bool>::value, ForX, ForBool>::type;
189 
190  using iterator_category = std::random_access_iterator_tag;
191  using value_type = ConstSel<T>;
192  using difference_type = std::ptrdiff_t;
193  using reference = typename std::conditional<Const, typename Vector::const_reference, typename Vector::reference>::type;
194  using pointer = ForXOrBool<ConstSel<value_type>*, void>;
196 
197  /// \name construction, conversion, destruction
198  ///@{
199  Iterator();
200  Iterator(const Iterator& that);
201  operator ConstIterator() const;
202  ~Iterator();
203  ///@}
204 
205  /// \name assignment, swapping
206  ///@{
207  Iterator &operator= (const Iterator& that);
208  void swap(Iterator& that);
209  ///@}
210 
211  /// \name element access
212  ///@{
213  reference operator* () const;
214  pointer operator->() const;
215  reference operator[](difference_type offset) const;
216  ///@}
217 
218  /// \name movement
219  ///@{
220  Iterator& operator++();
221  Iterator operator++(int);
222  Iterator& operator--();
223  Iterator operator--(int);
224  Iterator& operator+=(difference_type offset);
225  Iterator operator+ (difference_type offset) const;
226  Iterator& operator-=(difference_type offset);
227  Iterator operator- (difference_type offset) const;
228  ///@}
229 
230  /// \name comparison
231  ///@{
232  difference_type operator-(const Iterator<true, Forward>& that) const;
233  difference_type operator-(const Iterator<false, Forward>& that) const;
234  bool operator==(const Iterator<true, Forward>& that) const;
235  bool operator==(const Iterator<false, Forward>& that) const;
236  bool operator!=(const Iterator<true, Forward>& that) const;
237  bool operator!=(const Iterator<false, Forward>& that) const;
238  bool operator< (const Iterator<true, Forward>& that) const;
239  bool operator< (const Iterator<false, Forward>& that) const;
240  bool operator> (const Iterator<true, Forward>& that) const;
241  bool operator> (const Iterator<false, Forward>& that) const;
242  bool operator<=(const Iterator<true, Forward>& that) const;
243  bool operator<=(const Iterator<false, Forward>& that) const;
244  bool operator>=(const Iterator<true, Forward>& that) const;
245  bool operator>=(const Iterator<false, Forward>& that) const;
246  ///@}
247  };
248  };
249 }
Mimics std::vector<bool>::reference.
Definition: Vector.hpp:18
Contains the public interface of OPALS.
Definition: AbsValueOrQuantile.hpp:8
A random access iterator class for Vector.
Definition: Vector.hpp:66
Mimics std::vector<T>
Definition: fwd.hpp:18