LCOV - code coverage report
Current view: top level - src/utils/common - VectorHelper.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 16 25 64.0 %
Date: 2024-05-02 15:31:40 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /****************************************************************************/
       2             : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3             : // Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
       4             : // This program and the accompanying materials are made available under the
       5             : // terms of the Eclipse Public License 2.0 which is available at
       6             : // https://www.eclipse.org/legal/epl-2.0/
       7             : // This Source Code may also be made available under the following Secondary
       8             : // Licenses when the conditions for such availability set forth in the Eclipse
       9             : // Public License 2.0 are satisfied: GNU General Public License, version 2
      10             : // or later which is available at
      11             : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
      12             : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
      13             : /****************************************************************************/
      14             : /// @file    VectorHelper.h
      15             : /// @author  Daniel Krajzewicz
      16             : /// @author  Jakob Erdmann
      17             : /// @author  Michael Behrisch
      18             : /// @date    Sept 2002
      19             : ///
      20             : // A simple vector of doubles
      21             : /****************************************************************************/
      22             : #pragma once
      23             : #include <config.h>
      24             : #include <vector>
      25             : #include <limits>
      26             : #include <algorithm>
      27             : #include <iostream>
      28             : 
      29             : 
      30             : // ===========================================================================
      31             : // class definitions
      32             : // ===========================================================================
      33             : /**
      34             :  *
      35             :  */
      36             : template<class T>
      37             : class VectorHelper {
      38             : public:
      39             :     static T sum(const std::vector<T>& v) {
      40             :         T sum = 0;
      41           0 :         for (typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); i++) {
      42           0 :             sum += *i;
      43             :         }
      44             :         return sum;
      45             :     }
      46             : 
      47         100 :     static void normaliseSum(std::vector<T>& v, T msum = 1.0) {
      48         100 :         if (msum == 0 || v.size() == 0) {
      49             :             // is an error; do nothing
      50             :             return;
      51             :         }
      52             :         T rsum = sum(v);
      53           0 :         if (rsum == 0) {
      54           0 :             set(v, (T) 1.0 * msum / (T) v.size());
      55             :             return;
      56             :         }
      57           0 :         div(v, rsum / msum);
      58             :     }
      59             : 
      60             :     static void div(std::vector<T>& v, T by) {
      61           0 :         for (typename std::vector<T>::iterator i = v.begin(); i != v.end(); i++) {
      62           0 :             *i /= by;
      63             :         }
      64             :     }
      65             : 
      66        6581 :     static void removeDouble(std::vector<T>& v) {
      67             :         typename std::vector<T>::iterator i = v.begin();
      68       20833 :         while (i != v.end()) {
      69       42037 :             for (typename std::vector<T>::iterator j = i + 1; j != v.end();) {
      70       27785 :                 if (*i == *j) {
      71             :                     j = v.erase(j);
      72             :                 } else {
      73             :                     j++;
      74             :                 }
      75             :             }
      76             :             i++;
      77             :         }
      78        6581 :     }
      79             : 
      80             : 
      81             :     static void set(std::vector<T>& v, T to) {
      82           0 :         for (typename std::vector<T>::iterator i = v.begin(); i != v.end(); i++) {
      83           0 :             *i = to;
      84             :         }
      85             :     }
      86             : 
      87             :     static T maxValue(const std::vector<T>& v) {
      88             :         T m = -std::numeric_limits<T>::max();
      89     1567873 :         for (typename std::vector<T>::const_iterator j = v.begin() ; j != v.end(); j++) {
      90      845937 :             if ((*j) > m) {
      91             :                 m = *j;
      92             :             }
      93             :         }
      94             :         return m;
      95             :     }
      96             : 
      97             :     static T minValue(const std::vector<T>& v) {
      98             :         T m = std::numeric_limits<T>::max();
      99       99773 :         for (typename std::vector<T>::const_iterator j = v.begin(); j != v.end(); j++) {
     100       79102 :             if ((*j) < m) {
     101             :                 m = *j;
     102             :             }
     103             :         }
     104             :         return m;
     105             :     }
     106             : 
     107             :     static void remove_smaller_than(std::vector<T>& v, T swell) {
     108             :         for (typename std::vector<T>::iterator j = v.begin(); j != v.end();) {
     109             :             if ((*j) < swell) {
     110             :                 j = v.erase(j);
     111             :             } else {
     112             :                 j++;
     113             :             }
     114             :         }
     115             :     }
     116             : 
     117             :     static void remove_larger_than(std::vector<T>& v, T swell) {
     118             :         for (typename std::vector<T>::iterator j = v.begin(); j != v.end();) {
     119             :             if ((*j) > swell) {
     120             :                 j = v.erase(j);
     121             :             } else {
     122             :                 j++;
     123             :             }
     124             :         }
     125             :     }
     126             : 
     127             :     static void add2All(std::vector<T>& v, T what) {
     128             :         for (typename std::vector<T>::iterator j = v.begin(); j != v.end(); j++) {
     129             :             (*j) += what;
     130             :         }
     131             :     }
     132             : 
     133             :     /// Returns the information whether at least one element is within both vectors
     134       78910 :     static bool subSetExists(const std::vector<T>& v1, const std::vector<T>& v2) {
     135      264071 :         for (typename std::vector<T>::const_iterator i = v1.begin(); i != v1.end(); i++) {
     136      185946 :             int val1 = (*i);
     137      185946 :             if (find(v2.begin(), v2.end(), val1) != v2.end()) {
     138         785 :                 return true;
     139             :             }
     140             :         }
     141             :         return false;
     142             :     }
     143             : 
     144             : 
     145             : 
     146             : };
     147             : 
     148             : template<class T>
     149             : std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
     150             :     for (typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); i++) {
     151             :         if (i != v.begin()) {
     152             :             os << ", ";
     153             :         }
     154             :         os << (*i);
     155             :     }
     156             :     return os;
     157             : }

Generated by: LCOV version 1.14