OpenStructure
index.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2020 by the OpenStructure authors
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 3.0 of the License, or (at your option)
9 // any later version.
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //------------------------------------------------------------------------------
19 
20 #ifndef OST_MM_INDEX_HH
21 #define OST_MM_INDEX_HH
22 
23 /*
24  Author: Marco Biasini
25  */
26 
27 #include <algorithm>
28 
29 #include <cstring>
30 
31 namespace ost { namespace mol{ namespace mm{
32 
33 namespace impl {
34 template <uint D>
35 class IndexBase {
36 public:
37  enum { Dimension = D };
38  IndexBase(const IndexBase& rhs) {
39  memcpy(data_, rhs.data_, sizeof(uint[D]));
40  }
42  memset(data_, 0, sizeof(uint[D]));
43  }
44 
45  IndexBase& operator=(const IndexBase& rhs) {
46  memcpy(data_, rhs.data_, sizeof(uint[D]));
47  return *this;
48  }
49  uint operator[](uint idx) const {
50  assert(idx < D);
51  return data_[idx];
52  }
54  assert(idx < D);
55  return data_[idx];
56  }
57  //allows index to be inserted into set
58  inline bool operator < (const IndexBase<D>& rhs) const{
59  return std::lexicographical_compare(data_, data_+D, rhs.data_, rhs.data_+D);
60  }
61 
62  inline bool operator==(const IndexBase<D>& rhs) const{
63  return std::equal(data_,data_+D,rhs.data_);
64  }
65 
66  inline bool operator!=(const IndexBase<D>& rhs) const{
67  return !(*this == rhs);
68  }
69 
70 private:
71  uint data_[D];
72 };
73 
74 } // namespace impl
75 
76 template <uint D>
77 class Index;
78 
79 template <>
80 class Index<1> : public impl::IndexBase<1> {
81 public:
82  Index() : impl::IndexBase<1>() {}
83  Index(uint a) {
84  (*this)[0]=a;
85  }
86  template <typename DS>
87  void Serialize(DS& ds){
88  ds & (*this)[0];
89  }
90 };
91 template <>
92 class Index<2> : public impl::IndexBase<2> {
93 public:
94  Index() : impl::IndexBase<2>() {}
95  Index(uint a, uint b) {
96  (*this)[0]=a;
97  (*this)[1]=b;
98  }
99  template <typename DS>
100  void Serialize(DS& ds){
101  ds & (*this)[0];
102  ds & (*this)[1];
103  }
104 };
105 template <>
106 class Index<3> : public impl::IndexBase<3> {
107 public:
108  Index() : impl::IndexBase<3>() {}
109  Index(uint a, uint b, uint c) {
110  (*this)[0]=a;
111  (*this)[1]=b;
112  (*this)[2]=c;
113  }
114  template <typename DS>
115  void Serialize(DS& ds){
116  ds & (*this)[0];
117  ds & (*this)[1];
118  ds & (*this)[2];
119  }
120 };
121 template <>
122 class Index<4> : public impl::IndexBase<4> {
123 public:
124  Index() : impl::IndexBase<4>() {}
125  Index(uint a, uint b, uint c, uint d) {
126  (*this)[0]=a;
127  (*this)[1]=b;
128  (*this)[2]=c;
129  (*this)[3]=d;
130  }
131  template <typename DS>
132  void Serialize(DS& ds){
133  ds & (*this)[0];
134  ds & (*this)[1];
135  ds & (*this)[2];
136  ds & (*this)[3];
137  }
138 };
139 template <>
140 class Index<5> : public impl::IndexBase<5> {
141 public:
142  Index() : impl::IndexBase<5>() {}
143  Index(uint a, uint b, uint c, uint d, uint e) {
144  (*this)[0]=a;
145  (*this)[1]=b;
146  (*this)[2]=c;
147  (*this)[3]=d;
148  (*this)[4]=e;
149  }
150  template <typename DS>
151  void Serialize(DS& ds){
152  ds & (*this)[0];
153  ds & (*this)[1];
154  ds & (*this)[2];
155  ds & (*this)[3];
156  ds & (*this)[4];
157  }
158 };
159 template <>
160 class Index<6> : public impl::IndexBase<6> {
161 public:
162  Index() : impl::IndexBase<6>() {}
163  Index(uint a, uint b, uint c, uint d, uint e, uint f) {
164  (*this)[0]=a;
165  (*this)[1]=b;
166  (*this)[2]=c;
167  (*this)[3]=d;
168  (*this)[4]=e;
169  (*this)[5]=f;
170  }
171  template <typename DS>
172  void Serialize(DS& ds){
173  ds & (*this)[0];
174  ds & (*this)[1];
175  ds & (*this)[2];
176  ds & (*this)[3];
177  ds & (*this)[4];
178  ds & (*this)[5];
179  }
180 };
181 template <>
182 class Index<7> : public impl::IndexBase<7> {
183 public:
184  Index() : impl::IndexBase<7>() {}
185  Index(uint a, uint b, uint c, uint d, uint e, uint f, uint g) {
186  (*this)[0]=a;
187  (*this)[1]=b;
188  (*this)[2]=c;
189  (*this)[3]=d;
190  (*this)[4]=e;
191  (*this)[5]=f;
192  (*this)[6]=g;
193  }
194  template <typename DS>
195  void Serialize(DS& ds){
196  ds & (*this)[0];
197  ds & (*this)[1];
198  ds & (*this)[2];
199  ds & (*this)[3];
200  ds & (*this)[4];
201  ds & (*this)[5];
202  ds & (*this)[6];
203  }
204 };
205 template<uint D>
207 public:
209  IndexIterator(const IndexType& s, const IndexType& e)
210  : start_(s), end_(e), current_(s) {
211 
212  }
213 
215  uint current_it=0;
216  while (++current_[current_it] > end_[current_it]) {
217  current_it++;
218  if (current_it < D) {
219  current_[current_it-1] = start_[current_it-1];
220  } else {
221  break;
222  }
223  }
224  return *this;
225  }
226  const IndexType& operator *() const {
227  return current_;
228  }
229  bool AtEnd() {
230  return current_[D-1] > end_[D-1];
231  }
232 private:
233  IndexType start_;
234  IndexType end_;
235  IndexType current_;
236 
237 };
238 
239 }}}
240 
241 #endif
void Serialize(DS &ds)
Definition: index.hh:87
Index(uint a, uint b)
Definition: index.hh:95
void Serialize(DS &ds)
Definition: index.hh:100
Index(uint a, uint b, uint c)
Definition: index.hh:109
void Serialize(DS &ds)
Definition: index.hh:115
Index(uint a, uint b, uint c, uint d)
Definition: index.hh:125
void Serialize(DS &ds)
Definition: index.hh:132
void Serialize(DS &ds)
Definition: index.hh:151
Index(uint a, uint b, uint c, uint d, uint e)
Definition: index.hh:143
Index(uint a, uint b, uint c, uint d, uint e, uint f)
Definition: index.hh:163
void Serialize(DS &ds)
Definition: index.hh:172
void Serialize(DS &ds)
Definition: index.hh:195
Index(uint a, uint b, uint c, uint d, uint e, uint f, uint g)
Definition: index.hh:185
IndexIterator< D > & operator++()
Definition: index.hh:214
const IndexType & operator*() const
Definition: index.hh:226
IndexIterator(const IndexType &s, const IndexType &e)
Definition: index.hh:209
uint & operator[](uint idx)
Definition: index.hh:53
IndexBase & operator=(const IndexBase &rhs)
Definition: index.hh:45
bool operator!=(const IndexBase< D > &rhs) const
Definition: index.hh:66
bool operator==(const IndexBase< D > &rhs) const
Definition: index.hh:62
bool operator<(const IndexBase< D > &rhs) const
Definition: index.hh:58
uint operator[](uint idx) const
Definition: index.hh:49
IndexBase(const IndexBase &rhs)
Definition: index.hh:38
unsigned int uint
Definition: base.hh:29
Definition: base.dox:1