File: data_store_int.cpp

package info (click to toggle)
entropybroker 2.9-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,600 kB
  • sloc: cpp: 14,386; sh: 934; makefile: 188; java: 148; perl: 12
file content (168 lines) | stat: -rw-r--r-- 3,926 bytes parent folder | download | duplicates (3)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <string.h>

#include "error.h"
#include "utils.h"
#include "data_store_int.h"

data_store_int::data_store_int(int n_samples_in, int interval_in) : n_samples(n_samples_in), interval(interval_in)
{
	values = (long long int *)calloc(n_samples, sizeof(long long int));
	counts = (int *)calloc(n_samples, sizeof(int));
	valid = (bool *)calloc(n_samples, sizeof(bool));

	cur_t = 0;
}

data_store_int::data_store_int(std::string file)
{
	FILE *fh = fopen(file.c_str(), "r");
	if (!fh)
		error_exit("failed to open %s", file.c_str());

	if (!get_int(fh, &n_samples))
		error_exit("short read on %s", file.c_str());
	if (!get_int(fh, &interval))
		error_exit("short read on %s", file.c_str());
	if (!get_int(fh, &cur_t))
		error_exit("short read on %s", file.c_str());

	values = (long long int *)calloc(n_samples, sizeof(long long int));
	counts = (int *)calloc(n_samples, sizeof(int));
	valid = (bool *)calloc(n_samples, sizeof(bool));

	for(int index=0; index<n_samples; index++)
	{
		if (!get_long_long_int(fh, &values[index]))
			error_exit("short read on %s", file.c_str());
		if (!get_int(fh, &counts[index]))
			error_exit("short read on %s", file.c_str());
		if (!get_bool(fh, &valid[index]))
			error_exit("short read on %s", file.c_str());
	}

	fclose(fh);
}

data_store_int::~data_store_int()
{
	free(counts);
	free(values);
	free(valid);
}

void data_store_int::dump(std::string file)
{
	FILE *fh = fopen(file.c_str(), "wb");
	if (!fh)
		error_exit("faile to create file %s", file.c_str());

	put_int(fh, n_samples);
	put_int(fh, interval);
	put_int(fh, cur_t);

	for(int index=0; index<n_samples; index++)
	{
		put_long_long_int(fh, values[index]);
		put_int(fh, counts[index]);
		put_bool(fh, valid[index]);
	}

	fclose(fh);
}

int data_store_int::init_data(int t)
{
	int cur_index = (t / interval) % n_samples;
	int prev_index = cur_t != -1 ? (cur_t / interval) % n_samples : -1;

	if (cur_index != prev_index && prev_index != -1)
	{
		// if the interval between now and previous value
		// is more than a second, then invalidate values
		// in between
		if (cur_index > prev_index)
		{
			int n = (cur_index - prev_index) - 1;

			if (n > 0)
			{
				memset(&values[prev_index + 1], 0x00, sizeof(long long int) * n);
				memset(&counts[prev_index + 1], 0x00, sizeof(int) * n);
				memset(&valid[prev_index + 1], 0x00, sizeof(bool) * n);
			}
		}
		else
		{
			int n = n_samples - prev_index;
			memset(&values[prev_index], 0x00, sizeof(long long int) * n);
			memset(&counts[prev_index], 0x00, sizeof(int) * n);
			memset(&valid[prev_index], 0x00, sizeof(bool) * n);

			memset(&values[0], 0x00, sizeof(long long int) * cur_index);
			memset(&counts[0], 0x00, sizeof(int) * cur_index);
			memset(&valid[0], 0x00, sizeof(bool) * cur_index);
		}

		values[cur_index] = 0;
		counts[cur_index] = 0;
		valid[cur_index] = true;
	}

	cur_t = t;

	return cur_index;
}

void data_store_int::add_avg(int t, int value)
{
	int index = init_data(t);

	values[index] += value;
	counts[index]++;
}

void data_store_int::add_sum(int t, int value)
{
	int index = init_data(t);

	values[index] += value;
	counts[index] = 1;
}

bool data_store_int::get(int index, double *value)
{
	if (counts[index])
	{
		*value = double(values[index]) / double(counts[index]);

		return true;
	}

	return false;
}

void data_store_int::get_data(long int **t, double **v, int *n)
{
	int offset_index = (cur_t / interval) % n_samples;

	*t = (long int *)malloc(sizeof(long int) * n_samples);
	*v = (double *)malloc(sizeof(double) * n_samples);
	*n = 0;

	long int start_t = cur_t - n_samples * interval;
	for(int index=0; index<n_samples; index++)
	{
		int cur_index = (offset_index + 1 + index) % n_samples;

		if (valid[cur_index])
		{
			(*t)[*n] = start_t + index * interval;
			(*v)[*n] = double(values[cur_index]) / double(counts[cur_index]);
			(*n)++;
		}
	}
}