Alexandria  2.25.0
SDC-CH common library for the Euclid project
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GridContainer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-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 
25 #ifndef GRIDCONTAINER_GRIDCONTAINER_H
26 #define GRIDCONTAINER_GRIDCONTAINER_H
27 
31 #include <iterator>
32 #include <map>
33 #include <memory>
34 #include <tuple>
35 #include <type_traits>
36 
37 namespace Euclid {
38 namespace GridContainer {
39 
96 template <typename GridCellManager, typename... AxesTypes>
98 public:
101 
102 private:
103  // The following aliases are used to simplify the definitions inside the class
105 
106  template <typename GCM>
107  static cell_type* ptr_test(...);
108 
109  template<typename GCM>
110  static typename GCM::pointer_type ptr_test(typename GCM::pointer_type*);
111 
112  template<typename GCM>
113  static cell_type& ref_test(...);
114 
115  template<typename GCM>
116  static typename GCM::reference_type ref_test(typename GCM::reference_type*);
117 
118 public:
122  typedef decltype(ref_test<GridCellManagerTraits<GridCellManager>>(nullptr)) reference_type;
123 
125  typedef std::tuple<GridAxis<AxesTypes>...> AxesTuple;
126 
127  // The following is a shortcut to retrieve the type of each axis
128  template <int I>
129  using axis_type = typename std::tuple_element<I, std::tuple<AxesTypes...>>::type;
130 
131  // The iterator type of the GridContainer. See at the end of the file for its declaration
132  template <typename CellType, typename PointerType, typename ReferenceType>
133  class iter;
134 
135  typedef iter<cell_type, pointer_type, reference_type> iterator;
136  typedef iter<cell_type const, pointer_type const, reference_type const> const_iterator;
137 
146  explicit GridContainer(GridAxis<AxesTypes>... axes);
147 
156  explicit GridContainer(std::tuple<GridAxis<AxesTypes>...> axes_tuple);
157 
161  template <typename... Args>
162  GridContainer(std::tuple<GridAxis<AxesTypes>...> axes_tuple, Args&&... args);
163 
165  GridContainer(GridContainer<GridCellManager, AxesTypes...>&&) = default;
166  GridContainer& operator=(GridContainer<GridCellManager, AxesTypes...>&&) = default;
167 
168  // Do not allow copying of GridContainer objects. This is done because these
169  // objects will most of the time be very big and copying them will be a
170  // bottleneck. To avoid unvoluntary copy constructor calls, this constructor
171  // is deleted.
172  GridContainer(const GridContainer<GridCellManager, AxesTypes...>&) = delete;
173  GridContainer& operator=(const GridContainer<GridCellManager, AxesTypes...>&) = delete;
174 
176  GridContainer copy() const;
177 
179  virtual ~GridContainer() = default;
180 
182  static constexpr size_t axisNumber();
183 
189  template <int I>
190  const GridAxis<axis_type<I>>& getAxis() const;
191 
193  const std::tuple<GridAxis<AxesTypes>...>& getAxesTuple() const;
194 
196  iterator begin();
197 
199  const_iterator begin() const;
200 
202  const_iterator cbegin();
203 
205  iterator end();
206 
208  const_iterator end() const;
209 
211  const_iterator cend();
212 
214  size_t size() const;
215 
226  const reference_type operator()(decltype(std::declval<GridAxis<AxesTypes>>().size())... indices) const;
227 
229  reference_type operator()(decltype(std::declval<GridAxis<AxesTypes>>().size())... indices);
230 
243  const reference_type at(decltype(std::declval<GridAxis<AxesTypes>>().size())... indices) const;
244 
246  reference_type at(decltype(std::declval<GridAxis<AxesTypes>>().size())... indices);
247 
253  std::tuple<decltype(std::declval<GridAxis<AxesTypes>>().size())...> infimum(const AxesTypes... coordinates) const;
254 
256  std::tuple<decltype(std::declval<GridAxis<AxesTypes>>().size())...>
257  infimum(const std::tuple<AxesTypes...>& coordinates) const;
258 
277  template <int I>
278  GridContainer<GridCellManager, AxesTypes...> fixAxisByIndex(size_t index);
279 
312  template <int I>
313  const GridContainer<GridCellManager, AxesTypes...> fixAxisByIndex(size_t index) const;
314 
333  template <int I>
334  GridContainer<GridCellManager, AxesTypes...> fixAxisByValue(const axis_type<I>& value);
335 
368  template <int I>
369  const GridContainer<GridCellManager, AxesTypes...> fixAxisByValue(const axis_type<I>& value) const;
370 
371  const GridCellManager& getCellManager() const {
372  return *m_cell_manager;
373  }
374 
375 private:
388 
392  GridContainer();
393 
404  GridContainer(const GridContainer<GridCellManager, AxesTypes...>& other, size_t axis, size_t index);
405 
409  template <int I>
410  const GridAxis<axis_type<I>>& getOriginalAxis() const;
411 
412 }; // end of class GridContainer
413 
427 template <typename GridCellManager, typename... AxesTypes>
428 template <typename CellType, typename PointerType, typename ReferenceType>
429 class GridContainer<GridCellManager, AxesTypes...>::iter
430  : public std::iterator<std::forward_iterator_tag, CellType, std::ptrdiff_t, PointerType, ReferenceType> {
431 public:
441  iter(const GridContainer<GridCellManager, AxesTypes...>& owner, const cell_manager_iter_type& data_iter);
442 
444  iter(const iter&) = default;
445 
447  iter(iter&&) = default;
448 
450  iter& operator=(const iter& other);
451 
453  iter& operator++();
454 
456  ReferenceType operator*();
457 
459  typename std::add_const<ReferenceType>::type operator*() const;
460 
462  PointerType operator->();
463 
465  typename std::add_const<PointerType>::type operator->() const;
466 
469  bool operator==(const iter& other) const;
470 
473  bool operator!=(const iter& other) const;
474 
477  template <int I>
478  size_t axisIndex() const;
479 
482  template <int I>
483  const axis_type<I>& axisValue() const;
484 
498  template <int I>
499  iter& fixAxisByIndex(size_t index);
500 
518  template <int I>
519  iter& fixAxisByValue(const axis_type<I>& value);
520 
530  template <typename OtherIter>
531  iter& fixAllAxes(const OtherIter& other);
532 
533 private:
534  const GridContainer<GridCellManager, AxesTypes...>& m_owner;
537  void forwardToIndex(size_t axis, size_t fixed_index);
538 
539 }; // end of class iter
540 
541 } // end of namespace GridContainer
542 } // end of namespace Euclid
543 
546 
547 #endif /* GRIDCONTAINER_GRIDCONTAINER_H */
size_t size() const
Returns the total number of cells of the grid.
GridContainer copy() const
But if needed be, allow explicit copies and let GridCellManager optimize them away if possible...
std::tuple< GridAxis< AxesTypes >...> m_axes
A tuple containing the axes of the grid.
std::map< size_t, size_t > m_fixed_indices
A map containing the axes which have been fixed, if this grid is a slice.
Class used by the GridContainer to access the different CellManagers.
GridContainer & operator=(GridContainer< GridCellManager, AxesTypes...> &&)=default
const reference_type at(decltype(std::declval< GridAxis< AxesTypes >>().size())...indices) const
const GridAxis< axis_type< I > > & getAxis() const
const_iterator cbegin()
Returns a constant iterator to the first cell of the grid.
const_iterator cend()
Returns a constant iterator to the cell after the last of the grid.
bool operator!=(const Euclid::SourceCatalog::Source::id_type &a, const Euclid::SourceCatalog::Source::id_type &b)
boost::variant specifies an equality operator (==), but, in older boost versions, not an inequality o...
Definition: Source.h:145
Representation of a multi-dimensional grid which contains axis information.
Definition: GridContainer.h:97
const GridCellManager & getCellManager() const
std::tuple< decltype(std::declval< GridAxis< AxesTypes >>().size())...> infimum(const AxesTypes...coordinates) const
Returns the grid indexes to the greatest knot less or equal to the given coordinates.
const GridAxis< axis_type< I > > & getOriginalAxis() const
Helper class for converting multi-dimensional grid coordinates to the index of a long data array and ...
iter< cell_type const, pointer_type const, reference_type const > const_iterator
static cell_type & ref_test(...)
GridCellManagerTraits< GridCellManager >::iterator cell_manager_iter_type
std::shared_ptr< GridCellManager > m_cell_manager
A pointer to the data of the grid.
decltype(ref_test< GridCellManagerTraits< GridCellManager >>(nullptr)) typedef reference_type
Reference type.
Provides information related with an axis of a GridContainer.
Definition: GridAxis.h:49
GridContainer< GridCellManager, AxesTypes...> fixAxisByIndex(size_t index)
Returns a slice of the grid based on an axis index.
const GridContainer< GridCellManager, AxesTypes...> & m_owner
GridContainer< GridCellManager, AxesTypes...> fixAxisByValue(const axis_type< I > &value)
Returns a slice of the grid based on an axis value.
std::tuple< GridAxis< AxesTypes >...> m_axes_fixed
a tuple containing the original axes of the full grid, if this grid is a slice
GridIndexHelper< AxesTypes...> m_index_helper_fixed
a helper class for calculations of the original axes indices
std::map< size_t, size_t > m_fixed_indices
typename std::tuple_element< I, std::tuple< AxesTypes...>>::type axis_type
static cell_type * ptr_test(...)
GridIndexHelper< AxesTypes...> m_index_helper
A helper class used for calculations of the axes indices.
static constexpr size_t axisNumber()
Returns the number of axes of the grid (dimensionality)
GridCellManagerTraits< GridCellManager >::data_type cell_type
The type of the values stored in the grid cells.
iterator end()
Returns an iterator to the cell after the last of the grid.
iterator begin()
Returns an iterator to the first cell of the grid.
GridCellManager::data_type data_type
The type of the data kept by the GridCellManager.
decltype(ptr_test< GridCellManagerTraits< GridCellManager >>(nullptr)) typedef pointer_type
Pointer type.
const std::tuple< GridAxis< AxesTypes >...> & getAxesTuple() const
Returns a tuple containing the information of all the grid axes.