69 |
11 Feb 06 |
jari |
// $Id$ |
69 |
11 Feb 06 |
jari |
2 |
|
95 |
05 Apr 06 |
jari |
3 |
/* |
95 |
05 Apr 06 |
jari |
Copyright (C) 2004 Jari Häkkinen |
95 |
05 Apr 06 |
jari |
Copyright (C) 2005 Jari Häkkinen, Peter Johansson, Markus Ringnér |
95 |
05 Apr 06 |
jari |
Copyright (C) 2006 Jari Häkkinen |
95 |
05 Apr 06 |
jari |
7 |
|
95 |
05 Apr 06 |
jari |
This file is part of the thep c++ tools library, |
95 |
05 Apr 06 |
jari |
http://lev.thep.lu.se/trac/c++_tools |
95 |
05 Apr 06 |
jari |
10 |
|
95 |
05 Apr 06 |
jari |
The c++ tools library is free software; you can redistribute it |
95 |
05 Apr 06 |
jari |
and/or modify it under the terms of the GNU General Public License |
824 |
26 Nov 08 |
jari |
as published by the Free Software Foundation; either version 3 of |
95 |
05 Apr 06 |
jari |
the License, or (at your option) any later version. |
95 |
05 Apr 06 |
jari |
15 |
|
95 |
05 Apr 06 |
jari |
The c++ tools library is distributed in the hope that it will be |
95 |
05 Apr 06 |
jari |
useful, but WITHOUT ANY WARRANTY; without even the implied warranty |
95 |
05 Apr 06 |
jari |
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
95 |
05 Apr 06 |
jari |
General Public License for more details. |
95 |
05 Apr 06 |
jari |
20 |
|
95 |
05 Apr 06 |
jari |
You should have received a copy of the GNU General Public License |
824 |
26 Nov 08 |
jari |
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 |
/// \file stl_utility.h |
69 |
11 Feb 06 |
jari |
30 |
/// |
69 |
11 Feb 06 |
jari |
/// There are a number of useful functionality missing in the Standard |
69 |
11 Feb 06 |
jari |
/// Template Library, STL. This file is an effort to provide |
69 |
11 Feb 06 |
jari |
/// 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 |
/// Print out a pair |
110 |
13 Jun 06 |
jari |
45 |
/// |
110 |
13 Jun 06 |
jari |
// This is in namespace std because we have not figured out how to have |
110 |
13 Jun 06 |
jari |
// 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 |
/// STL provides operator< for the pair.first element, but none for |
69 |
11 Feb 06 |
jari |
/// pair.second. This template provides this and can be used as the |
69 |
11 Feb 06 |
jari |
/// 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 |
/// @return true if x.second<y.second or (x.second==y.second and |
69 |
11 Feb 06 |
jari |
/// 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 |
/// @return true if x.second<y.second or (x.second==y.second and |
110 |
13 Jun 06 |
jari |
/// 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 |
/// Function reading from istream to vector of doubles. Function |
69 |
11 Feb 06 |
jari |
/// reads the line until next '\\n'. The line is splitted with |
69 |
11 Feb 06 |
jari |
/// respect to whitespaces and push_backed into the vector. The |
69 |
11 Feb 06 |
jari |
/// vector is emptied before the reading starts. Unexpected |
69 |
11 Feb 06 |
jari |
/// characters are currently skipped with a warning message. |
110 |
13 Jun 06 |
jari |
/// NaN is supported to be a double (case-insensitive) |
69 |
11 Feb 06 |
jari |
100 |
/// |
69 |
11 Feb 06 |
jari |
/// @return false if end of stream |
69 |
11 Feb 06 |
jari |
102 |
/// |
69 |
11 Feb 06 |
jari |
/// @note The functionality of this function will change in the |
69 |
11 Feb 06 |
jari |
/// future. The overall functionality will be the same but the |
69 |
11 Feb 06 |
jari |
/// 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 |
/// Function reading from istream to vector of ints. Function |
69 |
11 Feb 06 |
jari |
/// reads the line until next '\\n'. The line is splitted with |
69 |
11 Feb 06 |
jari |
/// respect to whitespaces and push_backed into the vector. The |
69 |
11 Feb 06 |
jari |
/// vector is emptied before the reading starts. Unexpected |
69 |
11 Feb 06 |
jari |
/// characters are currently skipped with a warning message. |
110 |
13 Jun 06 |
jari |
/// NaN is supported to be an int (case-insensitive) |
69 |
11 Feb 06 |
jari |
116 |
/// |
69 |
11 Feb 06 |
jari |
/// @return false if end of stream |
69 |
11 Feb 06 |
jari |
118 |
/// |
69 |
11 Feb 06 |
jari |
/// @note The functionality of this function will change in the |
69 |
11 Feb 06 |
jari |
/// future. The overall functionality will be the same but the |
69 |
11 Feb 06 |
jari |
/// 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 |
/// Function reading from istream to vector of strings. Function |
69 |
11 Feb 06 |
jari |
/// reads the line until next '\\n'. The line is splitted with |
69 |
11 Feb 06 |
jari |
/// respect to whitespaces and push_backed into the vector. The |
69 |
11 Feb 06 |
jari |
/// vector is emptied before the reading starts. |
69 |
11 Feb 06 |
jari |
130 |
/// |
69 |
11 Feb 06 |
jari |
/// @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 |
/// 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 |
/// 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 |