diff options
| author | steven-na <noreply.github@stvnc.dev> | 2026-08-06 02:36:01 -0700 |
|---|---|---|
| committer | steven-na <noreply.github@stvnc.dev> | 2026-08-06 02:36:01 -0700 |
| commit | 5d575f8cd99e9bc064b3d998f17b194a818e3546 (patch) | |
| tree | 8a8e1485dda924aeefb5428e26dbcca74f74721e /src/slidingwindow.c | |
| parent | d07b96b9914e706630dc1d2807ecfa9e104945d5 (diff) | |
Sliding window array implementation
Diffstat (limited to 'src/slidingwindow.c')
| -rw-r--r-- | src/slidingwindow.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/slidingwindow.c b/src/slidingwindow.c new file mode 100644 index 0000000..883e852 --- /dev/null +++ b/src/slidingwindow.c @@ -0,0 +1,43 @@ +#include "slidingwindow.h" +#include "common.h" +#include "smrt_arena.h" + +#include <string.h> + +sliding_window_array_t *sw_create(smrt_arena_t *arena, u64 elem_size, u64 size) { + sliding_window_array_t *sw = smrt_arena_push(arena, sizeof(sliding_window_array_t) + 2 * elem_size * size, true); + sw->size = size; + sw->element_size_bytes = elem_size; + + return sw; +} + +void sw_insert(sliding_window_array_t *sw, void *x) { + if (sw->num_elements == sw->size) { + sw->start_idx++; + } + + if ((sw->start_idx + sw->size) > (2 * sw->size)) { + u64 src_offset = SLIDING_WINDOW_BASE_POS + (sw->start_idx * sw->element_size_bytes); + u64 bytes_to_copy = (sw->size - 1) * sw->element_size_bytes; + memmove( + ((u8*)sw + SLIDING_WINDOW_BASE_POS), + ((u8*)sw + src_offset), + bytes_to_copy + ); + + sw->start_idx = 0; + } + + u64 new_pos = sw->start_idx + sw->num_elements; + + if (new_pos >= sw->start_idx + sw->size) { + new_pos = sw->start_idx + sw->size - 1; + } + + memcpy(((u8*)sw + SLIDING_WINDOW_BASE_POS + (new_pos * sw->element_size_bytes)), x, sw->element_size_bytes); + + if (sw->num_elements < sw->size) { + sw->num_elements++; + } +} |