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