LCOV - code coverage report
Current view: top level - tarch/la - Vector.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 29 32 90.6 %
Date: 2025-06-25 11:26:37 Functions: 0 0 -

          Line data    Source code
       1             : // Copyright (C) 2015 Technische Universitaet Muenchen
       2             : // This file is part of the Mamico project. For conditions of distribution
       3             : // and use, please see the copyright notice in Mamico's main folder, or at
       4             : // www5.in.tum.de/mamico
       5             : #ifndef _TARCH_LA_VECTOR_H_
       6             : #define _TARCH_LA_VECTOR_H_
       7             : #include "tarch/TarchDefinitions.h"
       8             : #include <cmath>
       9             : #include <iostream>
      10             : #include <type_traits>
      11             : 
      12             : namespace tarch {
      13             : namespace la {
      14             : template <int size, class T> class Vector;
      15             : }
      16             : } // namespace tarch
      17             : 
      18             : /**     light-weight implementation of a vector class.
      19             :  *  @tparam size Size
      20             :  *  @tparam T Data type
      21             :  *  @author Philipp Neumann
      22             :  */
      23             : 
      24        3024 : template <int size, class T> class tarch::la::Vector {
      25             : private:
      26             :   T _entries[size];
      27             : 
      28             : public:
      29    10661742 :   Vector() {}
      30             :   /** @brief init. vector with a scalar value.
      31             :    *    @param t scalar value for the initialization
      32             :    */
      33             : 
      34     3463730 :   Vector(const T& t) {
      35    14191892 :     for (int i = 0; i < size; i++) {
      36    10728838 :       _entries[i] = t;
      37             :     }
      38             :   }
      39             :   /** @brief special constructor for 2D left empty for general purpose vector
      40             :    *    @param t0 scalar value for the initialization
      41             :    *    @param t1 scalar value for the initialization
      42             :    */
      43         224 :   Vector(const T& t0, const T& t1) {
      44             :     static_assert(size == 2, "ERROR Vector(t0,t1) only valid for 2D vectors!");
      45         224 :     _entries[0] = t0;
      46         192 :     _entries[1] = t1;
      47          32 :   }
      48             :   /** @brief special constructor for 3D; left empty for general purpose vector
      49             :    *    @param t0 scalar value for the initialization
      50             :    *    @param t1 scalar value for the initialization
      51             :    *    @param t2 scalar value for the initialization
      52             :    */
      53     4295688 :   Vector(const T& t0, const T& t1, const T& t2) {
      54             :     static_assert(size == 3, "ERROR Vector(t0,t1,t2) only valid for 3D vectors!");
      55     4295688 :     _entries[0] = t0;
      56     4295688 :     _entries[1] = t1;
      57     4254479 :     _entries[2] = t2;
      58       40708 :   }
      59             :   /** @brief constructor init. vector from vector
      60             :    *    @param v Vector for the initialization
      61             :    */
      62    22685912 :   Vector(const Vector<size, T>& v) {
      63   317203448 :     for (int i = 0; i < size; i++) {
      64   240610144 :       _entries[i] = v[i];
      65             :     }
      66             :   }
      67             :   /** @brief assigns a value to all vector entries.
      68             :    *    @param t scalar value for the assignment
      69             :    */
      70             :   void assign(const T& t) {
      71             :     for (int i = 0; i < size; i++) {
      72             :       _entries[i] = t;
      73             :     }
      74             :   }
      75             :   /** @brief operator overloading for vector assignment
      76             :    *    @param v Vector for the assignment
      77             :    */
      78     4252296 :   Vector<size, T>& operator=(const Vector<size, T>& v) {
      79    18130192 :     for (int i = 0; i < size; i++) {
      80    13602212 :       _entries[i] = v[i];
      81             :     }
      82             :     return *this;
      83             :   }
      84             :   /** @brief operator overloading; access to vector entries; both () and [] are
      85             :    * allowed
      86             :    *    @param i index
      87             :    */
      88    89917377 :   T& operator[](int i) {
      89             : #if (TARCH_DEBUG == TARCH_YES)
      90             :     if (i < 0 || i > size - 1) {
      91             :       std::cout << "ERROR Vector T& operator[]: i out of range!" << std::endl;
      92             :       exit(EXIT_FAILURE);
      93             :     }
      94             : #endif
      95       52856 :     return _entries[i];
      96             :   }
      97             :   /** @brief operator overloading; access to vector entries; both () and [] are
      98             :    * allowed; !!! Attention: const
      99             :    *    @param i index
     100             :    */
     101   289381690 :   const T& operator[](int i) const {
     102             : #if (TARCH_DEBUG == TARCH_YES)
     103             :     if (i < 0 || i > size - 1) {
     104             :       std::cout << "ERROR Vector const T& operator[]: i out of range!" << std::endl;
     105             :       exit(EXIT_FAILURE);
     106             :     }
     107             : #endif
     108       38124 :     return _entries[i];
     109             :   }
     110             :   /** @brief operator overloading; add a vector to this existing one (this)
     111             :    *    @param v vector that has to be added
     112             :    */
     113           0 :   Vector<size, T>& operator+=(const Vector<size, T>& v) {
     114           0 :     for (int i = 0; i < size; i++) {
     115           0 :       _entries[i] += v[i];
     116             :     }
     117             :     return *this;
     118             :   }
     119             :   /** @brief operator overloading; subtracts a vector from the existing one
     120             :    * (this)
     121             :    *    @param v vector that has to be subtracted
     122             :    */
     123             :   Vector<size, T>& operator-=(const Vector<size, T>& v) {
     124      161728 :     for (int i = 0; i < size; i++) {
     125      121296 :       _entries[i] -= v[i];
     126             :     }
     127             :     return *this;
     128             :   }
     129             : 
     130             :   /** cast Vector<size, T> to Vector<size, convert_to_T> */
     131        1528 :   template <class convert_to_T> explicit operator Vector<size, convert_to_T>() const {
     132             :     Vector<size, convert_to_T> ans;
     133      103392 :     for (unsigned int i = 0; i < size; i++) {
     134       77544 :       ans[i] = static_cast<convert_to_T>(_entries[i]);
     135             :     }
     136             :     return ans;
     137             :   }
     138             : };
     139             : 
     140             : #include "VectorOperations.cpph"
     141             : #endif // _TARCH_LA_VECTOR_H_

Generated by: LCOV version 1.14