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.org/sumo
3 // Copyright (C) 2001-2020 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 <vector>
24 #include <limits>
25 #include <algorithm>
26 #include <iostream>
27 
28 
29 // ===========================================================================
30 // class definitions
31 // ===========================================================================
35 template<class T>
36 class VectorHelper {
37 public:
38  static T sum(const std::vector<T>& v) {
39  T sum = 0;
40  for (typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); i++) {
41  sum += *i;
42  }
43  return sum;
44  }
45 
46  static void normaliseSum(std::vector<T>& v, T msum = 1.0) {
47  if (msum == 0 || v.size() == 0) {
48  // is an error; do nothing
49  return;
50  }
51  T rsum = sum(v);
52  if (rsum == 0) {
53  set(v, (T) 1.0 * msum / (T) v.size());
54  return;
55  }
56  div(v, rsum / msum);
57  }
58 
59  static void div(std::vector<T>& v, T by) {
60  for (typename std::vector<T>::iterator i = v.begin(); i != v.end(); i++) {
61  *i /= by;
62  }
63  }
64 
65  static void removeDouble(std::vector<T>& v) {
66  typename std::vector<T>::iterator i = v.begin();
67  while (i != v.end()) {
68  for (typename std::vector<T>::iterator j = i + 1; j != v.end();) {
69  if (*i == *j) {
70  j = v.erase(j);
71  } else {
72  j++;
73  }
74  }
75  i++;
76  }
77  }
78 
79 
80  static void set(std::vector<T>& v, T to) {
81  for (typename std::vector<T>::iterator i = v.begin(); i != v.end(); i++) {
82  *i = to;
83  }
84  }
85 
86  static T maxValue(const std::vector<T>& v) {
87  T m = -std::numeric_limits<T>::max();
88  for (typename std::vector<T>::const_iterator j = v.begin() ; j != v.end(); j++) {
89  if ((*j) > m) {
90  m = *j;
91  }
92  }
93  return m;
94  }
95 
96  static T minValue(const std::vector<T>& v) {
97  T m = std::numeric_limits<T>::max();
98  for (typename std::vector<T>::const_iterator j = v.begin(); j != v.end(); j++) {
99  if ((*j) < m) {
100  m = *j;
101  }
102  }
103  return m;
104  }
105 
106  static void remove_smaller_than(std::vector<T>& v, T swell) {
107  for (typename std::vector<T>::iterator j = v.begin(); j != v.end();) {
108  if ((*j) < swell) {
109  j = v.erase(j);
110  } else {
111  j++;
112  }
113  }
114  }
115 
116  static void remove_larger_than(std::vector<T>& v, T swell) {
117  for (typename std::vector<T>::iterator j = v.begin(); j != v.end();) {
118  if ((*j) > swell) {
119  j = v.erase(j);
120  } else {
121  j++;
122  }
123  }
124  }
125 
126  static void add2All(std::vector<T>& v, T what) {
127  for (typename std::vector<T>::iterator j = v.begin(); j != v.end(); j++) {
128  (*j) += what;
129  }
130  }
131 
133  static bool subSetExists(const std::vector<T>& v1, const std::vector<T>& v2) {
134  for (typename std::vector<T>::const_iterator i = v1.begin(); i != v1.end(); i++) {
135  int val1 = (*i);
136  if (find(v2.begin(), v2.end(), val1) != v2.end()) {
137  return true;
138  }
139  }
140  return false;
141  }
142 
143 
144 
145 };
146 
147 template<class T>
148 std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
149  for (typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); i++) {
150  if (i != v.begin()) {
151  os << ", ";
152  }
153  os << (*i);
154  }
155  return os;
156 }
std::ostream & operator<<(std::ostream &os, const std::vector< T > &v)
Definition: VectorHelper.h:148
static void div(std::vector< T > &v, T by)
Definition: VectorHelper.h:59
static T maxValue(const std::vector< T > &v)
Definition: VectorHelper.h:86
static void remove_larger_than(std::vector< T > &v, T swell)
Definition: VectorHelper.h:116
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:133
static void set(std::vector< T > &v, T to)
Definition: VectorHelper.h:80
static void normaliseSum(std::vector< T > &v, T msum=1.0)
Definition: VectorHelper.h:46
static void remove_smaller_than(std::vector< T > &v, T swell)
Definition: VectorHelper.h:106
static void removeDouble(std::vector< T > &v)
Definition: VectorHelper.h:65
static T minValue(const std::vector< T > &v)
Definition: VectorHelper.h:96
static T sum(const std::vector< T > &v)
Definition: VectorHelper.h:38
static void add2All(std::vector< T > &v, T what)
Definition: VectorHelper.h:126