source: trunk/yat/utility/utility.h @ 1339

Last change on this file since 1339 was 1339, checked in by Peter, 13 years ago

Adding \since tag for new classes and functions

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.8 KB
Line 
1#ifndef _theplu_yat_utility_utility_
2#define _theplu_yat_utility_utility_
3
4// $Id: utility.h 1339 2008-06-06 23:58:39Z 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
42namespace theplu {
43namespace yat {
44namespace 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     \since New in yat 0.5
60   */
61  template<typename T>
62  bool is(const std::string& s);
63
64  ///
65  /// @return true if string is a double
66  ///
67  /// \deprecated Provided for backward compatibility with the 0.4
68  /// API. Use is<double>(const std::string&)
69  ///
70  bool is_double(const std::string&);
71
72  /**
73     @return true if string \a s fulfills regular expression \verbatim
74     ^\w* \endverbatim \a other \verbatim \w*$ \endverbatim (case
75     insensitive)
76  */
77  bool is_equal(std::string s, std::string other);
78
79  ///
80  /// @return true if string is a float
81  ///
82  /// \deprecated Provided for backward compatibility with the 0.4
83  /// API. Use is<float>(const std::string&)
84  ///
85  bool is_float(const std::string&);
86
87  ///
88  /// @return true if string is an int
89  ///
90  /// \deprecated Provided for backward compatibility with the 0.4
91  /// API. Use is<int>(const std::string&)
92  ///
93  bool is_int(const std::string&);
94
95  ///
96  /// @return true if string is "nan" (case-insensitive)
97  ///
98  bool is_nan(const std::string& s);
99
100  /**
101     \brief Perform quantile normalization
102
103     \since New in yat 0.5
104   */
105  void quantile_normalize(Matrix&);
106
107  // template implementations
108  template<typename T>
109  T convert(const std::string& s)
110  {
111    if (is_nan(s))
112      return std::numeric_limits<T>::quiet_NaN();
113    if (is_equal(s, "inf"))
114      return std::numeric_limits<T>::infinity();
115    if (is_equal(s, "-inf"))
116      if (std::numeric_limits<T>::is_signed)
117        return -std::numeric_limits<T>::infinity();
118      else
119        throw std::runtime_error(std::string("convert(\"")+s+
120                                 std::string("\"): type is unsigned") );
121    std::stringstream ss(s);
122    T a;
123    ss >> a;
124    bool ok = true;
125    if(ss.fail()) 
126      ok = false;
127    // Check that nothing is left on stream
128    std::string b;
129    ss >> b;
130    if (!b.empty() || !ok)
131      throw std::runtime_error(std::string("convert(\"")+s+std::string("\")"));
132    return a;
133  }
134
135  template<typename T>
136  bool is(const std::string& s)
137  {
138    if (is_nan(s))
139      return std::numeric_limits<T>::has_quiet_NaN;
140    if (is_equal(s, "inf"))
141      return std::numeric_limits<T>::has_infinity;
142    if (is_equal(s, "-inf"))
143      return std::numeric_limits<T>::has_infinity && 
144        std::numeric_limits<T>::is_signed;
145    std::stringstream ss(s);
146    T a;
147    ss >> a;
148    if(ss.fail())
149      return false;
150    // Check that nothing is left on stream
151    std::string b;
152    ss >> b;
153    return b.empty();
154  }
155
156}}} // of namespace utility, yat, and theplu
157
158#endif
Note: See TracBrowser for help on using the repository browser.