Created
March 26, 2025 10:24
-
-
Save MX-2000/810438464e32cbbc253fc1b30e8ee285 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef struct Log Log; | |
struct Log { | |
float score; | |
float moves; | |
}; | |
typedef struct LogBuffer LogBuffer; | |
struct LogBuffer { | |
Log *logs; | |
int length; | |
int idx; | |
}; | |
LogBuffer *allocate_logbuffer(int size) { | |
LogBuffer *logs = (LogBuffer *)calloc(1, sizeof(LogBuffer)); | |
logs->logs = (Log *)calloc(size, sizeof(Log)); | |
logs->length = size; | |
logs->idx = 0; | |
return logs; | |
} | |
void free_logbuffer(LogBuffer *buffer) { | |
free(buffer->logs); | |
free(buffer); | |
} | |
void add_log(LogBuffer *logs, Log *log) { | |
if (logs->idx == logs->length) { | |
return; | |
} | |
logs->logs[logs->idx] = *log; | |
logs->idx += 1; | |
// printf("Log: %f, %f \n", log->score, log->moves); | |
} | |
Log aggregate_and_clear(LogBuffer *logs) { | |
Log log = {0}; | |
if (logs->idx == 0) { | |
return log; | |
} | |
for (int i = 0; i < logs->idx; i++) { | |
log.score += logs->logs[i].score; | |
log.moves += logs->logs[i].moves; | |
} | |
log.score /= logs->idx; | |
log.moves /= logs->idx; | |
logs->idx = 0; | |
return log; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment