source: trunk/c++_tools/utility/utility.cc @ 616

Last change on this file since 616 was 616, checked in by Jari Häkkinen, 15 years ago

Removed gslapi namespace and put the code into utility namespace.
Move #ifndef _header_ idiom to top of touched header files.
Removed unneccesary #includes, and added needed #includes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.8 KB
Line 
1// $Id: utility.cc 616 2006-08-31 08:52:02Z jari $
2
3/*
4  Copyright (C) 2005 Jari Häkkinen, Markus Ringnér
5  Copyright (C) 2006 Jari Häkkinen
6
7  This file is part of the thep c++ tools library,
8                                http://lev.thep.lu.se/trac/c++_tools
9
10  The c++ tools library is free software; you can redistribute it
11  and/or modify it under the terms of the GNU General Public License
12  as published by the Free Software Foundation; either version 2 of
13  the License, or (at your option) any later version.
14
15  The c++ tools library is distributed in the hope that it will be
16  useful, but WITHOUT ANY WARRANTY; without even the implied warranty
17  of 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 <c++_tools/utility/utility.h>
27
28#include <c++_tools/random/random.h>
29#include <c++_tools/utility/stl_utility.h>
30#include <c++_tools/utility/vector.h>
31
32#include <sstream>
33#include <string>
34
35namespace theplu {
36namespace utility {
37
38  bool is_double(const std::string& s)
39  {
40    std::stringstream ss(s);
41    double a;
42    ss>>a;
43    if(ss.fail()) 
44      return false;
45    // Check that nothing is left on stream
46    std::string b;
47    ss >> b;
48    return (b.size() ? false : true);
49  }
50 
51  bool is_float(const std::string& s)
52  {
53    std::stringstream ss(s);
54    float a;
55    ss>>a;
56    if(ss.fail()) 
57      return false;
58    // Check that nothing is left on stream
59    std::string b;
60    ss >> b;
61    return (b.size() ? false : true);
62  }
63
64  bool is_int(const std::string& s)
65  {
66    std::stringstream ss(s);
67    int a;
68    ss >> a;
69    if(ss.fail()) 
70      return false;
71    // Check that nothing is left on stream
72    std::string b;
73    ss >> b;
74    return (b.size() ? false : true);
75  }
76
77  bool is_nan(const std::string& s)
78  {
79    std::stringstream ss(s);
80    std::string s2;
81    ss >> s2; // to trim surrounding whitespaces
82    to_lower(s2);
83    // Check that nothing is left on stream
84    std::string s3;
85    ss >> s3;
86    if(s3.size())
87      return false;
88    std::string nan("nan");
89    return (nan==s2);
90  }
91
92
93  std::pair<size_t, size_t> minmax_index(const vector& vec,
94                                         const std::vector<size_t>& subset)
95  {
96    size_t min_index = subset[0];
97    size_t max_index = subset[0];
98    for (unsigned int i=1; i<subset.size(); i++)
99      if (vec[subset[i]] < vec[min_index])
100        min_index = subset[i];
101      else if (vec[subset[i]] > vec[max_index])
102        max_index = subset[i];
103    return std::pair<size_t,size_t>(min_index, max_index);
104  }
105
106
107  void shuffle(vector& invec)
108  {
109    vector vec(invec);
110    random::DiscreteUniform rnd;
111    for (size_t i=0; i<vec.size(); i++){
112      size_t index = rnd(vec.size()-i);
113      invec[i]=vec(index);
114      vec(index)=vec(vec.size()-i-1);
115    }
116  }
117
118
119}} // end of namespace utility and namespace thep
Note: See TracBrowser for help on using the repository browser.