Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <time.h>
- typedef union {
- struct {
- float x, y;
- };
- struct {
- unsigned int next;
- };
- } item_t;
- typedef struct {
- item_t* items;
- size_t size;
- size_t used;
- size_t num_inserts;
- size_t num_deletes;
- size_t num_allocations;
- } bulk_data_t;
- void delete_item(bulk_data_t* bd, size_t index) {
- if (index >= bd->size) return;
- bd->items[index].next = bd->items[0].next;
- bd->items[0].next = index;
- bd->num_deletes++;
- }
- size_t alloc_item(bulk_data_t* bd) {
- bd->num_inserts++;
- size_t slot = bd->items[0].next;
- bd->items[0].next = bd->items[slot].next;
- if (slot) return slot;
- if (bd->used < bd->size) {
- slot = bd->used++;
- return slot;
- }
- bd->size += 5;
- bd->items = reallocarray(bd->items, bd->size, sizeof(item_t));
- bd->num_allocations++;
- return bd->used++;
- }
- int main() {
- srand(time(0));
- bulk_data_t bd = { NULL, 1, 1, 1, 0, 0 };
- bd.items = reallocarray(bd.items, bd.size, sizeof(item_t));
- bd.items[0].next = 1;
- for (int i = 0; i < 1000; i++) {
- if (rand() % 2 == 0) {
- alloc_item(&bd);
- } else {
- delete_item(&bd, rand() % bd.size);
- }
- }
- printf(
- "%zu - %zu - %zu - %zu - %zu\n",
- bd.size,
- bd.used,
- bd.num_allocations,
- bd.num_inserts,
- bd.num_deletes
- );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement