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

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

updating docs

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