-
Notifications
You must be signed in to change notification settings - Fork 7
/
miscutils.h
81 lines (64 loc) · 2.51 KB
/
miscutils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2013 Carlos Becker //
// Ecole Polytechnique Federale de Lausanne //
// Contact <[email protected]> for comments & bug reports //
// //
// This program is free software: you can redistribute it and/or modify //
// it under the terms of the version 3 of the GNU General Public License //
// as published by the Free Software Foundation. //
// //
// This program is distributed in the hope that it will be useful, but //
// WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
// General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////////
#ifndef _MISC_UTILS_H_
#define _MISC_UTILS_H_
#include <vector>
#include <sstream>
// some macros to print float/double
template<typename T>
inline const char * typeToString()
{
return "UNKNOWN";
}
template<>
inline const char * typeToString<float>()
{
return "Float";
}
template<>
inline const char * typeToString<double>()
{
return "Double";
}
// sample M indexes from 0..(N-1)
static void sampleWithoutReplacement( unsigned M, unsigned N, std::vector<unsigned> *idxs )
{
if (M > N) M = N;
unsigned max = N-1;
std::vector<unsigned> toSample(N);
for (unsigned i=0; i < N; i++)
toSample[i] = i;
idxs->resize(M);
for (unsigned i=0; i < M; i++)
{
const unsigned idx = (((unsigned long)rand()) * max) / RAND_MAX;
(*idxs)[i] = toSample[idx];
//printf("Idx: %d / %d\n", idx, toSample[idx]);
toSample[idx] = toSample[max];
max = max - 1;
}
}
// dummy func to convert something to string
template<typename T>
inline static std::string xToString( const T &val )
{
std::stringstream strStream;
strStream << val;
return strStream.str();
}
#endif