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 #include "MultiDimensionalVector.h" 00022 00023 #include <algorithm> 00024 #include <sstream> 00025 #include <iostream> 00026 #include <iterator> 00027 00028 namespace VecMath { 00029 00030 template <typename T, unsigned D> 00031 MultiDimensionalVector<T, D>::MultiDimensionalVector(): _data() { } 00032 00033 template <typename T, unsigned D> 00034 MultiDimensionalVector<T, D>::MultiDimensionalVector(unsigned n): _data(n) { } 00035 00036 template <typename T, unsigned D> 00037 MultiDimensionalVector<T, D>::MultiDimensionalVector( 00038 typename std::vector<MultiDimensionalVector<T, D-1> >::iterator first, 00039 typename std::vector<MultiDimensionalVector<T, D-1> >::iterator last): 00040 MultiDimensionalVector<T, D-1>(), 00041 _data(first, last) { } 00042 00043 template <typename T, unsigned D> 00044 MultiDimensionalVector<T, D>::MultiDimensionalVector(const MultiDimensionalVector<T, D> &v): 00045 MultiDimensionalVector<T, D-1>(), 00046 _data(v.data()) { } 00047 00048 template <typename T, unsigned D> 00049 typename MultiDimensionalVector<T, D>::iterator 00050 MultiDimensionalVector<T, D>::begin() { 00051 return data().begin(); 00052 } 00053 00054 template <typename T, unsigned D> 00055 typename MultiDimensionalVector<T, D>::iterator 00056 MultiDimensionalVector<T, D>::end() { 00057 return data().end(); 00058 } 00059 00060 template <typename T, unsigned D> 00061 typename MultiDimensionalVector<T, D>::const_iterator 00062 MultiDimensionalVector<T, D>::begin() const { 00063 return data().begin(); 00064 } 00065 00066 template <typename T, unsigned D> 00067 typename MultiDimensionalVector<T, D>::const_iterator 00068 MultiDimensionalVector<T, D>::end() const { 00069 return data().end(); 00070 } 00071 00073 template <typename T, unsigned D> 00074 void MultiDimensionalVector<T, D>::push_back(const MultiDimensionalVector<T, D-1> &x) { 00075 data().push_back(x); 00076 } 00077 00078 template <typename T, unsigned D> 00079 MultiDimensionalVector<T, D-1> &MultiDimensionalVector<T, D>::back() { 00080 return data().back(); 00081 } 00082 00083 template <typename T, unsigned D> 00084 const MultiDimensionalVector<T, D-1> &MultiDimensionalVector<T, D>::back() const { 00085 return data().back(); 00086 } 00087 00091 template <typename T, unsigned D> 00092 typename MultiDimensionalVector<T, D>::iterator 00093 MultiDimensionalVector<T, D>::erase(iterator position) { 00094 return data().erase(position); 00095 } 00096 00101 template <typename T, unsigned D> 00102 typename MultiDimensionalVector<T, D>::iterator 00103 MultiDimensionalVector<T, D>::erase(iterator first, iterator last) { 00104 return data().erase(first, last); 00105 } 00106 00110 template <typename T, unsigned D> 00111 MultiDimensionalVector<T, D-1> &MultiDimensionalVector<T, D>::operator [] (unsigned i) { 00112 return data()[i]; 00113 } 00114 00118 template <typename T, unsigned D> 00119 const MultiDimensionalVector<T, D-1> &MultiDimensionalVector<T, D>::operator [] (unsigned i) const { 00120 return data()[i]; 00121 } 00122 00123 template <typename T, unsigned D> 00124 unsigned MultiDimensionalVector<T, D>::size() const { 00125 return data().size(); 00126 } 00127 00128 template <typename T, unsigned D> 00129 unsigned MultiDimensionalVector<T, D>::empty() const { 00130 return data().empty(); 00131 } 00132 00133 template <typename T, unsigned D> void MultiDimensionalVector<T, D>::clear() { 00134 for (typename MultiDimensionalVector<T, D>::iterator i = data().begin(); i != data().end(); ++i) { 00135 i->clear(); 00136 } 00137 data().clear(); 00138 } 00139 00141 template <typename T, unsigned D> 00142 void MultiDimensionalVector<T, D>::resize(unsigned new_size) { 00143 data().resize(new_size); 00144 } 00145 00149 template <typename T, unsigned D> 00150 void MultiDimensionalVector<T, D>::resize(unsigned new_size, MultiDimensionalVector<T, D-1> x) { 00151 data().resize(new_size, x); 00152 } 00153 00155 template <typename T, unsigned D> 00156 void MultiDimensionalVector<T, D>::reserve(unsigned new_size) { 00157 data().reserve(new_size); 00158 } 00159 00161 template <typename T, unsigned D> 00162 bool MultiDimensionalVector<T, D>::operator == (const MultiDimensionalVector<T, D> &other) const { 00163 return data() == other.data(); 00164 } 00165 00167 template <typename T, unsigned D> 00168 bool MultiDimensionalVector<T, D>::operator != (const MultiDimensionalVector<T, D> &other) const { 00169 return data() != other.data(); 00170 } 00171 00175 template <typename T, unsigned D> 00176 template <typename Function> 00177 Function MultiDimensionalVector<T, D>::for_each(Function f) { 00178 for (iterator i = begin(); i != end(); ++i) { 00179 f = i->for_each(f); 00180 } 00181 return f; 00182 } 00183 00184 template <typename T, unsigned D> 00185 template <typename Function> 00186 Function MultiDimensionalVector<T, D>::for_each(Function f) const { 00187 for (const_iterator i = begin(); i != end(); ++i) { 00188 f = i->for_each(f); 00189 } 00190 return f; 00191 } 00192 00193 template <typename T, unsigned D> 00194 std::string MultiDimensionalVector<T, D>::toString() const { 00195 std::ostringstream o; 00196 o << *this; 00197 return o.str(); 00198 } 00199 00200 template <typename T, unsigned D> 00201 void MultiDimensionalVector<T, D>::print() const { 00202 std::cerr << data().size() << " elements: " << "< "; 00203 for (typename std::vector<MultiDimensionalVector<T, D-1> >::const_iterator 00204 i = data().begin(); 00205 i != data().end(); ++i) { 00206 i->print(); 00207 } 00208 std::cerr << " >" << std::endl; 00209 } 00210 00211 template <typename T, unsigned D> 00212 std::vector<MultiDimensionalVector<T, D-1> > &MultiDimensionalVector<T, D>::data() { 00213 return _data; 00214 } 00215 00216 template <typename T, unsigned D> 00217 const std::vector<MultiDimensionalVector<T, D-1> > &MultiDimensionalVector<T, D>::data() const { 00218 return _data; 00219 } 00220 00226 template<typename T, unsigned D> 00227 std::ostream& operator<<(std::ostream& s, MultiDimensionalVector<T, D> const& v) { 00228 00229 if (MultiDimensionalVector<T, 1>::print_num_elements()) s << v.size() << " elements: "; 00230 00231 for (unsigned i = MultiDimensionalVector<T, 1>::max_dimension(); i > D; --i) s << " "; 00232 00233 s << "(\n"; 00234 00235 copy(v.begin(), v.end(), std::ostream_iterator<MultiDimensionalVector<T, D-1> >(s, "\n")); 00236 00237 for (unsigned i = MultiDimensionalVector<T, 1>::max_dimension(); i > D; --i) s << " "; 00238 00239 s << ")"; 00240 00241 return s; 00242 } 00243 00251 template <typename T, unsigned D> 00252 typename MultiDimensionalVector<T, D>::iterator find(MultiDimensionalVector<T, D-1> const &find_me, 00253 MultiDimensionalVector<T, D> &v) { 00254 typename MultiDimensionalVector<T, D>::iterator i = v.begin(); 00255 while(*i != find_me && i != v.end()) ++i; 00256 return i; 00257 } 00258 00271 template<typename T1, typename T2, unsigned D, typename Function> 00272 Function for_each(MultiDimensionalVector<T1, D> &v1, 00273 MultiDimensionalVector<T2, D> &v2, 00274 Function f) { 00275 size_t max_element = std::min(v1.size(), v2.size()); 00276 for (size_t i = 0; i < max_element; ++i) { 00277 f = for_each(v1[i], v2[i], f); 00278 } 00279 return f; 00280 } 00281 00294 template<typename T1, typename T2, unsigned D, typename Function> 00295 Function for_each(const MultiDimensionalVector<T1, D> &v1, 00296 const MultiDimensionalVector<T2, D> &v2, 00297 Function f) { 00298 size_t max_element = std::min(v1.size(), v2.size()); 00299 for (size_t i = 0; i < max_element; ++i) { 00300 f = for_each(v1[i], v2[i], f); 00301 } 00302 return f; 00303 } 00304 00321 template<typename T1, typename T2, typename T3, unsigned D, typename Function> 00322 Function for_each(MultiDimensionalVector<T1, D> &v1, 00323 MultiDimensionalVector<T2, D> &v2, 00324 MultiDimensionalVector<T3, D> &v3, 00325 Function f) { 00326 size_t max_element = std::min(std::min(v1.size(), v2.size()), v3.size()); 00327 for (size_t i = 0; i < max_element; ++i) { 00328 f = for_each(v1[i], v2[i], v3[i], f); 00329 } 00330 return f; 00331 } 00332 00349 template<typename T1, typename T2, typename T3, unsigned D, typename Function> 00350 Function for_each(const MultiDimensionalVector<T1, D> &v1, 00351 const MultiDimensionalVector<T2, D> &v2, 00352 const MultiDimensionalVector<T3, D> &v3, 00353 Function f) { 00354 size_t max_element = std::min(std::min(v1.size(), v2.size()), v3.size()); 00355 for (size_t i = 0; i < max_element; ++i) { 00356 f = for_each(v1[i], v2[i], v3[i], f); 00357 } 00358 return f; 00359 } 00360 00362 00363 template <typename T> 00364 MultiDimensionalVector<T, 1>::MultiDimensionalVector(): _data() { } 00365 00366 template <typename T> 00367 MultiDimensionalVector<T, 1>::MultiDimensionalVector(unsigned n): _data(n) { } 00368 00369 template <typename T> 00370 MultiDimensionalVector<T, 1>::MultiDimensionalVector(std::vector<T> data): _data(data) { } 00371 00372 template <typename T> 00373 typename MultiDimensionalVector<T, 1>::iterator 00374 MultiDimensionalVector<T, 1>::begin() { 00375 return data().begin(); 00376 } 00377 00378 template <typename T> 00379 typename MultiDimensionalVector<T, 1>::iterator 00380 MultiDimensionalVector<T, 1>::end() { 00381 return data().end(); 00382 } 00383 00384 template <typename T> 00385 typename MultiDimensionalVector<T, 1>::const_iterator 00386 MultiDimensionalVector<T, 1>::begin() const { 00387 return data().begin(); 00388 } 00389 00390 template <typename T> 00391 typename MultiDimensionalVector<T, 1>::const_iterator 00392 MultiDimensionalVector<T, 1>::end() const { 00393 return data().end(); 00394 } 00395 00397 template <typename T> 00398 void MultiDimensionalVector<T, 1>::push_back(const T &x) { 00399 data().push_back(x); 00400 } 00401 00402 template <typename T> 00403 T &MultiDimensionalVector<T, 1>::back() { 00404 return data().back(); 00405 } 00406 00407 template <typename T> 00408 const T &MultiDimensionalVector<T, 1>::back() const { 00409 return data().back(); 00410 } 00411 00415 template <typename T> 00416 typename MultiDimensionalVector<T, 1>::iterator 00417 MultiDimensionalVector<T, 1>::erase(iterator position) { 00418 return data().erase(position); 00419 } 00420 00425 template <typename T> 00426 typename MultiDimensionalVector<T, 1>::iterator 00427 MultiDimensionalVector<T, 1>::erase(iterator first, iterator last) { 00428 return data().erase(first, last); 00429 } 00430 00434 template <typename T> 00435 T &MultiDimensionalVector<T, 1>::operator [] (unsigned i) { 00436 return data()[i]; 00437 } 00438 00442 template <typename T> 00443 const T &MultiDimensionalVector<T, 1>::operator [] (unsigned i) const { 00444 return data()[i]; 00445 } 00446 00447 template <typename T> 00448 unsigned MultiDimensionalVector<T, 1>::size() const { 00449 return data().size(); 00450 } 00451 00452 template <typename T> 00453 unsigned MultiDimensionalVector<T, 1>::empty() const { 00454 return data().empty(); 00455 } 00456 00457 template <typename T> 00458 void MultiDimensionalVector<T, 1>::clear() { 00459 data().clear(); 00460 } 00461 00463 template <typename T> 00464 void MultiDimensionalVector<T, 1>::resize(unsigned new_size) { 00465 data().resize(new_size); 00466 } 00467 00471 template <typename T> 00472 void MultiDimensionalVector<T, 1>::resize(unsigned new_size, T x) { 00473 data().resize(new_size, x); 00474 } 00475 00477 template <typename T> 00478 void MultiDimensionalVector<T, 1>::reserve(unsigned new_size) { 00479 data().reserve(new_size); 00480 } 00481 00483 template <typename T> 00484 bool MultiDimensionalVector<T, 1>::operator == (const MultiDimensionalVector<T, 1> &other) const { 00485 return data() == other.data(); 00486 } 00487 00489 template <typename T> 00490 bool MultiDimensionalVector<T, 1>::operator != (const MultiDimensionalVector<T, 1> &other) const { 00491 return data() != other.data(); 00492 } 00493 00494 template <typename T> 00495 template <typename Function> 00496 Function MultiDimensionalVector<T, 1>::for_each(Function f) { 00497 return std::for_each(begin(), end(), f); 00498 } 00499 00500 template <typename T> 00501 template <typename Function> 00502 Function MultiDimensionalVector<T, 1>::for_each(Function f) const { 00503 return std::for_each(begin(), end(), f); 00504 } 00505 00506 template <typename T> 00507 std::string MultiDimensionalVector<T, 1>::toString() const { 00508 std::ostringstream o; 00509 o << *this; 00510 return o.str(); 00511 } 00512 00513 template <typename T> 00514 void MultiDimensionalVector<T, 1>::print() const { 00515 std::cerr << "< "; 00516 for (typename std::vector<T>::const_iterator i = data().begin(); 00517 i != data().end(); ++i) { 00518 std::cerr << *i << " "; 00519 } 00520 std::cerr << "> "; 00521 } 00522 00523 template <typename T> 00524 bool MultiDimensionalVector<T, 1>::print_num_elements() { 00525 return PRINT_NUM_ELEMENTS; 00526 } 00527 00528 template <typename T> 00529 unsigned MultiDimensionalVector<T, 1>::max_dimension() { 00530 return MAX_DIMENSION; 00531 } 00532 00533 template <typename T> 00534 std::vector<T> &MultiDimensionalVector<T, 1>::data() { 00535 return _data; 00536 } 00537 00538 template <typename T> 00539 const std::vector<T> &MultiDimensionalVector<T, 1>::data() const { 00540 return _data; 00541 } 00542 00544 00549 template<typename T> 00550 std::ostream& operator<<(std::ostream& s, MultiDimensionalVector<T, 1> const& v) { 00551 00552 if (MultiDimensionalVector<T, 1>::print_num_elements()) s << v.size() << " elements: "; 00553 for (unsigned i = MultiDimensionalVector<T, 1>::max_dimension(); i > 1; --i) s << " "; 00554 s << "("; 00555 copy(v.begin(), v.end(), std::ostream_iterator<T>(s, " ")); 00556 for (unsigned i = MultiDimensionalVector<T, 1>::max_dimension(); i > 1; --i) s << " "; 00557 s << ")"; 00558 return s; 00559 00560 } 00561 00562 00574 template<typename T1, typename T2, typename Function> 00575 Function for_each(MultiDimensionalVector<T1, 1> &v1, 00576 MultiDimensionalVector<T2, 1> &v2, 00577 Function f) { 00578 size_t max_element = std::min(v1.size(), v2.size()); 00579 for (size_t i = 0; i < max_element; ++i) { 00580 f(v1[i], v2[i]); 00581 } 00582 return f; 00583 } 00584 00596 template<typename T1, typename T2, typename Function> 00597 Function for_each(const MultiDimensionalVector<T1, 1> &v1, 00598 const MultiDimensionalVector<T2, 1> &v2, 00599 Function f) { 00600 size_t max_element = std::min(v1.size(), v2.size()); 00601 for (size_t i = 0; i < max_element; ++i) { 00602 f(v1[i], v2[i]); 00603 } 00604 return f; 00605 } 00606 00622 template<typename T1, typename T2, typename T3, typename Function> 00623 Function for_each(MultiDimensionalVector<T1, 1> &v1, 00624 MultiDimensionalVector<T2, 1> &v2, 00625 MultiDimensionalVector<T3, 1> &v3, 00626 Function f) { 00627 size_t max_element = std::min(std::min(v1.size(), v2.size()), v3.size()); 00628 for (size_t i = 0; i < max_element; ++i) { 00629 f(v1[i], v2[i], v3[i]); 00630 } 00631 return f; 00632 } 00633 00649 template<typename T1, typename T2, typename T3, typename Function> 00650 Function for_each(const MultiDimensionalVector<T1, 1> &v1, 00651 const MultiDimensionalVector<T2, 1> &v2, 00652 const MultiDimensionalVector<T3, 1> &v3, 00653 Function f) { 00654 size_t max_element = std::min(std::min(v1.size(), v2.size()), v3.size()); 00655 for (size_t i = 0; i < max_element; ++i) { 00656 f(v1[i], v2[i], v3[i]); 00657 } 00658 return f; 00659 } 00660 00661 00662 }