Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <stdint.h>
- #include <stdlib.h>
- typedef struct spatial_hash_s {
- uint32_t width;
- uint32_t height;
- uint16_t cellmax; // max # of entries per cell
- uint64_t entrysize; // size of a single entry
- } spatial_hash_t;
- typedef struct spatial_index_s {
- uint16_t count; // # of entries held in this cell
- } spatial_index_t;
- #define SHASH_CELL_AT(h,x,y) \
- ((spatial_index_t*)(((uint8_t*)((h)+1))+(((y)*(h)->width+(x))*(sizeof(spatial_index_t)+((h)->entrysize)*((h)->cellmax)))))
- #define shash_new(type, w, h, cellmax) \
- (spatial_hash((w),(h),(cellmax),sizeof(type)))
- spatial_hash_t* spatial_hash(uint32_t w, uint32_t h, uint16_t cellmax, uint16_t entrysize) {
- uint32_t cells = w * h;
- uint64_t rowsize = (sizeof(spatial_index_t) + (cellmax * entrysize));
- uint64_t total_size = sizeof(spatial_hash_t) + (cells * rowsize);
- spatial_hash_t *sh = malloc(total_size);
- memset(sh, 0, total_size); // zero out the memory
- sh->width = w;
- sh->height = h;
- sh->cellmax = cellmax;
- sh->entrysize = entrysize;
- return sh;
- }
- // shash_get(sh, 0, 0, geometry_entry, &count, &results)
- #define shash_get(type, hash, x, y, countref) \
- ((type*)spatial_hash_get((hash),(x),(y),(countref)))
- void* spatial_hash_get(spatial_hash_t *sh, uint16_t x, uint16_t y, uint16_t *count) {
- spatial_index_t *row = SHASH_CELL_AT(sh, x, y);
- *count = row->count;
- return (void*)(row+1);
- }
- #define shash_append spatial_hash_append
- void spatial_hash_append(spatial_hash_t *sh, uint16_t x, uint16_t y, void* val) {
- spatial_index_t *cell = SHASH_CELL_AT(sh, x, y);
- void *free_entry = (void*)(((char*)(cell+1))+(cell->count)*sh->entrysize);
- memcpy(free_entry, val, sh->entrysize);
- cell->count++;
- }
- #define shash_delete spatial_hash_delete
- void spatial_hash_delete(spatial_hash_t *sh, uint16_t x, uint16_t y, uint16_t i) {
- spatial_index_t *cell = SHASH_CELL_AT(sh, x, y);
- char *base = (char*)(cell+1);
- while (i < cell->count) {
- // overwrite with next entry
- memcpy(base + (i*sh->entrysize), base + ((i+1)*sh->entrysize), sh->entrysize);
- i++;
- }
- cell->count--;
- }
- void spatial_hash_reset(spatial_hash_t *sh) {
- int cells = sh->width * sh->height;
- uint64_t cellsize = (sizeof(spatial_index_t) + (sh->cellmax * sh->entrysize));
- uint64_t total_size = sizeof(spatial_hash_t) + (cells * cellsize);
- memset((void*)(sh+1), 0, total_size);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement