00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef OST_QA_INDEX_HH
00020 #define OST_QA_INDEX_HH
00021
00022
00023
00024
00025
00026 #include <ost/stdint.hh>
00027
00028 #include <cstring>
00029 #include <boost/shared_ptr.hpp>
00030 #include <ost/qa/module_config.hh>
00031
00032 namespace ost { namespace qa {
00033
00034 namespace impl {
00035 template <uint32_t D>
00036 class IndexBase {
00037 public:
00038 enum { Dimension = D };
00039 IndexBase(const IndexBase& rhs) {
00040 memcpy(data_, rhs.data_, sizeof(uint32_t[D]));
00041 }
00042 IndexBase() {
00043 memset(data_, 0, sizeof(uint32_t[D]));
00044 }
00045 IndexBase& operator=(const IndexBase& rhs) {
00046 memcpy(data_, rhs.data_, sizeof(uint32_t[D]));
00047 return *this;
00048 }
00049 uint32_t operator[](uint32_t idx) const {
00050 assert(idx <= D);
00051 return data_[idx];
00052 }
00053 uint32_t& operator[](uint32_t idx) {
00054 assert(idx <= D);
00055 return data_[idx];
00056 }
00057 private:
00058 uint32_t data_[D];
00059 };
00060
00061 }
00062
00063 template <uint32_t D>
00064 class Index;
00065
00066 template <>
00067 class Index<1> : public impl::IndexBase<1> {
00068 public:
00069 Index() : impl::IndexBase<1>() {}
00070 Index(uint32_t a) {
00071 (*this)[0]=a;
00072 }
00073 };
00074 template <>
00075 class Index<2> : public impl::IndexBase<2> {
00076 public:
00077 Index() : impl::IndexBase<2>() {}
00078 Index(uint32_t a, uint32_t b) {
00079 (*this)[0]=a;
00080 (*this)[1]=b;
00081 }
00082 };
00083
00084 template <>
00085 class Index<3> : public impl::IndexBase<3> {
00086 public:
00087 Index() : impl::IndexBase<3>() {}
00088 Index(uint32_t a, uint32_t b, uint32_t c) {
00089 (*this)[0]=a;
00090 (*this)[1]=b;
00091 (*this)[2]=c;
00092 }
00093 };
00094
00095 template <>
00096 class Index<4> : public impl::IndexBase<4> {
00097 public:
00098 Index() : impl::IndexBase<4>() {}
00099 Index(uint32_t a, uint32_t b, uint32_t c, uint32_t d) {
00100 (*this)[0]=a;
00101 (*this)[1]=b;
00102 (*this)[2]=c;
00103 (*this)[3]=d;
00104 }
00105 };
00106
00107 template <>
00108 class Index<5> : public impl::IndexBase<5> {
00109 public:
00110 Index() : impl::IndexBase<5>() {}
00111 Index(uint32_t a, uint32_t b, uint32_t c, uint32_t d, uint32_t e) {
00112 (*this)[0]=a;
00113 (*this)[1]=b;
00114 (*this)[2]=c;
00115 (*this)[3]=d;
00116 (*this)[4]=e;
00117 }
00118 };
00119
00120 template <>
00121 class Index<6> : public impl::IndexBase<6> {
00122 public:
00123 Index() : impl::IndexBase<6>() {}
00124 Index(uint32_t a, uint32_t b, uint32_t c, uint32_t d, uint32_t e, uint32_t f) {
00125 (*this)[0]=a;
00126 (*this)[1]=b;
00127 (*this)[2]=c;
00128 (*this)[3]=d;
00129 (*this)[4]=e;
00130 (*this)[5]=f;
00131 }
00132 };
00133
00134 template <>
00135 class Index<7> : public impl::IndexBase<7> {
00136 public:
00137 Index() : impl::IndexBase<7>() {}
00138 Index(uint32_t a, uint32_t b, uint32_t c, uint32_t d, uint32_t e, uint32_t f, uint32_t g) {
00139 (*this)[0]=a;
00140 (*this)[1]=b;
00141 (*this)[2]=c;
00142 (*this)[3]=d;
00143 (*this)[4]=e;
00144 (*this)[5]=f;
00145 (*this)[6]=g;
00146 }
00147 };
00148
00149 template<uint32_t D>
00150 class IndexIterator {
00151 public:
00152 typedef Index<D> IndexType;
00153 IndexIterator(const IndexType& s, const IndexType& e)
00154 : start_(s), end_(e), current_(s) {
00155
00156 }
00157
00158 IndexIterator<D>& operator++() {
00159 uint32_t current_it=0;
00160 while (++current_[current_it] > end_[current_it]) {
00161 current_it++;
00162 if (current_it < D) {
00163 current_[current_it-1] = start_[current_it-1];
00164 } else {
00165 break;
00166 }
00167 }
00168 return *this;
00169 }
00170 const IndexType& operator *() const {
00171 return current_;
00172 }
00173 bool AtEnd() {
00174 return current_[D-1] > end_[D-1];
00175 }
00176 private:
00177 IndexType start_;
00178 IndexType end_;
00179 IndexType current_;
00180
00181 };
00182
00183 }}
00184
00185 #endif