LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
GridContainers.h
Go to the documentation of this file.
1 
16 #ifndef LARDATA_UTILITIES_GRIDCONTAINERS_H
17 #define LARDATA_UTILITIES_GRIDCONTAINERS_H
18 
19 // LArSoft libraries
21 
22 // C/C++ standard libraries
23 #include <array>
24 #include <vector>
25 
26 namespace util {
27 
28  namespace details {
29 
40  template <typename DATUM, typename IXMAN>
42 
43  public:
44  using Datum_t = DATUM;
45  using Indexer_t = IXMAN;
46 
48 
49  static constexpr unsigned int dims() { return IXMAN::dims(); }
50 
52  using CellIndex_t = typename Indexer_t::CellIndex_t;
53 
55  using CellIndexOffset_t = typename Indexer_t::CellIndexOffset_t;
56 
58  using CellDimIndex_t = typename Indexer_t::CellDimIndex_t;
59 
61  using CellID_t = typename Indexer_t::CellID_t;
62 
64  using Cell_t = std::vector<Datum_t>;
65 
67  using Cells_t = std::vector<Cell_t>;
68 
71 
75  {}
76 
79 
81  size_t size() const { return indices.size(); }
82 
84  bool has(CellIndexOffset_t index) const { return indices.has(index); }
85 
87 
90 
92  CellIndex_t index(CellID_t const& id) const { return indices[id]; }
93 
95  CellIndexOffset_t indexOffset(CellID_t const& origin, CellID_t const& cellID) const
96  {
97  return indices.offset(origin, cellID);
98  }
99 
101  Cell_t& operator[](CellID_t const& id) { return cell(id); }
102 
104  Cell_t const& operator[](CellID_t const& id) const { return cell(id); }
105 
108 
110  Cell_t const& operator[](CellIndex_t index) const { return data[index]; }
111 
113 
116 
118  void insert(CellID_t const& cellID, Datum_t const& elem) { cell(cellID).push_back(elem); }
119 
121  void insert(CellID_t const& cellID, Datum_t&& elem)
122  {
123  cell(cellID).push_back(std::move(elem));
124  }
125 
127  void insert(CellIndex_t index, Datum_t const& elem) { data[index].push_back(elem); }
128 
130  void insert(CellIndex_t index, Datum_t&& elem) { data[index].push_back(std::move(elem)); }
131 
133 
135  Indexer_t const& indexManager() const { return indices; }
136 
137  protected:
139 
141 
143  Cell_t& cell(CellID_t const& cellID) { return data[index(cellID)]; }
144 
146  Cell_t const& cell(CellID_t const& cellID) const { return data[index(cellID)]; }
147 
148  }; // GridContainerBase<>
149 
150  } // namespace details
151 
159  template <typename DATUM, typename IXMAN>
160  class GridContainerBase1D : public details::GridContainerBase<DATUM, IXMAN> {
162  static_assert(Base_t::dims() >= 1, "GridContainerBase1D must have dimensions 1 or larger.");
163 
164  public:
165  using Base_t::GridContainerBase;
166 
169 
171  bool hasX(typename Base_t::CellDimIndex_t index) const { return Base_t::indices.hasX(index); }
172 
174  size_t sizeX() const { return Base_t::indices.sizeX(); }
175 
177 
178  protected:
179  }; // GridContainerBase1D<>
180 
188  template <typename DATUM, typename IXMAN>
189  class GridContainerBase2D : public GridContainerBase1D<DATUM, IXMAN> {
191  static_assert(Base_t::dims() >= 2, "GridContainerBase2D must have dimensions 2 or larger.");
192 
193  public:
194  using Base_t::GridContainerBase1D;
195 
198 
200  size_t sizeY() const { return Base_t::indices.sizeY(); }
201 
203  bool hasY(typename Base_t::CellDimIndex_t index) const { return Base_t::indices.hasY(index); }
204 
206 
207  protected:
208  }; // GridContainerBase2D<>
209 
217  template <typename DATUM, typename IXMAN>
218  class GridContainerBase3D : public GridContainerBase2D<DATUM, IXMAN> {
220  static_assert(Base_t::dims() >= 3, "GridContainerBase3D must have dimensions 3 or larger.");
221 
222  public:
223  using Base_t::GridContainerBase2D;
224 
227 
229  size_t sizeZ() const { return Base_t::indices.sizeZ(); }
230 
232  bool hasZ(typename Base_t::CellDimIndex_t index) const { return Base_t::indices.hasZ(index); }
233 
235 
236  protected:
237  }; // GridContainerBase3D<>
238 
247  template <typename DATUM>
249 
258  template <typename DATUM>
260 
261 } // namespace util
262 
263 #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.
Definition: PIDAAlg.h:26
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)
auto array(Array const &a)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:250
DATUM Datum_t
type of contained datum
Indexer_t indices
manager of the indices of the container
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.
Cell_t & operator[](CellIndex_t index)
Returns a reference to to the cell with specified index.
typename Indexer_t::CellDimIndex_t CellDimIndex_t
type of difference between indices
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.
Cell_t const & operator[](CellIndex_t index) const
Returns a constant reference to the cell with 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.
Cell_t const & operator[](CellID_t const &id) const
Returns a constant reference to the specified cell.
constexpr Point origin()
Returns a origin position with a point of the specified type.
Definition: geo_vectors.h:229
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.