-
Notifications
You must be signed in to change notification settings - Fork 0
/
GLSH_Util.h
106 lines (84 loc) · 1.87 KB
/
GLSH_Util.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#ifndef GLSH_UTIL_H_
#define GLSH_UTIL_H_
#include <string>
#include <vector>
#include <sstream> // stringstream
#include <cstdlib> // rand and srand
#include <ctime> // time for random seed
namespace glsh {
//
// Random numbers
//
inline void InitRandom()
{
std::srand((unsigned)time(NULL) ^ (unsigned)std::rand());
}
inline void InitRandom(unsigned seed)
{
std::srand(seed);
}
inline float UnitRandom()
{
return std::rand() / (RAND_MAX + 1.0f);
}
inline float Random(float lower, float upper)
{
return lower + UnitRandom() * (upper - lower);
}
inline int RandomInt(int lower, int upper)
{
return lower + (int)(UnitRandom() * (upper - lower));
}
inline int RandomInt(int upper)
{
return RandomInt(0, upper);
}
//
// File handling stuff
//
std::string ReadTextFile(const std::string& fname);
//
// string handling stuff
//
//
// break string up into non-whitespace tokens
//
std::vector<std::string> Tokenize(const std::string& str);
//
// Split a string using a character delimeter
//
// Split("ab&c&def", '&') --> { "ab", "c", "def" }
// Split("c&", '&') --> { "c", "" }
// Split("&c", '&') --> { "", "c" }
// Split("&", '&') --> { "", "" }
// Split("&&", '&') --> { "", "", "" }
// Split("abc", '&') --> { "abc" }
// Split("", '&') --> { }
//
std::vector<std::string> Split(const std::string& str, char delimiter);
template <typename T>
std::string ToString(const T& value)
{
std::ostringstream ss;
ss << value;
return ss.str();
}
template <typename T>
T FromString(const std::string& str)
{
std::istringstream ss(str);
T value;
ss >> value;
return value;
}
inline bool StringBeginsWith(const std::string& s, const std::string& prefix)
{
return s.compare(0, prefix.length(), prefix) == 0;
}
// magic
inline bool IsPowerOf2(int x)
{
return x > 0 && !(x & (x - 1));
}
}
#endif