mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 19:40:18 +00:00
add c11_chunkedvector
This commit is contained in:
parent
03e368b755
commit
10a3c0885b
32
include/pocketpy/common/chunkedvector.h
Normal file
32
include/pocketpy/common/chunkedvector.h
Normal file
@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include "vector.h"
|
||||
|
||||
typedef struct c11_chunkedvector_chunk {
|
||||
int length;
|
||||
int capacity;
|
||||
int elem_size;
|
||||
void* data;
|
||||
} c11_chunkedvector_chunk;
|
||||
|
||||
typedef struct c11_chunkedvector {
|
||||
c11_vector /*T=c11_chunkedvector_chunk*/ chunks;
|
||||
int length;
|
||||
int capacity;
|
||||
int elem_size;
|
||||
int initial_chunks;
|
||||
} c11_chunkedvector;
|
||||
|
||||
// chunks[0]: size=1, total_capacity=1
|
||||
// chunks[1]: size=2, total_capacity=3
|
||||
// chunks[2]: size=4, total_capacity=7
|
||||
// chunks[3]: size=8, total_capacity=15
|
||||
// chunks[4]: size=16, total_capacity=31
|
||||
// chunks[5]: size=32, total_capacity=63
|
||||
// ...
|
||||
// chunks[n]: size=2^n, total_capacity=2^(n+1)-1
|
||||
|
||||
void c11_chunkedvector__ctor(c11_chunkedvector* self, int elem_size, int initial_chunks);
|
||||
void c11_chunkedvector__dtor(c11_chunkedvector* self);
|
||||
void* c11_chunkedvector__emplace(c11_chunkedvector* self);
|
||||
void* c11_chunkedvector__at(c11_chunkedvector* self, int index);
|
35
src/common/chunkedvector.c
Normal file
35
src/common/chunkedvector.c
Normal file
@ -0,0 +1,35 @@
|
||||
#include "pocketpy/common/chunkedvector.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "pocketpy/common/utils.h"
|
||||
#include "pocketpy/config.h"
|
||||
|
||||
void c11_chunkedvector__ctor(c11_chunkedvector* self, int elem_size, int initial_chunks) {
|
||||
c11_vector__ctor(&self->chunks, sizeof(c11_chunkedvector_chunk));
|
||||
self->length = 0;
|
||||
self->capacity = 0;
|
||||
self->elem_size = elem_size;
|
||||
self->initial_chunks = initial_chunks;
|
||||
|
||||
for(int i = 0; i < initial_chunks; i++) {
|
||||
// TODO: optimize?
|
||||
c11_chunkedvector_chunk chunk;
|
||||
chunk.length = 0;
|
||||
chunk.capacity = (1 << i);
|
||||
chunk.elem_size = elem_size;
|
||||
chunk.data = PK_MALLOC(chunk.capacity * elem_size);
|
||||
c11_vector__push(c11_chunkedvector_chunk, &self->chunks, chunk);
|
||||
initial_capacity += chunk.capacity;
|
||||
}
|
||||
}
|
||||
|
||||
void c11_chunkedvector__dtor(c11_chunkedvector* self) {}
|
||||
|
||||
void* c11_chunkedvector__emplace(c11_chunkedvector* self) {}
|
||||
|
||||
static int c11__bit_length(unsigned int n) { return n == 0 ? 0 : 32 - __builtin_clz(n); }
|
||||
|
||||
void* c11_chunkedvector__at(c11_chunkedvector* self, int index) {
|
||||
int chunk_index = c11__bit_length(index) - 1;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user