-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMisc.cc
82 lines (62 loc) · 1.84 KB
/
Misc.cc
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
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <string>
#include <ctime> //For the gen_time_random().
void gen_random(std::string *s, const int len) {
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
(*s).clear();
for(int i=0; i<len; ++i) (*s) += alphanum[rand()%(sizeof(alphanum)-1)];
return;
}
void seed_time(void){
//Seed the rand() function with the current time.
srand(time(NULL));
return;
}
std::string inttostring(long int number){
std::stringstream ss;
ss << number;
return ss.str();
}
std::string floattostring(float number){
std::stringstream ss;
ss << number;
return ss.str();
}
template <class T>
std::string Xtostring(T numb){
std::stringstream ss;
ss << numb;
return ss.str();
}
template std::string Xtostring<int>(int);
template std::string Xtostring<long int>(long int);
template std::string Xtostring<float>(float);
template std::string Xtostring<double>(double);
template <class T>
T stringtoX(std::string text){
std::stringstream ss(text);
T temp;
ss >> temp;
return temp;
}
template float stringtoX<float>(std::string);
template double stringtoX<double>(std::string);
template int stringtoX<int>(std::string);
template unsigned int stringtoX<unsigned int>(std::string);
template long int stringtoX<long int>(std::string);
//Turns a string into a number suitable for seeding a PRNG.
unsigned long simple_hash(std::string &str){
//See the excellent resource at http://www.cse.yorku.ca/~oz/hash.html . A bunch of nearly one-liners.
//This is the djb2 algorithm.
unsigned long hash = 5381;
int c;
if(str.empty()) return 0;
const char *spot = str.data();
while(c = *spot++) hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}