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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // A simple vector of doubles
17 /****************************************************************************/
18 #ifndef VectorHelper_h
19 #define VectorHelper_h
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 
26 #include <vector>
27 #include <limits>
28 #include <algorithm>
29 #include <iostream>
30 
31 
32 // ===========================================================================
33 // class definitions
34 // ===========================================================================
38 template<class T>
39 class VectorHelper {
40 public:
41  static T sum(const std::vector<T>& v) {
42  T sum = 0;
43  for (typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); i++) {
44  sum += *i;
45  }
46  return sum;
47  }
48 
49  static void normaliseSum(std::vector<T>& v, T msum = 1.0) {
50  if (msum == 0 || v.size() == 0) {
51  // is an error; do nothing
52  return;
53  }
54  T rsum = sum(v);
55  if (rsum == 0) {
56  set(v, (T) 1.0 * msum / (T) v.size());
57  return;
58  }
59  div(v, rsum / msum);
60  }
61 
62  static void div(std::vector<T>& v, T by) {
63  for (typename std::vector<T>::iterator i = v.begin(); i != v.end(); i++) {
64  *i /= by;
65  }
66  }
67 
68  static void removeDouble(std::vector<T>& v) {
69  typename std::vector<T>::iterator i = v.begin();
70  while (i != v.end()) {
71  for (typename std::vector<T>::iterator j = i + 1; j != v.end();) {
72  if (*i == *j) {
73  j = v.erase(j);
74  } else {
75  j++;
76  }
77  }
78  i++;
79  }
80  }
81 
82 
83  static void set(std::vector<T>& v, T to) {
84  for (typename std::vector<T>::iterator i = v.begin(); i != v.end(); i++) {
85  *i = to;
86  }
87  }
88 
89  static T maxValue(const std::vector<T>& v) {
90  T m = -std::numeric_limits<T>::max();
91  for (typename std::vector<T>::const_iterator j = v.begin() ; j != v.end(); j++) {
92  if ((*j) > m) {
93  m = *j;
94  }
95  }
96  return m;
97  }
98 
99  static T minValue(const std::vector<T>& v) {
100  T m = std::numeric_limits<T>::max();
101  for (typename std::vector<T>::const_iterator j = v.begin(); j != v.end(); j++) {
102  if ((*j) < m) {
103  m = *j;
104  }
105  }
106  return m;
107  }
108 
109  static void remove_smaller_than(std::vector<T>& v, T swell) {
110  for (typename std::vector<T>::iterator j = v.begin(); j != v.end();) {
111  if ((*j) < swell) {
112  j = v.erase(j);
113  } else {
114  j++;
115  }
116  }
117  }
118 
119  static void remove_larger_than(std::vector<T>& v, T swell) {
120  for (typename std::vector<T>::iterator j = v.begin(); j != v.end();) {
121  if ((*j) > swell) {
122  j = v.erase(j);
123  } else {
124  j++;
125  }
126  }
127  }
128 
129  static void add2All(std::vector<T>& v, T what) {
130  for (typename std::vector<T>::iterator j = v.begin(); j != v.end(); j++) {
131  (*j) += what;
132  }
133  }
134 
136  static bool subSetExists(const std::vector<T>& v1, const std::vector<T>& v2) {
137  for (typename std::vector<T>::const_iterator i = v1.begin(); i != v1.end(); i++) {
138  int val1 = (*i);
139  if (find(v2.begin(), v2.end(), val1) != v2.end()) {
140  return true;
141  }
142  }
143  return false;
144  }
145 
146 
147 
148 };
149 
150 template<class T>
151 std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
152  for (typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); i++) {
153  if (i != v.begin()) {
154  os << ", ";
155  }
156  os << (*i);
157  }
158  return os;
159 }
160 
161 
162 
163 #endif
164 
165 /****************************************************************************/
166 
VectorHelper::removeDouble
static void removeDouble(std::vector< T > &v)
Definition: VectorHelper.h:68
VectorHelper::div
static void div(std::vector< T > &v, T by)
Definition: VectorHelper.h:62
VectorHelper::normaliseSum
static void normaliseSum(std::vector< T > &v, T msum=1.0)
Definition: VectorHelper.h:49
VectorHelper::remove_smaller_than
static void remove_smaller_than(std::vector< T > &v, T swell)
Definition: VectorHelper.h:109
operator<<
std::ostream & operator<<(std::ostream &os, const std::vector< T > &v)
Definition: VectorHelper.h:151
VectorHelper
Definition: VectorHelper.h:39
VectorHelper::subSetExists
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:136
VectorHelper::remove_larger_than
static void remove_larger_than(std::vector< T > &v, T swell)
Definition: VectorHelper.h:119
VectorHelper::set
static void set(std::vector< T > &v, T to)
Definition: VectorHelper.h:83
VectorHelper::maxValue
static T maxValue(const std::vector< T > &v)
Definition: VectorHelper.h:89
VectorHelper::sum
static T sum(const std::vector< T > &v)
Definition: VectorHelper.h:41
VectorHelper::add2All
static void add2All(std::vector< T > &v, T what)
Definition: VectorHelper.h:129
VectorHelper::minValue
static T minValue(const std::vector< T > &v)
Definition: VectorHelper.h:99