Derive-C
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1#pragma once
2#include <stddef.h>
3#include <stdint.h>
4
5#include <derive-c/core.h>
6#include <derive-c/panic.h>
7
8static inline bool is_power_of_2(size_t x) { return x != 0 && (x & (x - 1)) == 0; }
9
10static inline size_t apply_capacity_policy(size_t capacity) {
11 // TODO(oliverkillane): play with overallocation policy
12 return next_power_of_2(capacity + capacity / 2);
13}
14
15static inline size_t modulus_capacity(size_t index, size_t capacity) {
17 // NOTE: If we know capacity is a power of 2, we can reduce the cost of 'index + 1 % capacity'
18 return index & (capacity - 1);
19}
20
21static size_t const PROBE_DISTANCE = 1;
22static size_t const INITIAL_CAPACITY = 32;
static size_t next_power_of_2(size_t x)
Definition core.h:24
#define DEBUG_ASSERT(expr)
Definition panic.h:24
static bool is_power_of_2(size_t x)
Definition utils.h:8
static size_t const INITIAL_CAPACITY
Definition utils.h:22
static size_t modulus_capacity(size_t index, size_t capacity)
Definition utils.h:15
static size_t const PROBE_DISTANCE
Definition utils.h:21
static size_t apply_capacity_policy(size_t capacity)
Definition utils.h:10