Last active
February 5, 2025 09:04
-
-
Save memononen/ffa28cd3d0fec93e1ad40b45a112058c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#define MAX_SIZE (64*1024) | |
static struct my_data data[MAX_SIZE]; | |
static short free_idx[MAX_SIZE]; | |
static short first_free_idx = -1; | |
// init; populate free items | |
free_idx[MAX_SIZE-1] = -1; | |
for (int i = 0; i < MAX_SIZE-1; ++i) { | |
free_idx[i] = i+1; | |
} | |
first_free_idx = 0; | |
// insert; store index elsewhere | |
index = first_free_idx; | |
first_free_idx = free_idx[index]; | |
free_idx[index] = -1; | |
data[index] = ...input; | |
// lookup using stored index | |
output = data[idx]; | |
// update existing stored element | |
data[idx] = input; | |
// remove used stored element | |
free_idx[idx] = first_free_idx; | |
first_free_idx = idx; | |
// branchless iterate over all elements | |
for (int i = 0; i < MAX_SIZE; ++i) { | |
var = free_idx[i] == -1 ? ... : ...; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment