SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
VectorHelper.h
Go to the documentation of this file.
1 /****************************************************************************/
9 // A simple vector of SUMOReals
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2015 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 #ifndef VectorHelper_h
23 #define VectorHelper_h
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <vector>
36 #include <limits>
37 #include <algorithm>
38 #include <iostream>
39 
40 
41 // ===========================================================================
42 // class definitions
43 // ===========================================================================
47 template<class T>
48 class VectorHelper {
49 public:
50  static T sum(const std::vector<T>& v) {
51  T sum = 0;
52  for (typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); i++) {
53  sum += *i;
54  }
55  return sum;
56  }
57 
58  static void normaliseSum(std::vector<T>& v, T msum = 1.0) {
59  if (msum == 0 || v.size() == 0) {
60  // is an error; do nothing
61  return;
62  }
63  T rsum = sum(v);
64  if (rsum == 0) {
65  set(v, (T) 1.0 * msum / (T) v.size());
66  return;
67  }
68  div(v, rsum / msum);
69  }
70 
71  static void div(std::vector<T>& v, T by) {
72  for (typename std::vector<T>::iterator i = v.begin(); i != v.end(); i++) {
73  *i /= by;
74  }
75  }
76 
77  static void removeDouble(std::vector<T>& v) {
78  typename std::vector<T>::iterator i = v.begin();
79  while (i != v.end()) {
80  for (typename std::vector<T>::iterator j = i + 1; j != v.end();) {
81  if (*i == *j) {
82  j = v.erase(j);
83  } else {
84  j++;
85  }
86  }
87  i++;
88  }
89  }
90 
91 
92  static void set(std::vector<T>& v, T to) {
93  for (typename std::vector<T>::iterator i = v.begin(); i != v.end(); i++) {
94  *i = to;
95  }
96  }
97 
98  static T maxValue(const std::vector<T>& v) {
100  for (typename std::vector<T>::const_iterator j = v.begin() ; j != v.end(); j++) {
101  if ((*j) > m) {
102  m = *j;
103  }
104  }
105  return m;
106  }
107 
108  static T minValue(const std::vector<T>& v) {
110  for (typename std::vector<T>::const_iterator j = v.begin(); j != v.end(); j++) {
111  if ((*j) < m) {
112  m = *j;
113  }
114  }
115  return m;
116  }
117 
118  static void remove_smaller_than(std::vector<T>& v, T swell) {
119  for (typename std::vector<T>::iterator j = v.begin(); j != v.end();) {
120  if ((*j) < swell) {
121  j = v.erase(j);
122  } else {
123  j++;
124  }
125  }
126  }
127 
128  static void remove_larger_than(std::vector<T>& v, T swell) {
129  for (typename std::vector<T>::iterator j = v.begin(); j != v.end();) {
130  if ((*j) > swell) {
131  j = v.erase(j);
132  } else {
133  j++;
134  }
135  }
136  }
137 
138  static void add2All(std::vector<T>& v, T what) {
139  for (typename std::vector<T>::iterator j = v.begin(); j != v.end(); j++) {
140  (*j) += what;
141  }
142  }
143 
145  static bool subSetExists(const std::vector<T>& v1, const std::vector<T>& v2) {
146  for (typename std::vector<T>::const_iterator i = v1.begin(); i != v1.end(); i++) {
147  int val1 = (*i);
148  if (find(v2.begin(), v2.end(), val1) != v2.end()) {
149  return true;
150  }
151  }
152  return false;
153  }
154 
155 
156 
157 };
158 
159 template<class T>
160 std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
161  for (typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); i++) {
162  if (i != v.begin()) {
163  os << ", ";
164  }
165  os << (*i);
166  }
167  return os;
168 }
169 
170 
171 
172 #endif
173 
174 /****************************************************************************/
175 
static T sum(const std::vector< T > &v)
Definition: VectorHelper.h:50
static void add2All(std::vector< T > &v, T what)
Definition: VectorHelper.h:138
static void remove_smaller_than(std::vector< T > &v, T swell)
Definition: VectorHelper.h:118
static T minValue(const std::vector< T > &v)
Definition: VectorHelper.h:108
#define max(a, b)
Definition: polyfonts.c:65
static void removeDouble(std::vector< T > &v)
Definition: VectorHelper.h:77
static void div(std::vector< T > &v, T by)
Definition: VectorHelper.h:71
static void normaliseSum(std::vector< T > &v, T msum=1.0)
Definition: VectorHelper.h:58
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:145
static void remove_larger_than(std::vector< T > &v, T swell)
Definition: VectorHelper.h:128
static void set(std::vector< T > &v, T to)
Definition: VectorHelper.h:92
static T maxValue(const std::vector< T > &v)
Definition: VectorHelper.h:98