SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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-sim.org/
12 // Copyright (C) 2001-2014 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 <algorithm>
37 #include <iostream>
38 
39 
40 // ===========================================================================
41 // class definitions
42 // ===========================================================================
46 template<class T>
47 class VectorHelper {
48 public:
49  static T sum(const std::vector<T>& v) {
50  T sum = 0;
51  for (typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); i++) {
52  sum += *i;
53  }
54  return sum;
55  }
56 
57  static void normaliseSum(std::vector<T>& v, T msum = 1.0) {
58  if (msum == 0 || v.size() == 0) {
59  // is an error; do nothing
60  return;
61  }
62  T rsum = sum(v);
63  if (rsum == 0) {
64  set(v, (T) 1.0 * msum / (T) v.size());
65  return;
66  }
67  div(v, rsum / msum);
68  }
69 
70  static void div(std::vector<T>& v, T by) {
71  for (typename std::vector<T>::iterator i = v.begin(); i != v.end(); i++) {
72  *i /= by;
73  }
74  }
75 
76  static void removeDouble(std::vector<T>& v) {
77  typename std::vector<T>::iterator i = v.begin();
78  while (i != v.end()) {
79  for (typename std::vector<T>::iterator j = i + 1; j != v.end();) {
80  if (*i == *j) {
81  j = v.erase(j);
82  } else {
83  j++;
84  }
85  }
86  i++;
87  }
88  }
89 
90 
91  static void set(std::vector<T>& v, T to) {
92  for (typename std::vector<T>::iterator i = v.begin(); i != v.end(); i++) {
93  *i = to;
94  }
95  }
96 
97  static T maxValue(const std::vector<T>& v) {
98  SUMOReal m = *(v.begin());
99  for (typename std::vector<T>::const_iterator j = v.begin() + 1; j != v.end(); j++) {
100  if ((*j) > m) {
101  m = *j;
102  }
103  }
104  return m;
105  }
106 
107  static T minValue(const std::vector<T>& v) {
108  SUMOReal m = *(v.begin());
109  for (typename std::vector<T>::const_iterator j = v.begin() + 1; j != v.end(); j++) {
110  if ((*j) < m) {
111  m = *j;
112  }
113  }
114  return m;
115  }
116 
117  static void remove_smaller_than(std::vector<T>& v, T swell) {
118  for (typename std::vector<T>::iterator j = v.begin(); j != v.end();) {
119  if ((*j) < swell) {
120  j = v.erase(j);
121  } else {
122  j++;
123  }
124  }
125  }
126 
127  static void remove_larger_than(std::vector<T>& v, T swell) {
128  for (typename std::vector<T>::iterator j = v.begin(); j != v.end();) {
129  if ((*j) > swell) {
130  j = v.erase(j);
131  } else {
132  j++;
133  }
134  }
135  }
136 
137  static void add2All(std::vector<T>& v, T what) {
138  for (typename std::vector<T>::iterator j = v.begin(); j != v.end(); j++) {
139  (*j) += what;
140  }
141  }
142 
144  static bool subSetExists(const std::vector<T>& v1, const std::vector<T>& v2) {
145  for (typename std::vector<T>::const_iterator i = v1.begin(); i != v1.end(); i++) {
146  int val1 = (*i);
147  if (find(v2.begin(), v2.end(), val1) != v2.end()) {
148  return true;
149  }
150  }
151  return false;
152  }
153 
154 
155 
156 };
157 
158 template<class T>
159 std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
160  for (typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); i++) {
161  if (i != v.begin()) {
162  os << ", ";
163  }
164  os << (*i);
165  }
166  return os;
167 }
168 
169 
170 
171 #endif
172 
173 /****************************************************************************/
174 
static T sum(const std::vector< T > &v)
Definition: VectorHelper.h:49
static void add2All(std::vector< T > &v, T what)
Definition: VectorHelper.h:137
static void remove_smaller_than(std::vector< T > &v, T swell)
Definition: VectorHelper.h:117
static T minValue(const std::vector< T > &v)
Definition: VectorHelper.h:107
static void removeDouble(std::vector< T > &v)
Definition: VectorHelper.h:76
static void div(std::vector< T > &v, T by)
Definition: VectorHelper.h:70
static void normaliseSum(std::vector< T > &v, T msum=1.0)
Definition: VectorHelper.h:57
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:144
static void remove_larger_than(std::vector< T > &v, T swell)
Definition: VectorHelper.h:127
static void set(std::vector< T > &v, T to)
Definition: VectorHelper.h:91
#define SUMOReal
Definition: config.h:215
static T maxValue(const std::vector< T > &v)
Definition: VectorHelper.h:97