RMF
DataSetIndexD.h
Go to the documentation of this file.
1 /**
2  * \file RMF/HDF5/DataSetIndexD.h
3  * \brief Handle read/write of Model data from/to files.
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef RMF_HDF5_DATA_SET_INDEX_D_H
10 #define RMF_HDF5_DATA_SET_INDEX_D_H
11 
12 #include "RMF/config.h"
13 #include "infrastructure_macros.h"
14 #include <H5public.h>
15 #include "types.h"
16 #include <boost/functional/hash.hpp>
17 #include <stdexcept>
18 
19 RMF_ENABLE_WARNINGS namespace RMF {
20  namespace HDF5 {
21  /** Store an index into a data set. Typedefs are provides
22  for 1,2 and 3 dimension indexes, name like
23  DataSetIndex2D.
24  */
25  template <int D>
26  class DataSetIndexD {
27  hsize_t d_[D];
28  int compare(const DataSetIndexD<D>& o) const {
29  for (unsigned int i = 0; i < D; ++i) {
30  if (d_[i] < o.d_[i])
31  return -1;
32  else if (d_[i] > o.d_[i])
33  return 1;
34  }
35  return 0;
36  }
37 
38  public:
39  DataSetIndexD(const Ints& o) {
40  RMF_USAGE_CHECK(o.size() == D, "Wrong number of values");
41  std::copy(o.begin(), o.end(), d_);
42  }
43  DataSetIndexD() { std::fill(d_, d_ + D, -1); }
44  DataSetIndexD(unsigned int i) {
45  RMF_USAGE_CHECK(D == 1, "Constructor does not match dimension.");
46  d_[0] = i;
47  }
48  DataSetIndexD(unsigned int i, unsigned int j) {
49  RMF_USAGE_CHECK(D == 2, "Constructor does not match dimension.");
50  d_[0] = i;
51  if (D > 1) d_[1] = j;
52  }
53  DataSetIndexD(unsigned int i, unsigned int j, unsigned int k) {
54  RMF_USAGE_CHECK(D == 3, "Constructor does not match dimension.");
55  d_[0] = i;
56  // for clang
57  if (D > 1) d_[1] = j;
58  if (D > 2) d_[2] = k;
59  }
60 #ifndef SWIG
61  hsize_t& operator[](unsigned int i) {
62  RMF_INDEX_CHECK(i, D);
63  return d_[i];
64  }
65  hsize_t operator[](unsigned int i) const {
66  RMF_INDEX_CHECK(i, D);
67  return d_[i];
68  }
69  typedef const hsize_t* const_iterator;
70  const_iterator begin() const { return d_; }
71  const_iterator end() const { return d_ + D; }
72  typedef hsize_t* iterator;
73  iterator begin() { return d_; }
74  iterator end() { return d_ + D; }
75  hsize_t* get() const { return const_cast<hsize_t*>(d_); }
76 #endif
77  int __getitem__(unsigned int i) const {
78  if (i >= D) {
79  // special for swig/python
80  throw std::runtime_error("out of range");
81  }
82  return operator[](i);
83  }
84 
85  unsigned int get_dimension() const { return D; }
86  RMF_SHOWABLE(DataSetIndexD, Ints(d_, d_ + D));
88  RMF_HASHABLE(DataSetIndexD, size_t ret = 0;
89  for (unsigned int i = 0; i < D; ++i) {
90  boost::hash_combine(ret, static_cast<size_t>(d_[i]));
91  } return ret;);
92  };
93 
94  //! Produce hash values for boost hash tables.
95  template <int D>
96  inline std::size_t hash_value(const DataSetIndexD<D>& t) {
97  return t.__hash__();
98  }
99 
100 #ifndef RMF_DOXYGEN
101  typedef DataSetIndexD<1> DataSetIndex1D;
102  typedef std::vector<DataSetIndex1D> DataSetIndex1Ds;
103  typedef DataSetIndexD<2> DataSetIndex2D;
104  typedef std::vector<DataSetIndex2D> DataSetIndex2Ds;
105  typedef DataSetIndexD<3> DataSetIndex3D;
106  typedef std::vector<DataSetIndex3D> DataSetIndex3Ds;
107 #endif
108 
109  } /* namespace HDF5 */
110 } /* namespace RMF */
111 
112 RMF_DISABLE_WARNINGS
113 
114 #endif /* RMF_HDF5_DATA_SET_INDEX_D_H */
Various general useful macros for IMP.
std::size_t hash_value(const DataSetIndexD< D > &t)
Produce hash values for boost hash tables.
Definition: DataSetIndexD.h:96
#define RMF_COMPARISONS(Name)
Implement comparison in a class using field as the variable to compare.
std::vector< Int > Ints
Definition: HDF5/types.h:30
#define RMF_HASHABLE(name, hashret)
Implement a hash function for the class.
Handle read/write of Model data from/to files.