-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathraw.c
61 lines (54 loc) · 1.49 KB
/
raw.c
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
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.
#include "bench.h"
static void raw_calloc(Raw *raw_) {
raw_->data_size_ = kNumData;
raw_->data_ = calloc(sizeof(double), raw_->data_size_);
raw_->pos_ = 0;
}
static void raw_realloc(Raw *raw_) {
raw_->data_size_ *= 2;
raw_->data_ = realloc(raw_->data_, sizeof(double) * raw_->data_size_);
if (!raw_->data_) {
fprintf(stderr, "realloc failed\n");
exit(1);
}
}
void raw_clear(Raw *raw_) {
if (raw_->data_)
free(raw_->data_);
raw_calloc(raw_);
}
void raw_add(Raw *raw_, double value) {
if (!raw_->data_)
raw_calloc(raw_);
if (raw_->data_size_ < raw_->pos_ + 1)
raw_realloc(raw_);
raw_->data_[raw_->pos_] = value;
raw_->pos_++;
}
char* raw_to_string(Raw *raw_) {
if (!raw_->data_)
raw_calloc(raw_);
size_t r_size = 1024;
char *r = malloc(sizeof(char) * r_size);
strcpy(r, "");
char buf[200];
for (int i = 0; i < raw_->pos_; i++) {
snprintf(buf, sizeof(buf), "%.4f\n", raw_->data_[i]);
if (r_size < strlen(r) + strlen(buf)) {
r = realloc(r, r_size * 2);
r_size *= 2;
}
strcat(r, buf);
}
return r;
}
void raw_print(FILE *stream, Raw *raw_) {
if (!raw_->data_)
raw_calloc(raw_);
fprintf(stream, "num,time\n");
for (int i = 0; i < raw_->pos_; i++)
fprintf(stream, "%d,%.4f\n", i, raw_->data_[i]);
}