forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitvector.c
61 lines (50 loc) · 1.14 KB
/
bitvector.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// This file is a part of Julia. License is MIT: https://julialang.org/license
/*
bit vector primitives
*/
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "dtypes.h"
#include "bitvector.h"
#ifdef _OS_WINDOWS_
#include <malloc.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
uint32_t *bitvector_resize(uint32_t *b, uint64_t oldsz, uint64_t newsz,
int initzero)
{
uint32_t *p;
size_t sz = ((newsz+31)>>5) * sizeof(uint32_t);
p = (uint32_t*)LLT_REALLOC(b, sz);
if (p == NULL) return NULL;
if (initzero && newsz>oldsz) {
size_t osz = ((oldsz+31)>>5) * sizeof(uint32_t);
memset(&p[osz/sizeof(uint32_t)], 0, sz-osz);
}
return p;
}
uint32_t *bitvector_new(uint64_t n, int initzero)
{
return bitvector_resize(NULL, 0, n, initzero);
}
size_t bitvector_nwords(uint64_t nbits)
{
return ((nbits+31)>>5);
}
void bitvector_set(uint32_t *b, uint64_t n, uint32_t c)
{
if (c)
b[n>>5] |= (1<<(n&31));
else
b[n>>5] &= ~(1<<(n&31));
}
uint32_t bitvector_get(uint32_t *b, uint64_t n)
{
return b[n>>5] & (1<<(n&31));
}
#ifdef __cplusplus
}
#endif