| 1 |
/// @file |
| 2 |
/// @ingroup cgraph_utils |
| 3 |
/// @brief API for compacted arrays of booleans |
| 4 |
/// |
| 5 |
/// The straightforward way to construct a dynamic array of booleans is to |
| 6 |
/// `calloc` an array of `bool` values. However, this wastes a lot of memory. |
| 7 |
/// Typically 8 bits per byte, which really adds up for large arrays. |
| 8 |
/// |
| 9 |
/// The following implements an alternative that stores 8 array elements per |
| 10 |
/// byte. Using this over the `bool` implementation described above decreases |
| 11 |
/// heap pressure and increases locality of reference, at the cost of a few |
| 12 |
/// (inexpensive) shifts and masks. |
| 13 |
/// |
| 14 |
/// As a bonus, short arrays are stored directly inline, avoiding heap |
| 15 |
/// allocation altogether. This is essentially Small String Optimization applied |
| 16 |
/// to a boolean array. |
| 17 |
/// |
| 18 |
/// The above design is essentially what C++’s `std::vector<bool>` does. |
| 19 |
/// |
| 20 |
/// This is deliberately implemented header-only so even Graphviz components |
| 21 |
/// that do not link against cgraph can use it. |
| 22 |
|
| 23 |
#pragma once |
| 24 |
|
| 25 |
#include <assert.h> |
| 26 |
#include <inttypes.h> |
| 27 |
#include <stdbool.h> |
| 28 |
#include <stdint.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
#include <util/alloc.h> |
| 32 |
|
| 33 |
/// a compressed array of boolean values |
| 34 |
/// |
| 35 |
/// Note that this complies with the zero-is-initialization idiom. That is, C99 |
| 36 |
/// zero initializing one of these (`bitarray_t b = {0}`) or `memset`ing one of |
| 37 |
/// these to zero gives you a valid zero-length bit array. |
| 38 |
typedef struct { |
| 39 |
union { |
| 40 |
uint8_t block[sizeof(uint8_t *)]; ///< inline storage for small arrays |
| 41 |
uint8_t *base; ///< start of the underlying allocated buffer |
| 42 |
}; |
| 43 |
size_t size_bits; ///< extent in bits |
| 44 |
} bitarray_t; |
| 45 |
|
| 46 |
/// create an array of the given element length |
| 47 |
static inline bitarray_t bitarray_new(size_t size_bits) { |
| 48 |
|
| 49 |
bitarray_t ba = {.size_bits = size_bits}; |
| 50 |
|
| 51 |
// if the array is small enough, we can use inline storage |
| 52 |
if (size_bits <= sizeof(ba.block) * 8) { |
| 53 |
// nothing to be done |
| 54 |
|
| 55 |
// otherwise we need to heap-allocate |
| 56 |
} else { |
| 57 |
size_t capacity = size_bits / 8 + (size_bits % 8 == 0 ? 0 : 1); |
| 58 |
ba.base = gv_calloc(capacity, sizeof(uint8_t)); |
| 59 |
} |
| 60 |
|
| 61 |
return ba; |
| 62 |
} |
| 63 |
|
| 64 |
/// get the value of the given element |
| 65 |
static inline bool bitarray_get(bitarray_t self, size_t index) { |
| 66 |
assert(index < self.size_bits && "out of bounds access"); |
| 67 |
|
| 68 |
// determine if this array is stored inline or not |
| 69 |
const uint8_t *base; |
| 70 |
if (self.size_bits <= sizeof(self.block) * 8) { |
| 71 |
base = self.block; |
| 72 |
} else { |
| 73 |
base = self.base; |
| 74 |
} |
| 75 |
|
| 76 |
return (base[index / 8] >> (index % 8)) & 1; |
| 77 |
} |
| 78 |
|
| 79 |
/// set or clear the value of the given element |
| 80 |
static inline void bitarray_set(bitarray_t *self, size_t index, bool value) { |
| 81 |
assert(index < self->size_bits && "out of bounds access"); |
| 82 |
|
| 83 |
// determine if this array is stored inline or not |
| 84 |
uint8_t *base; |
| 85 |
if (self->size_bits <= sizeof(self->block) * 8) { |
| 86 |
base = self->block; |
| 87 |
} else { |
| 88 |
base = self->base; |
| 89 |
} |
| 90 |
|
| 91 |
if (value) { |
| 92 |
base[index / 8] |= (uint8_t)(UINT8_C(1) << (index % 8)); |
| 93 |
} else { |
| 94 |
base[index / 8] &= (uint8_t)~(UINT8_C(1) << (index % 8)); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/// clear all bits in a bit array |
| 99 |
static inline void bitarray_clear(bitarray_t *self) { |
| 100 |
assert(self != NULL); |
| 101 |
|
| 102 |
// determine if this array is stored inline or not |
| 103 |
uint8_t *const base = |
| 104 |
self->size_bits <= sizeof(self->block) * 8 ? self->block : self->base; |
| 105 |
|
| 106 |
// calculate byte extent covering the array |
| 107 |
const size_t size = self->size_bits / 8 + (self->size_bits % 8 == 0 ? 0 : 1); |
| 108 |
|
| 109 |
// zero all bits |
| 110 |
memset(base, 0, size); |
| 111 |
} |
| 112 |
|
| 113 |
/// free underlying resources and leave a bit array empty |
| 114 |
static inline void bitarray_reset(bitarray_t *self) { |
| 115 |
assert(self != NULL); |
| 116 |
|
| 117 |
// is this array stored out of line? |
| 118 |
if (self->size_bits > sizeof(self->block) * 8) |
| 119 |
free(self->base); |
| 120 |
|
| 121 |
*self = (bitarray_t){0}; |
| 122 |
} |