source: trunk/c++_tools/utility/stl_utility.h @ 675

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

References #83. Changing project name to yat. Compilation will fail in this revision.

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