Skip to content

Instantly share code, notes, and snippets.

@odeblic
Created July 30, 2017 07:56
Show Gist options
  • Save odeblic/e824712aa2ec9f9e5131a37c3ec04081 to your computer and use it in GitHub Desktop.
Save odeblic/e824712aa2ec9f9e5131a37c3ec04081 to your computer and use it in GitHub Desktop.
To be tested...
#include <iostream>
#include <limits>
#include <math.h>
// x[i]=i++ + 1;
int array[5][2] = {{1, 2, 3}, {10, 20, 30}};
int x = array[++i, ++j];
using namespace std;
struct Object
{
Object()
{
std::cout << "Object::Object()" << endl;
}
~Object()
{
std::cout << "Object::~Object()" << '\n';
}
};
f( new X(i++), new Y(i) );
Object * obj = new Object();
obj.~Object();
class Array {
private:
int* data;
unsigned size;
int lBound, hBound;
public:
Array(int low, int high)
:size(high-low+1), lBound(low), hBound(high), data(new int[size]) {}
};
auto x = f() + g() + h();
template <typename T>
struct Statistics
{
T min;
T max;
T cumul;
uint64_t count;
Statistics()
: min(std::numeric_limits<T>::max()), max(std::numeric_limits<T>::min()), cumul(0), count(0)
{
}
void add(T datapoint)
{
count++;
cumul += datapoint;
if (datapoint > max)
{
max = datapoint;
}
if (datapoint < min)
{
min = datapoint;
}
}
T getRange() const
{
return max - min;
}
T getAverage() const
{
return cumul / count;
}
void print() const
{
std::cout << " min: " << min << std::endl;
std::cout << " max: " << max << std::endl;
std::cout << " cumul: " << cumul << std::endl;
std::cout << " count: " << count << std::endl;
std::cout << " range: " << getRange() << std::endl;
std::cout << " average: " << getAverage() << std::endl;
}
};
void testStats()
{
Statistics<double> stats;
for (auto x : {1.5, 6., -1., 2.8, -3.1})
{
stats.add(x);
}
stats.print();
}
void testSlicedStats()
{
Statistics<double> stats[10];
for (auto x : {7.9, -4.5, -1002.4, 28.2, -3.7, 99.9, 7563.14})
{
size_t i = log10(x);
stats[i].add(x);
}
for (auto s : stats)
{
std::cout << "==================" << std::endl;
s.print();
}
}
int main()
{
testStats();
testSlicedStats();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment