example works, no errors

This commit is contained in:
Gregory Burd 2024-04-04 19:56:31 -04:00
parent 723ddb29cb
commit 6ffa748974
2 changed files with 35 additions and 88 deletions

View file

@ -1,33 +1,27 @@
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "../include/sparsemap.h"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wvariadic-macros"
#define __diag(format, ...) \
__diag_(__FILE__, __LINE__, __func__, format, ##__VA_ARGS__)
#define __diag(...) \
do { fprintf(stderr, "%s:%d:%s(): ",__FILE__, __LINE__, __func__);\
fprintf(stderr, __VA_ARGS__); } while (0)
#pragma GCC diagnostic pop
void __attribute__((format(printf, 4, 5))) __diag_(const char *file,
int line, const char *func, const char *format, ...)
{
va_list args;
va_start(args, format);
fprintf(stderr, "%s:%d:%s(): ", file, line, func);
vfprintf(stderr, format, args);
va_end(args);
}
// NOTE: currently, this code serves as a sample and unittest.
int main() {
int size = 4;
size_t size = 4;
setbuf(stderr, 0); // disable buffering
__diag("Please wait a moment...");
#if 1
sparsemap_t mmap, *map = &mmap;
uint8_t buffer[1024];
uint8_t buffer2[1024];
sparsemap_t *map = sparsemap(buffer, sizeof(buffer), 0);
sparsemap_init(map, buffer, sizeof(buffer), 0);
assert(sparsemap_get_size(map) == size);
sparsemap_set(map, 0, true);
assert(sparsemap_get_size(map) == size + 4 + 8 + 8);
@ -43,7 +37,7 @@ int main() {
assert(sparsemap_get_size(map) == size + 4 + 8 + 8);
sparsemap_clear(map);
__diag(".");
fprintf(stderr, ".");
// set [0..100000]
for (int i = 0; i < 100000; i++) {
@ -58,7 +52,7 @@ int main() {
assert(sparsemap_is_set(map, i) == true);
}
__diag(".");
fprintf(stderr, ".");
for (int i = 0; i < 100000; i++) {
assert(sparsemap_is_set(map, i) == true);
@ -76,7 +70,7 @@ int main() {
}
sparsemap_clear(map);
__diag(".");
fprintf(stderr, ".");
// set [10000..0]
for (int i = 10000; i >= 0; i--) {
@ -87,7 +81,7 @@ int main() {
for (int i = 10000; i >= 0; i--) {
assert(sparsemap_is_set(map, i) == true);
__diag(".");
fprintf(stderr, ".");
}
// open and compare
@ -95,6 +89,7 @@ int main() {
for (int i = 0; i < 10000; i++) {
assert(sparsemap_is_set(sm2, i) == sparsemap_is_set(map, i));
}
free(sm2);
// unset [10000..0]
for (int i = 10000; i >= 0; i--) {
@ -107,7 +102,7 @@ int main() {
assert(sparsemap_is_set(map, i) == false);
}
__diag(".");
fprintf(stderr, ".");
sparsemap_clear(map);
sparsemap_set(map, 0, true);
@ -127,7 +122,7 @@ int main() {
assert(sparsemap_is_set(map, 2048 * 2 + 2) == false);
sparsemap_clear(map);
__diag(".");
fprintf(stderr, ".");
for (int i = 0; i < 100000; i++) {
sparsemap_set(map, i, true);
@ -137,7 +132,7 @@ int main() {
}
sparsemap_clear(map);
__diag(".");
fprintf(stderr, ".");
for (int i = 1; i < 513; i++) {
sparsemap_set(map, i, true);
@ -147,7 +142,7 @@ int main() {
}
sparsemap_clear(map);
__diag(".");
fprintf(stderr, ".");
for (size_t i = 0; i < 8; i++) {
sparsemap_set(map, i * 10, true);
@ -157,7 +152,9 @@ int main() {
}
// split and move, aligned to MiniMap capacity
sm2 = sparsemap(buffer2, sizeof(buffer2), 0);
sparsemap_t _sm2;
sm2 = &_sm2;
sparsemap_init(sm2, buffer2, sizeof(buffer2), 0);
sparsemap_clear(sm2);
for (int i = 0; i < 2048 * 2; i++) {
sparsemap_set(map, i, true);
@ -171,10 +168,10 @@ int main() {
assert(sparsemap_is_set(map, i) == false);
assert(sparsemap_is_set(sm2, i) == true);
}
__diag(".");
fprintf(stderr, ".");
// split and move, aligned to BitVector capacity
sm2 = sparsemap(buffer2, sizeof(buffer2), 0);
sparsemap_init(sm2, buffer2, sizeof(buffer2), 0);
sparsemap_clear(map);
for (int i = 0; i < 2048 * 3; i++) {
sparsemap_set(map, i, true);
@ -189,41 +186,5 @@ int main() {
assert(sparsemap_is_set(sm2, i) == true);
}
__diag("ok\n");
#else
//
// This code was used to create the lookup table for
// sparsemap::MiniMap<>::calc_vector_size()
//
__diag(" ");
for (unsigned int ch = 0; ch <= 0xff; ch++) {
if (ch > 0 && ch % 16 == 0)
__diag("\n ");
/*
// check if value is invalid (contains 2#01)
if ((ch & (0x3 << 0)) >> 0 == 1
|| (ch & (0x3 << 2)) >> 2 == 1
|| (ch & (0x3 << 4)) >> 4 == 1
|| (ch & (0x3 << 6)) >> 6 == 1) {
//__diag("%d: -1\n", (int)ch);
__diag(" -1,");
continue;
}
*/
// count all occurrences of 2#10
int size = 0;
if ((ch & (0x3 << 0)) >> 0 == 2)
size++;
if ((ch & (0x3 << 2)) >> 2 == 2)
size++;
if ((ch & (0x3 << 4)) >> 4 == 2)
size++;
if ((ch & (0x3 << 6)) >> 6 == 2)
size++;
//__diag("%u: %d\n", (unsigned int)ch, size);
__diag(" %d,", size);
fprintf(stderr, " ok\n");
}
#endif
}

View file

@ -169,19 +169,6 @@ __sm_chunk_map_init(__sm_chunk_t *map, uint8_t *data)
map->m_data = (sm_bitvec_t *)data;
}
/**
* Allocate and initialize a chunk map.
*/
static __sm_chunk_t *
__sm_chunk_map(uint8_t *data)
{
__sm_chunk_t *chunk = (__sm_chunk_t *)calloc(1, sizeof(__sm_chunk_t));
if (chunk) {
__sm_chunk_map_init(chunk, data);
}
return chunk;
}
/**
* Returns the maximum capacity of this __sm_chunk_t.
*/
@ -320,23 +307,23 @@ static int
__sm_chunk_map_set(__sm_chunk_t *map, size_t idx, bool value, size_t *pos,
sm_bitvec_t *fill, bool retried)
{
/* in which sm_bitvec_t is |idx| stored? */
/* In which sm_bitvec_t is |idx| stored? */
size_t bv = idx / SM_BITS_PER_VECTOR;
__sm_assert(bv < SM_FLAGS_PER_INDEX);
/* now retrieve the flags of that sm_bitvec_t */
/* Now retrieve the flags of that sm_bitvec_t. */
size_t flags = ((*map->m_data) & ((sm_bitvec_t)SM_FLAG_MASK << (bv * 2))) >>
(bv * 2);
assert(flags != SM_PAYLOAD_NONE);
if (flags == SM_PAYLOAD_ZEROS) {
/* easy - set bit to 0 in a sm_bitvec_t of zeroes */
/* Easy - set bit to 0 in a sm_bitvec_t of zeroes. */
if (value == false) {
*pos = 0;
*fill = 0;
return SM_OK;
}
/* the sparsemap must grow this __sm_chunk_t by one additional sm_bitvec_t,
then try again */
/* The sparsemap must grow this __sm_chunk_t by one additional sm_bitvec_t,
then try again. */
if (!retried) {
*pos = 1 + __sm_chunk_map_get_position(map, bv);
*fill = 0;
@ -650,18 +637,17 @@ static ssize_t
__sm_get_chunk_map_offset(sparsemap_t *map, size_t idx)
{
size_t count;
uint8_t *p;
sm_idx_t start = 0;
count = __sm_get_chunk_map_count(map);
if (count == 0) {
return (-1);
}
p = __sm_get_chunk_map_data(map, 0);
uint8_t *start = __sm_get_chunk_map_data(map, 0);
uint8_t *p = start;
for (size_t i = 0; i < count - 1; i++) {
// TODO: was "sm_idx_t start = *(sm_idx_t *)p;" review this...
start = *(sm_idx_t *)p;
sm_idx_t start = *(sm_idx_t *)p; //TODO wtf...
__sm_assert(start == __sm_get_aligned_offset(start));
__sm_chunk_t chunk;
__sm_chunk_map_init(&chunk, p + sizeof(sm_idx_t));
@ -892,8 +878,8 @@ sparsemap_set(sparsemap_t *map, size_t idx, bool value)
/* We just added another chunk map! */
__sm_set_chunk_map_count(map, __sm_get_chunk_map_count(map) + 1);
// we already inserted an additional sm_bitvec_t; later on there
// is no need to grow the vector even further
/* We already inserted an additional sm_bitvec_t; later on there
is no need to grow the vector even further. */
dont_grow = true;
}
@ -909,7 +895,7 @@ sparsemap_set(sparsemap_t *map, size_t idx, bool value)
}
size_t size = __sm_chunk_map_get_size(&chunk);
offset += sizeof(sm_idx_t) + size;
offset += (ssize_t)(sizeof(sm_idx_t) + size);
p += sizeof(sm_idx_t) + size;
uint8_t buf[sizeof(sm_idx_t) + sizeof(sm_bitvec_t) * 2] = { 0 };