Utility.h
4.03 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#ifndef UTILITY_H
#define UTILITY_H
#include <iostream>
#include <sstream>
#include <fstream>
#include <cassert>
#include <vector>
#include <stdexcept>
#include <map>
#include <set>
//#include <unordered_map> // need compiler option -std=c++0x
#include <tr1/unordered_map>
#include <queue>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cassert>
#include <ctime>
#include <iomanip>
#include <limits>
using namespace std;
///Returns a random number between 0 and 1
inline double random01(){
return (double)rand()/(double)RAND_MAX;
}
///Returns a random integer between 0 and the argument aMax
inline unsigned randomUnsigned(unsigned aMax){
return (unsigned)(rand() % aMax);
}
template <class InType>
void PermuteVector(vector<InType> & t)
{
for (unsigned i=0;i<t.size();++i){
unsigned j=randomUnsigned(t.size());
swap(t[i],t[j]);
}
}
///Implements a type converter via streams
template <class OutType, class InType>
OutType stream_cast(const InType & t)
{
stringstream ss;
ss << t; // first insert value to stream
OutType result; // value will be converted to OutType
ss >> result; // write value to result
return result;
}
///Return an integer hash value for a given input integer in a given domain range
inline int IntHashSimple(int key, int aModulo)
{
key = ~key + (key << 15); // key = (key << 15) - key - 1;
key = key ^ (key >> 12);
key = key + (key << 2);
key = key ^ (key >> 4);
key = key * 2057; // key = (key + (key << 3)) + (key << 11);
key = key ^ (key >> 16);
return key%aModulo;
}
///Return an integer hash value for a given input integer in a given domain range given an additional seed to select the random hash function
inline int IntHash(int key, int aModulo, unsigned aSeed){
const double A=sqrt(2)-1;
return IntHashSimple(key*(aSeed+1)*A,aModulo);
}
unsigned RSHash(const string& str);
unsigned RSHash(const vector<unsigned>& aV);
unsigned APHash(const string& str);
unsigned APHash(const vector<unsigned>& aV);
unsigned HashFunc(const string& str, unsigned aBitMask=2147483647);
unsigned HashFunc(const vector<unsigned>& aList, unsigned aBitMask=2147483647);
//------------------------------------------------------------------------------------------------------------------------
///Returns the time between the creation and destruction of an object of TimerClass
class TimerClass {
public:
TimerClass();
~TimerClass();
private:
std::time_t start_sec;
std::clock_t start;
};
//------------------------------------------------------------------------------------------------------------------------
///Plots the increase of an internal counter (which has to be
///explicitely increased by calling the Count member). Returns also
///the time elapsed between the creation and the destruction of an
///object of ProgressBar.
class ProgressBar{
public:
ProgressBar(unsigned aStep=100);
~ProgressBar();
void Begin();
void Count();
unsigned End();
private:
unsigned mStep;
unsigned mCounter;
TimerClass mTimer;
};
//------------------------------------------------------------------------------------------------------------------------
///Implements safe access policies to a vector container and offers
///members to compute various statistical estimators.
class VectorClass{
friend ostream& operator<<(ostream& out,const VectorClass& aV);
public:
VectorClass();
VectorClass(unsigned aSize);
void operator=(const VectorClass& aVector);
VectorClass(const VectorClass& aVector);
VectorClass(const vector<double>& aVector);
void Init(unsigned aSize);
void Import(const string& aFileName);
void Clear();
unsigned Size()const;
ostream& Output(ostream& out)const;
void PushBack(double aValue);
double& operator[](unsigned i);
double operator[](unsigned i)const;
double Sum()const;
double Mean()const;
double StandardDeviation()const;
double Order(double aOrder)const;
double Median()const;
double MedianAbsoluteDifference()const;
double Min()const;
double Max()const;
VectorClass RemoveNulls();
ostream& OutputStatistics(ostream& out);
protected:
vector<double> mV;
};
#endif