Eclipse SUMO - Simulation of Urban MObility
VectorHelper.h
Go to the documentation of this file.
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 /****************************************************************************/
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 // ===========================================================================
36 template<class T>
37 class VectorHelper {
38 public:
39  static T sum(const std::vector<T>& v) {
40  T sum = 0;
41  for (typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); i++) {
42  sum += *i;
43  }
44  return sum;
45  }
46 
47  static void normaliseSum(std::vector<T>& v, T msum = 1.0) {
48  if (msum == 0 || v.size() == 0) {
49  // is an error; do nothing
50  return;
51  }
52  T rsum = sum(v);
53  if (rsum == 0) {
54  set(v, (T) 1.0 * msum / (T) v.size());
55  return;
56  }
57  div(v, rsum / msum);
58  }
59 
60  static void div(std::vector<T>& v, T by) {
61  for (typename std::vector<T>::iterator i = v.begin(); i != v.end(); i++) {
62  *i /= by;
63  }
64  }
65 
66  static void removeDouble(std::vector<T>& v) {
67  typename std::vector<T>::iterator i = v.begin();
68  while (i != v.end()) {
69  for (typename std::vector<T>::iterator j = i + 1; j != v.end();) {
70  if (*i == *j) {
71  j = v.erase(j);
72  } else {
73  j++;
74  }
75  }
76  i++;
77  }
78  }
79 
80 
81  static void set(std::vector<T>& v, T to) {
82  for (typename std::vector<T>::iterator i = v.begin(); i != v.end(); i++) {
83  *i = to;
84  }
85  }
86 
87  static T maxValue(const std::vector<T>& v) {
88  T m = -std::numeric_limits<T>::max();
89  for (typename std::vector<T>::const_iterator j = v.begin() ; j != v.end(); j++) {
90  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  for (typename std::vector<T>::const_iterator j = v.begin(); j != v.end(); j++) {
100  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 
134  static bool subSetExists(const std::vector<T>& v1, const std::vector<T>& v2) {
135  for (typename std::vector<T>::const_iterator i = v1.begin(); i != v1.end(); i++) {
136  int val1 = (*i);
137  if (find(v2.begin(), v2.end(), val1) != v2.end()) {
138  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 }
std::ostream & operator<<(std::ostream &os, const std::vector< T > &v)
Definition: VectorHelper.h:149
static void div(std::vector< T > &v, T by)
Definition: VectorHelper.h:60
static T maxValue(const std::vector< T > &v)
Definition: VectorHelper.h:87
static void remove_larger_than(std::vector< T > &v, T swell)
Definition: VectorHelper.h:117
static bool subSetExists(const std::vector< T > &v1, const std::vector< T > &v2)
Returns the information whether at least one element is within both vectors.
Definition: VectorHelper.h:134
static void set(std::vector< T > &v, T to)
Definition: VectorHelper.h:81
static void normaliseSum(std::vector< T > &v, T msum=1.0)
Definition: VectorHelper.h:47
static void remove_smaller_than(std::vector< T > &v, T swell)
Definition: VectorHelper.h:107
static void removeDouble(std::vector< T > &v)
Definition: VectorHelper.h:66
static T minValue(const std::vector< T > &v)
Definition: VectorHelper.h:97
static T sum(const std::vector< T > &v)
Definition: VectorHelper.h:39
static void add2All(std::vector< T > &v, T what)
Definition: VectorHelper.h:127