ObjectArray2d.h
Go to the documentation of this file.
1 /** This class provides a 2D array-like structure which holds things
2  * of templated type. It's not fancy.
3  */
4 
5 #ifndef WIRECELL_OBJECTARRAY2D
6 #define WIRECELL_OBJECTARRAY2D
7 
8 #include <vector>
9 
10 namespace WireCell {
11 
12  // little helper to give something that looks like a 2D array of objects.
13  template <typename Thing>
14  class ObjectArray2d {
15  public:
16  typedef std::vector<Thing> store_t;
17  typedef typename store_t::iterator iterator;
19 
20  ObjectArray2d(size_t nrows=0, size_t ncols=0)
21  : m_nrows(nrows), m_ncols(ncols) {
22  if (nrows and ncols) {
23  m_things.resize(nrows*ncols);
24  }
25  }
26 
27  void resize(size_t nrows, size_t ncols) {
28  m_nrows = nrows;
29  m_ncols = ncols;
30  m_things.resize(nrows*ncols);
31  }
32 
33  void reset() {
34  m_things.clear();
35  m_things.resize(m_nrows*m_ncols);
36  }
37 
38  const Thing& operator()(size_t irow, size_t icol) const {
39  return m_things.at(icol + m_ncols*irow);
40  }
41  Thing& operator()(size_t irow, size_t icol) {
42  return m_things.at(icol + m_ncols*irow);
43  }
44 
45  // range based access to the underlying things
46  iterator begin() { return m_things.begin(); }
47  iterator end() { return m_things.end(); }
48  const_iterator begin() const { return m_things.begin(); }
49  const_iterator end() const { return m_things.end(); }
50  private:
51  // column major
52  store_t m_things;
53  size_t m_nrows, m_ncols;
54 
55  };
56 }
57 
58 #endif
store_t::const_iterator const_iterator
Definition: ObjectArray2d.h:18
intermediate_table::iterator iterator
const_iterator begin() const
Definition: ObjectArray2d.h:48
Thing & operator()(size_t irow, size_t icol)
Definition: ObjectArray2d.h:41
intermediate_table::const_iterator const_iterator
ObjectArray2d(size_t nrows=0, size_t ncols=0)
Definition: ObjectArray2d.h:20
store_t::iterator iterator
Definition: ObjectArray2d.h:17
std::vector< Thing > store_t
Definition: ObjectArray2d.h:16
Definition: Main.h:22
const_iterator end() const
Definition: ObjectArray2d.h:49
const Thing & operator()(size_t irow, size_t icol) const
Definition: ObjectArray2d.h:38
unsigned nrows(sqlite3 *db, std::string const &tablename)
Definition: helpers.cc:84
void resize(size_t nrows, size_t ncols)
Definition: ObjectArray2d.h:27