GridContainers.h
Go to the documentation of this file.
1 /**
2  * @file GridContainers.h
3  * @brief Containers with indices in 1, 2 and 3 dimensions
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date June 29, 2016
6  *
7  * This header provides:
8  *
9  * * GridContainer2D: container on data in 2D space
10  * * GridContainer3D: container of data in 3D space
11  * * GridContainerBase: base class for containers in a N-dimension space
12  *
13  * This is a pure header that contains only template classes.
14  */
15 
16 #ifndef LARDATA_UTILITIES_GRIDCONTAINERS_H
17 #define LARDATA_UTILITIES_GRIDCONTAINERS_H
18 
19 // LArSoft libraries
21 
22 // C/C++ standard libraries
23 #include <vector>
24 #include <array>
25 
26 
27 namespace util {
28 
29  namespace details {
30 
31  /**
32  * @brief Base class for a container of data arranged on a grid
33  * @tparam DATUM type of datum to be contained
34  * @tparam IXMAN type of the grid index manager
35  *
36  * This is base class for `GridContainer#D` classes.
37  * It provides the full functionality, to which the other classes add
38  * some dimension-specific interface.
39  *
40  */
41  template <typename DATUM, typename IXMAN>
43 
44  public:
45  using Datum_t = DATUM; ///< type of contained datum
46  using Indexer_t = IXMAN; /// type of index manager
47 
49 
50 
51  static constexpr unsigned int dims() { return IXMAN::dims(); }
52 
53  /// type of index for direct access to the cell
54  using CellIndex_t = typename Indexer_t::CellIndex_t;
55 
56  /// type of difference between indices
57  using CellIndexOffset_t = typename Indexer_t::CellIndexOffset_t;
58 
59  /// type of difference between indices
60  using CellDimIndex_t = typename Indexer_t::CellDimIndex_t;
61 
62  /// type of cell coordinate (x, y, z)
63  using CellID_t = typename Indexer_t::CellID_t;
64 
65  /// type of a single cell container
66  using Cell_t = std::vector<Datum_t>;
67 
68  /// type of container holding all cells
69  using Cells_t = std::vector<Cell_t>;
70 
71  /// type of iterator to all cells
73 
74  /// Constructor: specifies the size of the container and allocates it
76  : indices(dims)
77  , data(indices.size())
78  {}
79 
80  /// @{
81  /// @name Data structure
82 
83  /// Returns the total size of the container
84  size_t size() const { return indices.size(); }
85 
86  /// Returns whether the specified index is valid
87  bool has(CellIndexOffset_t index) const { return indices.has(index); }
88 
89  /// @}
90 
91  /// @{
92  /// @name Data access
93 
94  /// Return the index of the element from its cell coordinates (no check!)
95  CellIndex_t index(CellID_t const& id) const { return indices[id]; }
96 
97  /// Returns the difference in index from two cells
99  (CellID_t const& origin, CellID_t const& cellID) const
100  { return indices.offset(origin, cellID); }
101 
102  /// Returns a reference to the specified cell
103  Cell_t& operator[] (CellID_t const& id) { return cell(id); }
104 
105  /// Returns a constant reference to the specified cell
106  Cell_t const& operator[] (CellID_t const& id) const { return cell(id); }
107 
108  /// Returns a reference to to the cell with specified index
110 
111  /// Returns a constant reference to the cell with specified index
112  Cell_t const& operator[] (CellIndex_t index) const { return data[index]; }
113 
114  ///@}
115 
116  /// @{
117  /// @name Data insertion
118 
119  /// Copies an element into the specified cell
120  void insert(CellID_t const& cellID, Datum_t const& elem)
121  { cell(cellID).push_back(elem); }
122 
123  /// Moves an element into the specified cell
124  void insert(CellID_t const& cellID, Datum_t&& elem)
125  { cell(cellID).push_back(std::move(elem)); }
126 
127  /// Copies an element into the cell with the specified index
128  void insert(CellIndex_t index, Datum_t const& elem)
129  { data[index].push_back(elem); }
130 
131  /// Moves an element into the cell with the specified index
133  { data[index].push_back(std::move(elem)); }
134 
135  /// @}
136 
137  /// Returns the index manager of the grid
138  Indexer_t const& indexManager() const { return indices; }
139 
140 
141  protected:
142  Indexer_t indices; ///< manager of the indices of the container
143 
144  Cells_t data; ///< organised collection of points
145 
146  /// Returns a reference to the specified cell
147  Cell_t& cell(CellID_t const& cellID)
148  { return data[index(cellID)]; }
149 
150  /// Returns a constant reference to the specified cell
151  Cell_t const& cell(CellID_t const& cellID) const
152  { return data[index(cellID)]; }
153 
154  }; // GridContainerBase<>
155 
156  } // namespace details
157 
158 
159  /**
160  * @brief Base class for a container of data arranged on a 1D-grid
161  * @tparam DATUM type of datum to be contained
162  * @tparam IXMAN type of the grid index manager
163  *
164  *
165  */
166  template <typename DATUM, typename IXMAN>
167  class GridContainerBase1D: public details::GridContainerBase<DATUM, IXMAN> {
169  static_assert(Base_t::dims() >= 1,
170  "GridContainerBase1D must have dimensions 1 or larger.");
171 
172  public:
173 
174  using Base_t::GridContainerBase;
175 
176  /// @{
177  /// @name Data structure
178 
179  /// Returns whether the specified x index is valid
180  bool hasX(typename Base_t::CellDimIndex_t index) const
181  { return Base_t::indices.hasX(index); }
182 
183  /// Returns the size of the container in the first dimension (x)
184  size_t sizeX() const { return Base_t::indices.sizeX(); }
185 
186  /// @}
187 
188  protected:
189  }; // GridContainerBase1D<>
190 
191 
192  /**
193  * @brief Base class for a container of data arranged on a 2D-grid
194  * @tparam DATUM type of datum to be contained
195  * @tparam IXMAN type of the grid index manager
196  *
197  *
198  */
199  template <typename DATUM, typename IXMAN>
200  class GridContainerBase2D: public GridContainerBase1D<DATUM, IXMAN> {
202  static_assert(Base_t::dims() >= 2,
203  "GridContainerBase2D must have dimensions 2 or larger.");
204 
205  public:
206 
207  using Base_t::GridContainerBase1D;
208 
209  /// @{
210  /// @name Data structure
211 
212  /// Returns the size of the container in the second dimension (y)
213  size_t sizeY() const { return Base_t::indices.sizeY(); }
214 
215  /// Returns whether the specified x index is valid
216  bool hasY(typename Base_t::CellDimIndex_t index) const
217  { return Base_t::indices.hasY(index); }
218 
219  /// @}
220 
221  protected:
222  }; // GridContainerBase2D<>
223 
224 
225  /**
226  * @brief Base class for a container of data arranged on a 3D-grid
227  * @tparam DATUM type of datum to be contained
228  * @tparam IXMAN type of the grid index manager
229  *
230  *
231  */
232  template <typename DATUM, typename IXMAN>
233  class GridContainerBase3D: public GridContainerBase2D<DATUM, IXMAN> {
235  static_assert(Base_t::dims() >= 3,
236  "GridContainerBase3D must have dimensions 3 or larger.");
237 
238  public:
239 
240  using Base_t::GridContainerBase2D;
241 
242  /// @{
243  /// @name Data structure
244 
245  /// Returns the size of the container in the third dimension (z)
246  size_t sizeZ() const { return Base_t::indices.sizeZ(); }
247 
248  /// Returns whether the specified x index is valid
249  bool hasZ(typename Base_t::CellDimIndex_t index) const
250  { return Base_t::indices.hasZ(index); }
251 
252  /// @}
253 
254  protected:
255  }; // GridContainerBase3D<>
256 
257 
258  /**
259  * @brief Container allowing 2D indexing
260  * @tparam DATUM type of contained data
261  * @see GridContainer2DIndices
262  *
263  * This is an alias for GridContainerBase2D, with a proper index manager.
264  * See the documentation of GridContainerBase2D.
265  */
266  template <typename DATUM>
268 
269 
270  /**
271  * @brief Container allowing 3D indexing
272  * @tparam DATUM type of contained data
273  * @see GridContainer3DIndices
274  *
275  * This is an alias for GridContainerBase3D, with a proper index manager.
276  * See the documentation of GridContainerBase3D.
277  */
278  template <typename DATUM>
280 
281 } // namespace util
282 
283 
284 #endif // LARDATA_UTILITIES_GRIDCONTAINERS_H
CellIndexOffset_t indexOffset(CellID_t const &origin, CellID_t const &cellID) const
Returns the difference in index from two cells.
static constexpr unsigned int dims()
Namespace for general, non-LArSoft-specific utilities.
Cells_t data
organised collection of points
bool hasX(typename Base_t::CellDimIndex_t index) const
Returns whether the specified x index is valid.
bool hasZ(typename Base_t::CellDimIndex_t index) const
Returns whether the specified x index is valid.
size_t sizeX() const
Returns the size of the container in the first dimension (x)
Cell_t & cell(CellID_t const &cellID)
Returns a reference to the specified cell.
Indexer_t const & indexManager() const
Returns the index manager of the grid.
std::vector< Datum_t > Cell_t
type of a single cell container
typename Cells_t::const_iterator const_iterator
type of iterator to all cells
size_t sizeY() const
Returns the size of the container in the second dimension (y)
size_t sizeZ() const
Returns the size of the container in the third dimension (z)
intermediate_table::const_iterator const_iterator
GridContainerBase(std::array< size_t, dims()> const &dims)
Constructor: specifies the size of the container and allocates it.
void insert(CellID_t const &cellID, Datum_t const &elem)
Copies an element into the specified cell.
std::vector< Cell_t > Cells_t
type of container holding all cells
Cell_t & operator[](CellID_t const &id)
Returns a reference to the specified cell.
typename Indexer_t::CellID_t CellID_t
type of cell coordinate (x, y, z)
Indexer_t indices
manager of the indices of the container
def move(depos, offset)
Definition: depos.py:107
Base class for a container of data arranged on a grid.
typename Indexer_t::CellIndex_t CellIndex_t
type of index for direct access to the cell
Base class for a container of data arranged on a 2D-grid.
constexpr std::array< std::size_t, geo::vect::dimension< Vector >)> indices()
Returns a sequence of indices valid for a vector of the specified type.
typename Indexer_t::CellIndexOffset_t CellIndexOffset_t
type of difference between indices
Cell_t const & cell(CellID_t const &cellID) const
Returns a constant reference to the specified cell.
long long int CellID_t
Definition: CaloRawDigit.h:24
typename Indexer_t::CellDimIndex_t CellDimIndex_t
type of difference between indices
auto array(Array const &a)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:228
void insert(CellID_t const &cellID, Datum_t &&elem)
Moves an element into the specified cell.
bool has(CellIndexOffset_t index) const
Returns whether the specified index is valid.
void insert(CellIndex_t index, Datum_t &&elem)
Moves an element into the cell with the specified index.
CellIndex_t index(CellID_t const &id) const
Return the index of the element from its cell coordinates (no check!)
void insert(CellIndex_t index, Datum_t const &elem)
Copies an element into the cell with the specified index.
Base class for a container of data arranged on a 1D-grid.
bool hasY(typename Base_t::CellDimIndex_t index) const
Returns whether the specified x index is valid.
constexpr Point origin()
Returns a origin position with a point of the specified type.
Definition: geo_vectors.h:227
Base class for a container of data arranged on a 3D-grid.
Classes to manage containers with indices in 1, 2 and 3 dimensions.
size_t size() const
Returns the total size of the container.