GridContainerIndices.h
Go to the documentation of this file.
1 /**
2  * @file GridContainerIndices.h
3  * @brief Classes to manage 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  * * GridContainer2DIndices: index manager for object in a 2D space
10  * * GridContainer3DIndices: index manager for object in a 3D space
11  *
12  * These classes have methods whose names reflect the idea of a physical space
13  * ("x", "y", "z"). The functionality is provided by TensorIndices class.
14  *
15  * This is a pure header that contains only template classes.
16  */
17 
18 #ifndef LARDATA_UTILITIES_GRIDCONTAINERINDICES_H
19 #define LARDATA_UTILITIES_GRIDCONTAINERINDICES_H
20 
21 
22 // LArSoft libraries
24 
25 // C/C++ standard libraries
26 #include <cstddef> // std::ptrdiff_t
27 #include <array>
28 
29 
30 namespace util {
31 
32  namespace details {
33 
34  /// Index manager for a container of data arranged on a DIMS-dimension grid
35  template <unsigned int DIMS>
38  public:
39 
40  /// Returns the number of dimensions in this object
41  static constexpr unsigned int dims() { return DIMS; }
42 
43  /// type of index for direct access to the cell
45 
46  /// type of difference between indices
47  using CellIndexOffset_t = std::ptrdiff_t;
48 
49  /// type of difference between indices along a dimension
51 
52  /// type of cell coordinate (x, y, z)
53  using CellID_t = std::array<CellDimIndex_t, dims()>;
54 
55  /// Constructor: specifies the size of the container and allocates it
56  GridContainerIndicesBase(std::array<size_t, dims()> const& new_dims)
57  : indices(new_dims.begin())
58  {}
59 
60  /// @{
61  /// @name Grid structure
62 
63  /// Returns whether the specified index is valid
65  { return indices.hasLinIndex(index); }
66 
67  /// Returns the number of cells in the grid
68  size_t size() const { return indices.size(); }
69 
70  /// @}
71 
72  /// @{
73  /// @name Indexing
74 
75  /// Returns the index of the element from its cell coordinates (no check!)
76  CellIndex_t operator[] (CellID_t id) const { return index(id); }
77 
78  /// Returns the difference in index of cellID respect to origin
80  (CellID_t const& origin, CellID_t const& cellID) const
81  { return index(cellID) - index(origin); }
82 
83  /// @}
84 
85  protected:
86  IndexManager_t indices; ///< the actual worker
87 
88  /// Returns the index of the element from its cell coordinates (no check!)
90  { return indices(id.begin()); }
91 
92  }; // GridContainerIndicesBase
93 
94  } // namespace details
95 
96 
97 
98  /// Index manager for a container of data arranged on a >=1-dim grid
99  template <unsigned int DIMS = 1U>
102  {
103  static_assert(DIMS >= 1U,
104  "Dimensions for GridContainerIndicesBase1D must be at least 1");
106 
107  public:
108 
109  using Base_t::GridContainerIndicesBase;
110 
111  /// @{
112  /// @name Grid structure
113 
114  /// Returns whether the specified x index is valid
115  bool hasX(typename Base_t::CellDimIndex_t index) const
116  { return Base_t::indices.template hasIndex<0>(index); }
117 
118  /// Returns the number of cells on the x axis of the grid
119  size_t sizeX() const { return Base_t::indices.template dim<0>(); }
120 
121  /// @}
122 
123  }; // GridContainerIndicesBase1D
124 
125 
126  /// Index manager for a container of data arranged on a >=2-dim grid
127  template <unsigned int DIMS = 2U>
129  static_assert(DIMS >= 2U,
130  "Dimensions for GridContainerIndicesBase2D must be at least 2");
131 
133 
134  public:
135 
136  using Base_t::GridContainerIndicesBase1D;
137 
138  /// @{
139  /// @name Grid structure
140 
141  /// Returns whether the specified y index is valid
142  bool hasY(typename Base_t::CellDimIndex_t index) const
143  { return Base_t::indices.template hasIndex<1>(index); }
144 
145  /// Returns the number of cells on the y axis of the grid
146  size_t sizeY() const { return Base_t::indices.template dim<1>(); }
147 
148  /// @}
149 
150  }; // GridContainerIndicesBase2D
151 
152 
153  /// Index manager for a container of data arranged on a >=3-dim grid
154  template <unsigned int DIMS = 3U>
156  static_assert(DIMS >= 3U,
157  "Dimensions for GridContainerIndicesBase3D must be at least 3");
159 
160  public:
161 
162  using Base_t::GridContainerIndicesBase2D;
163 
164  /// @{
165  /// @name Grid structure
166 
167  /// Returns whether the specified z index is valid
168  bool hasZ(typename Base_t::CellDimIndex_t index) const
169  { return Base_t::indices.template hasIndex<2>(index); }
170 
171  /// Returns the number of cells on the z axis of the grid
172  size_t sizeZ() const { return Base_t::indices.template dim<2>(); }
173 
174  /// @}
175 
176  }; // GridContainerIndicesBase3D
177 
178 
179 
180  /// Index manager for a container of data arranged on a 2D grid
182 
183  /// Index manager for a container of data arranged on a 3D grid
185 
186 
187 } // namespace util
188 
189 
190 
191 #endif // LARDATA_UTILITIES_GRIDCONTAINERINDICES_H
192 
static constexpr unsigned int dims()
Returns the number of dimensions in this object.
std::ptrdiff_t CellIndexOffset_t
type of difference between indices
size_t size() const
Returns the number of cells in the grid.
Base_t::LinIndex_t LinIndex_t
Type of the linear index.
Namespace for general, non-LArSoft-specific utilities.
CellIndexOffset_t CellDimIndex_t
type of difference between indices along a dimension
Index manager for a container of data arranged on a >=2-dim grid.
CellIndex_t index(CellID_t id) const
Returns the index of the element from its cell coordinates (no check!)
IndexManager_t indices
the actual worker
typename IndexManager_t::LinIndex_t CellIndex_t
type of index for direct access to the cell
TensorIndices class to flatten multi-dimension indices into linear.
std::array< CellDimIndex_t, dims()> CellID_t
type of cell coordinate (x, y, z)
size_t sizeZ() const
Returns the number of cells on the z axis of the grid.
Index manager for a container of data arranged on a DIMS-dimension 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.
bool hasZ(typename Base_t::CellDimIndex_t index) const
Returns whether the specified z index is valid.
bool hasX(typename Base_t::CellDimIndex_t index) const
Returns whether the specified x index is valid.
Index manager for a container of data arranged on a >=3-dim grid.
CellIndexOffset_t offset(CellID_t const &origin, CellID_t const &cellID) const
Returns the difference in index of cellID respect to origin.
size_t sizeY() const
Returns the number of cells on the y axis of the grid.
bool hasLinIndex(LinIndex_t linIndex) const
Returns whether the specified linear index is valid in this tensor.
GridContainerIndicesBase(std::array< size_t, dims()> const &new_dims)
Constructor: specifies the size of the container and allocates it.
Index manager for a container of data arranged on a >=1-dim grid.
auto array(Array const &a)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:228
bool has(CellIndexOffset_t index) const
Returns whether the specified index is valid.
CellIndex_t operator[](CellID_t id) const
Returns the index of the element from its cell coordinates (no check!)
DimSize_t size() const
Returns the size of the minor tensor.
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
bool hasY(typename Base_t::CellDimIndex_t index) const
Returns whether the specified y index is valid.
constexpr Point origin()
Returns a origin position with a point of the specified type.
Definition: geo_vectors.h:227
size_t sizeX() const
Returns the number of cells on the x axis of the grid.