aboutsummaryrefslogtreecommitdiffstats
path: root/CommonLibs/Utils.h
blob: 0bc738e8dcdfd442e044b8e606260befb051828a (plain)
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
144
145
146
147
148
/*
* Copyright 2011 Range Networks, Inc.
* All Rights Reserved.
*
* This software is distributed under multiple licenses;
* see the COPYING file in the main directory for licensing
* information for this specific distribuion.
*
* This use of this software may be subject to additional restrictions.
* See the LEGAL file in the main directory for details.

    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.
*/

#ifndef GPRSUTILS_H
#define GPRSUTILS_H
#include <stdint.h>
#include <stdarg.h>
#include <string>
#include <string.h>
#include <math.h>		// for sqrtf
#include "Logger.h"


namespace Utils {

extern double timef();					// high resolution time
extern const std::string timestr();		// A timestamp to print in messages.
extern void sleepf(double howlong);	// high resolution sleep
extern int gcd(int x, int y);

// It is irritating to create a string just to interface to the brain-damaged
// C++ stream class, but this is only used for debug messages.
std::string format(const char *fmt, ...) __attribute__((format (printf,1,2)));

int cstrSplit(char *in, char **pargv,int maxargc, const char *splitchars=NULL);

// For classes with a text() function, provide a function to return a String,
// and also a standard << stream function that takes a pointer to the object.
// We dont provide the function that takes a reference to the object
// because it is too highly overloaded and generally doesnt work.
class Text2Str {
	public:
	virtual void text(std::ostream &os) const = 0;
	std::string str() const;
};
std::ostream& operator<<(std::ostream& os, const Text2Str *val);

#if 0
// Generic Activity Timer.  Lots of controls to make everybody happy.
class ATimer {
	double mStart;
	//bool mActive;
	double mLimitTime;
	public:
	ATimer() : mStart(0), mLimitTime(0) { }
	ATimer(double wLimitTime) : mStart(0), mLimitTime(wLimitTime) { }
	void start() { mStart=timef(); }
	void stop() { mStart=0; }
	bool active() { return !!mStart; }
	double elapsed() { return timef() - mStart; }
	bool expired() { return elapsed() > mLimitTime; }
};
#endif


struct BitSet {
	unsigned mBits;
	void setBit(unsigned whichbit) { mBits |= 1<<whichbit; }
	void clearBit(unsigned whichbit) { mBits &= ~(1<<whichbit); }
	unsigned getBit(unsigned whichbit) const { return mBits & (1<<whichbit); }
	bool isSet(unsigned whichbit) const { return mBits & (1<<whichbit); }
	unsigned bits() const { return mBits; }
	operator int(void) const { return mBits; }
	BitSet() { mBits = 0; }
};

// Store current, min, max and compute running average and standard deviation.
template<class Type> struct Statistic {
	Type mCurrent, mMin, mMax;		// min,max optional initialization so you can print before adding any values.
	unsigned mCnt;
	double mSum;
	//double mSum2;	// sum of squares.
	// (Type) cast needed in case Type is an enum, stupid language.
	Statistic() : mCurrent((Type)0), mMin((Type)0), mMax((Type)0), mCnt(0), mSum(0) /*,mSum2(0)*/ {}
	// Set the current value and add a statisical point.
	void addPoint(Type val) {
		mCurrent = val;
		if (mCnt == 0 || val < mMin) {mMin = val;}
		if (mCnt == 0 || val > mMax) {mMax = val;}
		mCnt++;
		mSum += val;
		//mSum2 += val * val;
	}
	Type getCurrent() const {	// Return current value.
		return mCnt ? mCurrent : 0;
	}
	double getAvg() const { 			// Return average.
		return mCnt==0 ? 0 : mSum/mCnt; 
	};
	//float getSD() const { 	// Return standard deviation.  Use low precision square root function.
	//	return mCnt==0 ? 0 : sqrtf(mCnt * mSum2 - mSum*mSum) / mCnt;
	//}

	void text(std::ostream &os) const {	// Print everything in parens.
		os << "("<<mCurrent;
		if (mMin != mMax) {	// Not point in printing all this stuff if min == max.
			os <<LOGVAR2("min",mMin)<<LOGVAR2("max",mMax)<<LOGVAR2("avg",getAvg());
			if (mCnt <= 999999) {
				os <<LOGVAR2("N",mCnt);
			} else { // Shorten this up:
				char buf[10], *ep;
				sprintf(buf,"%.3g",round(mCnt));
				if ((ep = strchr(buf,'e')) && ep[1] == '+') { strcpy(ep+1,ep+2); }
				os << LOGVAR2("N",buf);
			}
			// os<<LOGVAR2("sd",getSD())  standard deviation not interesting
		}
		os << ")";
		// " min="<<mMin <<" max="<<mMax <<format(" avg=%4g sd=%3g)",getAvg(),getSD());
	}
	// Not sure if this works:
	//std::string statStr() const {
	//	return (std::string)mCurrent + " min=" + (std::string) mMin +" max="+(string)mMax+ format(" avg=%4g sd=%3g",getAvg(),getSD());
	//}
};

// This I/O mechanism is so dumb:
std::ostream& operator<<(std::ostream& os, const Statistic<int> &stat);
std::ostream& operator<<(std::ostream& os, const Statistic<unsigned> &stat);
std::ostream& operator<<(std::ostream& os, const Statistic<float> &stat);
std::ostream& operator<<(std::ostream& os, const Statistic<double> &stat);


// Yes, they botched and left this out:
std::ostream& operator<<(std::ostream& os, std::ostringstream& ss);

std::ostream &osprintf(std::ostream &os, const char *fmt, ...) __attribute__((format (printf,2,3)));

std::string replaceAll(const std::string input, const std::string search, const std::string replace);

};	// namespace

using namespace Utils;

#endif