Last active
May 18, 2020 22:22
-
-
Save Journeyman1337/e5e25ca7e62da41b622846cbd034dd33 to your computer and use it in GitHub Desktop.
This is a datastructure based on the concept of generational indexing mentioned in this talk: https://www.youtube.com/watch?v=P9u8x13W7UE. This type of datastructure is very useful for use as the backbone of an Entity Component System.
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
/* | |
Copyright 2020 Daniel Valcour | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
#ifdef __cplusplus | |
extern "C" | |
{ | |
#endif | |
#include "generational.h" | |
typedef struct gendat | |
{ | |
uint32_t generation; | |
void* dat; | |
}; | |
typedef struct genarena | |
{ | |
size_t allocate_size; | |
size_t size; | |
size_t count; | |
size_t generation; | |
struct gendat* data_array; | |
size_t unused_size; | |
size_t unused_count; | |
size_t* unused_array; | |
}; | |
static inline void reallocate_data(struct genarena* arena) | |
{ | |
struct gendat* new_data_array = (struct gendat*)malloc(sizeof(struct gendat) * (arena->size + arena->allocate_size)); | |
for (size_t index = 0; index < arena->size; index++) | |
{ | |
new_data_array[index] = arena->data_array[index]; | |
} | |
arena->size += arena->allocate_size; | |
free(arena->data_array); | |
arena->data_array = new_data_array; | |
} | |
static inline void reallocate_unused(struct genarena* arena) | |
{ | |
size_t* new_unused_array = (size_t*)malloc(sizeof(size_t) * (arena->unused_size + arena->allocate_size)); | |
for (size_t index = 0; index < arena->unused_size; index++) | |
{ | |
new_unused_array[index] = arena->unused_array[index]; | |
} | |
arena->unused_size += arena->allocate_size; | |
free(arena->unused_array); | |
arena->unused_array = new_unused_array; | |
} | |
void* arena_init(size_t allocate_size) | |
{ | |
struct genarena* ret = malloc(sizeof(struct genarena)); | |
ret->allocate_size = allocate_size; | |
ret->size = allocate_size; | |
ret->count = 0; | |
ret->generation = 1; | |
ret->data_array = (struct gendat*)malloc(sizeof(struct gendat) * ret->size); | |
ret->unused_size = allocate_size; | |
ret->unused_count = 0; | |
ret->unused_array = (size_t*)malloc(sizeof(size_t) * ret->unused_size); | |
return ret; | |
} | |
size_t arena_get_next_generation(void* arena) | |
{ | |
return ((struct genarena*)arena)->generation; | |
} | |
size_t arena_get_count(void* arena) | |
{ | |
return ((struct genarena*)arena)->count - ((struct genarena*)arena)->unused_count; | |
} | |
size_t arena_get_size(void* arena) | |
{ | |
return ((struct genarena*)arena)->size; | |
} | |
size_t arena_get_unused_count(void* arena) | |
{ | |
return ((struct genarena*)arena)->unused_count; | |
} | |
struct gendex arena_add_item(void* arena, void* pointer_to_item) | |
{ | |
struct gendex ret; | |
if (((struct genarena*)arena)->unused_count == 0) | |
{ | |
if (((struct genarena*)arena)->count + ((struct genarena*)arena)->unused_count == ((struct genarena*)arena)->size) | |
{ | |
reallocate_data(((struct genarena*)arena)); | |
} | |
ret.index = ((struct genarena*)arena)->count++; | |
ret.generation = ((struct genarena*)arena)->generation++; | |
((struct genarena*)arena)->data_array[ret.index].dat = pointer_to_item; | |
((struct genarena*)arena)->data_array[ret.index].generation = ret.generation; | |
} | |
else //if (((struct genarena*)arena)->unused_count > 0) | |
{ | |
ret.index = ((struct genarena*)arena)->unused_array[--((struct genarena*)arena)->unused_count]; | |
ret.generation = ((struct genarena*)arena)->generation++; | |
((struct genarena*)arena)->data_array[ret.index].dat = pointer_to_item; | |
((struct genarena*)arena)->data_array[ret.index].generation = ret.generation; | |
} | |
return ret; | |
} | |
void* arena_get_item(void* arena, struct gendex gi) | |
{ | |
if (((struct genarena*)arena)->data_array[gi.index].generation == gi.generation) | |
{ | |
return ((struct genarena*)arena)->data_array[gi.index].dat; | |
} | |
return (void*)0; | |
} | |
void arena_remove_item(void* arena, struct gendex gi) | |
{ | |
if (((struct genarena*)arena)->data_array[gi.index].generation == gi.generation) | |
{ | |
((struct genarena*)arena)->data_array[gi.index].generation = 0; | |
if (((struct genarena*)arena)->unused_count == ((struct genarena*)arena)->unused_size) | |
{ | |
reallocate_unused(((struct genarena*)arena)); | |
} | |
((struct genarena*)arena)->unused_array[((struct genarena*)arena)->unused_count++] = gi.index; | |
} | |
} | |
bool arena_has_item(void* arena, struct gendex gi) | |
{ | |
return ((struct genarena*)arena)->data_array[gi.index].generation == gi.generation; | |
} | |
void arena_free(void* arena) | |
{ | |
free(((struct genarena*)arena)->data_array); | |
free(((struct genarena*)arena)->unused_array); | |
free((struct genarena*)arena); | |
} | |
#ifdef __cplusplus | |
} // extern "C" | |
#endif |
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
/* | |
Copyright 2020 Daniel Valcour | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
#ifndef JGENERATION_H__ | |
#define JGENERATION_H__ | |
#ifdef __cplusplus | |
extern "C" | |
{ | |
#endif | |
#include <stdint.h> | |
#include <stdbool.h> | |
typedef struct gendex | |
{ | |
size_t index; | |
uint32_t generation; | |
}; | |
void* arena_init(size_t); | |
size_t arena_get_next_generation(void*); | |
size_t arena_get_count(void*); | |
size_t arena_get_size(void*); | |
size_t arena_get_unused_count(void*); | |
struct gendex arena_add_item(void*, void*); | |
void* arena_get_item(void*, struct gendex); | |
void arena_remove_item(void*, struct gendex); | |
bool arena_has_item(void*, struct gendex); | |
void arena_free(void*); | |
#ifdef __cplusplus | |
} // extern "C" | |
#endif | |
#endif // header guard |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment