Skip to content

Instantly share code, notes, and snippets.

@jweinst1
Last active June 16, 2024 09:04
Show Gist options
  • Save jweinst1/f11bc9facf4efbe2b3160f13259d2c82 to your computer and use it in GitHub Desktop.
Save jweinst1/f11bc9facf4efbe2b3160f13259d2c82 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <assert.h>
static const size_t MIN_ROW_SIZE = sizeof(size_t) * 2;
int storage_row_empty(const char* row) {
return row[0] == '\0';
}
void storage_row_set(char* row, const char* key, const char* val) {
while((*row++ = *key++));
while((*row++ = *val++));
}
char* storage_block_create(size_t row_size, size_t row_count) {
assert(row_size >= MIN_ROW_SIZE);
char* block = calloc(1, row_size * (row_count + 1));
size_t* size_setter = (size_t*)block;
size_setter[0] = row_size;
size_setter[1] = row_count;
return block;
}
char* storage_block_get(char* block, size_t row) {
size_t* header = (size_t*)block;
if (row >= header[1]) {
return NULL;
}
return block + (header[0] * ( row + 1));
}
size_t storage_block_row_size(const char* block) {
size_t* header = (size_t*)block;
return header[0];
}
struct void_buf {
size_t len;
size_t cap;
void** items;
};
void void_buf_init(struct void_buf* vb, size_t capac) {
vb->len = 0;
vb->cap = capac;
vb->items = malloc(sizeof(void*) * capac);
}
void void_buf_expand(struct void_buf* vb, size_t added_size) {
vb->cap += added_size;
vb->items = realloc(vb->items, vb->cap);
}
void void_buf_append(struct void_buf* vb, void* item) {
if (vb->len == vb->cap) {
void_buf_expand(vb, vb->cap);
}
vb->items[vb->len++] = item;
}
void* void_buf_last(struct void_buf* vb) {
if (vb->len) {
return vb->items[vb->len - 1];
}
return NULL;
}
void void_buf_deinit(struct void_buf* vb) {
vb->len = 0;
vb->cap = 0;
free(vb->items);
}
char* hash_block_create(size_t amount) {
char* block = calloc(1, sizeof(size_t) * 2 * (amount + 1));
size_t* size_setter = (size_t*)block;
size_setter[0] = amount;
return block;
}
struct storage_ptr {
size_t block_n;
size_t row_n;
}; // todo just do char* with offset?
struct hash_block {
size_t len;
struct storage_ptr* storage_rows;
}; // todo think about disc representation
// top level structure
struct hash_map {
struct void_buf hash_blocks;
struct void_buf storage_blocks;
};
int main(int argc, char const *argv[])
{
char bin[20] = {'\0'};
storage_row_set(bin, "fo", "bo");
printf("%c\n", bin[0]);
printf("%c\n", bin[1]);
printf("%d\n", bin[2]);
printf("%c\n", bin[3]);
printf("%c\n", bin[4]);
printf("%d\n", bin[5]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment