Advertisement
Patasuss

c++ vecs

Jun 29th, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <initializer_list>
  4. #include <stdexcept>
  5. #include <cmath>
  6.  
  7. typedef unsigned int uint;
  8.  
  9. template<uint dim, typename ValueType>
  10. class vec {
  11. public:
  12.   vec();
  13.   vec(const std::initializer_list<ValueType> pValues);
  14.  
  15.   template<typename StorageType=long long>
  16.   auto length() -> decltype(std::sqrt(static_cast<StorageType>(0)));
  17.   template<typename ValueTypeRight>
  18.   vec<dim, ValueType> dot(vec<dim, ValueTypeRight> pOther);
  19.  
  20.   ValueType& operator[](std::size_t pIndex);
  21.  
  22. private:
  23.   ValueType elements[dim];
  24. };
  25.  
  26. template<uint dim, typename ValueType>
  27. vec<dim, ValueType>::vec() {
  28. }
  29.  
  30. template<uint dim, typename ValueType>
  31. vec<dim, ValueType>::vec(const std::initializer_list<ValueType>  pValues) {
  32.   if(pValues.size()!=dim) {
  33.     throw std::runtime_error("Length of initializer_list does not match dimension");
  34.   }
  35.   int i=0;
  36.   for(ValueType element : pValues) {
  37.     elements[i] = element;
  38.     ++i;
  39.   }
  40. }
  41.  
  42. template<uint dim, typename ValueType, typename StorageType=long long>
  43. auto vec<dim, ValueType>::length() -> decltype(std::sqrt(static_cast<StorageType>(0)))
  44. {
  45.   StorageType tmp = 0;
  46.   for(uint e=0; e<dim; ++e) {
  47.     tmp += std::pow(elements[0],2);
  48.   }
  49.  
  50.   return std::sqrt(tmp);
  51. }
  52. /*
  53. template<uint dim, typename ValueType, typename ValueTypeRight>
  54. vec<dim, ValueType>::dot(
  55. */
  56.  
  57. template<uint dim, typename ValueType>
  58. ValueType& vec<dim, ValueType>::operator[](const std::size_t index) {
  59.   return elements[index];
  60. }
  61.  
  62. template<uint dim, typename ValueTypeLeft, typename ValueTypeRight>
  63. auto operator+(vec<dim, ValueTypeLeft> pLeft, vec<dim, ValueTypeRight> pRight)
  64.   -> vec<dim, decltype(pLeft[0]+pRight[0])>
  65. {
  66.   vec<dim, decltype(pLeft[0]+pRight[0])> result;
  67.   for(int e=0; e<dim; ++e) {
  68.     result[e] = pLeft[e] + pLeft[e];
  69.   }
  70.  
  71.   return result;
  72. }
  73.  
  74. template<uint dim, typename ValueTypeLeft, typename ValueTypeRight>
  75. auto operator+(vec<dim, ValueTypeLeft> pLeft, ValueTypeRight pRight)
  76.   -> vec<dim, decltype(pLeft[0]+pRight)>
  77. {
  78.   vec<dim, decltype(pLeft[0]+pRight)> result;
  79.   for(uint e=0; e<dim; ++e) {
  80.     result[e] = pLeft[e] + pRight;
  81.   }
  82.  
  83.   return result;
  84. }
  85.  
  86. template<uint dim, typename ValueTypeLeft, typename ValueTypeRight>
  87. auto operator*(vec<dim, ValueTypeLeft> pLeft, vec<dim, ValueTypeRight> pRight)
  88.   -> vec<dim, decltype(pLeft[0]*pRight[0])>
  89. {
  90.   vec<dim, decltype(pLeft[0]*pRight[0])> result;
  91.   for(uint e=0; e<dim; ++e) {
  92.     result[e] = pLeft[e]*pRight[e];
  93.   }
  94.  
  95.   return result;
  96. }
  97.  
  98. template<uint dim, typename ValueTypeLeft, typename ValueTypeRight>
  99. auto operator*(vec<dim, ValueTypeLeft> pLeft, const ValueTypeRight pRight)
  100.   -> vec<dim, decltype(pLeft[0]*pRight)>
  101. {
  102.   vec<dim, decltype(pLeft[0]*pRight)> result;
  103.   for(uint e=0; e<dim; ++e) {
  104.     result[e] = pLeft[e] * pRight;
  105.   }
  106.  
  107.   return result;
  108. }
  109.  
  110. template<uint dim, typename ValueType>
  111. std::ostream& operator<<(std::ostream& pOS, vec<dim, ValueType> pVec) {
  112.   pOS << "(";
  113.   for(uint e=0; e<dim; ++e) {
  114.     pOS << pVec[e];
  115.     if(e!=dim-1) {
  116.       pOS << ",";
  117.     }
  118.   }
  119.  
  120.   pOS << ")";
  121. }
  122.  
  123. void testFunction() {
  124.   vec<3, int> firstVec{1,2,3};
  125.   vec<3, int> secondVec{1,3,3};
  126.   vec<3, float> thirdVec{1,2,3};
  127.   double mulVal = 3;
  128.   double addVal = 3;
  129.   std::cout << "First Vector: " << firstVec << std::endl;
  130.   std::cout << "Second Vector: " << secondVec << std::endl;
  131.   std::cout << firstVec << "+" << secondVec << "=" << firstVec+secondVec << std::endl;
  132.   std::cout << firstVec << "*" << secondVec << "=" << firstVec*secondVec << std::endl;
  133.   std::cout << firstVec << "*" << thirdVec << "=" << firstVec*thirdVec << std::endl;
  134.   std::cout << firstVec << "*" << mulVal << "=" << firstVec*mulVal << std::endl;
  135.   std::cout << firstVec << "+" << addVal << "=" << firstVec+addVal << std::endl;
  136.  
  137.   std::cout << "sqrt: " << std::sqrt(static_cast<int>(2)) << std::endl;
  138. }
  139.  
  140. int main() {
  141.   try {
  142.     testFunction();
  143.   } catch(std::exception& e) {
  144.     std::cout << "Exception:" <<  e.what() << std::endl;
  145.   }
  146.   return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement