#pragma once
#include <assert.h>
-#include <cgraph/likely.h>
-#include <errno.h>
+#include <cgraph/alloc.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
-#include <stdio.h>
#include <stdlib.h>
#include <string.h>
} bitarray_t;
/// create an array of the given element length
-static inline int bitarray_new(bitarray_t *self, size_t size_bits) {
- assert(self != NULL);
- assert(self->size_bits == 0);
+static inline bitarray_t bitarray_new(size_t size_bits) {
+
+ bitarray_t ba = {.size_bits = size_bits};
// if the array is small enough, we can use inline storage
- if (size_bits <= sizeof(self->block) * 8) {
- memset(self->block, 0, sizeof(self->block));
+ if (size_bits <= sizeof(ba.block) * 8) {
+ // nothing to be done
// otherwise we need to heap-allocate
} else {
size_t capacity = size_bits / 8 + (size_bits % 8 == 0 ? 0 : 1);
- uint8_t *base = calloc(capacity, sizeof(self->base[0]));
- if (UNLIKELY(base == NULL))
- return ENOMEM;
-
- self->base = base;
- }
-
- self->size_bits = size_bits;
-
- return 0;
-}
-
-/// `bitarray_new` for callers who cannot handle failure
-static inline bitarray_t bitarray_new_or_exit(size_t size_bits) {
-
- bitarray_t ba;
- memset(&ba, 0, sizeof(ba));
-
- int error = bitarray_new(&ba, size_bits);
- if (UNLIKELY(error != 0)) {
- fprintf(stderr, "out of memory\n");
- exit(EXIT_FAILURE);
+ ba.base = gv_calloc(capacity, sizeof(uint8_t));
}
return ba;
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
-#include <string.h>
// helper for basic construction and destruction, with nothing in-between
static void create_reset(size_t size) {
- bitarray_t b;
- memset(&b, 0, sizeof(b));
- int r = bitarray_new(&b, size);
- assert(r == 0);
+ bitarray_t b = bitarray_new(size);
bitarray_reset(&b);
}
// setting and unsetting of all bits
static void set_unset(size_t size) {
- bitarray_t b;
- memset(&b, 0, sizeof(b));
- int r = bitarray_new(&b, size);
- assert(r == 0);
+ bitarray_t b = bitarray_new(size);
// set and unset each bit
for (size_t i = 0; i < size; ++i) {
size_t size = 10;
- bitarray_t b;
- memset(&b, 0, sizeof(b));
- int r = bitarray_new(&b, size);
- assert(r == 0);
+ bitarray_t b = bitarray_new(size);
// set and unset each bit
for (size_t i = 0; i < size; ++i) {
// reuse it with a different size
size = 1023;
- r = bitarray_new(&b, size);
- assert(r == 0);
+ b = bitarray_new(size);
// set and unset each bit
for (size_t i = 0; i < size; ++i) {
// redundant write to a bit
static void double_set(size_t size, bool value) {
- bitarray_t b;
- memset(&b, 0, sizeof(b));
- int r = bitarray_new(&b, size);
- assert(r == 0);
+ bitarray_t b = bitarray_new(size);
static const size_t index = 7;
assert(!bitarray_get(b, index));
layerWidthInfo[i].nodeGroupsInLayer = gv_calloc(nNodeGroups, sizeof(nodeGroup_t*));
assert(nNodeGroups >= 0);
- layerWidthInfo[i].removed = bitarray_new_or_exit((size_t)nNodeGroups);
+ layerWidthInfo[i].removed = bitarray_new((size_t)nNodeGroups);
layerWidthInfo[i].layerNumber = i;
layerWidthInfo[i].nNodeGroupsInLayer = 0;
}
num_visited_nodes =
bfs_bounded(vertex, graph, dist, &Q, bound, visited_nodes);
- bitarray_t node_in_neighborhood = bitarray_new_or_exit(n);
+ bitarray_t node_in_neighborhood = bitarray_new(n);
for (i = 0; i < num_visited_nodes; i++) {
bitarray_set(&node_in_neighborhood, visited_nodes[i], true);
}
/* array of arrays of node indices in each cluster */
int **cs,*cn;
int i,j,nclusters=0;
- bitarray_t assigned = bitarray_new_or_exit(agnnodes(g));
+ bitarray_t assigned = bitarray_new(agnnodes(g));
cluster_data *cdata = GNEW(cluster_data);
cdata->ntoplevel = agnnodes(g);
}
graph_sgd *graph = N_NEW(1, graph_sgd);
graph->sources = N_NEW(n_nodes + 1, size_t);
- graph->pinneds = bitarray_new_or_exit(n_nodes);
+ graph->pinneds = bitarray_new(n_nodes);
graph->targets = N_NEW(n_edges, size_t);
graph->weights = N_NEW(n_edges, float);
} else if (model == MODEL_SUBSET) {
// i,j,k refer to actual node indices, while x,y refer to edge indices in graph->targets
// initialise to no neighbours
- bitarray_t neighbours_i = bitarray_new_or_exit(graph->n);
- bitarray_t neighbours_j = bitarray_new_or_exit(graph->n);
+ bitarray_t neighbours_i = bitarray_new(graph->n);
+ bitarray_t neighbours_j = bitarray_new(graph->n);
for (size_t i = 0; i < graph->n; i++) {
int deg_i = 0;
for (size_t x = graph->sources[i]; x < graph->sources[i + 1]; x++) {
int flip, boxes_t *decomp) {
int i;
int tr_start;
- bitarray_t visited = bitarray_new_or_exit(tr->length);
+ bitarray_t visited = bitarray_new(tr->length);
mchain = gv_calloc(tr->length, sizeof(monchain_t));
vert = gv_calloc(nsegs + 1, sizeof(vertexchain_t));
assert(!SparseMatrix_has_diagonal(A));
- bitarray_t checked = bitarray_new_or_exit(m);
+ bitarray_t checked = bitarray_new(m);
angles = gv_calloc(nangles_max, sizeof(double));
leaves = gv_calloc(nleaves_max, sizeof(int));