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  class 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  class 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(iterator beg, iterator end);
91 
92  ~Vector();
93  ///@}
94 
95  /// \name assignment, swapping
96  ///@{
97  Vector& operator=(const Vector& that);
98  void assign(size_type count, const T& value);
99  void assign(iterator beg, iterator end);
100  void assign(const_iterator beg, const_iterator end);
101  void assign(reverse_iterator beg, reverse_iterator end);
102  void assign(const_reverse_iterator beg, const_reverse_iterator end);
103  void swap(Vector& that);
104  ///@}
105 
106  /// \name iterator traversal
107  /// forward/reverse, const/non-const
108  ///@{
109  iterator begin() noexcept;
110  iterator end() noexcept;
111  const_iterator begin() const noexcept;
112  const_iterator end() const noexcept;
113  const_iterator cbegin() const noexcept;
114  const_iterator cend() const noexcept;
115 
116  reverse_iterator rbegin() noexcept;
117  reverse_iterator rend() noexcept;
118  const_reverse_iterator rbegin() const noexcept;
119  const_reverse_iterator rend() const noexcept;
120  const_reverse_iterator crbegin() const noexcept;
121  const_reverse_iterator crend() const noexcept;
122  ///@}
123 
124  /// \name element access
125  ///@{
126  reference at(size_type pos);
127  const_reference at(size_type pos) const;
128 
129  reference operator[](size_type n);
130  const_reference operator[](size_type n) const;
131 
132  reference front();
133  reference back();
134  const_reference front() const;
135  const_reference back() const;
136  ///@}
137 
138  /// \name size
139  ///@{
140  size_type size() const noexcept;
141  size_type max_size() const noexcept;
142  size_type capacity() const noexcept;
143  bool empty() const noexcept;
144  void shrink_to_fit();
145  void clear() noexcept;
146  void resize(size_type n, T value = T());
147  void reserve(size_type n);
148  ///@}
149 
150  /// \name insertion
151  ///@{
152  void push_back(const T& val);
153 
154  iterator insert(const_iterator pos, const T& value);
155  iterator insert(const_iterator pos, size_type n, const T& value);
156  iterator insert(const_iterator pos, iterator beg, iterator end);
160  ///@}
161 
162  /// \name removal
163  ///@{
164  void pop_back();
165  iterator erase(const_iterator pos);
166  iterator erase(const_iterator beg, const_iterator end);
167  ///@}
168 
169  /// A random access iterator class for Vector
170  /** \tparam Const constant-ness
171  \tparam Forward forward/reverse traversal
172 
173  \author wk
174  \date 02.02.2011
175  */
176  template<bool Const, bool Forward>
177  class Iterator
178  {
179  class Impl;
180  Impl *pimpl_;
181 
182  public:
183  template<class U>
184  using ConstSel = typename std::conditional<Const, U const, U>::type;
185  template<class ForX, class ForBool>
186  using ForXOrBool = typename std::conditional<!std::is_same<T, bool>::value, ForX, ForBool>::type;
187 
188  using iterator_category = std::random_access_iterator_tag;
189  using value_type = ConstSel<T>;
190  using difference_type = std::ptrdiff_t;
191  using reference = typename std::conditional<Const, typename Vector::const_reference, typename Vector::reference>::type;
192  using pointer = ForXOrBool<ConstSel<value_type>*, void>;
194 
195  /// \name construction, conversion, destruction
196  ///@{
197  Iterator();
198  Iterator(const Iterator& that);
199  operator ConstIterator() const;
200  ~Iterator();
201  ///@}
202 
203  /// \name assignment, swapping
204  ///@{
205  Iterator &operator= (const Iterator& that);
206  void swap(Iterator& that);
207  ///@}
208 
209  /// \name element access
210  ///@{
211  reference operator* () const;
212  pointer operator->() const;
213  reference operator[](difference_type offset) const;
214  ///@}
215 
216  /// \name movement
217  ///@{
218  Iterator& operator++();
219  Iterator operator++(int);
220  Iterator& operator--();
221  Iterator operator--(int);
222  Iterator& operator+=(difference_type offset);
223  Iterator operator+ (difference_type offset) const;
224  Iterator& operator-=(difference_type offset);
225  Iterator operator- (difference_type offset) const;
226  ///@}
227 
228  /// \name comparison
229  ///@{
230  difference_type operator-(const Iterator<true, Forward>& that) const;
231  difference_type operator-(const Iterator<false, Forward>& that) const;
232  bool operator==(const Iterator<true, Forward>& that) const;
233  bool 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  ///@}
245  };
246  };
247 }
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