source: trunk/lib/utility/stl_utility.h @ 570

Last change on this file since 570 was 570, checked in by Jari Häkkinen, 16 years ago

Added copyright statement to files.

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