-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcodecpp.cpp
More file actions
128 lines (105 loc) · 2.1 KB
/
codecpp.cpp
File metadata and controls
128 lines (105 loc) · 2.1 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
#include "codecpp.h"
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
//----------- Implementation of class RunningStatics -----------//
Runstat::Runstat()
{
std::cout << " [TRACE] Object created Ok." << std::endl;
m_sum = 0.0;
m_sumsq = 0.0;
m_N = 0;
}
Runstat::~Runstat()
{
std::cout << " [TRACE] Object deleted OK" << std::endl;
}
void
Runstat::add(double x)
{
m_sum += x;
m_sumsq += x * x;
m_N++;
}
size_t
Runstat::size() const
{
return m_N;
}
void
Runstat::reset()
{
m_sum = 0.0;
m_sumsq = 0.0;
m_N = 0;
}
double
Runstat::mean() const
{
return m_sum / m_N;
}
double
Runstat::sdev() const
{
size_t N = m_N;
return std::sqrt((m_sumsq - m_sum * m_sum / N) / (N - 1.0));
}
//--------- C-Interface for class std::string ------------------//
hString string_new()
{
return new std::string{};
}
hString string_new1(const char* text)
{
return new std::string(text);
}
// Copy constructor
hString string_copy(hString self)
{
std::string* p = reinterpret_cast<std::string*>(self);
return new std::string(*p);
}
void string_del(hString self)
{
delete reinterpret_cast<std::string*>(self);
}
void string_add(hString self, const char* text)
{
auto p = reinterpret_cast<std::string*>(self);
*p = *p + text;
}
void string_disp(hString self, const char* name)
{
auto p = reinterpret_cast<std::string*>(self);
std::cout << name << " / std::string{ " << *p << "} " << std::endl;
}
//---------- C-Interface for class Runstat ---------------------//
pStat Runstat_new()
{
return new (std::nothrow) Runstat();
}
void Runstat_del(pStat self)
{
delete reinterpret_cast<Runstat*>(self);
}
void Runstat_add(pStat self, double x)
{
auto p = reinterpret_cast<Runstat*>(self);
p->add(x);
}
double Runstat_mean(pStat self)
{
Runstat* p = reinterpret_cast<Runstat*>(self);
return p->mean();
}
double Runstat_sdev(pStat self)
{
Runstat* p = reinterpret_cast<Runstat*>(self);
return p->sdev();
}
size_t Runstat_size(pStat self)
{
Runstat* p = reinterpret_cast<Runstat*>(self);
return p->size();
}