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