LibraryLink Utilities  3.0.1
Modern C++ wrapper over LibraryLink and WSTP
IterableContainer.hpp
Go to the documentation of this file.
1 
6 #ifndef LLU_CONTAINERS_ITERATORS_ITERABLECONTAINER_HPP
7 #define LLU_CONTAINERS_ITERATORS_ITERABLECONTAINER_HPP
8 
9 #include <iterator>
10 #include <vector>
11 
12 #include "LLU/LibraryData.h"
13 
14 namespace LLU {
19  template<typename T>
21  public:
23  using value_type = T;
24 
26  using iterator = value_type*;
27 
29  using const_iterator = const value_type*;
30 
32  using reverse_iterator = std::reverse_iterator<iterator>;
33 
35  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
36 
39 
41  using const_reference = const value_type&;
42 
43  public:
44  // Provide special member functions
45  virtual ~IterableContainer() = default;
46 
50  value_type* data() noexcept {
51  return getData();
52  }
53 
57  const value_type* data() const noexcept {
58  return getData();
59  }
60 
64  mint size() const noexcept {
65  return getSize();
66  }
67 
71  iterator begin() noexcept {
72  return getData();
73  }
74 
78  const_iterator begin() const noexcept {
79  return getData();
80  }
81 
85  const_iterator cbegin() const noexcept {
86  return getData();
87  }
88 
92  iterator end() noexcept {
93  return std::next(begin(), getSize());
94  }
95 
99  const_iterator end() const noexcept {
100  return std::next(begin(), getSize());
101  }
102 
106  const_iterator cend() const noexcept {
107  return std::next(cbegin(), getSize());
108  }
109 
114  return std::make_reverse_iterator(end());
115  }
116 
120  const_reverse_iterator rbegin() const noexcept {
121  return std::make_reverse_iterator(end());
122  }
123 
127  const_reverse_iterator crbegin() const noexcept {
128  return std::make_reverse_iterator(cend());
129  }
130 
134  reverse_iterator rend() noexcept {
135  return std::make_reverse_iterator(begin());
136  }
137 
141  const_reverse_iterator rend() const noexcept {
142  return std::make_reverse_iterator(begin());
143  }
144 
148  const_reverse_iterator crend() const noexcept {
149  return std::make_reverse_iterator(cbegin());
150  }
151 
156  reference operator[](mint index) {
157  return *(begin() + index);
158  }
159 
164  const_reference operator[](mint index) const {
165  return *(cbegin() + index);
166  }
167 
173  return *begin();
174  }
175 
181  return *cbegin();
182  }
183 
189  return *(end() - 1);
190  }
191 
197  return *(cend() - 1);
198  }
199 
204  [[nodiscard]] std::vector<value_type> asVector() const {
205  return std::vector<value_type> {cbegin(), cend()};
206  }
207 
208  private:
212  virtual T* getData() const noexcept = 0;
213 
217  virtual mint getSize() const noexcept = 0;
218  };
219 
220 } // namespace LLU
221 
222 #endif // LLU_CONTAINERS_ITERATORS_ITERABLECONTAINER_HPP
LLU::IterableContainer
Abstract class that provides iterators (c/r/begin and c/r/end methods) and subscript operator for any...
Definition: IterableContainer.hpp:20
LLU::IterableContainer::operator[]
const_reference operator[](mint index) const
Get a constant reference to the data element at given position.
Definition: IterableContainer.hpp:164
LLU::IterableContainer::data
value_type * data() noexcept
Get raw pointer to underlying data.
Definition: IterableContainer.hpp:50
LLU
Main namespace of LibraryLink Utilities.
Definition: Queue.h:13
LLU::IterableContainer::front
const_reference front() const
Get constant reference to the first element.
Definition: IterableContainer.hpp:180
LLU::IterableContainer< double >::iterator
value_type * iterator
Iterator type.
Definition: IterableContainer.hpp:26
LLU::IterableContainer::size
mint size() const noexcept
Get total number of elements in the container.
Definition: IterableContainer.hpp:64
LibraryData.h
LLU::IterableContainer::rend
reverse_iterator rend() noexcept
Get iterator after the end of underlying data.
Definition: IterableContainer.hpp:134
LLU::IterableContainer< double >::const_iterator
const value_type * const_iterator
Constant iterator type.
Definition: IterableContainer.hpp:29
LLU::IterableContainer::data
const value_type * data() const noexcept
Get raw pointer to const underlying data.
Definition: IterableContainer.hpp:57
LLU::IterableContainer< double >::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator type.
Definition: IterableContainer.hpp:32
LLU::IterableContainer< double >::value_type
double value_type
Type of elements stored.
Definition: IterableContainer.hpp:23
LLU::IterableContainer::asVector
std::vector< value_type > asVector() const
Copy contents of the data to a std::vector of matching type.
Definition: IterableContainer.hpp:204
LLU::IterableContainer::rend
const_reverse_iterator rend() const noexcept
Get constant iterator after the end of underlying data.
Definition: IterableContainer.hpp:141
LLU::IterableContainer::rbegin
reverse_iterator rbegin() noexcept
Get iterator at the beginning of underlying data.
Definition: IterableContainer.hpp:113
LLU::IterableContainer::crbegin
const_reverse_iterator crbegin() const noexcept
Get constant iterator at the beginning of underlying data.
Definition: IterableContainer.hpp:127
LLU::IterableContainer::operator[]
reference operator[](mint index)
Get a reference to the data element at given position.
Definition: IterableContainer.hpp:156
LLU::IterableContainer::rbegin
const_reverse_iterator rbegin() const noexcept
Get constant iterator at the beginning of underlying data.
Definition: IterableContainer.hpp:120
LLU::IterableContainer::back
reference back()
Get reference to the last element.
Definition: IterableContainer.hpp:188
LLU::IterableContainer< double >::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Constant reverse iterator type.
Definition: IterableContainer.hpp:35
LLU::IterableContainer::cbegin
const_iterator cbegin() const noexcept
Get constant iterator at the beginning of underlying data.
Definition: IterableContainer.hpp:85
LLU::IterableContainer::front
reference front()
Get reference to the first element.
Definition: IterableContainer.hpp:172
LLU::IterableContainer::begin
const_iterator begin() const noexcept
Get constant iterator at the beginning of underlying data.
Definition: IterableContainer.hpp:78
LLU::IterableContainer::end
const_iterator end() const noexcept
Get constant iterator after the end of underlying data.
Definition: IterableContainer.hpp:99
LLU::IterableContainer::back
const_reference back() const
Get constant reference to the last element.
Definition: IterableContainer.hpp:196
LLU::IterableContainer::cend
const_iterator cend() const noexcept
Get constant iterator after the end of underlying data.
Definition: IterableContainer.hpp:106
LLU::IterableContainer::begin
iterator begin() noexcept
Get iterator at the beginning of underlying data.
Definition: IterableContainer.hpp:71
LLU::IterableContainer::crend
const_reverse_iterator crend() const noexcept
Get constant iterator after the end of underlying data.
Definition: IterableContainer.hpp:148
LLU::IterableContainer::end
iterator end() noexcept
Get iterator after the end of underlying data.
Definition: IterableContainer.hpp:92
LLU::IterableContainer< double >::reference
value_type & reference
Reference type.
Definition: IterableContainer.hpp:38
LLU::IterableContainer< double >::const_reference
const value_type & const_reference
Constant reference type.
Definition: IterableContainer.hpp:41