HyperspaceExplorer 0.7.1
|
00001 /* 00002 Hyperspace Explorer - vizualizing higher-dimensional geometry 00003 Copyright (C) 2009-2010 Lene Preuss <lene.preuss@gmail.com> 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License along 00016 with this program; if not, write to the Free Software Foundation, Inc., 00017 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00018 00019 */ 00020 00021 #ifndef MULTIDIMENSIONAL_VECTOR_H 00022 #define MULTIDIMENSIONAL_VECTOR_H 00023 00024 #include <vector> 00025 00026 namespace VecMath { 00027 00029 00048 template <typename T, unsigned D> 00049 class MultiDimensionalVector: public MultiDimensionalVector<T, D-1> { 00050 00051 public: 00052 00054 typedef typename std::vector<MultiDimensionalVector<T, D-1> >::iterator iterator; 00055 00057 typedef typename std::vector<MultiDimensionalVector<T, D-1> >::const_iterator const_iterator; 00058 00060 MultiDimensionalVector(); 00061 00063 MultiDimensionalVector(unsigned n); 00064 00066 MultiDimensionalVector(typename std::vector<MultiDimensionalVector<T, D-1> >::iterator first, 00067 typename std::vector<MultiDimensionalVector<T, D-1> >::iterator last); 00068 00070 MultiDimensionalVector(const MultiDimensionalVector<T, D> &v); 00071 00072 MultiDimensionalVector(MultiDimensionalVector<T, D> &&other) { 00073 _data = std::move(other._data); 00074 } 00075 00076 MultiDimensionalVector & operator=(const MultiDimensionalVector<T, D> &other) { 00077 if (this == &other) return *this; 00078 _data = other._data; 00079 return *this; 00080 } 00081 00082 MultiDimensionalVector & operator=(MultiDimensionalVector<T, D> &&other) { 00083 if (this == &other) return *this; 00084 _data = std::move(other._data); 00085 return *this; 00086 } 00087 00089 iterator begin(); 00090 00092 iterator end(); 00093 00095 const_iterator begin() const; 00096 00098 const_iterator end() const; 00099 00101 void push_back(const MultiDimensionalVector<T, D-1> &x); 00102 00104 MultiDimensionalVector<T, D-1> &back(); 00105 00107 const MultiDimensionalVector<T, D-1> &back() const; 00108 00110 iterator erase(iterator position); 00111 00113 iterator erase(iterator first, iterator last); 00114 00116 MultiDimensionalVector<T, D-1> &operator [] (unsigned i); 00117 00119 const MultiDimensionalVector<T, D-1> &operator [] (unsigned i) const; 00120 00122 unsigned size() const; 00123 00125 unsigned empty() const; 00126 00128 void clear(); 00129 00131 void resize(unsigned new_size); 00132 00134 void resize(unsigned new_size, MultiDimensionalVector<T, D-1> x); 00135 00137 void reserve(unsigned new_size); 00138 00140 bool operator == (const MultiDimensionalVector<T, D> &other) const; 00141 00143 bool operator != (const MultiDimensionalVector<T, D> &other) const; 00144 00146 template <typename Function> Function for_each(Function f); 00148 template <typename Function> Function for_each(Function f) const; 00149 00151 std::string toString() const; 00152 00154 void print() const; 00155 00156 protected: 00157 00159 std::vector<MultiDimensionalVector<T, D-1> > &data(); 00160 00162 const std::vector<MultiDimensionalVector<T, D-1> > &data() const; 00163 00164 private: 00166 00167 std::vector<MultiDimensionalVector<T, D-1> > _data; 00168 }; 00169 00171 template<typename T, unsigned D> 00172 std::ostream& operator<<(std::ostream& s, MultiDimensionalVector<T, D> const& v); 00173 00175 template <typename T, unsigned D> 00176 typename MultiDimensionalVector<T, D>::iterator find(MultiDimensionalVector<T, D-1> const &find_me, 00177 MultiDimensionalVector<T, D> &v); 00178 00180 template<typename T1, typename T2, unsigned D, typename Function> 00181 Function for_each(MultiDimensionalVector<T1, D> &v1, 00182 MultiDimensionalVector<T2, D> &v2, 00183 Function f); 00184 00186 template<typename T1, typename T2, unsigned D, typename Function> 00187 Function for_each(const MultiDimensionalVector<T1, D> &v1, 00188 const MultiDimensionalVector<T2, D> &v2, 00189 Function f); 00190 00192 template<typename T1, typename T2, typename T3, unsigned D, typename Function> 00193 Function for_each(MultiDimensionalVector<T1, D> &v1, 00194 MultiDimensionalVector<T2, D> &v2, 00195 MultiDimensionalVector<T3, D> &v3, 00196 Function f); 00197 00199 template<typename T1, typename T2, typename T3, unsigned D, typename Function> 00200 Function for_each(const MultiDimensionalVector<T1, D> &v1, 00201 const MultiDimensionalVector<T2, D> &v2, 00202 const MultiDimensionalVector<T3, D> &v3, 00203 Function f); 00204 00206 template<typename T> 00207 class MultiDimensionalVector<T, 1> { 00208 00209 public: 00210 00212 typedef typename std::vector<T>::iterator iterator; 00214 typedef typename std::vector<T>::const_iterator const_iterator; 00215 00217 MultiDimensionalVector(); 00219 MultiDimensionalVector(unsigned n); 00220 00221 MultiDimensionalVector(std::vector<T> data); 00222 00224 iterator begin(); 00226 iterator end(); 00228 const_iterator begin() const; 00230 const_iterator end() const; 00231 00233 void push_back(const T &x); 00234 00236 T &back(); 00237 00239 const T &back() const; 00240 00242 iterator erase(iterator position); 00243 00245 iterator erase(iterator first, iterator last); 00246 00248 T &operator [] (unsigned i); 00250 const T &operator [] (unsigned i) const; 00251 00253 unsigned size() const; 00255 unsigned empty() const; 00256 00258 void clear(); 00259 00261 void resize(unsigned new_size); 00262 00264 void resize(unsigned new_size, T x); 00265 00267 void reserve(unsigned new_size); 00268 00270 bool operator == (const MultiDimensionalVector<T, 1> &other) const; 00271 00273 bool operator != (const MultiDimensionalVector<T, 1> &other) const; 00274 00276 template <typename Function> Function for_each(Function f); 00278 template <typename Function> Function for_each(Function f) const; 00279 00281 std::string toString() const; 00282 00284 void print() const; 00285 00287 static bool print_num_elements(); 00289 static unsigned max_dimension(); 00290 00291 protected: 00293 std::vector<T> &data(); 00295 const std::vector<T> &data() const; 00296 00297 private: 00299 std::vector<T> _data; 00300 00302 static const bool PRINT_NUM_ELEMENTS = false; 00304 static const unsigned MAX_DIMENSION = 4; 00305 00306 }; 00307 00309 template<typename T> 00310 std::ostream& operator<<(std::ostream& s, MultiDimensionalVector<T, 1> const& v); 00311 00313 template<typename T1, typename T2, typename Function> 00314 Function for_each(MultiDimensionalVector<T1, 1> &v1, 00315 MultiDimensionalVector<T2, 1> &v2, 00316 Function f); 00317 00319 template<typename T1, typename T2, typename Function> 00320 Function for_each(const MultiDimensionalVector<T1, 1> &v1, 00321 const MultiDimensionalVector<T2, 1> &v2, 00322 Function f); 00323 00325 template<typename T1, typename T2, typename T3, typename Function> 00326 Function for_each(MultiDimensionalVector<T1, 1> &v1, 00327 MultiDimensionalVector<T2, 1> &v2, 00328 MultiDimensionalVector<T3, 1> &v3, 00329 Function f); 00330 00332 template<typename T1, typename T2, typename T3, typename Function> 00333 Function for_each(const MultiDimensionalVector<T1, 1> &v1, 00334 const MultiDimensionalVector<T2, 1> &v2, 00335 const MultiDimensionalVector<T3, 1> &v3, 00336 Function f); 00337 00338 } 00339 00340 00341 #endif