chunkedvector.c 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "pocketpy/common/chunkedvector.h"
  2. #include "pocketpy/pocketpy.h"
  3. #include <assert.h>
  4. #if defined(_MSC_VER)
  5. #include <intrin.h>
  6. #endif
  7. PK_INLINE int c11__bit_length(unsigned long x) {
  8. #if(defined(__clang__) || defined(__GNUC__))
  9. return x == 0 ? 0 : (int)sizeof(unsigned long) * 8 - __builtin_clzl(x);
  10. #elif defined(_MSC_VER)
  11. _Static_assert(sizeof(unsigned long) <= 4, "unsigned long is greater than 4 bytes");
  12. unsigned long msb;
  13. if(_BitScanReverse(&msb, x)) { return (int)msb + 1; }
  14. return 0;
  15. #else
  16. const int BIT_LENGTH_TABLE[32] = {0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  17. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5};
  18. int msb = 0;
  19. while(x >= 32) {
  20. msb += 6;
  21. x >>= 6;
  22. }
  23. msb += BIT_LENGTH_TABLE[x];
  24. return msb;
  25. #endif
  26. }
  27. void c11_chunkedvector__ctor(c11_chunkedvector* self, int elem_size, int initial_chunks) {
  28. if(initial_chunks < 5) initial_chunks = 5;
  29. c11_vector__ctor(&self->chunks, sizeof(c11_chunkedvector_chunk));
  30. self->length = 0;
  31. self->capacity = (1U << (unsigned int)initial_chunks) - 1U;
  32. self->elem_size = elem_size;
  33. self->initial_chunks = initial_chunks;
  34. void* chunks_data = PK_MALLOC(elem_size * ((1U << (unsigned int)initial_chunks) - 1));
  35. for(int i = 0; i < initial_chunks; i++) {
  36. c11_chunkedvector_chunk chunk = {.length = 0,
  37. .capacity = 1U << i,
  38. .data = (char*)chunks_data + elem_size * ((1U << i) - 1U)};
  39. c11_vector__push(c11_chunkedvector_chunk, &self->chunks, chunk);
  40. }
  41. }
  42. void c11_chunkedvector__dtor(c11_chunkedvector* self) {
  43. for(int index = self->initial_chunks; index < self->chunks.length; index++) {
  44. c11_chunkedvector_chunk* chunk = c11__at(c11_chunkedvector_chunk, &self->chunks, index);
  45. PK_FREE(chunk->data);
  46. }
  47. c11_chunkedvector_chunk* initial_chunk = c11__at(c11_chunkedvector_chunk, &self->chunks, 0);
  48. PK_FREE(initial_chunk->data);
  49. c11_vector__dtor(&self->chunks);
  50. }
  51. void* c11_chunkedvector__emplace(c11_chunkedvector* self) {
  52. if(self->length == self->capacity) {
  53. #ifndef NDEBUG
  54. c11_chunkedvector_chunk last_chunk =
  55. c11_vector__back(c11_chunkedvector_chunk, &self->chunks);
  56. assert(last_chunk.capacity == last_chunk.length);
  57. #endif
  58. c11_chunkedvector_chunk chunk = {
  59. .length = 0,
  60. .capacity = 1U << (unsigned int)self->chunks.length,
  61. .data = PK_MALLOC(self->elem_size * ((1U << (unsigned int)self->chunks.length)))};
  62. self->capacity += chunk.capacity;
  63. c11_vector__push(c11_chunkedvector_chunk, &self->chunks, chunk);
  64. }
  65. #if 1
  66. int last_chunk_index = c11__bit_length(self->length + 1) - 1;
  67. c11_chunkedvector_chunk* last_chunk =
  68. c11__at(c11_chunkedvector_chunk, &self->chunks, last_chunk_index);
  69. #else
  70. // This is not correct, because there is some pre-allocated chunks
  71. c11_chunkedvector_chunk* last_chunk = &c11_vector__back(c11_chunkedvector_chunk, &self->chunks);
  72. #endif
  73. void* p = (char*)last_chunk->data + self->elem_size * last_chunk->length;
  74. last_chunk->length++;
  75. self->length++;
  76. return p;
  77. }
  78. void* c11_chunkedvector__at(c11_chunkedvector* self, int index) {
  79. int chunk_index = c11__bit_length(index + 1) - 1;
  80. c11_chunkedvector_chunk* chunk = c11__at(c11_chunkedvector_chunk, &self->chunks, chunk_index);
  81. return (char*)chunk->data + (index + 1 - (1U << (unsigned int)chunk_index)) * self->elem_size;
  82. }