Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <initializer_list>
- #include <stdexcept>
- #include <cmath>
- typedef unsigned int uint;
- template<uint dim, typename ValueType>
- class vec {
- public:
- vec();
- vec(const std::initializer_list<ValueType> pValues);
- template<typename StorageType=long long>
- auto length() -> decltype(std::sqrt(static_cast<StorageType>(0)));
- template<typename ValueTypeRight>
- vec<dim, ValueType> dot(vec<dim, ValueTypeRight> pOther);
- ValueType& operator[](std::size_t pIndex);
- private:
- ValueType elements[dim];
- };
- template<uint dim, typename ValueType>
- vec<dim, ValueType>::vec() {
- }
- template<uint dim, typename ValueType>
- vec<dim, ValueType>::vec(const std::initializer_list<ValueType> pValues) {
- if(pValues.size()!=dim) {
- throw std::runtime_error("Length of initializer_list does not match dimension");
- }
- int i=0;
- for(ValueType element : pValues) {
- elements[i] = element;
- ++i;
- }
- }
- template<uint dim, typename ValueType, typename StorageType=long long>
- auto vec<dim, ValueType>::length() -> decltype(std::sqrt(static_cast<StorageType>(0)))
- {
- StorageType tmp = 0;
- for(uint e=0; e<dim; ++e) {
- tmp += std::pow(elements[0],2);
- }
- return std::sqrt(tmp);
- }
- /*
- template<uint dim, typename ValueType, typename ValueTypeRight>
- vec<dim, ValueType>::dot(
- */
- template<uint dim, typename ValueType>
- ValueType& vec<dim, ValueType>::operator[](const std::size_t index) {
- return elements[index];
- }
- template<uint dim, typename ValueTypeLeft, typename ValueTypeRight>
- auto operator+(vec<dim, ValueTypeLeft> pLeft, vec<dim, ValueTypeRight> pRight)
- -> vec<dim, decltype(pLeft[0]+pRight[0])>
- {
- vec<dim, decltype(pLeft[0]+pRight[0])> result;
- for(int e=0; e<dim; ++e) {
- result[e] = pLeft[e] + pLeft[e];
- }
- return result;
- }
- template<uint dim, typename ValueTypeLeft, typename ValueTypeRight>
- auto operator+(vec<dim, ValueTypeLeft> pLeft, ValueTypeRight pRight)
- -> vec<dim, decltype(pLeft[0]+pRight)>
- {
- vec<dim, decltype(pLeft[0]+pRight)> result;
- for(uint e=0; e<dim; ++e) {
- result[e] = pLeft[e] + pRight;
- }
- return result;
- }
- template<uint dim, typename ValueTypeLeft, typename ValueTypeRight>
- auto operator*(vec<dim, ValueTypeLeft> pLeft, vec<dim, ValueTypeRight> pRight)
- -> vec<dim, decltype(pLeft[0]*pRight[0])>
- {
- vec<dim, decltype(pLeft[0]*pRight[0])> result;
- for(uint e=0; e<dim; ++e) {
- result[e] = pLeft[e]*pRight[e];
- }
- return result;
- }
- template<uint dim, typename ValueTypeLeft, typename ValueTypeRight>
- auto operator*(vec<dim, ValueTypeLeft> pLeft, const ValueTypeRight pRight)
- -> vec<dim, decltype(pLeft[0]*pRight)>
- {
- vec<dim, decltype(pLeft[0]*pRight)> result;
- for(uint e=0; e<dim; ++e) {
- result[e] = pLeft[e] * pRight;
- }
- return result;
- }
- template<uint dim, typename ValueType>
- std::ostream& operator<<(std::ostream& pOS, vec<dim, ValueType> pVec) {
- pOS << "(";
- for(uint e=0; e<dim; ++e) {
- pOS << pVec[e];
- if(e!=dim-1) {
- pOS << ",";
- }
- }
- pOS << ")";
- }
- void testFunction() {
- vec<3, int> firstVec{1,2,3};
- vec<3, int> secondVec{1,3,3};
- vec<3, float> thirdVec{1,2,3};
- double mulVal = 3;
- double addVal = 3;
- std::cout << "First Vector: " << firstVec << std::endl;
- std::cout << "Second Vector: " << secondVec << std::endl;
- std::cout << firstVec << "+" << secondVec << "=" << firstVec+secondVec << std::endl;
- std::cout << firstVec << "*" << secondVec << "=" << firstVec*secondVec << std::endl;
- std::cout << firstVec << "*" << thirdVec << "=" << firstVec*thirdVec << std::endl;
- std::cout << firstVec << "*" << mulVal << "=" << firstVec*mulVal << std::endl;
- std::cout << firstVec << "+" << addVal << "=" << firstVec+addVal << std::endl;
- std::cout << "sqrt: " << std::sqrt(static_cast<int>(2)) << std::endl;
- }
- int main() {
- try {
- testFunction();
- } catch(std::exception& e) {
- std::cout << "Exception:" << e.what() << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement