-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_buffer.c
768 lines (663 loc) · 24.7 KB
/
single_buffer.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>
#include <time.h>
/* PARAMETERS */
// THRESHOLD
#define THRESHOLD 1000 // Local condition
// DELTA
#define DELTA 1000 // Local condition
// AGGREGATE
/**
* AVG = 0
* SUM = 1
**/
#define AGGREGATE 1 // Specify aggregation function
#define AGGREGATE_THRESHOLD 1000 // Local condition
#define MAX_CHARS 256 // Max admitted characters in a line representing event
#define MAX_FRAMES 120000 // Max admitted size of multi-buffer, pay attention to choose an evict policy that does not cause overflow
#define MAX_TUPLES 200000
#define DEBUG false
// tuple definition
typedef struct Data {
long timestamp;
long key;
double A; // value
} tuple;
// buffer element definition
typedef struct Node {
tuple data;
struct Node* next;
} node;
// context definition
typedef struct Context {
int frame_type;
double v;
int count;
bool start;
} context;
// window definition
typedef struct Window {
long size;
long t_start;
long t_end;
node* current;
node* tail;
context* context;
long index;
} window;
int delay_execution(int ms){
const struct timespec ts = {
ms / 1000, /* seconds */
(ms % 1000) * 1000 * 1000 /* nano seconds */
};
return nanosleep(&ts, NULL);
}
/**
* This function decides which aggregation function has to be applied to the current frame
* @param agg_function aggregation to apply
* @param buffer current frame
* @return the result of the aggregation
*/
double aggregation(int agg_function, node* buffer);
/**** AGGREGATION FUNCTIONS ****/
double avg(node* buffer);
double sum(node* buffer);
/*******************************/
/** FRAMES FUNCTIONS **/
// PREDICATES
// The predicate functions trigger the actions depending on the framing scheme.
bool close_pred(tuple data, context *pContext);
bool update_pred(tuple data, context *pContext);
bool open_pred(tuple data, context *pContext);
// FUNCTIONS
// Concrete implementation of the framing action
context *close(tuple tuple, context *C, node* buffer);
context *update(tuple tuple, context *C, node **buffer, node *list) ;
context *open(tuple tuple, context *C, node **buffer);
/**********************/
/** BUFFER FUNCTIONS **/
void enqueue(node** last, tuple data);
void insertInOrder(window frames[], long W, window newWindow, int startIdx) ;
void free_buffer(window frames[], int num_tuples);
void free_temp_buffer(node* head);
void print_buffer(node* head);
/**********************/
/** SECRET FUNCTIONS **/
bool tick(tuple data);
int extract_data(window frames[], int startIdx, int W, long targetTimestamp);
/**********************/
/**
* Perform window eviction when frame is closed
* @param buffer pointer to the head of list representing the current frame to be evicted
*/
void evict(window window, node* content){
}
bool tick(tuple data) {
if (data.timestamp >= 0) return true;
else return false;
}
int compareTimestamp(const void* a, const void* b) {
const node* nodeA = ((window*)a)->current;
const node* nodeB = ((window*)b)->current;
return (nodeA->data.timestamp - nodeB->data.timestamp);
}
int customBinarySearch(window frames[], int startIdx, int endIdx, long targetTimestamp) {
while (startIdx <= endIdx) {
int mid = startIdx + (endIdx - startIdx) / 2;
delay_execution(10);
if (frames[mid].current->data.timestamp == targetTimestamp) {
return mid; // Element found
}
if (frames[mid].current->data.timestamp < targetTimestamp) {
startIdx = mid + 1;
} else {
endIdx = mid - 1;
}
}
return -1; // Element not found
}
int extract_data(window frames[], int startIdx, int W, long targetTimestamp) {
window key;
key.current = (node*)malloc(sizeof(node));
key.current->data.timestamp = targetTimestamp;
window* result = (window*)bsearch(&key, &frames[startIdx], W - startIdx, sizeof(window), compareTimestamp);
free(key.current);
if (result != NULL) {
return result - frames;
}
return -1;
}
/**
* Insert a tuple in the buffer
* @param last pointer to last element of the buffer
* @param data the tuple to be appended
*/
void enqueue(node** last, tuple data) {
node* new_node = (node*)malloc(sizeof(node));
new_node->data = data;
new_node->next = NULL;
if (*last == NULL) {
*last = new_node;
return;
}
node* last_node = *last;
last_node->next = new_node;
*last = new_node;
}
void insertInOrder(window frames[], long W, window newWindow, int startIdx) {
if (W >= MAX_FRAMES) {
printf("Error: Array is full, cannot insert.\n");
return;
}
int insertIndex = 0;
while (insertIndex < W && frames[insertIndex].current->data.timestamp < newWindow.current->data.timestamp) {
insertIndex++;
}
// move the elements
for (long i = W; i > insertIndex; i--) {
frames[i] = frames[i - 1];
}
frames[insertIndex] = newWindow;
}
void heapify(window arr[], int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && compareTimestamp(&arr[left], &arr[largest]) > 0) {
largest = left;
}
if (right < n && compareTimestamp(&arr[right], &arr[largest]) > 0) {
largest = right;
}
if (largest != i) {
window temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}
void heapSort(window arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
for (int i = n - 1; i > 0; i--) {
window temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
node* newNode(tuple data){
node* new_node = (node*)malloc(sizeof(node));
new_node->data = data;
new_node->next = NULL;
return new_node;
}
void free_buffer(window frames[], int num_tuples) {
for(int i = 0; i < num_tuples; i++){
free(frames[i].current);
}
}
void free_temp_buffer(node* head) {
node* current = head;
while (current != NULL) {
node* next = current->next;
free(current);
current = next;
}
}
int main(int argc, char *argv[]) {
if (argc != 8) {
perror("Usage: <input file path> <Frame type (THRESHOLD = 0 | DELTA = 1 | AGGREGATE = 2)> <Report policy (ON CLOSE = 0 | ON UPDATE = 1)> <Order policy (IN ORDER = 0 | OUT OF ORDER = 1)> <Buffer type (0 = SINGLE BUFFER | 1 = MULTI BUFFER)\n ");
return 1;
}
char *file_path = argv[1];
/**
* THRESHOLD = 0 -> Detect time periods where a specified attribute is over under a specified threshold.
* DELTA = 1 -> A new frame starts whenever the value of a particular attribute changes by more than an amount.
* AGGREGATE = 2 -> End a frame when an aggregate of the values of a specified attribute within the frame exceeds a threshold.
**/
int FRAME = atoi(argv[2]);
/**
* ON CLOSE = 0 -> Report every time a frame is closed
* ON UPDATE = 1 -> Report every time a frame is updated
**/
int REPORT_POLICY = atoi(argv[3]);
/**
* IN ORDER = 0 -> Every tuple is expected to arrive with increasing timestamp
* OUT OF ORDER (eager sort) = 1 -> Tuple can arrive with out of order timestamps, sorting is executed eagerly
* OUT OF ORDER (lazy sort) = 2 -> Tuple can arrive with out of order timestamps, sorting is executed lazily
**/
int ORDER_POLICY = atoi(argv[4]);
/**
* SINGLE BUFFER = 0
* MULTI BUFFER = 1
**/
int BUFFER_TYPE = atoi(argv[5]);
/**
* EVICTION POLICY PARAMETERS
*/
// SINGLE BUFFER: after X frames created / MULTI BUFFER: after X ms passed
// evict first Y frames
// Condition: SB Y < X, MB: in X ms no more than Y frames created
int X = atoi(argv[6]);
int Y = atoi(argv[7]);
// TUPLES
int num_tuples = 0;
char line[MAX_CHARS];
// BUFFER
node* head_buffer = NULL;
node* tail_buffer = NULL;
node* buffer_iter = NULL;
node* buffer = NULL;
node* current = NULL;
node* tail = NULL;
bool head;
// CONTEXT
context* C = (context*)malloc(sizeof(context));
C->frame_type = FRAME;
C->count = 0;
C->start = false;
C->v = -1;
tuple curr_tuple;
window frames[MAX_FRAMES];
tuple tuples[MAX_TUPLES];
long frames_count = 1;
long W = 0;
long evict_head;
node *new_buffer_head;
long multi_buffer_head = 0;
long single_buffer_head = 0;
long frames_count_iterator = 0;
long multi_buffer_head_iterator = 0;
int buffer_size = 0;
bool first =true;
// SECRET
window curr_window;
window report_window;
node* content = NULL;
int last=-1; // used to propagate last operation for report
// BENCHMARKING
clock_t begin_add;
clock_t begin_scope;
clock_t begin_content;
clock_t begin_evict;
clock_t end_add;
clock_t end_scope;
clock_t end_content;
clock_t end_evict;
double time_spent_add;
double time_spent_scope;
double time_spent_content;
double time_spent_evict;
if (BUFFER_TYPE == 0) printf("add,scope,content,evict,n\n");
if (BUFFER_TYPE == 1) printf("scope,add,content,evict,n\n");
// parse CSV
FILE *file = fopen(file_path, "r");
if (file == NULL) {
perror("Error loading input file");
return 1;
}
// ignore header
if (fgets(line, 256, file) == NULL) {
perror("Error reading CSV header");
fclose(file);
return 1;
}
// process stream
while (fgets(line, 256, file) != NULL) {
char *token = strtok(line, ",");
int count = 0;
// read tuple from file and store attributes
tuple data;
while (token != NULL) {
if (count == 0) {
data.timestamp = strtol(token, NULL, 10);
} else if (count == 1) {
data.key = strtol(token, NULL, 10);
} else if (count == 2) {
data.A = strtod(token, NULL);
}
token = strtok(NULL, ",");
count++;
}
if (BUFFER_TYPE == 0){ // Single Buffer
/** Start Add **/
begin_add = clock();
if (ORDER_POLICY == 0 || ORDER_POLICY == 2) { // In-Order & Out-of-Order (lazy sort)
frames[num_tuples].current = newNode(data);
}
else if (ORDER_POLICY == 1) { // Out-of-Order (eager sort)
window new_window;
new_window.current = newNode(data);
insertInOrder(frames, num_tuples, new_window, single_buffer_head);
}
end_add = clock();
time_spent_add = (double) (end_add - begin_add) *1000 / CLOCKS_PER_SEC;
printf("%f,", time_spent_add);
/** End Add **/
}
num_tuples++; // index of the tail of the buffer
if (tick(data)) {
if (BUFFER_TYPE == 0){ // Single Buffer
/** Start Scope **/
begin_scope = clock();
if (ORDER_POLICY == 2){ // Out-of-Order (lazy sort)
heapSort(frames + single_buffer_head, num_tuples - single_buffer_head);
}
// Frame Operator
head = true;
curr_window.size = 0;
buffer = NULL;
tail = NULL;
long frames_count_tmp = 0;
long iterator = single_buffer_head;
while (iterator < num_tuples) {
curr_tuple = frames[iterator].current->data;
// process tuple
last = -1;
if (close_pred(curr_tuple, C)) {
C = close(curr_tuple, C, current);
last = 0;
if (data.timestamp >= curr_window.t_start){
report_window.t_start = curr_window.t_start;
//report_window.t_end = curr_tuple.timestamp;
report_window.size = curr_window.size;
}
}
if (update_pred(curr_tuple, C)) {
C = update(curr_tuple, C, &tail, current);
curr_window.size++;
last = 1;
report_window.t_start = curr_window.t_start;
report_window.t_end = curr_tuple.timestamp;
report_window.size = curr_window.size;
}
if (open_pred(curr_tuple, C)) {
C = open(curr_tuple, C, &tail);
current = tail;
curr_window.size = 1;
if (head) { // save pointer to first element of the list representing the buffer
buffer = tail;
head = false;
}
curr_window.t_start = curr_tuple.timestamp;
frames_count_tmp++;
if (frames_count_tmp == Y+1) {
evict_head = curr_window.t_start;
}
}
//curr_window.t_end = curr_tuple.timestamp;
iterator++;
}
frames_count = frames_count_tmp;
C->count = 0;
C->start = false;
C->v = -1;
end_scope = clock();
time_spent_scope = (double)(end_scope - begin_scope) *1000 / CLOCKS_PER_SEC;
printf("%f,", time_spent_scope);
/** End Scope **/
} else if (BUFFER_TYPE == 1){ // Multi Buffer
/** Start Scope **/
begin_scope = clock();
if (frames_count == 0 && first) {
current = NULL;
tail = NULL;
curr_window.index = -1;
first = false;
} else {
if (multi_buffer_head < frames_count) {
for (long i = multi_buffer_head; i < frames_count; i++) {
if (data.timestamp >= frames[i].t_start &&
(data.timestamp <= frames[i].t_end || frames[i].t_end == -1)) {
curr_window.index = i;
current = frames[i].current;
tail = frames[i].tail;
}
}
} else if (multi_buffer_head >= frames_count) {
for (long i = multi_buffer_head; i < MAX_FRAMES; i++) {
if (data.timestamp >= frames[i].t_start &&
(data.timestamp <= frames[i].t_end || frames[i].t_end == -1)) {
curr_window.index = i;
current = frames[i].current;
tail = frames[i].tail;
}
}
for (long i = 0; i < frames_count; i++) {
if (data.timestamp >= frames[i].t_start &&
(data.timestamp <= frames[i].t_end || frames[i].t_end == -1)) {
curr_window.index = i;
current = frames[i].current;
tail = frames[i].tail;
}
}
}
}
end_scope = clock();
time_spent_scope = (double)(end_scope - begin_scope) *1000 / CLOCKS_PER_SEC;
printf("%f,", time_spent_scope);
/** End Scope **/
/** Start Add **/
begin_add = clock();
curr_tuple = data;
last = -1;
if (close_pred(curr_tuple, C)) {
C = close(curr_tuple, C, current);
last = 0;
frames[curr_window.index].t_end = curr_tuple.timestamp;
report_window.index = curr_window.index;
}
if (update_pred(curr_tuple, C)) {
C = update(curr_tuple, C, &tail, current);
last = 1;
frames[curr_window.index].size++;
frames[curr_window.index].tail = tail;
report_window.index = curr_window.index;
}
if (open_pred(curr_tuple, C)) {
C = open(curr_tuple, C, &tail);
current = tail;
if (frames_count == MAX_FRAMES) {
frames_count = 0;
curr_window.index = -1;
}
curr_window.index++;
frames[curr_window.index].size = 1;
frames[curr_window.index].current = current;
frames[curr_window.index].tail = tail;
frames[curr_window.index].t_start = curr_tuple.timestamp;
frames[curr_window.index].t_end = -1;
frames_count++;
W++;
}
end_add = clock();
time_spent_add = (double) (end_add - begin_add) *1000 / CLOCKS_PER_SEC;
printf("%f,", time_spent_add);
/** End Add **/
} else printf("Buffer type %d not recognized", BUFFER_TYPE);
/** Start Report **/
if (REPORT_POLICY == last) {
/** End Report **/
/** Start Content **/
begin_content = clock();
int content_index;
if (BUFFER_TYPE == 0) content_index = customBinarySearch(frames, single_buffer_head, num_tuples-1, report_window.t_start); // binary search
if (BUFFER_TYPE == 1) content = frames[report_window.index].current;
end_content = clock();
time_spent_content = (double)(end_content - begin_content) *1000 / CLOCKS_PER_SEC;
printf("%f,", time_spent_content);
/** End Content **/
/** DEBUG CODE **/
// print content
if (DEBUG) {
if (BUFFER_TYPE == 0) {
printf("\n %ld: [%ld, %ld] (size %ld) {content index = %d} -> ", frames_count, report_window.t_start, report_window.t_end, report_window.size, content_index);
for (int i = content_index; i < report_window.size + content_index; i++){
if (i == MAX_FRAMES) i = 0;
printf("(ts: %ld, value: %f) ", frames[i].current->data.timestamp, frames[i].current->data.A);
}
}
if (BUFFER_TYPE == 1) {
printf("\ntot frames: %ld, curr frame size: %ld\n", ((frames_count - multi_buffer_head)%MAX_FRAMES+MAX_FRAMES)%MAX_FRAMES, frames[report_window.index].size);
int print_count = 0;
printf("frame [%ld, %ld] -> ", frames[report_window.index].t_start, frames[report_window.index].t_end);
//node *iterator = frames[curr_window.index].current;
while (content != NULL && print_count < frames[report_window.index].size) {
printf("(ts: %ld, value: %f) ", content->data.timestamp,
content->data.A);
content = content->next;
print_count++;
}
}
printf("\n");
}
/** END DEBUG CODE **/
} else printf("-1,");
if (BUFFER_TYPE == 0) free_temp_buffer(buffer);
if (X != -1) {
if (BUFFER_TYPE == 0) { // SINGLE BUFFER: find a tuple in the buffer that is the pointer to the head of the buffer after eviction
if (frames_count % X == 0) { // check eviction policy
// Policy: after X frames are created, evict first Y frames
/** Start Evict **/
begin_evict = clock();
single_buffer_head = customBinarySearch(frames, single_buffer_head, num_tuples, evict_head);
// todo: implement binary search on circular array to get infinite available memory
frames_count = X - Y;
end_evict = clock();
time_spent_evict = (double)(end_evict - begin_evict) *1000 / CLOCKS_PER_SEC;
printf("%f,%d\n", time_spent_evict,num_tuples);
/** End Evict **/
} else printf("-1,%d\n", num_tuples);
}
if (BUFFER_TYPE == 1) {
if (data.timestamp % X == 0) { // check eviction policy
begin_evict = clock();
/** Start Evict **/
multi_buffer_head = (multi_buffer_head + Y) % MAX_FRAMES;
end_evict = clock();
time_spent_evict = (double)(end_evict - begin_evict) *1000 / CLOCKS_PER_SEC;
printf("%f,%ld\n", time_spent_evict,W);
/** End Evict **/
} else printf("-1,%ld\n", W);
}
}
else if (BUFFER_TYPE == 1) printf("-1,%ld\n", W);
else if (BUFFER_TYPE == 0) printf("-1,%d\n", num_tuples);
}
}
fclose(file); // close input file stream
// free up memory
if (BUFFER_TYPE == 0) free_buffer(frames, num_tuples);
if (BUFFER_TYPE == 1) free_temp_buffer(frames[0].current);
free(C);
return 0;
}
// Open a new frame and insert the current processed tuple.
context *open(tuple tuple, context *C, node **buffer) {
enqueue(buffer, tuple);
switch (C->frame_type) {
case 0:
C->count++;
return C;
case 1:
C->start = true;
C->v = tuple.A;
return C;
case 2:
C->v = tuple.A; // aggregation function on a single value corresponds to that value
C->start = true;
return C;
}
}
// Update the current frame, extends it to include the current processed tuple.
context *update(tuple tuple, context *C, node **buffer, node *list) {
enqueue(buffer, tuple);
switch (C->frame_type) {
case 0:
C->count++;
return C;
case 1:
return C;
case 2:
C->v = aggregation(AGGREGATE, list);
return C;
}
}
// Close the current frame and check for global conditions to eventually evict.
// Removes the evicted/discarded frame from the buffer.
context *close(tuple tuple, context *C, node* buffer) {
switch (C->frame_type) {
case 0:
C->count = 0;
return C;
case 1:
C->start = false;
return C;
case 2:
C->start = false;
return C;
}
}
bool open_pred(tuple tuple, context *C) {
switch (C->frame_type) {
case 0: return (tuple.A >= THRESHOLD && C->count == 0);
case 1: return (!(C->start));
case 2: return (!(C->start));
}
}
bool update_pred(tuple tuple, context *C) {
switch (C->frame_type) {
case 0: return (tuple.A >= THRESHOLD && C->count > 0);
case 1: return (fabs(C->v - tuple.A) < DELTA && C->start);
case 2: return (C->v < AGGREGATE_THRESHOLD && C->start);
}
}
bool close_pred(tuple tuple, context *C) {
switch (C->frame_type) {
case 0: return (tuple.A < THRESHOLD && C->count > 0);
case 1: return (fabs(C->v - tuple.A) >= DELTA && C->start);
case 2: return (C->v >= AGGREGATE_THRESHOLD && C->start);
}
}
double aggregation(int agg_function, node* buffer){
switch (agg_function) {
case 0: return avg(buffer);
case 1: return sum(buffer);
default:
perror("Error while selecting aggregation function");
return -1;
}
}
double avg(node* buffer) {
if (buffer == NULL) {
perror("Empty buffer, impossible to compute aggregate function\n");
return -1;
}
double sum = 0;
int count = 0;
node* current = buffer;
while (current != NULL) {
sum += current->data.A;
count++;
current = current->next;
}
return (double) sum / count;
}
double sum(node* buffer) {
if (buffer == NULL) {
perror("Empty buffer, impossible to compute aggregate function\n");
return -1;
}
double sum = 0;
node* current = buffer;
while (current != NULL) {
sum += current->data.A;
current = current->next;
}
return sum;
}