Alexandria  2.27.0
SDC-CH common library for the Euclid project
GridCellManagerVectorOfVectors.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef GRIDCONTAINER_GRIDCELLMANAGERVECTOROFVECTORS_H
20 #define GRIDCONTAINER_GRIDCELLMANAGERVECTOROFVECTORS_H
21 
24 #include <cstddef>
25 #include <vector>
26 
27 namespace Euclid {
28 namespace GridContainer {
29 
30 template <typename T>
31 struct VectorValueProxy;
32 
37 template <typename T>
39 
43  struct StrideIterator {
44  StrideIterator(typename std::vector<T>::iterator start, int stride) : m_i(start), m_stride(stride) {}
45 
46  bool operator!=(const StrideIterator& other) const {
47  return m_i != other.m_i;
48  }
49 
50  bool operator>(const StrideIterator& other) const {
51  return m_i > other.m_i;
52  }
53 
55  m_i += m_stride;
56  return *this;
57  }
58 
60  m_i += diff;
61  return *this;
62  }
63 
64  ptrdiff_t operator-(const StrideIterator& other) const {
65  return (m_i - other.m_i) / m_stride;
66  }
67 
69  return {m_i, m_i + m_stride};
70  }
71 
73  return {m_i, m_i + m_stride};
74  }
75 
76  private:
78  int m_stride;
79  };
80 
88  GridCellManagerVectorOfVectors(size_t size, int nested_values)
89  : m_values(size * nested_values), m_cell_size(nested_values) {}
90 
95 
100 
105 
114  auto iter = m_values.begin() + i * m_cell_size;
115  return {iter, iter + m_cell_size};
116  }
117 
118  size_t getCellSize() const {
119  return m_cell_size;
120  }
121 
122  size_t getTotalSize() const {
123  return m_values.size();
124  }
125 
126 private:
129 
131 };
132 
133 template <typename T>
137 
139  return m_begin;
140  }
141 
143  return m_end;
144  }
145 
147  return m_begin;
148  }
149 
150  const_iterator end() const {
151  return m_end;
152  }
153 
155  return m_begin;
156  }
157 
159  return m_end;
160  }
161 
162  std::size_t size() const {
163  return m_end - m_begin;
164  }
165 
167  return const_cast<VectorValueProxy*>(this);
168  }
169 
171  std::copy(other.begin(), other.end(), begin());
172  return *this;
173  }
174 
175  bool operator==(const VectorValueProxy& other) const {
176  return size() == other.size() && std::equal(m_begin, m_end, other.m_begin);
177  }
178 
179  bool operator!=(const VectorValueProxy& other) const {
180  return size() != other.size() || !std::equal(m_begin, m_end, other.m_begin);
181  }
182 
183  operator std::vector<T>() const {
184  return {begin(), end()};
185  }
186 
187  T& operator[](const size_t i) {
188  return *(m_begin + i);
189  }
190 
191  const T& operator[](const size_t i) const {
192  return *(m_begin + i);
193  }
194 
195 private:
197  : m_begin(begin_), m_end(end_){};
198 
200 
201  friend struct GridCellManagerVectorOfVectors<T>;
202 };
203 
207 template <typename T>
213 
214  static std::unique_ptr<GridCellManagerVectorOfVectors<T>> factory(size_t size, size_t nested_values) {
215  return Euclid::make_unique<GridCellManagerVectorOfVectors<T>>(size, nested_values);
216  }
217 
219  return {c.m_values.begin(), c.m_cell_size};
220  }
221 
223  return {c.m_values.end(), c.m_cell_size};
224  }
225 };
226 } // namespace GridContainer
227 } // namespace Euclid
228 
229 #endif // GRIDCONTAINER_GRIDCELLMANAGERVECTOROFVECTORS_H
T copy(T... args)
T equal(T... args)
static std::unique_ptr< GridCellManagerVectorOfVectors< T > > factory(size_t size, size_t nested_values)
Class used by the GridContainer to access the different CellManagers.
static size_t size(const GridCellManager &cell_manager)
StrideIterator(typename std::vector< T >::iterator start, int stride)
GridCellManagerVectorOfVectors(GridCellManagerVectorOfVectors &&)=default
GridCellManagerVectorOfVectors(const GridCellManagerVectorOfVectors &)=delete
VectorValueProxy(typename std::vector< T >::iterator begin_, typename std::vector< T >::iterator end_)
typename std::vector< T >::const_iterator const_iterator
VectorValueProxy & operator=(const VectorValueProxy &other)
bool operator!=(const VectorValueProxy &other) const
bool operator==(const VectorValueProxy &other) const