1 | // $Id: utility.cc 737 2007-01-07 13:48:38Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2005 Jari Häkkinen, Markus Ringnér |
---|
5 | Copyright (C) 2006 Jari Häkkinen |
---|
6 | Copyright (C) 2007 Peter Johansson |
---|
7 | |
---|
8 | This file is part of the yat library, http://lev.thep.lu.se/trac/yat |
---|
9 | |
---|
10 | The yat library is free software; you can redistribute it and/or |
---|
11 | modify it under the terms of the GNU General Public License as |
---|
12 | published by the Free Software Foundation; either version 2 of the |
---|
13 | License, or (at your option) any later version. |
---|
14 | |
---|
15 | The yat library is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License |
---|
21 | along with this program; if not, write to the Free Software |
---|
22 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
23 | 02111-1307, USA. |
---|
24 | */ |
---|
25 | |
---|
26 | #include "utility.h" |
---|
27 | |
---|
28 | #include "matrix.h" |
---|
29 | #include "stl_utility.h" |
---|
30 | #include "vector.h" |
---|
31 | #include "yat/random/random.h" |
---|
32 | |
---|
33 | #include <sstream> |
---|
34 | #include <string> |
---|
35 | |
---|
36 | namespace theplu { |
---|
37 | namespace yat { |
---|
38 | namespace utility { |
---|
39 | |
---|
40 | bool is_double(const std::string& s) |
---|
41 | { |
---|
42 | std::stringstream ss(s); |
---|
43 | double a; |
---|
44 | ss>>a; |
---|
45 | if(ss.fail()) |
---|
46 | return false; |
---|
47 | // Check that nothing is left on stream |
---|
48 | std::string b; |
---|
49 | ss >> b; |
---|
50 | return (b.size() ? false : true); |
---|
51 | } |
---|
52 | |
---|
53 | bool is_float(const std::string& s) |
---|
54 | { |
---|
55 | std::stringstream ss(s); |
---|
56 | float a; |
---|
57 | ss>>a; |
---|
58 | if(ss.fail()) |
---|
59 | return false; |
---|
60 | // Check that nothing is left on stream |
---|
61 | std::string b; |
---|
62 | ss >> b; |
---|
63 | return (b.size() ? false : true); |
---|
64 | } |
---|
65 | |
---|
66 | bool is_int(const std::string& s) |
---|
67 | { |
---|
68 | std::stringstream ss(s); |
---|
69 | int a; |
---|
70 | ss >> a; |
---|
71 | if(ss.fail()) |
---|
72 | return false; |
---|
73 | // Check that nothing is left on stream |
---|
74 | std::string b; |
---|
75 | ss >> b; |
---|
76 | return (b.size() ? false : true); |
---|
77 | } |
---|
78 | |
---|
79 | bool is_nan(const std::string& s) |
---|
80 | { |
---|
81 | std::stringstream ss(s); |
---|
82 | std::string s2; |
---|
83 | ss >> s2; // to trim surrounding whitespaces |
---|
84 | to_lower(s2); |
---|
85 | // Check that nothing is left on stream |
---|
86 | std::string s3; |
---|
87 | ss >> s3; |
---|
88 | if(s3.size()) |
---|
89 | return false; |
---|
90 | std::string nan("nan"); |
---|
91 | return (nan==s2); |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | std::pair<size_t, size_t> minmax_index(const vector& vec, |
---|
96 | const std::vector<size_t>& subset) |
---|
97 | { |
---|
98 | size_t min_index = subset[0]; |
---|
99 | size_t max_index = subset[0]; |
---|
100 | for (unsigned int i=1; i<subset.size(); i++) |
---|
101 | if (vec[subset[i]] < vec[min_index]) |
---|
102 | min_index = subset[i]; |
---|
103 | else if (vec[subset[i]] > vec[max_index]) |
---|
104 | max_index = subset[i]; |
---|
105 | return std::pair<size_t,size_t>(min_index, max_index); |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | void shuffle(vector& invec) |
---|
110 | { |
---|
111 | vector vec(invec); |
---|
112 | random::DiscreteUniform rnd; |
---|
113 | for (size_t i=0; i<vec.size(); i++){ |
---|
114 | size_t index = rnd(vec.size()-i); |
---|
115 | invec[i]=vec(index); |
---|
116 | vec(index)=vec(vec.size()-i-1); |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | }}} // end of namespace utility, yat and thep |
---|