SUMO - Simulation of Urban MObility

VectorHelper.h

Go to the documentation of this file.
00001 /****************************************************************************/
00009 // A simple vector of SUMOReals
00010 /****************************************************************************/
00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00013 /****************************************************************************/
00014 //
00015 //   This file is part of SUMO.
00016 //   SUMO is free software: you can redistribute it and/or modify
00017 //   it under the terms of the GNU General Public License as published by
00018 //   the Free Software Foundation, either version 3 of the License, or
00019 //   (at your option) any later version.
00020 //
00021 /****************************************************************************/
00022 #ifndef VectorHelper_h
00023 #define VectorHelper_h
00024 
00025 
00026 // ===========================================================================
00027 // included modules
00028 // ===========================================================================
00029 #ifdef _MSC_VER
00030 #include <windows_config.h>
00031 #else
00032 #include <config.h>
00033 #endif
00034 
00035 #include <vector>
00036 #include <algorithm>
00037 #include <iostream>
00038 
00039 
00040 // ===========================================================================
00041 // definitions
00042 // ===========================================================================
00043 typedef std::vector<bool> BoolVector;
00044 typedef std::vector<SUMOReal> DoubleVector;
00045 typedef std::vector<int> IntVector;
00046 
00047 
00048 // ===========================================================================
00049 // class definitions
00050 // ===========================================================================
00054 template<class T>
00055 class VectorHelper {
00056 public:
00057     static T sum(const std::vector<T> &v) {
00058         T sum = 0;
00059         for (typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); i++) {
00060             sum += *i;
00061         }
00062         return sum;
00063     }
00064 
00065     static void normaliseSum(std::vector<T> &v, T msum = 1.0) {
00066         if (msum == 0) {
00067             // is an error; do nothing
00068             return;
00069         }
00070         T rsum = sum(v);
00071         if (rsum == 0) {
00072             set(v, (T) 1.0 * msum / (T) v.size());
00073             return;
00074         }
00075         div(v, rsum / msum);
00076     }
00077 
00078     static void div(std::vector<T> &v, T by) {
00079         for (typename std::vector<T>::iterator i = v.begin(); i != v.end(); i++) {
00080             *i /= by;
00081         }
00082     }
00083 
00084     static void removeDouble(std::vector<T> &v) {
00085         typename std::vector<T>::iterator i = v.begin();
00086         while (i != v.end()) {
00087             for (typename std::vector<T>::iterator j = i + 1; j != v.end();) {
00088                 if (*i == *j) {
00089                     j = v.erase(j);
00090                 } else {
00091                     j++;
00092                 }
00093             }
00094             i++;
00095         }
00096     }
00097 
00098 
00099     static void set(std::vector<T> &v, T to) {
00100         for (typename std::vector<T>::iterator i = v.begin(); i != v.end(); i++) {
00101             *i = to;
00102         }
00103     }
00104 
00105     static T maxValue(const std::vector<T> &v) {
00106         SUMOReal m = *(v.begin());
00107         for (typename std::vector<T>::const_iterator j = v.begin() + 1; j != v.end(); j++) {
00108             if ((*j) > m) {
00109                 m = *j;
00110             }
00111         }
00112         return m;
00113     }
00114 
00115     static T minValue(const std::vector<T> &v) {
00116         SUMOReal m = *(v.begin());
00117         for (typename std::vector<T>::const_iterator j = v.begin() + 1; j != v.end(); j++) {
00118             if ((*j) < m) {
00119                 m = *j;
00120             }
00121         }
00122         return m;
00123     }
00124 
00125     static void remove_smaller_than(std::vector<T> &v, T swell) {
00126         for (typename std::vector<T>::iterator j = v.begin(); j != v.end();) {
00127             if ((*j) < swell) {
00128                 j = v.erase(j);
00129             } else {
00130                 j++;
00131             }
00132         }
00133     }
00134 
00135     static void remove_larger_than(std::vector<T> &v, T swell) {
00136         for (typename std::vector<T>::iterator j = v.begin(); j != v.end();) {
00137             if ((*j) > swell) {
00138                 j = v.erase(j);
00139             } else {
00140                 j++;
00141             }
00142         }
00143     }
00144 
00145     static void add2All(std::vector<T> &v, T what) {
00146         for (typename std::vector<T>::iterator j = v.begin(); j != v.end(); j++) {
00147             (*j) += what;
00148         }
00149     }
00150 
00152     static bool subSetExists(const std::vector<T> &v1, const std::vector<T> &v2) {
00153         for (typename std::vector<T>::const_iterator i = v1.begin(); i != v1.end(); i++) {
00154             int val1 = (*i);
00155             if (find(v2.begin(), v2.end(), val1) != v2.end()) {
00156                 return true;
00157             }
00158         }
00159         return false;
00160     }
00161 
00162 
00163 
00164 };
00165 
00166 template<class T>
00167 std::ostream& operator<<(std::ostream& os, const std::vector<T> &v) {
00168     for (typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); i++) {
00169         if (i != v.begin()) {
00170             os << ", ";
00171         }
00172         os << (*i);
00173     }
00174     return os;
00175 }
00176 
00177 
00178 
00179 #endif
00180 
00181 /****************************************************************************/
00182 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines