source: trunk/yat/utility/utility.cc @ 680

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

Addresses #153. Introduced yat namespace. Removed alignment namespace. Clean up of code.

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