plugins/base1/se.lu.thep.wenni/trunk/lib/c++_tools/utility/stl_utility.h

Code
Comments
Other
Rev Date Author Line
69 11 Feb 06 jari 1 // $Id$
69 11 Feb 06 jari 2
95 05 Apr 06 jari 3 /*
95 05 Apr 06 jari 4   Copyright (C) 2004 Jari Häkkinen
95 05 Apr 06 jari 5   Copyright (C) 2005 Jari Häkkinen, Peter Johansson, Markus Ringnér
95 05 Apr 06 jari 6   Copyright (C) 2006 Jari Häkkinen
95 05 Apr 06 jari 7
95 05 Apr 06 jari 8   This file is part of the thep c++ tools library,
95 05 Apr 06 jari 9                                 http://lev.thep.lu.se/trac/c++_tools
95 05 Apr 06 jari 10
95 05 Apr 06 jari 11   The c++ tools library is free software; you can redistribute it
95 05 Apr 06 jari 12   and/or modify it under the terms of the GNU General Public License
824 26 Nov 08 jari 13   as published by the Free Software Foundation; either version 3 of
95 05 Apr 06 jari 14   the License, or (at your option) any later version.
95 05 Apr 06 jari 15
95 05 Apr 06 jari 16   The c++ tools library is distributed in the hope that it will be
95 05 Apr 06 jari 17   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
95 05 Apr 06 jari 18   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
95 05 Apr 06 jari 19   General Public License for more details.
95 05 Apr 06 jari 20
95 05 Apr 06 jari 21   You should have received a copy of the GNU General Public License
824 26 Nov 08 jari 22   along with WeNNI. If not, see <http://www.gnu.org/licenses/>.
95 05 Apr 06 jari 23 */
95 05 Apr 06 jari 24
69 11 Feb 06 jari 25 #ifndef _theplu_utility_stl_utility_
69 11 Feb 06 jari 26 #define _theplu_utility_stl_utility_ 
69 11 Feb 06 jari 27
69 11 Feb 06 jari 28 ///
69 11 Feb 06 jari 29 /// \file stl_utility.h
69 11 Feb 06 jari 30 ///
69 11 Feb 06 jari 31 /// There are a number of useful functionality missing in the Standard
69 11 Feb 06 jari 32 /// Template Library, STL. This file is an effort to provide
69 11 Feb 06 jari 33 /// extensions to STL functionality.
69 11 Feb 06 jari 34 ///
69 11 Feb 06 jari 35
110 13 Jun 06 jari 36 #include<ostream>
69 11 Feb 06 jari 37 #include <string>
69 11 Feb 06 jari 38 #include <utility>
69 11 Feb 06 jari 39 #include <vector>
69 11 Feb 06 jari 40
110 13 Jun 06 jari 41 namespace std {
110 13 Jun 06 jari 42
110 13 Jun 06 jari 43   ///
110 13 Jun 06 jari 44   /// Print out a pair 
110 13 Jun 06 jari 45   ///
110 13 Jun 06 jari 46   // This is in namespace std because we have not figured out how to have
110 13 Jun 06 jari 47   // pair and its operator<< in different namespaces
110 13 Jun 06 jari 48   template <class T1, class T2>  
110 13 Jun 06 jari 49   std::ostream& operator<<(std::ostream& out, const std::pair<T1,T2>& p) 
110 13 Jun 06 jari 50   { out << p.first << "\t" << p.second; return out; }
110 13 Jun 06 jari 51
110 13 Jun 06 jari 52 }
110 13 Jun 06 jari 53
69 11 Feb 06 jari 54 namespace theplu {
69 11 Feb 06 jari 55 namespace utility {
69 11 Feb 06 jari 56
110 13 Jun 06 jari 57
69 11 Feb 06 jari 58   ///
69 11 Feb 06 jari 59   /// STL provides operator< for the pair.first element, but none for
69 11 Feb 06 jari 60   /// pair.second. This template provides this and can be used as the
69 11 Feb 06 jari 61   /// comparison object in generic functions such as the STL sort.
69 11 Feb 06 jari 62   ///
69 11 Feb 06 jari 63   template <class T1,class T2>
69 11 Feb 06 jari 64   struct pair_value_compare
69 11 Feb 06 jari 65   {
69 11 Feb 06 jari 66     ///
69 11 Feb 06 jari 67     /// @return true if x.second<y.second or (x.second==y.second and
69 11 Feb 06 jari 68     /// x.first<y.first)
69 11 Feb 06 jari 69     ///
69 11 Feb 06 jari 70     inline bool operator()(const std::pair<T1,T2>& x,
69 11 Feb 06 jari 71                            const std::pair<T1,T2>& y) {
69 11 Feb 06 jari 72       return ((x.second<y.second) ||
69 11 Feb 06 jari 73               (!(y.second<x.second) && (x.first<y.first))); 
69 11 Feb 06 jari 74     }
69 11 Feb 06 jari 75   };
69 11 Feb 06 jari 76
69 11 Feb 06 jari 77   ///
110 13 Jun 06 jari 78   ///
110 13 Jun 06 jari 79   template <class T1,class T2>
110 13 Jun 06 jari 80   struct pointer_compare
110 13 Jun 06 jari 81   {
110 13 Jun 06 jari 82     ///
110 13 Jun 06 jari 83     /// @return true if x.second<y.second or (x.second==y.second and
110 13 Jun 06 jari 84     /// x.first<y.first)
110 13 Jun 06 jari 85     ///
110 13 Jun 06 jari 86     inline bool operator()(const std::pair<T1,T2>& x,
110 13 Jun 06 jari 87                            const std::pair<T1,T2>& y) {
110 13 Jun 06 jari 88       return ((x.second<y.second) ||
110 13 Jun 06 jari 89               (!(y.second<x.second) && (x.first<y.first))); 
110 13 Jun 06 jari 90     }
110 13 Jun 06 jari 91   };
110 13 Jun 06 jari 92
110 13 Jun 06 jari 93   ///
69 11 Feb 06 jari 94   /// Function reading from istream to vector of doubles. Function
69 11 Feb 06 jari 95   /// reads the line until next '\\n'. The line is splitted with
69 11 Feb 06 jari 96   /// respect to whitespaces and push_backed into the vector. The
69 11 Feb 06 jari 97   /// vector is emptied before the reading starts. Unexpected
69 11 Feb 06 jari 98   /// characters are currently skipped with a warning message.
110 13 Jun 06 jari 99   /// NaN is supported to be a double (case-insensitive)
69 11 Feb 06 jari 100   ///
69 11 Feb 06 jari 101   /// @return false if end of stream
69 11 Feb 06 jari 102   ///
69 11 Feb 06 jari 103   /// @note The functionality of this function will change in the
69 11 Feb 06 jari 104   /// future. The overall functionality will be the same but the
69 11 Feb 06 jari 105   /// outcome of unexpected events will change.
69 11 Feb 06 jari 106   ///
69 11 Feb 06 jari 107   bool read_to_double(std::istream&, std::vector<double>&);
69 11 Feb 06 jari 108
69 11 Feb 06 jari 109   ///
69 11 Feb 06 jari 110   /// Function reading from istream to vector of ints. Function
69 11 Feb 06 jari 111   /// reads the line until next '\\n'. The line is splitted with
69 11 Feb 06 jari 112   /// respect to whitespaces and push_backed into the vector. The
69 11 Feb 06 jari 113   /// vector is emptied before the reading starts. Unexpected
69 11 Feb 06 jari 114   /// characters are currently skipped with a warning message.
110 13 Jun 06 jari 115   /// NaN is supported to be an int (case-insensitive)
69 11 Feb 06 jari 116   ///
69 11 Feb 06 jari 117   /// @return false if end of stream
69 11 Feb 06 jari 118   ///
69 11 Feb 06 jari 119   /// @note The functionality of this function will change in the
69 11 Feb 06 jari 120   /// future. The overall functionality will be the same but the
69 11 Feb 06 jari 121   /// outcome of unexpected events will change.
69 11 Feb 06 jari 122   ///
69 11 Feb 06 jari 123   bool read_to_int(std::istream&, std::vector<int>&);
69 11 Feb 06 jari 124
69 11 Feb 06 jari 125   ///
69 11 Feb 06 jari 126   /// Function reading from istream to vector of strings. Function
69 11 Feb 06 jari 127   /// reads the line until next '\\n'. The line is splitted with
69 11 Feb 06 jari 128   /// respect to whitespaces and push_backed into the vector. The
69 11 Feb 06 jari 129   /// vector is emptied before the reading starts.
69 11 Feb 06 jari 130   ///
69 11 Feb 06 jari 131   /// @return false if end of stream
69 11 Feb 06 jari 132   ///
69 11 Feb 06 jari 133   bool read_to_string(std::istream&, std::vector<std::string>&);
69 11 Feb 06 jari 134
110 13 Jun 06 jari 135   ///
110 13 Jun 06 jari 136   /// Function converting a string to lower case
110 13 Jun 06 jari 137   ///
110 13 Jun 06 jari 138   inline void to_lower(std::string& s) {
110 13 Jun 06 jari 139     transform(s.begin(),s.end(), s.begin(), tolower);
110 13 Jun 06 jari 140   }
69 11 Feb 06 jari 141
110 13 Jun 06 jari 142   ///
110 13 Jun 06 jari 143   /// Function converting a string to upper case
110 13 Jun 06 jari 144   ///
110 13 Jun 06 jari 145   inline void to_upper(std::string& s) {
110 13 Jun 06 jari 146     transform(s.begin(),s.end(), s.begin(), toupper);
110 13 Jun 06 jari 147   }
110 13 Jun 06 jari 148
110 13 Jun 06 jari 149
69 11 Feb 06 jari 150 }} // of namespace utility and namespace theplu
69 11 Feb 06 jari 151
69 11 Feb 06 jari 152 #endif