2024-04-04 19:24:02 +00:00
|
|
|
/*
|
2024-04-10 19:34:19 +00:00
|
|
|
* Copyright (c) 2024 Gregory Burd <greg@burd.me>. All rights reserved.
|
2024-04-04 19:24:02 +00:00
|
|
|
*
|
2024-04-10 19:34:19 +00:00
|
|
|
* 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:
|
2024-04-04 19:24:02 +00:00
|
|
|
*
|
2024-04-10 19:34:19 +00:00
|
|
|
* 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.
|
2024-04-04 19:24:02 +00:00
|
|
|
*/
|
|
|
|
|
2024-04-24 20:32:09 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2024-04-10 19:34:19 +00:00
|
|
|
#include <assert.h>
|
2024-04-24 20:32:09 +00:00
|
|
|
#include <errno.h>
|
2024-04-10 19:34:19 +00:00
|
|
|
#include <popcount.h>
|
|
|
|
#include <sparsemap.h>
|
2024-04-24 20:32:09 +00:00
|
|
|
#include <stdarg.h>
|
2024-04-11 03:16:06 +00:00
|
|
|
#include <stdbool.h>
|
2024-04-24 20:32:09 +00:00
|
|
|
#include <stddef.h>
|
2024-04-11 03:16:06 +00:00
|
|
|
#include <stdint.h>
|
2024-04-24 20:32:09 +00:00
|
|
|
#include <stdio.h>
|
2024-04-11 03:16:06 +00:00
|
|
|
#include <stdlib.h>
|
2024-04-24 20:32:09 +00:00
|
|
|
#include <string.h>
|
2024-04-04 19:24:02 +00:00
|
|
|
|
|
|
|
#ifdef SPARSEMAP_DIAGNOSTIC
|
|
|
|
#pragma GCC diagnostic push
|
2024-04-05 14:34:59 +00:00
|
|
|
#pragma GCC diagnostic ignored "-Wpedantic"
|
2024-04-04 19:24:02 +00:00
|
|
|
#pragma GCC diagnostic ignored "-Wvariadic-macros"
|
2024-04-07 20:38:57 +00:00
|
|
|
#define __sm_diag(format, ...) __sm_diag_(__FILE__, __LINE__, __func__, format, ##__VA_ARGS__)
|
2024-04-04 19:24:02 +00:00
|
|
|
#pragma GCC diagnostic pop
|
2024-04-07 20:38:57 +00:00
|
|
|
void __attribute__((format(printf, 4, 5))) __sm_diag_(const char *file, int line, const char *func, const char *format, ...)
|
2024-04-04 19:24:02 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
fprintf(stderr, "%s:%d:%s(): ", file, line, func);
|
2024-04-08 22:14:47 +00:00
|
|
|
va_start(args, format);
|
2024-04-04 19:24:02 +00:00
|
|
|
vfprintf(stderr, format, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-07 20:38:57 +00:00
|
|
|
#define __sm_assert(expr) \
|
|
|
|
if (!(expr)) \
|
2024-04-30 15:20:48 +00:00
|
|
|
fprintf(stderr, "%s:%d:%s(): assertion failed! %s\n", __FILE__, __LINE__, __func__, #expr)
|
2024-04-24 20:32:09 +00:00
|
|
|
|
|
|
|
#define __sm_when_diag(expr) \
|
|
|
|
if (1) \
|
|
|
|
expr
|
2024-04-04 19:24:02 +00:00
|
|
|
#else
|
2024-04-24 20:32:09 +00:00
|
|
|
#define __sm_diag(file, line, func, format, ...) ((void)0)
|
2024-04-04 19:24:02 +00:00
|
|
|
#define __sm_assert(expr) ((void)0)
|
2024-04-24 20:32:09 +00:00
|
|
|
#define __sm_when_diag(expr) \
|
|
|
|
if (0) \
|
|
|
|
expr
|
2024-04-04 19:24:02 +00:00
|
|
|
#endif
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-24 20:32:09 +00:00
|
|
|
#define IS_8_BYTE_ALIGNED(addr) (((uintptr_t)(addr)&0x7) == 0)
|
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
enum __SM_CHUNK_INFO {
|
|
|
|
/* metadata overhead: 4 bytes for __sm_chunk_t count */
|
|
|
|
SM_SIZEOF_OVERHEAD = sizeof(uint32_t),
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-07 20:38:57 +00:00
|
|
|
/* number of bits that can be stored in a sm_bitvec_t */
|
2024-04-04 19:24:02 +00:00
|
|
|
SM_BITS_PER_VECTOR = (sizeof(sm_bitvec_t) * 8),
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* number of flags that can be stored in a single index byte */
|
|
|
|
SM_FLAGS_PER_INDEX_BYTE = 4,
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* number of flags that can be stored in the index */
|
|
|
|
SM_FLAGS_PER_INDEX = (sizeof(sm_bitvec_t) * SM_FLAGS_PER_INDEX_BYTE),
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* maximum capacity of a __sm_chunk (in bits) */
|
|
|
|
SM_CHUNK_MAX_CAPACITY = (SM_BITS_PER_VECTOR * SM_FLAGS_PER_INDEX),
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* sm_bitvec_t payload is all zeros (2#00) */
|
|
|
|
SM_PAYLOAD_ZEROS = 0,
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* sm_bitvec_t payload is all ones (2#11) */
|
|
|
|
SM_PAYLOAD_ONES = 3,
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* sm_bitvec_t payload is mixed (2#10) */
|
|
|
|
SM_PAYLOAD_MIXED = 2,
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* sm_bitvec_t is not used (2#01) */
|
|
|
|
SM_PAYLOAD_NONE = 1,
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-24 20:32:09 +00:00
|
|
|
/* a mask for checking flags (2 bits, 2#11) */
|
2024-04-04 19:24:02 +00:00
|
|
|
SM_FLAG_MASK = 3,
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* return code for set(): ok, no further action required */
|
|
|
|
SM_OK = 0,
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* return code for set(): needs to grow this __sm_chunk_t */
|
|
|
|
SM_NEEDS_TO_GROW = 1,
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* return code for set(): needs to shrink this __sm_chunk_t */
|
|
|
|
SM_NEEDS_TO_SHRINK = 2
|
|
|
|
};
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-24 20:32:09 +00:00
|
|
|
#define SM_CHUNK_GET_FLAGS(from, at) (((from)) & ((sm_bitvec_t)SM_FLAG_MASK << ((at)*2))) >> ((at)*2)
|
2024-04-10 19:34:19 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
typedef struct {
|
|
|
|
sm_bitvec_t *m_data;
|
|
|
|
} __sm_chunk_t;
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-24 20:32:09 +00:00
|
|
|
struct __attribute__((aligned(8))) sparsemap {
|
2024-04-11 03:16:06 +00:00
|
|
|
size_t m_capacity; /* The total size of m_data */
|
|
|
|
size_t m_data_used; /* The used size of m_data */
|
2024-04-24 20:32:09 +00:00
|
|
|
uint8_t *m_data; /* The serialized bitmap data */
|
2024-04-10 19:34:19 +00:00
|
|
|
};
|
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/**
|
|
|
|
* Calculates the number of sm_bitvec_ts required by a single byte with flags
|
|
|
|
* (in m_data[0]).
|
|
|
|
*/
|
|
|
|
static size_t
|
|
|
|
__sm_chunk_map_calc_vector_size(uint8_t b)
|
|
|
|
{
|
|
|
|
// clang-format off
|
|
|
|
static int lookup[] = {
|
|
|
|
0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 2, 1, 0, 0, 1, 0,
|
|
|
|
0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 2, 1, 0, 0, 1, 0,
|
|
|
|
1, 1, 2, 1, 1, 1, 2, 1, 2, 2, 3, 2, 1, 1, 2, 1,
|
|
|
|
0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 2, 1, 0, 0, 1, 0,
|
|
|
|
0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 2, 1, 0, 0, 1, 0,
|
|
|
|
0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 2, 1, 0, 0, 1, 0,
|
|
|
|
1, 1, 2, 1, 1, 1, 2, 1, 2, 2, 3, 2, 1, 1, 2, 1,
|
|
|
|
0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 2, 1, 0, 0, 1, 0,
|
|
|
|
1, 1, 2, 1, 1, 1, 2, 1, 2, 2, 3, 2, 1, 1, 2, 1,
|
|
|
|
1, 1, 2, 1, 1, 1, 2, 1, 2, 2, 3, 2, 1, 1, 2, 1,
|
|
|
|
2, 2, 3, 2, 2, 2, 3, 2, 3, 3, 4, 3, 2, 2, 3, 2,
|
|
|
|
1, 1, 2, 1, 1, 1, 2, 1, 2, 2, 3, 2, 1, 1, 2, 1,
|
|
|
|
0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 2, 1, 0, 0, 1, 0,
|
|
|
|
0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 2, 1, 0, 0, 1, 0,
|
|
|
|
1, 1, 2, 1, 1, 1, 2, 1, 2, 2, 3, 2, 1, 1, 2, 1,
|
|
|
|
0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 2, 1, 0, 0, 1, 0
|
|
|
|
};
|
|
|
|
// clang-format on
|
2024-04-24 20:32:09 +00:00
|
|
|
return (size_t)lookup[b];
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 16:10:21 +00:00
|
|
|
/** @brief Returns the position of a sm_bitvec_t in m_data.
|
|
|
|
*
|
|
|
|
* Each chunk has a set of bitvec that are sometimes abbreviated due to
|
|
|
|
* compression (e.g. when a bitvec is all zeros or ones there is no need
|
|
|
|
* to store anything, so no wasted space).
|
|
|
|
*
|
|
|
|
* @param[in] map The chunk in question.
|
|
|
|
* @param[in] bv The index of the vector to find in the chunk.
|
|
|
|
* @returns the position of a sm_bitvec_t in m_data
|
2024-04-04 19:24:02 +00:00
|
|
|
*/
|
|
|
|
static size_t
|
|
|
|
__sm_chunk_map_get_position(__sm_chunk_t *map, size_t bv)
|
|
|
|
{
|
2024-04-29 16:10:21 +00:00
|
|
|
/* Handle 4 indices (1 byte) at a time. */
|
2024-04-07 20:38:57 +00:00
|
|
|
size_t num_bytes = bv / ((size_t)SM_FLAGS_PER_INDEX_BYTE * SM_BITS_PER_VECTOR);
|
2024-04-04 19:24:02 +00:00
|
|
|
|
|
|
|
size_t position = 0;
|
|
|
|
register uint8_t *p = (uint8_t *)map->m_data;
|
|
|
|
for (size_t i = 0; i < num_bytes; i++, p++) {
|
|
|
|
position += __sm_chunk_map_calc_vector_size(*p);
|
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
bv -= num_bytes * SM_FLAGS_PER_INDEX_BYTE;
|
|
|
|
for (size_t i = 0; i < bv; i++) {
|
2024-04-10 19:34:19 +00:00
|
|
|
size_t flags = SM_CHUNK_GET_FLAGS(*map->m_data, i);
|
2024-04-04 19:24:02 +00:00
|
|
|
if (flags == SM_PAYLOAD_MIXED) {
|
|
|
|
position++;
|
|
|
|
}
|
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-24 20:32:09 +00:00
|
|
|
return position;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-29 16:10:21 +00:00
|
|
|
/** @brief Initialize __sm_chunk_t with provided data.
|
|
|
|
*
|
|
|
|
* @param[in] map The chunk in question.
|
|
|
|
* @param[in] data The memory to use within this chunk.
|
2024-04-04 19:24:02 +00:00
|
|
|
*/
|
2024-04-07 20:38:57 +00:00
|
|
|
static inline void
|
2024-04-04 19:24:02 +00:00
|
|
|
__sm_chunk_map_init(__sm_chunk_t *map, uint8_t *data)
|
|
|
|
{
|
|
|
|
map->m_data = (sm_bitvec_t *)data;
|
|
|
|
}
|
|
|
|
|
2024-04-29 16:10:21 +00:00
|
|
|
/** @brief Examines the chunk to determine its current capacity.
|
|
|
|
*
|
|
|
|
* @param[in] map The chunk in question.
|
|
|
|
* @returns the maximum capacity in bytes of this __sm_chunk_t
|
2024-04-04 19:24:02 +00:00
|
|
|
*/
|
|
|
|
static size_t
|
|
|
|
__sm_chunk_map_get_capacity(__sm_chunk_t *map)
|
|
|
|
{
|
|
|
|
size_t capacity = SM_CHUNK_MAX_CAPACITY;
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
register uint8_t *p = (uint8_t *)map->m_data;
|
|
|
|
for (size_t i = 0; i < sizeof(sm_bitvec_t); i++, p++) {
|
|
|
|
if (!*p) {
|
|
|
|
continue;
|
2024-04-03 00:41:55 +00:00
|
|
|
}
|
2024-04-04 19:24:02 +00:00
|
|
|
for (int j = 0; j < SM_FLAGS_PER_INDEX_BYTE; j++) {
|
2024-04-10 19:34:19 +00:00
|
|
|
size_t flags = SM_CHUNK_GET_FLAGS(*p, j);
|
2024-04-04 19:24:02 +00:00
|
|
|
if (flags == SM_PAYLOAD_NONE) {
|
|
|
|
capacity -= SM_BITS_PER_VECTOR;
|
2024-04-03 00:41:55 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-24 20:32:09 +00:00
|
|
|
return capacity;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 16:10:21 +00:00
|
|
|
/** @brief Sets the capacity of this chunk.
|
|
|
|
*
|
|
|
|
* @param[in] map The chunk in question.
|
|
|
|
* @param[in] capacity The new capacity in bytes to assign to the chunk,
|
|
|
|
* must be less than SM_CHUNK_MAX_CAPACITY.
|
2024-04-04 19:24:02 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
__sm_chunk_map_set_capacity(__sm_chunk_t *map, size_t capacity)
|
|
|
|
{
|
|
|
|
if (capacity >= SM_CHUNK_MAX_CAPACITY) {
|
|
|
|
return;
|
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
__sm_assert(capacity % SM_BITS_PER_VECTOR == 0);
|
|
|
|
|
|
|
|
size_t reduced = 0;
|
|
|
|
register uint8_t *p = (uint8_t *)map->m_data;
|
2024-05-03 01:13:17 +00:00
|
|
|
for (ssize_t i = sizeof(sm_bitvec_t) - 1; i >= 0; i--) {
|
2024-04-04 19:24:02 +00:00
|
|
|
for (int j = SM_FLAGS_PER_INDEX_BYTE - 1; j >= 0; j--) {
|
2024-04-10 19:34:19 +00:00
|
|
|
p[i] &= ~((sm_bitvec_t)SM_PAYLOAD_ONES << (j * 2));
|
|
|
|
p[i] |= ((sm_bitvec_t)SM_PAYLOAD_NONE << (j * 2));
|
2024-04-04 19:24:02 +00:00
|
|
|
reduced += SM_BITS_PER_VECTOR;
|
|
|
|
if (capacity + reduced == SM_CHUNK_MAX_CAPACITY) {
|
|
|
|
__sm_assert(__sm_chunk_map_get_capacity(map) == capacity);
|
|
|
|
return;
|
2024-04-03 00:41:55 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
|
|
|
__sm_assert(__sm_chunk_map_get_capacity(map) == capacity);
|
|
|
|
}
|
|
|
|
|
2024-04-29 16:10:21 +00:00
|
|
|
/** @brief Examines the chunk to determine if it is empty.
|
|
|
|
*
|
|
|
|
* @param[in] map The chunk in question.
|
|
|
|
* @returns true if this __sm_chunk_t is empty
|
2024-04-04 19:24:02 +00:00
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
__sm_chunk_map_is_empty(__sm_chunk_t *map)
|
|
|
|
{
|
|
|
|
/* The __sm_chunk_t is empty if all flags (in m_data[0]) are zero. */
|
|
|
|
if (map->m_data[0] == 0) {
|
2024-04-24 20:32:09 +00:00
|
|
|
return true;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* It's also empty if all flags are Zero or None. */
|
|
|
|
register uint8_t *p = (uint8_t *)map->m_data;
|
|
|
|
for (size_t i = 0; i < sizeof(sm_bitvec_t); i++, p++) {
|
|
|
|
if (*p) {
|
|
|
|
for (int j = 0; j < SM_FLAGS_PER_INDEX_BYTE; j++) {
|
2024-04-10 19:34:19 +00:00
|
|
|
size_t flags = SM_CHUNK_GET_FLAGS(*p, j);
|
2024-04-04 19:24:02 +00:00
|
|
|
if (flags != SM_PAYLOAD_NONE && flags != SM_PAYLOAD_ZEROS) {
|
2024-04-24 20:32:09 +00:00
|
|
|
return false;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-24 20:32:09 +00:00
|
|
|
return true;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 16:10:21 +00:00
|
|
|
/** @brief Examines the chunk to determine its size.
|
|
|
|
*
|
|
|
|
* @param[in] map The chunk in question.
|
|
|
|
* @returns the size of the data buffer, in bytes.
|
2024-04-04 19:24:02 +00:00
|
|
|
*/
|
|
|
|
static size_t
|
|
|
|
__sm_chunk_map_get_size(__sm_chunk_t *map)
|
|
|
|
{
|
|
|
|
/* At least one sm_bitvec_t is required for the flags (m_data[0]) */
|
|
|
|
size_t size = sizeof(sm_bitvec_t);
|
|
|
|
/* Use a lookup table for each byte of the flags */
|
|
|
|
register uint8_t *p = (uint8_t *)map->m_data;
|
|
|
|
for (size_t i = 0; i < sizeof(sm_bitvec_t); i++, p++) {
|
|
|
|
size += sizeof(sm_bitvec_t) * __sm_chunk_map_calc_vector_size(*p);
|
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-24 20:32:09 +00:00
|
|
|
return size;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-29 16:10:21 +00:00
|
|
|
/** @brief Examines the chunk at \b idx to determine that bit's state (set,
|
|
|
|
* or unset).
|
|
|
|
*
|
|
|
|
* @param[in] map The chunk in question.
|
|
|
|
* @param[in] idx The 0-based index into this chunk to examine.
|
|
|
|
* @returns the value of a bit at index \b idx
|
2024-04-04 19:24:02 +00:00
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
__sm_chunk_map_is_set(__sm_chunk_t *map, size_t idx)
|
|
|
|
{
|
|
|
|
/* 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 */
|
2024-04-10 19:34:19 +00:00
|
|
|
size_t flags = SM_CHUNK_GET_FLAGS(*map->m_data, bv);
|
2024-04-04 19:24:02 +00:00
|
|
|
switch (flags) {
|
|
|
|
case SM_PAYLOAD_ZEROS:
|
|
|
|
case SM_PAYLOAD_NONE:
|
2024-04-24 20:32:09 +00:00
|
|
|
return false;
|
2024-04-04 19:24:02 +00:00
|
|
|
case SM_PAYLOAD_ONES:
|
2024-04-24 20:32:09 +00:00
|
|
|
return true;
|
2024-04-04 19:24:02 +00:00
|
|
|
default:
|
|
|
|
__sm_assert(flags == SM_PAYLOAD_MIXED);
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the sm_bitvec_t at |bv| */
|
|
|
|
sm_bitvec_t w = map->m_data[1 + __sm_chunk_map_get_position(map, bv)];
|
|
|
|
/* and finally check the bit in that sm_bitvec_t */
|
2024-04-24 20:32:09 +00:00
|
|
|
return (w & ((sm_bitvec_t)1 << (idx % SM_BITS_PER_VECTOR))) > 0;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 16:10:21 +00:00
|
|
|
/** @brief Assigns a state to a bit in the chunk (set or unset).
|
2024-04-04 19:24:02 +00:00
|
|
|
*
|
2024-04-29 16:10:21 +00:00
|
|
|
* Sets the value of a bit at index \b idx. Then updates position \b pos to the
|
|
|
|
* position of the sm_bitvec_t which is inserted/deleted and \b fill - the value
|
|
|
|
* of the fill word (used when growing).
|
|
|
|
*
|
|
|
|
* @param[in] map The chunk in question.
|
|
|
|
* @param[in] idx The 0-based index into this chunk to mutate.
|
|
|
|
* @param[in] value The new state for the \b idx'th bit.
|
|
|
|
* @param[in,out] pos The position of the sm_bitvec_t inserted/deleted within the chunk.
|
|
|
|
* @param[in,out] fill The value of the fill word (when growing).
|
|
|
|
* @param[in] retired When not retried, grow the map by a bitvec.
|
|
|
|
* @returns \b SM_NEEDS_TO_GROW, \b SM_NEEDS_TO_SHRINK, or \b SM_OK
|
|
|
|
* @note, the caller MUST to perform the relevant actions and call set() again,
|
|
|
|
* this time with \b retried = true.
|
2024-04-04 19:24:02 +00:00
|
|
|
*/
|
|
|
|
static int
|
2024-04-07 20:38:57 +00:00
|
|
|
__sm_chunk_map_set(__sm_chunk_t *map, size_t idx, bool value, size_t *pos, sm_bitvec_t *fill, bool retried)
|
2024-04-04 19:24:02 +00:00
|
|
|
{
|
2024-04-04 23:56:31 +00:00
|
|
|
/* In which sm_bitvec_t is |idx| stored? */
|
2024-04-04 19:24:02 +00:00
|
|
|
size_t bv = idx / SM_BITS_PER_VECTOR;
|
|
|
|
__sm_assert(bv < SM_FLAGS_PER_INDEX);
|
|
|
|
|
2024-04-04 23:56:31 +00:00
|
|
|
/* Now retrieve the flags of that sm_bitvec_t. */
|
2024-04-10 19:34:19 +00:00
|
|
|
size_t flags = SM_CHUNK_GET_FLAGS(*map->m_data, bv);
|
2024-04-04 19:24:02 +00:00
|
|
|
assert(flags != SM_PAYLOAD_NONE);
|
|
|
|
if (flags == SM_PAYLOAD_ZEROS) {
|
2024-04-04 23:56:31 +00:00
|
|
|
/* Easy - set bit to 0 in a sm_bitvec_t of zeroes. */
|
2024-04-04 19:24:02 +00:00
|
|
|
if (value == false) {
|
|
|
|
*pos = 0;
|
|
|
|
*fill = 0;
|
|
|
|
return SM_OK;
|
2024-04-03 00:41:55 +00:00
|
|
|
}
|
2024-04-04 23:56:31 +00:00
|
|
|
/* The sparsemap must grow this __sm_chunk_t by one additional sm_bitvec_t,
|
|
|
|
then try again. */
|
2024-04-04 19:24:02 +00:00
|
|
|
if (!retried) {
|
|
|
|
*pos = 1 + __sm_chunk_map_get_position(map, bv);
|
|
|
|
*fill = 0;
|
|
|
|
return SM_NEEDS_TO_GROW;
|
|
|
|
}
|
2024-04-10 19:34:19 +00:00
|
|
|
/* New flags are 2#10 meaning SM_PAYLOAD_MIXED. Currently, flags are set
|
|
|
|
to 2#00, so 2#00 | 2#10 = 2#10. */
|
|
|
|
*map->m_data |= ((sm_bitvec_t)SM_PAYLOAD_MIXED << (bv * 2));
|
2024-04-04 19:24:02 +00:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
} else if (flags == SM_PAYLOAD_ONES) {
|
2024-04-10 19:34:19 +00:00
|
|
|
/* Easy - set bit to 1 in a sm_bitvec_t of ones. */
|
2024-04-04 19:24:02 +00:00
|
|
|
if (value == true) {
|
|
|
|
*pos = 0;
|
|
|
|
*fill = 0;
|
|
|
|
return SM_OK;
|
|
|
|
}
|
2024-04-10 19:34:19 +00:00
|
|
|
/* The sparsemap must grow this __sm_chunk_t by one additional sm_bitvec_t,
|
|
|
|
then try again. */
|
2024-04-04 19:24:02 +00:00
|
|
|
if (!retried) {
|
|
|
|
*pos = 1 + __sm_chunk_map_get_position(map, bv);
|
|
|
|
*fill = (sm_bitvec_t)-1;
|
|
|
|
return SM_NEEDS_TO_GROW;
|
|
|
|
}
|
2024-04-10 19:34:19 +00:00
|
|
|
/* New flags are 2#10 meaning SM_PAYLOAD_MIXED. Currently, flags are
|
|
|
|
set to 2#11, so 2#11 ^ 2#01 = 2#10. */
|
|
|
|
map->m_data[0] ^= ((sm_bitvec_t)SM_PAYLOAD_NONE << (bv * 2));
|
2024-04-04 19:24:02 +00:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-10 19:34:19 +00:00
|
|
|
/* Now flip the bit. */
|
2024-04-04 19:24:02 +00:00
|
|
|
size_t position = 1 + __sm_chunk_map_get_position(map, bv);
|
|
|
|
sm_bitvec_t w = map->m_data[position];
|
|
|
|
if (value) {
|
|
|
|
w |= (sm_bitvec_t)1 << (idx % SM_BITS_PER_VECTOR);
|
|
|
|
} else {
|
|
|
|
w &= ~((sm_bitvec_t)1 << (idx % SM_BITS_PER_VECTOR));
|
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-10 19:34:19 +00:00
|
|
|
/* If this sm_bitvec_t is now all zeroes or ones then we can remove it. */
|
2024-04-04 19:24:02 +00:00
|
|
|
if (w == 0) {
|
|
|
|
map->m_data[0] &= ~((sm_bitvec_t)SM_PAYLOAD_ONES << (bv * 2));
|
|
|
|
*pos = position;
|
|
|
|
*fill = 0;
|
|
|
|
return SM_NEEDS_TO_SHRINK;
|
|
|
|
}
|
|
|
|
if (w == (sm_bitvec_t)-1) {
|
|
|
|
map->m_data[0] |= (sm_bitvec_t)SM_PAYLOAD_ONES << (bv * 2);
|
|
|
|
*pos = position;
|
|
|
|
*fill = 0;
|
|
|
|
return SM_NEEDS_TO_SHRINK;
|
|
|
|
}
|
|
|
|
|
|
|
|
map->m_data[position] = w;
|
|
|
|
*pos = 0;
|
|
|
|
*fill = 0;
|
|
|
|
return SM_OK;
|
|
|
|
}
|
|
|
|
|
2024-04-30 18:40:23 +00:00
|
|
|
/** @brief Merges into the chunk at \b offset all set bits from \b src.
|
|
|
|
*
|
|
|
|
* @param[in] map The chunk in question.
|
|
|
|
* @param[in] offset The fully aligned offset of the chunk to be merged.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
__sm_chunk_map_merge(sparsemap_t *map, sparsemap_idx_t offset, __sm_chunk_t src)
|
|
|
|
{
|
|
|
|
size_t capacity = __sm_chunk_map_get_capacity(&src);
|
|
|
|
for (sparsemap_idx_t j = 0; j < capacity; j++, offset++) {
|
|
|
|
if (__sm_chunk_map_is_set(&src, j)) {
|
|
|
|
sparsemap_set(map, offset, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @brief Finds the index of the \b n'th bit after \b offset bits with \b
|
|
|
|
* value.
|
2024-04-29 16:10:21 +00:00
|
|
|
*
|
|
|
|
* Scans the chunk \b map until after \b offset bits (of any value) have
|
|
|
|
* passed and then begins counting the bits that match \b value looking
|
|
|
|
* for the \b n'th bit. It may not be in this chunk, when it is offset is
|
|
|
|
*
|
|
|
|
* @param[in] map The chunk in question.
|
|
|
|
* @param[in] value Informs what we're seeking, a set or unset bit's position.
|
|
|
|
* @param offset[in,out] Sets \b offset to 0 if the n'th bit was found
|
|
|
|
* in this __sm_chunk_t, or reduced value of \b n bits observed the search up
|
|
|
|
* to a maximum of SM_BITS_PER_VECTOR.
|
|
|
|
* @returns the 0-based index of the n'th set bit when found, otherwise
|
|
|
|
* SM_BITS_PER_VECTOR
|
2024-04-04 19:24:02 +00:00
|
|
|
*/
|
|
|
|
static size_t
|
2024-04-30 15:25:03 +00:00
|
|
|
__sm_chunk_map_select(__sm_chunk_t *map, size_t n, ssize_t *offset, bool value)
|
2024-04-04 19:24:02 +00:00
|
|
|
{
|
|
|
|
size_t ret = 0;
|
|
|
|
register uint8_t *p;
|
|
|
|
|
|
|
|
p = (uint8_t *)map->m_data;
|
|
|
|
for (size_t i = 0; i < sizeof(sm_bitvec_t); i++, p++) {
|
2024-04-26 20:25:17 +00:00
|
|
|
if (*p == 0 && value) {
|
2024-04-04 19:24:02 +00:00
|
|
|
ret += (size_t)SM_FLAGS_PER_INDEX_BYTE * SM_BITS_PER_VECTOR;
|
|
|
|
continue;
|
2024-04-03 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
for (int j = 0; j < SM_FLAGS_PER_INDEX_BYTE; j++) {
|
2024-04-10 19:34:19 +00:00
|
|
|
size_t flags = SM_CHUNK_GET_FLAGS(*p, j);
|
2024-04-04 19:24:02 +00:00
|
|
|
if (flags == SM_PAYLOAD_NONE) {
|
|
|
|
continue;
|
2024-04-03 00:41:55 +00:00
|
|
|
}
|
2024-04-04 19:24:02 +00:00
|
|
|
if (flags == SM_PAYLOAD_ZEROS) {
|
2024-04-26 20:25:17 +00:00
|
|
|
if (value == true) {
|
2024-04-24 20:32:09 +00:00
|
|
|
ret += SM_BITS_PER_VECTOR;
|
|
|
|
continue;
|
|
|
|
} else {
|
2024-04-29 16:10:21 +00:00
|
|
|
if (n > SM_BITS_PER_VECTOR) {
|
|
|
|
n -= SM_BITS_PER_VECTOR;
|
2024-04-24 20:32:09 +00:00
|
|
|
ret += SM_BITS_PER_VECTOR;
|
|
|
|
continue;
|
|
|
|
}
|
2024-04-29 16:10:21 +00:00
|
|
|
*offset = -1;
|
|
|
|
return ret + n;
|
2024-04-24 20:32:09 +00:00
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
}
|
2024-04-04 19:24:02 +00:00
|
|
|
if (flags == SM_PAYLOAD_ONES) {
|
2024-04-28 16:26:31 +00:00
|
|
|
if (value == true) {
|
2024-04-29 16:10:21 +00:00
|
|
|
if (n > SM_BITS_PER_VECTOR) {
|
|
|
|
n -= SM_BITS_PER_VECTOR;
|
2024-04-24 20:32:09 +00:00
|
|
|
ret += SM_BITS_PER_VECTOR;
|
|
|
|
continue;
|
|
|
|
}
|
2024-04-29 16:10:21 +00:00
|
|
|
*offset = -1;
|
|
|
|
return ret + n;
|
2024-04-24 20:32:09 +00:00
|
|
|
} else {
|
2024-04-04 19:24:02 +00:00
|
|
|
ret += SM_BITS_PER_VECTOR;
|
|
|
|
continue;
|
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
}
|
2024-04-04 19:24:02 +00:00
|
|
|
if (flags == SM_PAYLOAD_MIXED) {
|
2024-04-07 20:38:57 +00:00
|
|
|
sm_bitvec_t w = map->m_data[1 + __sm_chunk_map_get_position(map, i * SM_FLAGS_PER_INDEX_BYTE + j)];
|
2024-04-04 19:24:02 +00:00
|
|
|
for (int k = 0; k < SM_BITS_PER_VECTOR; k++) {
|
2024-04-24 20:32:09 +00:00
|
|
|
if (value) {
|
|
|
|
if (w & ((sm_bitvec_t)1 << k)) {
|
2024-04-29 16:10:21 +00:00
|
|
|
if (n == 0) {
|
|
|
|
*offset = -1;
|
2024-04-24 20:32:09 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2024-04-29 16:10:21 +00:00
|
|
|
n--;
|
2024-04-24 20:32:09 +00:00
|
|
|
}
|
|
|
|
ret++;
|
|
|
|
} else {
|
|
|
|
if (!(w & ((sm_bitvec_t)1 << k))) {
|
2024-04-29 16:10:21 +00:00
|
|
|
if (n == 0) {
|
|
|
|
*offset = -1;
|
2024-04-24 20:32:09 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2024-04-29 16:10:21 +00:00
|
|
|
n--;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-24 20:32:09 +00:00
|
|
|
ret++;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-30 15:20:48 +00:00
|
|
|
*offset = n;
|
2024-04-24 20:32:09 +00:00
|
|
|
return ret;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-29 16:10:21 +00:00
|
|
|
/** @brief Counts the bits matching \b value in the range [0, \b idx]
|
|
|
|
* inclusive after ignoring the first \b offset bits in the chunk.
|
|
|
|
*
|
|
|
|
* Scans the chunk \b map until after \b offset bits (of any value) have
|
|
|
|
* passed and then begins counting the bits that match \b value. The
|
|
|
|
* result should never be greater than \b idx + 1 maxing out at
|
|
|
|
* SM_BITS_PER_VECTOR. A range of [0, 0] will count 1 bit at \b offset
|
|
|
|
* + 1 in this chunk. A range of [0, 9] will count 10 bits, starting
|
|
|
|
* with the 0th and ending with the 9th and return at most a count of
|
|
|
|
* 10.
|
|
|
|
*
|
|
|
|
* @param[in] map The chunk in question.
|
|
|
|
* @param offset[in,out] Decreases \b offset by the number of bits ignored,
|
|
|
|
* at most by SM_BITS_PER_VECTOR.
|
|
|
|
* @param[in] idx The ending value of the range (inclusive) to count.
|
|
|
|
* @param[out] pos The position of the last bit examined in this chunk, always
|
|
|
|
* <= SM_BITS_PER_VECTOR, used when counting unset bits that fall within this
|
|
|
|
* chunk's range but after the last set bit.
|
|
|
|
* @param[out] vec The last sm_bitvec_t, masked and shifted, so as to be able
|
|
|
|
* to examine the bits used in the last portion of the ranking as a way to
|
|
|
|
* skip forward during a #span() operation.
|
|
|
|
* @param[in] value Informs what we're seeking, set or unset bits.
|
|
|
|
* @returns the count of the bits matching \b value within the range.
|
2024-04-04 19:24:02 +00:00
|
|
|
*/
|
|
|
|
static size_t
|
2024-04-24 20:32:09 +00:00
|
|
|
__sm_chunk_map_rank(__sm_chunk_t *map, size_t *offset, size_t idx, size_t *pos, sm_bitvec_t *vec, bool value)
|
2024-04-04 19:24:02 +00:00
|
|
|
{
|
|
|
|
size_t ret = 0;
|
|
|
|
|
2024-04-24 20:32:09 +00:00
|
|
|
*pos = 0;
|
|
|
|
|
|
|
|
/* A chunk can only hold at most SM_CHUNK_MAX_CAPACITY bits, so if the
|
|
|
|
offset is larger than that, we're basically done. */
|
|
|
|
if (*offset > SM_CHUNK_MAX_CAPACITY) {
|
|
|
|
*pos = SM_CHUNK_MAX_CAPACITY;
|
|
|
|
*offset -= SM_CHUNK_MAX_CAPACITY;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
register uint8_t *p = (uint8_t *)map->m_data;
|
|
|
|
for (size_t i = 0; i < sizeof(sm_bitvec_t); i++, p++) {
|
|
|
|
for (int j = 0; j < SM_FLAGS_PER_INDEX_BYTE; j++) {
|
2024-04-10 19:34:19 +00:00
|
|
|
size_t flags = SM_CHUNK_GET_FLAGS(*p, j);
|
2024-04-04 19:24:02 +00:00
|
|
|
if (flags == SM_PAYLOAD_NONE) {
|
|
|
|
continue;
|
2024-04-03 00:41:55 +00:00
|
|
|
}
|
2024-04-04 19:24:02 +00:00
|
|
|
if (flags == SM_PAYLOAD_ZEROS) {
|
2024-04-24 20:32:09 +00:00
|
|
|
*vec = 0;
|
|
|
|
if (idx >= SM_BITS_PER_VECTOR) {
|
|
|
|
*pos += SM_BITS_PER_VECTOR;
|
|
|
|
idx -= SM_BITS_PER_VECTOR;
|
2024-04-10 02:43:56 +00:00
|
|
|
if (*offset > SM_BITS_PER_VECTOR) {
|
|
|
|
*offset = *offset - SM_BITS_PER_VECTOR;
|
2024-04-07 20:38:57 +00:00
|
|
|
} else {
|
2024-04-24 20:32:09 +00:00
|
|
|
if (value == false) {
|
|
|
|
ret += SM_BITS_PER_VECTOR - *offset;
|
|
|
|
}
|
2024-04-10 02:43:56 +00:00
|
|
|
*offset = 0;
|
2024-04-07 20:38:57 +00:00
|
|
|
}
|
2024-04-04 19:24:02 +00:00
|
|
|
} else {
|
2024-04-24 20:32:09 +00:00
|
|
|
*pos += idx + 1;
|
|
|
|
if (value == false) {
|
|
|
|
if (*offset > idx) {
|
|
|
|
*offset = *offset - idx;
|
|
|
|
} else {
|
|
|
|
ret += idx + 1 - *offset;
|
|
|
|
*offset = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return ret;
|
|
|
|
}
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
|
|
|
} else if (flags == SM_PAYLOAD_ONES) {
|
2024-04-24 20:32:09 +00:00
|
|
|
*vec = UINT64_MAX;
|
|
|
|
if (idx >= SM_BITS_PER_VECTOR) {
|
|
|
|
*pos += SM_BITS_PER_VECTOR;
|
|
|
|
idx -= SM_BITS_PER_VECTOR;
|
2024-04-10 02:43:56 +00:00
|
|
|
if (*offset > SM_BITS_PER_VECTOR) {
|
|
|
|
*offset = *offset - SM_BITS_PER_VECTOR;
|
2024-04-07 20:38:57 +00:00
|
|
|
} else {
|
2024-04-24 20:32:09 +00:00
|
|
|
if (value == true) {
|
|
|
|
ret += SM_BITS_PER_VECTOR - *offset;
|
2024-04-07 20:38:57 +00:00
|
|
|
}
|
2024-04-10 02:43:56 +00:00
|
|
|
*offset = 0;
|
2024-04-07 20:38:57 +00:00
|
|
|
}
|
2024-04-04 19:24:02 +00:00
|
|
|
} else {
|
2024-04-24 20:32:09 +00:00
|
|
|
*pos += idx + 1;
|
|
|
|
if (value == true) {
|
|
|
|
if (*offset > idx) {
|
|
|
|
*offset = *offset - idx;
|
|
|
|
} else {
|
|
|
|
ret += idx + 1 - *offset;
|
|
|
|
*offset = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return ret;
|
|
|
|
}
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
|
|
|
} else if (flags == SM_PAYLOAD_MIXED) {
|
2024-04-09 18:46:49 +00:00
|
|
|
sm_bitvec_t w = map->m_data[1 + __sm_chunk_map_get_position(map, i * SM_FLAGS_PER_INDEX_BYTE + j)];
|
2024-04-24 20:32:09 +00:00
|
|
|
if (idx >= SM_BITS_PER_VECTOR) {
|
|
|
|
*pos += SM_BITS_PER_VECTOR;
|
2024-04-10 02:12:07 +00:00
|
|
|
idx -= SM_BITS_PER_VECTOR;
|
2024-04-24 20:32:09 +00:00
|
|
|
uint64_t mask = *offset == 0 ? UINT64_MAX : ~(UINT64_MAX >> (SM_BITS_PER_VECTOR - (*offset >= 64 ? 64 : *offset)));
|
|
|
|
sm_bitvec_t mw;
|
|
|
|
if (value == true) {
|
|
|
|
mw = w & mask;
|
|
|
|
} else {
|
|
|
|
mw = ~w & mask;
|
|
|
|
}
|
|
|
|
size_t pc = popcountll(mw);
|
|
|
|
ret += pc;
|
2024-04-10 02:43:56 +00:00
|
|
|
*offset = (*offset > SM_BITS_PER_VECTOR) ? *offset - SM_BITS_PER_VECTOR : 0;
|
2024-04-04 19:24:02 +00:00
|
|
|
} else {
|
2024-04-24 20:32:09 +00:00
|
|
|
*pos += idx + 1;
|
|
|
|
sm_bitvec_t mw;
|
|
|
|
uint64_t mask;
|
|
|
|
uint64_t idx_mask = (idx == 63) ? UINT64_MAX : ((uint64_t)1 << (idx + 1)) - 1;
|
|
|
|
uint64_t offset_mask = *offset == 0 ? UINT64_MAX : ~(UINT64_MAX >> (SM_BITS_PER_VECTOR - (*offset >= 64 ? 64 : *offset)));
|
|
|
|
/* To count the set bits we need to mask off the portion of the vector that we need
|
|
|
|
to count then call popcount(). So, let's create a mask for the range between
|
|
|
|
offset and idx inclusive [*offset, idx]. */
|
|
|
|
mask = idx_mask & offset_mask;
|
2024-04-28 16:26:31 +00:00
|
|
|
if (value) {
|
2024-04-24 20:32:09 +00:00
|
|
|
mw = w & mask;
|
|
|
|
} else {
|
|
|
|
mw = ~w & mask;
|
|
|
|
}
|
|
|
|
int pc = popcountll(mw);
|
|
|
|
ret += pc;
|
2024-04-29 16:10:21 +00:00
|
|
|
*vec = mw >> ((*offset > 63) ? 63 : *offset);
|
2024-04-24 20:32:09 +00:00
|
|
|
*offset = *offset > idx ? *offset - idx + 1 : 0;
|
|
|
|
return ret;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-24 20:32:09 +00:00
|
|
|
return ret;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 16:10:21 +00:00
|
|
|
/** @brief Calls \b scanner with sm_bitmap_t for each vector in this chunk.
|
|
|
|
*
|
|
|
|
* Decompresses the whole chunk into separate bitmaps then calls visitor's
|
|
|
|
* \b #operator() function for all bits.
|
|
|
|
*
|
|
|
|
* @param[in] map The chunk in question.
|
|
|
|
* @param[in] start
|
|
|
|
* @param[in] scanner
|
2024-05-02 18:55:04 +00:00
|
|
|
* @param[in] skip The number of
|
2024-04-29 16:10:21 +00:00
|
|
|
* @returns the number of (set) bits that were passed to the scanner
|
2024-04-04 19:24:02 +00:00
|
|
|
*/
|
|
|
|
static size_t
|
2024-05-02 18:55:04 +00:00
|
|
|
__sm_chunk_map_scan(__sm_chunk_t *map, sm_idx_t start, void (*scanner)(sm_idx_t[], size_t, void *aux), size_t skip, void *aux)
|
2024-04-04 19:24:02 +00:00
|
|
|
{
|
|
|
|
size_t ret = 0;
|
|
|
|
register uint8_t *p = (uint8_t *)map->m_data;
|
|
|
|
sm_idx_t buffer[SM_BITS_PER_VECTOR];
|
|
|
|
for (size_t i = 0; i < sizeof(sm_bitvec_t); i++, p++) {
|
|
|
|
if (*p == 0) {
|
2024-05-02 18:55:04 +00:00
|
|
|
/* Skip chunks that are all zeroes. */
|
|
|
|
skip -= skip > SM_BITS_PER_VECTOR ? SM_BITS_PER_VECTOR : skip;
|
2024-04-04 19:24:02 +00:00
|
|
|
continue;
|
2024-04-03 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
for (int j = 0; j < SM_FLAGS_PER_INDEX_BYTE; j++) {
|
2024-04-10 19:34:19 +00:00
|
|
|
size_t flags = SM_CHUNK_GET_FLAGS(*p, j);
|
2024-04-04 19:24:02 +00:00
|
|
|
if (flags == SM_PAYLOAD_NONE || flags == SM_PAYLOAD_ZEROS) {
|
2024-05-02 18:55:04 +00:00
|
|
|
/* Skip when all zeroes. */
|
|
|
|
skip -= skip > SM_BITS_PER_VECTOR ? SM_BITS_PER_VECTOR : skip;
|
2024-04-04 19:24:02 +00:00
|
|
|
} else if (flags == SM_PAYLOAD_ONES) {
|
|
|
|
if (skip) {
|
|
|
|
if (skip >= SM_BITS_PER_VECTOR) {
|
|
|
|
skip -= SM_BITS_PER_VECTOR;
|
|
|
|
ret += SM_BITS_PER_VECTOR;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
size_t n = 0;
|
|
|
|
for (size_t b = skip; b < SM_BITS_PER_VECTOR; b++) {
|
|
|
|
buffer[n++] = start + b;
|
|
|
|
}
|
2024-05-02 18:55:04 +00:00
|
|
|
scanner(&buffer[0], n, aux);
|
2024-04-04 19:24:02 +00:00
|
|
|
ret += n;
|
|
|
|
skip = 0;
|
|
|
|
} else {
|
|
|
|
for (size_t b = 0; b < SM_BITS_PER_VECTOR; b++) {
|
|
|
|
buffer[b] = start + b;
|
|
|
|
}
|
2024-05-02 18:55:04 +00:00
|
|
|
scanner(&buffer[0], SM_BITS_PER_VECTOR, aux);
|
2024-04-04 19:24:02 +00:00
|
|
|
ret += SM_BITS_PER_VECTOR;
|
|
|
|
}
|
|
|
|
} else if (flags == SM_PAYLOAD_MIXED) {
|
2024-04-07 20:38:57 +00:00
|
|
|
sm_bitvec_t w = map->m_data[1 + __sm_chunk_map_get_position(map, i * SM_FLAGS_PER_INDEX_BYTE + j)];
|
2024-05-02 18:55:04 +00:00
|
|
|
size_t n = 0;
|
2024-04-04 19:24:02 +00:00
|
|
|
if (skip) {
|
2024-05-02 18:55:04 +00:00
|
|
|
if (skip >= SM_BITS_PER_VECTOR) {
|
|
|
|
skip -= SM_BITS_PER_VECTOR;
|
|
|
|
ret += SM_BITS_PER_VECTOR;
|
|
|
|
continue;
|
|
|
|
}
|
2024-04-04 19:24:02 +00:00
|
|
|
for (int b = 0; b < SM_BITS_PER_VECTOR; b++) {
|
2024-05-02 18:55:04 +00:00
|
|
|
if (skip > 0) {
|
2024-04-04 19:24:02 +00:00
|
|
|
skip--;
|
|
|
|
continue;
|
2024-05-02 18:55:04 +00:00
|
|
|
}
|
|
|
|
if (w & ((sm_bitvec_t)1 << b)) {
|
2024-04-04 19:24:02 +00:00
|
|
|
buffer[n++] = start + b;
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int b = 0; b < SM_BITS_PER_VECTOR; b++) {
|
|
|
|
if (w & ((sm_bitvec_t)1 << b)) {
|
|
|
|
buffer[n++] = start + b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret += n;
|
|
|
|
}
|
|
|
|
__sm_assert(n > 0);
|
2024-05-02 18:55:04 +00:00
|
|
|
scanner(&buffer[0], n, aux);
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
}
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-24 20:32:09 +00:00
|
|
|
return ret;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 16:10:21 +00:00
|
|
|
/** @brief Provides the number of chunks currently in the map.
|
|
|
|
*
|
|
|
|
* @param[in] map The sparsemap_t in question.
|
|
|
|
* @returns the number of chunk maps in the sparsemap
|
2024-04-04 19:24:02 +00:00
|
|
|
*/
|
|
|
|
static size_t
|
|
|
|
__sm_get_chunk_map_count(sparsemap_t *map)
|
|
|
|
{
|
2024-04-24 20:32:09 +00:00
|
|
|
return *(uint32_t *)&map->m_data[0];
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 16:10:21 +00:00
|
|
|
/** @brief Encapsulates the method to find the starting address of a chunk's
|
|
|
|
* data.
|
|
|
|
*
|
|
|
|
* @param[in] map The sparsemap_t in question.
|
|
|
|
* @param[in] offset The offset in bytes for the desired chunk map.
|
|
|
|
* @returns the data for the specified \b offset
|
2024-04-04 19:24:02 +00:00
|
|
|
*/
|
2024-04-10 19:34:19 +00:00
|
|
|
static inline uint8_t *
|
2024-04-04 19:24:02 +00:00
|
|
|
__sm_get_chunk_map_data(sparsemap_t *map, size_t offset)
|
|
|
|
{
|
2024-04-24 20:32:09 +00:00
|
|
|
return &map->m_data[SM_SIZEOF_OVERHEAD + offset];
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 16:10:21 +00:00
|
|
|
/** @brief Encapsulates the method to find the address of the first unused byte
|
|
|
|
* in \b m_data.
|
|
|
|
*
|
|
|
|
* @param[in] map The sparsemap_t in question.
|
|
|
|
* @returns a pointer after the end of the used data
|
2024-04-04 19:24:02 +00:00
|
|
|
*/
|
|
|
|
static uint8_t *
|
|
|
|
__sm_get_chunk_map_end(sparsemap_t *map)
|
|
|
|
{
|
|
|
|
uint8_t *p = __sm_get_chunk_map_data(map, 0);
|
|
|
|
size_t count = __sm_get_chunk_map_count(map);
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
p += sizeof(sm_idx_t);
|
|
|
|
__sm_chunk_t chunk;
|
|
|
|
__sm_chunk_map_init(&chunk, p);
|
|
|
|
p += __sm_chunk_map_get_size(&chunk);
|
|
|
|
}
|
2024-04-24 20:32:09 +00:00
|
|
|
return p;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 16:10:21 +00:00
|
|
|
/** @brief Provides the byte size amount of \b m_data consumed.
|
|
|
|
*
|
|
|
|
* @param[in] map The sparsemap_t in question.
|
|
|
|
* @returns the used size in the data buffer
|
2024-04-04 19:24:02 +00:00
|
|
|
*/
|
|
|
|
static size_t
|
|
|
|
__sm_get_size_impl(sparsemap_t *map)
|
|
|
|
{
|
|
|
|
uint8_t *start = __sm_get_chunk_map_data(map, 0);
|
|
|
|
uint8_t *p = start;
|
|
|
|
|
|
|
|
size_t count = __sm_get_chunk_map_count(map);
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
p += sizeof(sm_idx_t);
|
|
|
|
__sm_chunk_t chunk;
|
|
|
|
__sm_chunk_map_init(&chunk, p);
|
|
|
|
p += __sm_chunk_map_get_size(&chunk);
|
|
|
|
}
|
2024-04-24 20:32:09 +00:00
|
|
|
return SM_SIZEOF_OVERHEAD + p - start;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 16:10:21 +00:00
|
|
|
/** @brief Aligns to SM_BITS_PER_VECTOR a given index \b idx.
|
|
|
|
*
|
|
|
|
* @param[in] idx The index to align.
|
|
|
|
* @returns the aligned offset (aligned to sm_bitvec_t capacity).
|
2024-04-04 19:58:06 +00:00
|
|
|
*/
|
|
|
|
static sm_idx_t
|
|
|
|
__sm_get_aligned_offset(size_t idx)
|
|
|
|
{
|
|
|
|
const size_t capacity = SM_BITS_PER_VECTOR;
|
2024-04-24 20:32:09 +00:00
|
|
|
return (idx / capacity) * capacity;
|
2024-04-04 19:58:06 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 16:10:21 +00:00
|
|
|
/** @brief Aligns to SM_CHUNK_MAP_CAPACITY a given index \b idx.
|
|
|
|
*
|
|
|
|
* @param[in] idx The index to align.
|
|
|
|
* @returns the aligned offset (aligned to __sm_chunk_t capacity)
|
|
|
|
*/
|
|
|
|
static sm_idx_t
|
|
|
|
__sm_get_fully_aligned_offset(size_t idx)
|
|
|
|
{
|
|
|
|
const size_t capacity = SM_CHUNK_MAX_CAPACITY;
|
|
|
|
return (idx / capacity) * capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @brief Provides the byte offset of a chunk map at index \b idx.
|
|
|
|
*
|
|
|
|
* @param[in] map The sparsemap_t in question.
|
|
|
|
* @param[in] idx The index of the chunk map to locate.
|
2024-04-30 15:20:48 +00:00
|
|
|
* @returns the byte offset of a __sm_chunk_t in m_data, or -1 there
|
|
|
|
* are no chunks.
|
2024-04-04 19:24:02 +00:00
|
|
|
*/
|
2024-04-29 16:10:21 +00:00
|
|
|
static size_t
|
2024-04-24 20:32:09 +00:00
|
|
|
__sm_get_chunk_map_offset(sparsemap_t *map, sparsemap_idx_t idx)
|
2024-04-04 19:24:02 +00:00
|
|
|
{
|
2024-04-28 16:26:31 +00:00
|
|
|
size_t count = __sm_get_chunk_map_count(map);
|
2024-04-04 19:24:02 +00:00
|
|
|
if (count == 0) {
|
2024-04-24 20:32:09 +00:00
|
|
|
return -1;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-28 16:26:31 +00:00
|
|
|
uint8_t *start = __sm_get_chunk_map_data(map, 0);
|
|
|
|
uint8_t *p = start;
|
2024-04-24 20:32:09 +00:00
|
|
|
|
2024-04-28 16:26:31 +00:00
|
|
|
for (sparsemap_idx_t i = 0; i < count - 1; i++) {
|
|
|
|
sm_idx_t s = *(sm_idx_t *)p;
|
|
|
|
__sm_assert(s == __sm_get_aligned_offset(s));
|
|
|
|
__sm_chunk_t chunk;
|
|
|
|
__sm_chunk_map_init(&chunk, p + sizeof(sm_idx_t));
|
2024-04-30 15:20:48 +00:00
|
|
|
if (s >= idx || idx < s + __sm_chunk_map_get_capacity(&chunk)) {
|
2024-04-28 16:26:31 +00:00
|
|
|
break;
|
2024-04-24 20:32:09 +00:00
|
|
|
}
|
2024-04-28 16:26:31 +00:00
|
|
|
p += sizeof(sm_idx_t) + __sm_chunk_map_get_size(&chunk);
|
2024-04-24 20:32:09 +00:00
|
|
|
}
|
2024-04-28 16:26:31 +00:00
|
|
|
|
|
|
|
return (ssize_t)(p - start);
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-29 16:10:21 +00:00
|
|
|
/** @brief Sets the number of __sm_chunk_t's.
|
|
|
|
*
|
|
|
|
* @param[in] map The sparsemap_t in question.
|
|
|
|
* @param[in] new_count The new number of chunks in the map.
|
2024-04-04 19:24:02 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
__sm_set_chunk_map_count(sparsemap_t *map, size_t new_count)
|
|
|
|
{
|
|
|
|
*(uint32_t *)&map->m_data[0] = (uint32_t)new_count;
|
|
|
|
}
|
|
|
|
|
2024-04-29 16:10:21 +00:00
|
|
|
/** @brief Appends raw data at the end of used portion of \b m_data.
|
|
|
|
*
|
|
|
|
* @param[in] map The sparsemap_t in question.
|
|
|
|
* @param[in] buffer The bytes to copy into \b m_data.
|
|
|
|
* @param[in] buffer_size The size of the byte array \b buffer to copy.
|
2024-04-04 19:24:02 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
__sm_append_data(sparsemap_t *map, uint8_t *buffer, size_t buffer_size)
|
|
|
|
{
|
|
|
|
memcpy(&map->m_data[map->m_data_used], buffer, buffer_size);
|
|
|
|
map->m_data_used += buffer_size;
|
|
|
|
}
|
|
|
|
|
2024-04-29 16:10:21 +00:00
|
|
|
/** @brief Inserts data at \b offset in the middle of \b m_data.
|
|
|
|
*
|
|
|
|
* @param[in] map The sparsemap_t in question.
|
|
|
|
* @param[in] offset The offset in bytes into \b m_data to place the buffer.
|
|
|
|
* @param[in] buffer The bytes to copy into \b m_data.
|
|
|
|
* @param[in] buffer_size The size of the byte array \b buffer to copy.
|
2024-04-04 19:24:02 +00:00
|
|
|
*/
|
2024-04-24 20:32:09 +00:00
|
|
|
void
|
2024-04-07 20:38:57 +00:00
|
|
|
__sm_insert_data(sparsemap_t *map, size_t offset, uint8_t *buffer, size_t buffer_size)
|
2024-04-04 19:24:02 +00:00
|
|
|
{
|
|
|
|
uint8_t *p = __sm_get_chunk_map_data(map, offset);
|
|
|
|
memmove(p + buffer_size, p, map->m_data_used - offset);
|
|
|
|
memcpy(p, buffer, buffer_size);
|
|
|
|
map->m_data_used += buffer_size;
|
|
|
|
}
|
|
|
|
|
2024-04-29 16:10:21 +00:00
|
|
|
/** @brief Removes data from \b m_data.
|
|
|
|
*
|
|
|
|
* @param[in] map The sparsemap_t in question.
|
|
|
|
* @param[in] offset The offset in bytes into \b m_data at which to excise data.
|
|
|
|
* @param[in] gap_size The size of the excision.
|
2024-04-04 19:24:02 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
__sm_remove_data(sparsemap_t *map, size_t offset, size_t gap_size)
|
|
|
|
{
|
|
|
|
assert(map->m_data_used >= offset + gap_size);
|
|
|
|
uint8_t *p = __sm_get_chunk_map_data(map, offset);
|
|
|
|
memmove(p, p + gap_size, map->m_data_used - offset - gap_size);
|
|
|
|
map->m_data_used -= gap_size;
|
|
|
|
}
|
|
|
|
|
2024-04-29 16:10:21 +00:00
|
|
|
/*
|
|
|
|
* The following is the "Sparsemap" implementation, it uses chunk maps (code above)
|
|
|
|
* and is the public API for this compressed bitmap representation.
|
2024-04-07 20:38:57 +00:00
|
|
|
*/
|
2024-04-29 16:10:21 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
void
|
2024-04-04 19:58:06 +00:00
|
|
|
sparsemap_clear(sparsemap_t *map)
|
2024-04-04 19:24:02 +00:00
|
|
|
{
|
2024-04-24 20:32:09 +00:00
|
|
|
if (map == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2024-04-10 19:34:19 +00:00
|
|
|
memset(map->m_data, 0, map->m_capacity);
|
2024-04-04 19:24:02 +00:00
|
|
|
map->m_data_used = SM_SIZEOF_OVERHEAD;
|
|
|
|
__sm_set_chunk_map_count(map, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
sparsemap_t *
|
2024-04-24 20:32:09 +00:00
|
|
|
sparsemap(size_t size)
|
|
|
|
{
|
|
|
|
if (size == 0) {
|
|
|
|
size = 1024;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t data_size = (size * sizeof(uint8_t));
|
|
|
|
|
|
|
|
/* Ensure that m_data is 8-byte aligned. */
|
|
|
|
size_t total_size = sizeof(sparsemap_t) + data_size;
|
|
|
|
size_t padding = total_size % 8 == 0 ? 0 : 8 - (total_size % 8);
|
|
|
|
total_size += padding;
|
|
|
|
|
|
|
|
sparsemap_t *map = (sparsemap_t *)calloc(1, total_size);
|
|
|
|
if (map) {
|
|
|
|
uint8_t *data = (uint8_t *)(((uintptr_t)map + sizeof(sparsemap_t)) & ~(uintptr_t)7);
|
|
|
|
sparsemap_init(map, data, size);
|
|
|
|
__sm_when_diag({ __sm_assert(IS_8_BYTE_ALIGNED(map->m_data)); });
|
|
|
|
}
|
|
|
|
sparsemap_clear(map);
|
|
|
|
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
sparsemap_t *
|
|
|
|
sparsemap_wrap(uint8_t *data, size_t size)
|
2024-04-04 19:24:02 +00:00
|
|
|
{
|
|
|
|
sparsemap_t *map = (sparsemap_t *)calloc(1, sizeof(sparsemap_t));
|
|
|
|
if (map) {
|
2024-04-10 19:34:19 +00:00
|
|
|
sparsemap_init(map, data, size);
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2024-04-10 19:34:19 +00:00
|
|
|
sparsemap_init(sparsemap_t *map, uint8_t *data, size_t size)
|
2024-04-04 19:24:02 +00:00
|
|
|
{
|
2024-04-10 19:48:53 +00:00
|
|
|
map->m_data = data;
|
2024-04-10 19:34:19 +00:00
|
|
|
map->m_data_used = 0;
|
2024-04-24 20:32:09 +00:00
|
|
|
map->m_capacity = size;
|
2024-04-04 19:58:06 +00:00
|
|
|
sparsemap_clear(map);
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
2024-04-05 14:34:59 +00:00
|
|
|
void
|
2024-04-26 20:25:17 +00:00
|
|
|
sparsemap_open(sparsemap_t *map, uint8_t *data, size_t size)
|
2024-04-04 19:24:02 +00:00
|
|
|
{
|
2024-04-10 19:48:53 +00:00
|
|
|
map->m_data = data;
|
2024-04-24 20:32:09 +00:00
|
|
|
map->m_data_used = map->m_data_used > 0 ? map->m_data_used : 0;
|
2024-04-26 20:25:17 +00:00
|
|
|
map->m_capacity = size;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
2024-04-24 20:32:09 +00:00
|
|
|
sparsemap_t *
|
2024-04-26 20:25:17 +00:00
|
|
|
sparsemap_set_data_size(sparsemap_t *map, size_t size, uint8_t *data)
|
2024-04-04 19:24:02 +00:00
|
|
|
{
|
2024-04-26 20:25:17 +00:00
|
|
|
size_t data_size = (size * sizeof(uint8_t));
|
|
|
|
|
|
|
|
/* If this sparsemap was allocated by the sparsemap() API and we're not handed
|
|
|
|
a new data, it's up to us to resize it. */
|
|
|
|
if (data == NULL && (uintptr_t)map->m_data == (uintptr_t)map + sizeof(sparsemap_t) && size > map->m_capacity) {
|
2024-04-24 20:32:09 +00:00
|
|
|
|
|
|
|
/* Ensure that m_data is 8-byte aligned. */
|
|
|
|
size_t total_size = sizeof(sparsemap_t) + data_size;
|
|
|
|
size_t padding = total_size % 8 == 0 ? 0 : 8 - (total_size % 8);
|
|
|
|
total_size += padding;
|
|
|
|
|
|
|
|
sparsemap_t *m = (sparsemap_t *)realloc(map, total_size);
|
|
|
|
if (!m) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memset(((uint8_t *)m) + sizeof(sparsemap_t) + (m->m_capacity * sizeof(uint8_t)), 0, size - m->m_capacity + padding);
|
|
|
|
m->m_capacity = data_size;
|
|
|
|
m->m_data = (uint8_t *)(((uintptr_t)m + sizeof(sparsemap_t)) & ~(uintptr_t)7);
|
|
|
|
__sm_when_diag({ __sm_assert(IS_8_BYTE_ALIGNED(m->m_data)); }) return m;
|
|
|
|
} else {
|
2024-05-02 18:55:04 +00:00
|
|
|
/* NOTE: It is up to the caller to realloc their buffer and provide it here
|
|
|
|
for reassignment. */
|
2024-05-02 18:57:56 +00:00
|
|
|
if (data != NULL && data_size > sparsemap_get_capacity(map) && data != map->m_data) {
|
2024-04-26 20:25:17 +00:00
|
|
|
map->m_data = data;
|
|
|
|
}
|
2024-04-24 20:32:09 +00:00
|
|
|
map->m_capacity = size;
|
|
|
|
return map;
|
|
|
|
}
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
2024-04-09 13:13:38 +00:00
|
|
|
double
|
2024-04-11 03:16:06 +00:00
|
|
|
sparsemap_capacity_remaining(sparsemap_t *map)
|
|
|
|
{
|
2024-04-10 19:34:19 +00:00
|
|
|
if (map->m_data_used > map->m_capacity) {
|
2024-04-09 03:23:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2024-04-10 19:34:19 +00:00
|
|
|
if (map->m_capacity == 0) {
|
2024-04-09 13:13:38 +00:00
|
|
|
return 100.0;
|
|
|
|
}
|
2024-04-10 19:34:19 +00:00
|
|
|
return 100 - (((double)map->m_data_used / (double)map->m_capacity) * 100);
|
2024-04-09 03:23:22 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
size_t
|
2024-04-10 19:34:19 +00:00
|
|
|
sparsemap_get_capacity(sparsemap_t *map)
|
2024-04-04 19:24:02 +00:00
|
|
|
{
|
2024-04-24 20:32:09 +00:00
|
|
|
return map->m_capacity;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2024-04-24 20:32:09 +00:00
|
|
|
sparsemap_is_set(sparsemap_t *map, sparsemap_idx_t idx)
|
2024-04-04 19:24:02 +00:00
|
|
|
{
|
2024-04-04 19:58:06 +00:00
|
|
|
__sm_assert(sparsemap_get_size(map) >= SM_SIZEOF_OVERHEAD);
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* Get the __sm_chunk_t which manages this index */
|
|
|
|
ssize_t offset = __sm_get_chunk_map_offset(map, idx);
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* No __sm_chunk_t's available -> the bit is not set */
|
|
|
|
if (offset == -1) {
|
2024-04-24 20:32:09 +00:00
|
|
|
return false;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* Otherwise load the __sm_chunk_t */
|
|
|
|
uint8_t *p = __sm_get_chunk_map_data(map, offset);
|
|
|
|
sm_idx_t start = *(sm_idx_t *)p;
|
|
|
|
__sm_chunk_t chunk;
|
|
|
|
__sm_chunk_map_init(&chunk, p + sizeof(sm_idx_t));
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* Determine if the bit is out of bounds of the __sm_chunk_t; if yes then
|
2024-04-24 20:32:09 +00:00
|
|
|
the bit is not set. */
|
|
|
|
if (idx < start || (unsigned long)idx - start >= __sm_chunk_map_get_capacity(&chunk)) {
|
|
|
|
return false;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* Otherwise ask the __sm_chunk_t whether the bit is set. */
|
2024-04-24 20:32:09 +00:00
|
|
|
return __sm_chunk_map_is_set(&chunk, idx - start);
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-24 20:32:09 +00:00
|
|
|
sparsemap_idx_t
|
|
|
|
sparsemap_set(sparsemap_t *map, sparsemap_idx_t idx, bool value)
|
2024-04-04 19:24:02 +00:00
|
|
|
{
|
2024-04-04 19:58:06 +00:00
|
|
|
__sm_assert(sparsemap_get_size(map) >= SM_SIZEOF_OVERHEAD);
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* Get the __sm_chunk_t which manages this index */
|
|
|
|
ssize_t offset = __sm_get_chunk_map_offset(map, idx);
|
|
|
|
bool dont_grow = false;
|
2024-04-24 20:32:09 +00:00
|
|
|
if (map->m_data_used + sizeof(sm_idx_t) + sizeof(sm_bitvec_t) * 2 > map->m_capacity) {
|
|
|
|
errno = ENOSPC;
|
|
|
|
return SPARSEMAP_IDX_MAX;
|
|
|
|
}
|
|
|
|
|
2024-04-30 15:20:48 +00:00
|
|
|
/* If there are no __sm_chunk_t and the bit is set to zero then return
|
2024-04-04 19:24:02 +00:00
|
|
|
immediately; otherwise create an initial __sm_chunk_t. */
|
|
|
|
if (offset == -1) {
|
|
|
|
if (value == false) {
|
2024-04-24 20:32:09 +00:00
|
|
|
return idx;
|
2024-04-03 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
uint8_t buf[sizeof(sm_idx_t) + sizeof(sm_bitvec_t) * 2] = { 0 };
|
|
|
|
__sm_append_data(map, &buf[0], sizeof(buf));
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
uint8_t *p = __sm_get_chunk_map_data(map, 0);
|
2024-05-03 01:13:17 +00:00
|
|
|
*(sm_idx_t *)p = __sm_get_fully_aligned_offset(idx);
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
__sm_set_chunk_map_count(map, 1);
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-24 20:32:09 +00:00
|
|
|
/* We already inserted an additional sm_bitvec_t; given that has happened
|
|
|
|
there is no need to grow the vector even further. */
|
2024-04-04 19:24:02 +00:00
|
|
|
dont_grow = true;
|
|
|
|
offset = 0;
|
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* Load the __sm_chunk_t */
|
|
|
|
uint8_t *p = __sm_get_chunk_map_data(map, offset);
|
|
|
|
sm_idx_t start = *(sm_idx_t *)p;
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* The new index is smaller than the first __sm_chunk_t: create a new
|
|
|
|
__sm_chunk_t and insert it at the front. */
|
|
|
|
if (idx < start) {
|
|
|
|
if (value == false) {
|
|
|
|
/* nothing to do */
|
2024-04-24 20:32:09 +00:00
|
|
|
return idx;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
uint8_t buf[sizeof(sm_idx_t) + sizeof(sm_bitvec_t) * 2] = { 0 };
|
|
|
|
__sm_insert_data(map, offset, &buf[0], sizeof(buf));
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
size_t aligned_idx = __sm_get_fully_aligned_offset(idx);
|
|
|
|
if (start - aligned_idx < SM_CHUNK_MAX_CAPACITY) {
|
|
|
|
__sm_chunk_t chunk;
|
|
|
|
__sm_chunk_map_init(&chunk, p + sizeof(sm_idx_t));
|
|
|
|
__sm_chunk_map_set_capacity(&chunk, start - aligned_idx);
|
|
|
|
}
|
|
|
|
*(sm_idx_t *)p = start = aligned_idx;
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* We just added another chunk map! */
|
|
|
|
__sm_set_chunk_map_count(map, __sm_get_chunk_map_count(map) + 1);
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 23:56:31 +00:00
|
|
|
/* We already inserted an additional sm_bitvec_t; later on there
|
|
|
|
is no need to grow the vector even further. */
|
2024-04-04 19:24:02 +00:00
|
|
|
dont_grow = true;
|
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* A __sm_chunk_t exists, but the new index exceeds its capacities: create
|
|
|
|
a new __sm_chunk_t and insert it after the current one. */
|
|
|
|
else {
|
|
|
|
__sm_chunk_t chunk;
|
|
|
|
__sm_chunk_map_init(&chunk, p + sizeof(sm_idx_t));
|
2024-04-24 20:32:09 +00:00
|
|
|
if (idx - start >= (sparsemap_idx_t)__sm_chunk_map_get_capacity(&chunk)) {
|
2024-04-04 19:24:02 +00:00
|
|
|
if (value == false) {
|
|
|
|
/* nothing to do */
|
2024-04-24 20:32:09 +00:00
|
|
|
return idx;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
size_t size = __sm_chunk_map_get_size(&chunk);
|
2024-04-30 15:20:48 +00:00
|
|
|
offset += (sizeof(sm_idx_t) + size);
|
2024-04-04 19:24:02 +00:00
|
|
|
p += sizeof(sm_idx_t) + size;
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
uint8_t buf[sizeof(sm_idx_t) + sizeof(sm_bitvec_t) * 2] = { 0 };
|
|
|
|
__sm_insert_data(map, offset, &buf[0], sizeof(buf));
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
start += __sm_chunk_map_get_capacity(&chunk);
|
2024-04-24 20:32:09 +00:00
|
|
|
if ((sparsemap_idx_t)start + SM_CHUNK_MAX_CAPACITY < idx) {
|
2024-04-04 19:24:02 +00:00
|
|
|
start = __sm_get_fully_aligned_offset(idx);
|
2024-04-03 00:41:55 +00:00
|
|
|
}
|
2024-04-04 19:24:02 +00:00
|
|
|
*(sm_idx_t *)p = start;
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* We just added another chunk map! */
|
|
|
|
__sm_set_chunk_map_count(map, __sm_get_chunk_map_count(map) + 1);
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* We already inserted an additional sm_bitvec_t; later on there
|
|
|
|
is no need to grow the vector even further. */
|
|
|
|
dont_grow = true;
|
2024-04-03 00:41:55 +00:00
|
|
|
}
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
__sm_chunk_t chunk;
|
|
|
|
__sm_chunk_map_init(&chunk, p + sizeof(sm_idx_t));
|
|
|
|
|
|
|
|
/* Now update the __sm_chunk_t. */
|
|
|
|
size_t position;
|
|
|
|
sm_bitvec_t fill;
|
2024-04-07 20:38:57 +00:00
|
|
|
int code = __sm_chunk_map_set(&chunk, idx - start, value, &position, &fill, false);
|
2024-04-04 19:24:02 +00:00
|
|
|
switch (code) {
|
|
|
|
case SM_OK:
|
|
|
|
break;
|
|
|
|
case SM_NEEDS_TO_GROW:
|
|
|
|
if (!dont_grow) {
|
2024-04-30 15:20:48 +00:00
|
|
|
offset += (sizeof(sm_idx_t) + position * sizeof(sm_bitvec_t));
|
2024-04-04 19:24:02 +00:00
|
|
|
__sm_insert_data(map, offset, (uint8_t *)&fill, sizeof(sm_bitvec_t));
|
2024-04-03 00:41:55 +00:00
|
|
|
}
|
2024-04-28 16:26:31 +00:00
|
|
|
__sm_chunk_map_set(&chunk, idx - start, value, &position, &fill, true);
|
2024-04-04 19:24:02 +00:00
|
|
|
break;
|
|
|
|
case SM_NEEDS_TO_SHRINK:
|
|
|
|
/* If the __sm_chunk_t is empty then remove it. */
|
|
|
|
if (__sm_chunk_map_is_empty(&chunk)) {
|
|
|
|
__sm_assert(position == 1);
|
|
|
|
__sm_remove_data(map, offset, sizeof(sm_idx_t) + sizeof(sm_bitvec_t) * 2);
|
|
|
|
__sm_set_chunk_map_count(map, __sm_get_chunk_map_count(map) - 1);
|
|
|
|
} else {
|
2024-04-30 15:20:48 +00:00
|
|
|
offset += (sizeof(sm_idx_t) + position * sizeof(sm_bitvec_t));
|
2024-04-04 19:24:02 +00:00
|
|
|
__sm_remove_data(map, offset, sizeof(sm_bitvec_t));
|
2024-04-03 00:41:55 +00:00
|
|
|
}
|
2024-04-04 19:24:02 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
__sm_assert(!"shouldn't be here");
|
|
|
|
#ifdef DEBUG
|
|
|
|
abort();
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
2024-04-04 19:58:06 +00:00
|
|
|
__sm_assert(sparsemap_get_size(map) >= SM_SIZEOF_OVERHEAD);
|
2024-04-24 20:32:09 +00:00
|
|
|
return idx;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
2024-04-26 20:25:17 +00:00
|
|
|
sparsemap_idx_t
|
2024-04-24 20:32:09 +00:00
|
|
|
sparsemap_get_starting_offset(sparsemap_t *map)
|
2024-04-04 19:24:02 +00:00
|
|
|
{
|
2024-04-24 20:32:09 +00:00
|
|
|
size_t count = __sm_get_chunk_map_count(map);
|
|
|
|
if (count == 0) {
|
|
|
|
return 0;
|
2024-04-08 22:14:47 +00:00
|
|
|
}
|
2024-04-24 20:32:09 +00:00
|
|
|
sm_idx_t *chunk = (sm_idx_t *)__sm_get_chunk_map_data(map, 0);
|
2024-04-26 20:25:17 +00:00
|
|
|
return (sparsemap_idx_t)*chunk;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the used size in the data buffer.
|
|
|
|
*/
|
|
|
|
size_t
|
|
|
|
sparsemap_get_size(sparsemap_t *map)
|
|
|
|
{
|
|
|
|
if (map->m_data_used) {
|
2024-04-30 15:20:48 +00:00
|
|
|
__sm_when_diag({ __sm_assert(map->m_data_used == __sm_get_size_impl(map)); });
|
2024-04-24 20:32:09 +00:00
|
|
|
return map->m_data_used;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-24 20:32:09 +00:00
|
|
|
return map->m_data_used = __sm_get_size_impl(map);
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decompresses the whole bitmap; calls scanner for all bits.
|
|
|
|
*/
|
|
|
|
void
|
2024-05-02 18:55:04 +00:00
|
|
|
sparsemap_scan(sparsemap_t *map, void (*scanner)(sm_idx_t[], size_t, void *aux), size_t skip, void *aux)
|
2024-04-04 19:24:02 +00:00
|
|
|
{
|
|
|
|
uint8_t *p = __sm_get_chunk_map_data(map, 0);
|
|
|
|
size_t count = __sm_get_chunk_map_count(map);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
sm_idx_t start = *(sm_idx_t *)p;
|
|
|
|
p += sizeof(sm_idx_t);
|
|
|
|
__sm_chunk_t chunk;
|
|
|
|
__sm_chunk_map_init(&chunk, p);
|
2024-05-02 18:55:04 +00:00
|
|
|
size_t skipped = __sm_chunk_map_scan(&chunk, start, scanner, skip, aux);
|
2024-04-04 19:24:02 +00:00
|
|
|
if (skip) {
|
|
|
|
assert(skip >= skipped);
|
|
|
|
skip -= skipped;
|
2024-04-03 00:41:55 +00:00
|
|
|
}
|
2024-04-04 19:24:02 +00:00
|
|
|
p += __sm_chunk_map_get_size(&chunk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-30 15:20:48 +00:00
|
|
|
void
|
|
|
|
sparsemap_merge(sparsemap_t *map, sparsemap_t *other)
|
|
|
|
{
|
|
|
|
uint8_t *src, *dst;
|
|
|
|
size_t src_count = __sm_get_chunk_map_count(other), dst_count = __sm_get_chunk_map_count(map), max_chunk_count = src_count + dst_count;
|
|
|
|
|
|
|
|
dst = __sm_get_chunk_map_data(map, 0);
|
|
|
|
src = __sm_get_chunk_map_data(other, 0);
|
|
|
|
for (size_t i = 0; i < max_chunk_count && src_count; i++) {
|
|
|
|
sm_idx_t src_start = *(sm_idx_t *)src;
|
2024-04-30 17:58:35 +00:00
|
|
|
sm_idx_t dst_start = *(sm_idx_t *)dst;
|
|
|
|
src_start = __sm_get_fully_aligned_offset(src_start);
|
|
|
|
dst_start = __sm_get_fully_aligned_offset(dst_start);
|
2024-04-30 15:20:48 +00:00
|
|
|
if (src_start > dst_start && dst_count > 0) {
|
|
|
|
__sm_chunk_t dst_chunk;
|
|
|
|
__sm_chunk_map_init(&dst_chunk, dst + sizeof(sm_idx_t));
|
|
|
|
dst += sizeof(sm_idx_t) + __sm_chunk_map_get_size(&dst_chunk);
|
2024-04-30 17:58:35 +00:00
|
|
|
dst_count--;
|
2024-04-30 15:20:48 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (src_start == dst_start && dst_count > 0) {
|
|
|
|
/* Chunks overlap, merge them. */
|
|
|
|
__sm_chunk_t src_chunk;
|
|
|
|
__sm_chunk_map_init(&src_chunk, src + sizeof(sm_idx_t));
|
|
|
|
__sm_chunk_t dst_chunk;
|
|
|
|
__sm_chunk_map_init(&dst_chunk, dst + sizeof(sm_idx_t));
|
|
|
|
__sm_chunk_map_merge(map, src_start, src_chunk);
|
2024-04-30 17:58:35 +00:00
|
|
|
*(sm_idx_t *)dst = __sm_get_fully_aligned_offset(src_start);
|
2024-04-30 15:20:48 +00:00
|
|
|
src += sizeof(sm_idx_t) + __sm_chunk_map_get_size(&src_chunk);
|
|
|
|
dst += sizeof(sm_idx_t) + __sm_chunk_map_get_size(&dst_chunk);
|
|
|
|
dst_count--;
|
|
|
|
src_count--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (src_start < dst_start || dst_count == 0) {
|
|
|
|
__sm_chunk_t src_chunk;
|
|
|
|
__sm_chunk_map_init(&src_chunk, src + sizeof(sm_idx_t));
|
|
|
|
size_t src_size = __sm_chunk_map_get_size(&src_chunk);
|
|
|
|
if (dst_count == 0) {
|
|
|
|
__sm_append_data(map, src, sizeof(sm_idx_t) + src_size);
|
|
|
|
} else {
|
|
|
|
size_t offset = __sm_get_chunk_map_offset(map, dst_start);
|
|
|
|
__sm_insert_data(map, offset, src, sizeof(sm_idx_t) + src_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the chunk count and data_used. */
|
|
|
|
__sm_set_chunk_map_count(map, __sm_get_chunk_map_count(map) + 1);
|
|
|
|
|
|
|
|
/* Carry on to the next chunk. */
|
|
|
|
__sm_chunk_t dst_chunk;
|
|
|
|
__sm_chunk_map_init(&dst_chunk, dst + sizeof(sm_idx_t));
|
|
|
|
src += sizeof(sm_idx_t) + __sm_chunk_map_get_size(&src_chunk);
|
|
|
|
dst += sizeof(sm_idx_t) + __sm_chunk_map_get_size(&dst_chunk);
|
|
|
|
src_count--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
void
|
2024-04-24 20:32:09 +00:00
|
|
|
sparsemap_split(sparsemap_t *map, sparsemap_idx_t offset, sparsemap_t *other)
|
2024-04-04 19:24:02 +00:00
|
|
|
{
|
2024-04-24 20:32:09 +00:00
|
|
|
assert(offset % SM_BITS_PER_VECTOR == 0);
|
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* |dst| points to the destination buffer */
|
|
|
|
uint8_t *dst = __sm_get_chunk_map_end(other);
|
|
|
|
|
|
|
|
/* |src| points to the source-chunk map */
|
|
|
|
uint8_t *src = __sm_get_chunk_map_data(map, 0);
|
|
|
|
|
2024-04-24 20:32:09 +00:00
|
|
|
/* |offset| is relative to the beginning of this sparsemap_t; best
|
2024-04-04 19:24:02 +00:00
|
|
|
make it absolute. */
|
2024-04-24 20:32:09 +00:00
|
|
|
offset += *(sm_idx_t *)src;
|
2024-04-04 19:24:02 +00:00
|
|
|
|
|
|
|
bool in_middle = false;
|
|
|
|
uint8_t *prev = src;
|
|
|
|
size_t i, count = __sm_get_chunk_map_count(map);
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
sm_idx_t start = *(sm_idx_t *)src;
|
|
|
|
__sm_chunk_t chunk;
|
|
|
|
__sm_chunk_map_init(&chunk, src + sizeof(sm_idx_t));
|
2024-04-24 20:32:09 +00:00
|
|
|
if (start == offset) {
|
2024-04-04 19:24:02 +00:00
|
|
|
break;
|
2024-04-03 00:41:55 +00:00
|
|
|
}
|
2024-04-24 20:32:09 +00:00
|
|
|
if (start + __sm_chunk_map_get_capacity(&chunk) > (unsigned long)offset) {
|
2024-04-04 19:24:02 +00:00
|
|
|
in_middle = true;
|
|
|
|
break;
|
2024-04-03 00:41:55 +00:00
|
|
|
}
|
2024-04-24 20:32:09 +00:00
|
|
|
if (start > offset) {
|
2024-04-04 19:24:02 +00:00
|
|
|
src = prev;
|
|
|
|
i--;
|
|
|
|
break;
|
2024-04-03 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
prev = src;
|
|
|
|
src += sizeof(sm_idx_t) + __sm_chunk_map_get_size(&chunk);
|
|
|
|
}
|
|
|
|
if (i == count) {
|
|
|
|
assert(sparsemap_get_size(map) > SM_SIZEOF_OVERHEAD);
|
|
|
|
assert(sparsemap_get_size(other) > SM_SIZEOF_OVERHEAD);
|
|
|
|
return;
|
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* Now copy all the remaining chunks. */
|
|
|
|
int moved = 0;
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-24 20:32:09 +00:00
|
|
|
/* If |offset| is in the middle of a chunk then this chunk has to be split */
|
2024-04-04 19:24:02 +00:00
|
|
|
if (in_middle) {
|
|
|
|
uint8_t buf[sizeof(sm_idx_t) + sizeof(sm_bitvec_t) * 2] = { 0 };
|
|
|
|
memcpy(dst, &buf[0], sizeof(buf));
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-24 20:32:09 +00:00
|
|
|
*(sm_idx_t *)dst = offset;
|
2024-04-04 19:24:02 +00:00
|
|
|
dst += sizeof(sm_idx_t);
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* the |other| sparsemap_t now has one additional chunk */
|
|
|
|
__sm_set_chunk_map_count(other, __sm_get_chunk_map_count(other) + 1);
|
|
|
|
if (other->m_data_used != 0) {
|
|
|
|
other->m_data_used += sizeof(sm_idx_t) + sizeof(sm_bitvec_t);
|
2024-04-03 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
src += sizeof(sm_idx_t);
|
|
|
|
__sm_chunk_t s_chunk;
|
|
|
|
__sm_chunk_map_init(&s_chunk, src);
|
|
|
|
size_t capacity = __sm_chunk_map_get_capacity(&s_chunk);
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
__sm_chunk_t d_chunk;
|
|
|
|
__sm_chunk_map_init(&d_chunk, dst);
|
2024-04-24 20:32:09 +00:00
|
|
|
__sm_chunk_map_set_capacity(&d_chunk, capacity - (offset % capacity));
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* Now copy the bits. */
|
2024-04-24 20:32:09 +00:00
|
|
|
sparsemap_idx_t d = offset;
|
|
|
|
for (size_t j = offset % capacity; j < capacity; j++, d++) {
|
2024-04-04 19:24:02 +00:00
|
|
|
if (__sm_chunk_map_is_set(&s_chunk, j)) {
|
|
|
|
sparsemap_set(other, d, true);
|
2024-04-03 00:41:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
src += __sm_chunk_map_get_size(&s_chunk);
|
|
|
|
size_t dsize = __sm_chunk_map_get_size(&d_chunk);
|
|
|
|
dst += dsize;
|
|
|
|
i++;
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* Reduce the capacity of the source-chunk map. */
|
2024-04-24 20:32:09 +00:00
|
|
|
__sm_chunk_map_set_capacity(&s_chunk, offset % capacity);
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-30 15:20:48 +00:00
|
|
|
/* Now continue with all remaining chunk maps. */
|
2024-04-04 19:24:02 +00:00
|
|
|
for (; i < count; i++) {
|
|
|
|
sm_idx_t start = *(sm_idx_t *)src;
|
|
|
|
src += sizeof(sm_idx_t);
|
|
|
|
__sm_chunk_t chunk;
|
|
|
|
__sm_chunk_map_init(&chunk, src);
|
|
|
|
size_t s = __sm_chunk_map_get_size(&chunk);
|
|
|
|
|
|
|
|
*(sm_idx_t *)dst = start;
|
|
|
|
dst += sizeof(sm_idx_t);
|
|
|
|
memcpy(dst, src, s);
|
|
|
|
src += s;
|
|
|
|
dst += s;
|
|
|
|
|
|
|
|
moved++;
|
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* Force new calculation. */
|
|
|
|
other->m_data_used = 0;
|
|
|
|
map->m_data_used = 0;
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
/* Update the Chunk Map counters. */
|
|
|
|
__sm_set_chunk_map_count(map, __sm_get_chunk_map_count(map) - moved);
|
|
|
|
__sm_set_chunk_map_count(other, __sm_get_chunk_map_count(other) + moved);
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
assert(sparsemap_get_size(map) >= SM_SIZEOF_OVERHEAD);
|
|
|
|
assert(sparsemap_get_size(other) > SM_SIZEOF_OVERHEAD);
|
|
|
|
}
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-24 20:32:09 +00:00
|
|
|
sparsemap_idx_t
|
|
|
|
sparsemap_select(sparsemap_t *map, sparsemap_idx_t n, bool value)
|
2024-04-04 19:24:02 +00:00
|
|
|
{
|
|
|
|
assert(sparsemap_get_size(map) >= SM_SIZEOF_OVERHEAD);
|
2024-04-26 20:25:17 +00:00
|
|
|
sm_idx_t start;
|
2024-04-04 19:24:02 +00:00
|
|
|
size_t count = __sm_get_chunk_map_count(map);
|
2024-04-26 20:25:17 +00:00
|
|
|
|
2024-04-28 16:26:31 +00:00
|
|
|
uint8_t *p = __sm_get_chunk_map_data(map, 0);
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-28 16:26:31 +00:00
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
start = *(sm_idx_t *)p;
|
|
|
|
/* Start of this chunk is greater than n meaning there are a set of 0s
|
|
|
|
before the first 1 sufficient to consume n. */
|
|
|
|
if (value == false && i == 0 && start > n) {
|
|
|
|
return n;
|
2024-04-24 20:32:09 +00:00
|
|
|
}
|
2024-04-28 16:26:31 +00:00
|
|
|
p += sizeof(sm_idx_t);
|
|
|
|
__sm_chunk_t chunk;
|
|
|
|
__sm_chunk_map_init(&chunk, p);
|
|
|
|
|
2024-04-30 15:20:48 +00:00
|
|
|
ssize_t new_n = n;
|
2024-04-28 16:26:31 +00:00
|
|
|
size_t index = __sm_chunk_map_select(&chunk, n, &new_n, value);
|
|
|
|
if (new_n == -1) {
|
|
|
|
return start + index;
|
2024-04-26 20:25:17 +00:00
|
|
|
}
|
2024-04-28 16:26:31 +00:00
|
|
|
n = new_n;
|
|
|
|
|
|
|
|
p += __sm_chunk_map_get_size(&chunk);
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-28 16:26:31 +00:00
|
|
|
return SPARSEMAP_IDX_MAX;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2024-04-24 20:32:09 +00:00
|
|
|
sparsemap_rank_vec(sparsemap_t *map, size_t x, size_t y, bool value, sm_bitvec_t *vec)
|
2024-04-04 19:24:02 +00:00
|
|
|
{
|
|
|
|
assert(sparsemap_get_size(map) >= SM_SIZEOF_OVERHEAD);
|
2024-04-28 16:26:31 +00:00
|
|
|
size_t amt, gap, pos = 0, result = 0, prev = 0, count, len = y - x + 1;
|
2024-04-24 20:32:09 +00:00
|
|
|
uint8_t *p;
|
|
|
|
|
|
|
|
if (x > y) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
count = __sm_get_chunk_map_count(map);
|
|
|
|
|
|
|
|
if (count == 0) {
|
|
|
|
if (value == false) {
|
|
|
|
/* The count/rank of unset bits in an empty map is inf, so what you requested is the answer. */
|
2024-04-28 16:26:31 +00:00
|
|
|
return len;
|
2024-04-24 20:32:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p = __sm_get_chunk_map_data(map, 0);
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-04 19:24:02 +00:00
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
sm_idx_t start = *(sm_idx_t *)p;
|
2024-04-24 20:32:09 +00:00
|
|
|
/* [prev, start + pos), prev is the last bit examined 0-based. */
|
2024-04-28 16:26:31 +00:00
|
|
|
if (i == 0) {
|
|
|
|
gap = start;
|
|
|
|
} else {
|
|
|
|
if (prev + SM_CHUNK_MAX_CAPACITY == start) {
|
|
|
|
gap = 0;
|
|
|
|
} else {
|
|
|
|
gap = start - (prev + pos);
|
|
|
|
}
|
|
|
|
}
|
2024-04-24 20:32:09 +00:00
|
|
|
/* Start of this chunk is greater than the end of the desired range. */
|
|
|
|
if (start > y) {
|
|
|
|
if (value == true) {
|
2024-04-28 16:26:31 +00:00
|
|
|
/* We're counting set bits and this chunk starts after the range
|
|
|
|
[x, y], we're done. */
|
2024-04-24 20:32:09 +00:00
|
|
|
return result;
|
|
|
|
} else {
|
2024-04-28 16:26:31 +00:00
|
|
|
if (i == 0) {
|
|
|
|
/* We're counting unset bits and the first chunk starts after the
|
|
|
|
range meaning everything proceeding this chunk was zero and should
|
|
|
|
be counted, also we're done. */
|
|
|
|
result += (y - x) + 1;
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
/* We're counting unset bits and some chunk starts after the range, so
|
|
|
|
we've counted enough, we're done. */
|
|
|
|
if (pos > y) {
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
if (y - pos < gap) {
|
|
|
|
result += y - pos;
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
result += gap;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-24 20:32:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* The range and this chunk overlap. */
|
|
|
|
if (value == false) {
|
|
|
|
if (x > gap) {
|
|
|
|
x -= gap;
|
|
|
|
} else {
|
|
|
|
result += gap - x;
|
|
|
|
x = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (x > gap) {
|
|
|
|
x -= gap;
|
|
|
|
}
|
|
|
|
}
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-09 18:46:49 +00:00
|
|
|
prev = start;
|
2024-04-04 19:24:02 +00:00
|
|
|
p += sizeof(sm_idx_t);
|
|
|
|
__sm_chunk_t chunk;
|
|
|
|
__sm_chunk_map_init(&chunk, p);
|
2024-04-03 00:41:55 +00:00
|
|
|
|
2024-04-24 20:32:09 +00:00
|
|
|
/* Count all the set/unset inside this chunk. */
|
|
|
|
amt = __sm_chunk_map_rank(&chunk, &x, y - start, &pos, vec, value);
|
|
|
|
result += amt;
|
2024-04-04 19:24:02 +00:00
|
|
|
p += __sm_chunk_map_get_size(&chunk);
|
|
|
|
}
|
2024-04-24 20:32:09 +00:00
|
|
|
/* Count any additional unset bits that fall outside the last chunk but
|
|
|
|
within the range. */
|
|
|
|
if (value == false) {
|
|
|
|
size_t last = prev - 1 + pos;
|
|
|
|
if (y > last) {
|
|
|
|
result += y - last - x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2024-04-04 19:24:02 +00:00
|
|
|
}
|
2024-04-07 20:38:57 +00:00
|
|
|
|
|
|
|
size_t
|
2024-04-24 20:32:09 +00:00
|
|
|
sparsemap_rank(sparsemap_t *map, size_t x, size_t y, bool value)
|
2024-04-07 20:38:57 +00:00
|
|
|
{
|
2024-04-24 20:32:09 +00:00
|
|
|
sm_bitvec_t vec;
|
|
|
|
return sparsemap_rank_vec(map, x, y, value, &vec);
|
|
|
|
}
|
2024-04-07 20:38:57 +00:00
|
|
|
|
2024-04-24 20:32:09 +00:00
|
|
|
size_t
|
|
|
|
sparsemap_span(sparsemap_t *map, sparsemap_idx_t idx, size_t len, bool value)
|
|
|
|
{
|
2024-04-28 16:26:31 +00:00
|
|
|
size_t rank, nth;
|
2024-04-24 20:32:09 +00:00
|
|
|
sm_bitvec_t vec = 0;
|
2024-05-03 01:13:17 +00:00
|
|
|
sparsemap_idx_t offset;
|
2024-04-28 16:26:31 +00:00
|
|
|
|
|
|
|
/* When skipping forward to `idx` offset in the map we can determine how
|
|
|
|
many selects we can avoid by taking the rank of the range and starting
|
|
|
|
at that bit. */
|
2024-05-03 01:13:17 +00:00
|
|
|
nth = (idx == 0) ? 0 : sparsemap_rank(map, 0, idx - 1, value);
|
2024-04-28 16:26:31 +00:00
|
|
|
/* Find the first bit that matches value, then... */
|
|
|
|
offset = sparsemap_select(map, nth, value);
|
2024-04-07 20:38:57 +00:00
|
|
|
do {
|
2024-04-28 16:26:31 +00:00
|
|
|
/* See if the rank of the bits in the range starting at offset is equal
|
|
|
|
to the desired amount. */
|
2024-05-03 01:13:17 +00:00
|
|
|
rank = (len == 1) ? 1 : sparsemap_rank_vec(map, offset, offset + len - 1, value, &vec);
|
2024-04-28 16:26:31 +00:00
|
|
|
if (rank >= len) {
|
|
|
|
/* We've found what we're looking for, return the index of the first
|
|
|
|
bit in the range. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Now we try to jump forward as much as possible before we look for a
|
|
|
|
new match. We do this by counting the remaining bits in the returned
|
|
|
|
vec from the call to rank_vec(). */
|
2024-05-03 01:13:17 +00:00
|
|
|
int amt = 1;
|
|
|
|
if (vec > 0) {
|
|
|
|
/* The returned vec had som set bits, let's move forward in the map as much
|
|
|
|
as possible (max: 64 bit positions). */
|
2024-04-28 16:26:31 +00:00
|
|
|
int max = len > SM_BITS_PER_VECTOR ? SM_BITS_PER_VECTOR : len;
|
|
|
|
while (amt < max && (vec & 1 << amt)) {
|
|
|
|
amt++;
|
2024-04-07 20:38:57 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-28 16:26:31 +00:00
|
|
|
nth += amt;
|
2024-04-24 20:32:09 +00:00
|
|
|
offset = sparsemap_select(map, nth, value);
|
2024-04-28 16:26:31 +00:00
|
|
|
} while (SPARSEMAP_FOUND(offset));
|
2024-04-07 20:38:57 +00:00
|
|
|
|
2024-04-28 16:26:31 +00:00
|
|
|
return offset;
|
2024-04-07 20:38:57 +00:00
|
|
|
}
|