1 | #ifndef _theplu_yat_utility_utility_ |
---|
2 | #define _theplu_yat_utility_utility_ |
---|
3 | |
---|
4 | // $Id: utility.h 1312 2008-05-19 16:30:52Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2005 Jari Häkkinen, Peter Johansson, Markus Ringnér |
---|
8 | Copyright (C) 2006 Jari Häkkinen |
---|
9 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson |
---|
10 | Copyright (C) 2008 Peter Johansson |
---|
11 | |
---|
12 | This file is part of the yat library, http://trac.thep.lu.se/yat |
---|
13 | |
---|
14 | The yat library is free software; you can redistribute it and/or |
---|
15 | modify it under the terms of the GNU General Public License as |
---|
16 | published by the Free Software Foundation; either version 2 of the |
---|
17 | License, or (at your option) any later version. |
---|
18 | |
---|
19 | The yat library is distributed in the hope that it will be useful, |
---|
20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
22 | General Public License for more details. |
---|
23 | |
---|
24 | You should have received a copy of the GNU General Public License |
---|
25 | along with this program; if not, write to the Free Software |
---|
26 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
27 | 02111-1307, USA. |
---|
28 | */ |
---|
29 | |
---|
30 | /// |
---|
31 | /// @file utility/utility.h |
---|
32 | /// |
---|
33 | /// @brief Some useful functions are placed here |
---|
34 | /// |
---|
35 | |
---|
36 | #include <string> |
---|
37 | #include <stdexcept> |
---|
38 | #include <sstream> |
---|
39 | #include <utility> |
---|
40 | #include <vector> |
---|
41 | |
---|
42 | namespace theplu { |
---|
43 | namespace yat { |
---|
44 | namespace utility { |
---|
45 | |
---|
46 | class Matrix; |
---|
47 | |
---|
48 | /** |
---|
49 | \brief convert string to (numerical) type |
---|
50 | |
---|
51 | \throw runtime_error if conversion fails |
---|
52 | */ |
---|
53 | template<typename T> |
---|
54 | T convert(const std::string& s); |
---|
55 | |
---|
56 | /** |
---|
57 | \brief check if string is convertible to (numerical) type |
---|
58 | */ |
---|
59 | template<typename T> |
---|
60 | bool is(const std::string& s); |
---|
61 | |
---|
62 | /// |
---|
63 | /// @return true if string is a double |
---|
64 | /// |
---|
65 | /// \deprecated use is<double>(const std::string&) |
---|
66 | /// |
---|
67 | bool is_double(const std::string&); |
---|
68 | |
---|
69 | /** |
---|
70 | @return true if string \a s fulfills regular expression \verbatim |
---|
71 | ^\w* \endverbatim \a other \verbatim \w*$ \endverbatim (case |
---|
72 | insensitive) |
---|
73 | */ |
---|
74 | bool is_equal(std::string s, std::string other); |
---|
75 | |
---|
76 | /// |
---|
77 | /// @return true if string is a float |
---|
78 | /// |
---|
79 | /// \deprecated use is<float>(const std::string&) |
---|
80 | /// |
---|
81 | bool is_float(const std::string&); |
---|
82 | |
---|
83 | /// |
---|
84 | /// @return true if string is an int |
---|
85 | /// |
---|
86 | /// \deprecated use is<int>(const std::string&) |
---|
87 | /// |
---|
88 | bool is_int(const std::string&); |
---|
89 | |
---|
90 | /// |
---|
91 | /// @return true if string is "nan" (case-insensitive) |
---|
92 | /// |
---|
93 | bool is_nan(const std::string& s); |
---|
94 | |
---|
95 | /** |
---|
96 | \brief Perform quantile normalization |
---|
97 | */ |
---|
98 | void quantile_normalize(Matrix&); |
---|
99 | |
---|
100 | // template implementations |
---|
101 | template<typename T> |
---|
102 | T convert(const std::string& s) |
---|
103 | { |
---|
104 | if (is_nan(s)) |
---|
105 | return std::numeric_limits<T>::quiet_NaN(); |
---|
106 | if (is_equal(s, "inf")) |
---|
107 | return std::numeric_limits<T>::infinity(); |
---|
108 | if (is_equal(s, "-inf")) |
---|
109 | if (std::numeric_limits<T>::is_signed) |
---|
110 | return -std::numeric_limits<T>::infinity(); |
---|
111 | else |
---|
112 | throw std::runtime_error(std::string("convert(\"")+s+ |
---|
113 | std::string("\"): type is unsigned") ); |
---|
114 | std::stringstream ss(s); |
---|
115 | T a; |
---|
116 | ss >> a; |
---|
117 | bool ok = true; |
---|
118 | if(ss.fail()) |
---|
119 | ok = false; |
---|
120 | // Check that nothing is left on stream |
---|
121 | std::string b; |
---|
122 | ss >> b; |
---|
123 | if (!b.empty() || !ok) |
---|
124 | throw std::runtime_error(std::string("convert(\"")+s+std::string("\")")); |
---|
125 | return a; |
---|
126 | } |
---|
127 | |
---|
128 | template<typename T> |
---|
129 | bool is(const std::string& s) |
---|
130 | { |
---|
131 | if (is_nan(s)) |
---|
132 | return std::numeric_limits<T>::has_quiet_NaN; |
---|
133 | if (is_equal(s, "inf")) |
---|
134 | return std::numeric_limits<T>::has_infinity; |
---|
135 | if (is_equal(s, "-inf")) |
---|
136 | return std::numeric_limits<T>::has_infinity && |
---|
137 | std::numeric_limits<T>::is_signed; |
---|
138 | std::stringstream ss(s); |
---|
139 | T a; |
---|
140 | ss >> a; |
---|
141 | if(ss.fail()) |
---|
142 | return false; |
---|
143 | // Check that nothing is left on stream |
---|
144 | std::string b; |
---|
145 | ss >> b; |
---|
146 | return b.empty(); |
---|
147 | } |
---|
148 | |
---|
149 | }}} // of namespace utility, yat, and theplu |
---|
150 | |
---|
151 | #endif |
---|