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-2011 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 #ifndef OST_QA_INDEX_HH
20 #define OST_QA_INDEX_HH
21 
22 /*
23  Author: Marco Biasini
24  */
25 
26 #include <ost/stdint.hh>
27 
28 #include <cstring>
29 #include <boost/shared_ptr.hpp>
30 #include <ost/qa/module_config.hh>
31 
32 namespace ost { namespace qa {
33 
34 namespace impl {
35 template <uint32_t D>
36 class IndexBase {
37 public:
38  enum { Dimension = D };
39  IndexBase(const IndexBase& rhs) {
40  memcpy(data_, rhs.data_, sizeof(uint32_t[D]));
41  }
43  memset(data_, 0, sizeof(uint32_t[D]));
44  }
45  IndexBase& operator=(const IndexBase& rhs) {
46  memcpy(data_, rhs.data_, sizeof(uint32_t[D]));
47  return *this;
48  }
50  assert(idx <= D);
51  return data_[idx];
52  }
54  assert(idx <= D);
55  return data_[idx];
56  }
57 private:
58  uint32_t data_[D];
59 };
60 
61 } // namespace impl
62 
63 template <uint32_t D>
64 class Index;
65 
66 template <>
67 class Index<1> : public impl::IndexBase<1> {
68 public:
69  Index() : impl::IndexBase<1>() {}
71  (*this)[0]=a;
72  }
73 };
74 template <>
75 class Index<2> : public impl::IndexBase<2> {
76 public:
77  Index() : impl::IndexBase<2>() {}
79  (*this)[0]=a;
80  (*this)[1]=b;
81  }
82 };
83 
84 template <>
85 class Index<3> : public impl::IndexBase<3> {
86 public:
87  Index() : impl::IndexBase<3>() {}
89  (*this)[0]=a;
90  (*this)[1]=b;
91  (*this)[2]=c;
92  }
93 };
94 
95 template <>
96 class Index<4> : public impl::IndexBase<4> {
97 public:
98  Index() : impl::IndexBase<4>() {}
100  (*this)[0]=a;
101  (*this)[1]=b;
102  (*this)[2]=c;
103  (*this)[3]=d;
104  }
105 };
106 
107 template <>
108 class Index<5> : public impl::IndexBase<5> {
109 public:
110  Index() : impl::IndexBase<5>() {}
112  (*this)[0]=a;
113  (*this)[1]=b;
114  (*this)[2]=c;
115  (*this)[3]=d;
116  (*this)[4]=e;
117  }
118 };
119 
120 template <>
121 class Index<6> : public impl::IndexBase<6> {
122 public:
123  Index() : impl::IndexBase<6>() {}
125  (*this)[0]=a;
126  (*this)[1]=b;
127  (*this)[2]=c;
128  (*this)[3]=d;
129  (*this)[4]=e;
130  (*this)[5]=f;
131  }
132 };
133 
134 template <>
135 class Index<7> : public impl::IndexBase<7> {
136 public:
137  Index() : impl::IndexBase<7>() {}
139  (*this)[0]=a;
140  (*this)[1]=b;
141  (*this)[2]=c;
142  (*this)[3]=d;
143  (*this)[4]=e;
144  (*this)[5]=f;
145  (*this)[6]=g;
146  }
147 };
148 
149 template<uint32_t D>
151 public:
152  typedef Index<D> IndexType;
153  IndexIterator(const IndexType& s, const IndexType& e)
154  : start_(s), end_(e), current_(s) {
155 
156  }
157 
159  uint32_t current_it=0;
160  while (++current_[current_it] > end_[current_it]) {
161  current_it++;
162  if (current_it < D) {
163  current_[current_it-1] = start_[current_it-1];
164  } else {
165  break;
166  }
167  }
168  return *this;
169  }
170  const IndexType& operator *() const {
171  return current_;
172  }
173  bool AtEnd() {
174  return current_[D-1] > end_[D-1];
175  }
176 private:
177  IndexType start_;
178  IndexType end_;
179  IndexType current_;
180 
181 };
182 
183 }}
184 
185 #endif