Alexandria  2.16
Please provide a description of the project.
GridContainer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2020 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 
28 #include <memory>
29 #include <tuple>
30 #include <iterator>
31 #include <map>
32 #include <type_traits>
36 
37 namespace Euclid {
38 namespace GridContainer {
39 
96 template<typename GridCellManager, typename... AxesTypes>
98 
99  // The following aliases are used to simplify the definitions inside the class
101 
102 public:
103 
106 
108  typedef std::tuple<GridAxis<AxesTypes>...> AxesTuple;
109 
110  // The following is a shortcut to retrieve the type of each axis
111  template<int I>
112  using axis_type = typename std::tuple_element<I, std::tuple<AxesTypes...>>::type;
113 
114  // The iterator type of the GridContainer. See at the end of the file for its declaration
115  template<typename CellType>
116  class iter;
117 
118  typedef iter<cell_type> iterator;
119  typedef iter<cell_type const> const_iterator;
120 
129  explicit GridContainer(GridAxis<AxesTypes>... axes);
130 
139  explicit GridContainer(std::tuple<GridAxis<AxesTypes>...> axes_tuple);
140 
144 
145 
146  // Do not allow copying of GridContainer objects. This is done because these
147  // objects will most of the time be very big and copying them will be a
148  // bottleneck. To avoid unvoluntary copy constructor calls, this constructor
149  // is deleted.
152 
154  virtual ~GridContainer() = default;
155 
157  static constexpr size_t axisNumber();
158 
164  template<int I>
165  const GridAxis<axis_type<I>>& getAxis() const;
166 
168  const std::tuple<GridAxis<AxesTypes>...>& getAxesTuple() const;
169 
171  iterator begin();
172 
174  const_iterator begin() const;
175 
178 
180  iterator end();
181 
183  const_iterator end() const;
184 
187 
189  size_t size() const;
190 
201  const cell_type& operator()(decltype(std::declval<GridAxis<AxesTypes>>().size())... indices) const;
202 
204  cell_type& operator()(decltype(std::declval<GridAxis<AxesTypes>>().size())... indices);
205 
218  const cell_type& at(decltype(std::declval<GridAxis<AxesTypes>>().size())... indices) const;
219 
221  cell_type& at(decltype(std::declval<GridAxis<AxesTypes>>().size())... indices);
222 
241  template <int I>
242  GridContainer<GridCellManager, AxesTypes...> fixAxisByIndex(size_t index);
243 
276  template <int I>
277  const GridContainer<GridCellManager, AxesTypes...> fixAxisByIndex(size_t index) const;
278 
297  template <int I>
298  GridContainer<GridCellManager, AxesTypes...> fixAxisByValue(const axis_type<I>& value);
299 
332  template <int I>
333  const GridContainer<GridCellManager, AxesTypes...> fixAxisByValue(const axis_type<I>& value) const;
334 
335 private:
336 
338  std::tuple<GridAxis<AxesTypes>...> m_axes;
342  std::tuple<GridAxis<AxesTypes>...> m_axes_fixed {m_axes};
346  std::map<size_t, size_t> m_fixed_indices {};
348  std::shared_ptr<GridCellManager> m_cell_manager {
351  m_axes, TemplateLoopCounter<sizeof...(AxesTypes)-1>{}))
352  };
353 
364  GridContainer(const GridContainer<GridCellManager, AxesTypes...>& other, size_t axis, size_t index);
365 
369  template<int I>
370  const GridAxis<axis_type<I>>& getOriginalAxis() const;
371 
372 }; // end of class GridContainer
373 
374 
388 template<typename GridCellManager, typename... AxesTypes>
389 template<typename CellType>
390 class GridContainer<GridCellManager, AxesTypes...>::iter : public std::iterator<std::forward_iterator_tag, CellType> {
391 public:
392 
402  iter(const GridContainer<GridCellManager, AxesTypes...>& owner,
403  const cell_manager_iter_type& data_iter);
404 
406  iter(const iter<CellType>&) = default;
407 
409  iter(iter<CellType>&&) = default;
410 
412  iter& operator=(const iter& other);
413 
415  iter& operator++();
416 
418  CellType& operator*();
419 
421  typename std::add_const<CellType>::type& operator*() const;
422 
424  CellType* operator->();
425 
427  typename std::add_const<CellType>::type* operator->() const;
428 
431  bool operator==(const iter& other) const;
432 
435  bool operator!=(const iter& other) const;
436 
439  template<int I>
440  size_t axisIndex() const;
441 
444  template<int I>
445  const axis_type<I>& axisValue() const;
446 
460  template<int I>
461  iter& fixAxisByIndex(size_t index);
462 
480  template<int I>
481  iter& fixAxisByValue(const axis_type<I>& value);
482 
492  template<typename OtherIter>
493  iter& fixAllAxes(const OtherIter& other);
494 
495 private:
496 
497  const GridContainer<GridCellManager, AxesTypes...>& m_owner;
499  std::map<size_t, size_t> m_fixed_indices;
500  void forwardToIndex(size_t axis, size_t fixed_index);
501 
502 }; // end of class iter
503 
504 } // end of namespace GridContainer
505 } // end of namespace Euclid
506 
509 
510 #endif /* GRIDCONTAINER_GRIDCONTAINER_H */
511 
std::map< size_t, size_t > m_fixed_indices
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
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.
iter< cell_type const > const_iterator
std::tuple< GridAxis< AxesTypes >... > AxesTuple
The type of the tuple keeping the axes of the grid.
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.
const GridAxis< axis_type< I > > & getAxis() const
size_t size() const
Returns the total number of cells 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:149
Representation of a multi-dimensional grid which contains axis information.
Definition: GridContainer.h:97
GridContainer< GridCellManager, AxesTypes... > fixAxisByIndex(size_t index)
Returns a slice of the grid based on an axis index.
GridContainer & operator=(GridContainer< GridCellManager, AxesTypes... > &&)=default
Helper class for converting multi-dimensional grid coordinates to the index of a long data array and ...
GridCellManagerTraits< GridCellManager >::iterator cell_manager_iter_type
std::shared_ptr< GridCellManager > m_cell_manager
A pointer to the data of the grid.
const cell_type & at(decltype(std::declval< GridAxis< AxesTypes >>().size())... indices) const
Provides information related with an axis of a GridContainer.
Definition: GridAxis.h:49
GridContainer(GridAxis< AxesTypes >... axes)
Constructs a GridContainer with the given axes.
const cell_type & operator()(decltype(std::declval< GridAxis< AxesTypes >>().size())... indices) const
typename std::tuple_element< I, std::tuple< AxesTypes... > >::type axis_type
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
GridContainer construction helper class.
static std::unique_ptr< GridCellManager > factory(size_t size)
virtual ~GridContainer()=default
Default destructor.
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.
const GridContainer< GridCellManager, AxesTypes... > & m_owner
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.
const GridAxis< axis_type< I > > & getOriginalAxis() const
const std::tuple< GridAxis< AxesTypes >... > & getAxesTuple() const
Returns a tuple containing the information of all the grid axes.