| 1 |
|
| /* |
| 2 |
|
| * Copyright (C) the libgit2 contributors. All rights reserved. |
| 3 |
|
| * |
| 4 |
|
| * This file is part of libgit2, distributed under the GNU GPL v2 with |
| 5 |
|
| * a Linking Exception. For full terms see the included COPYING file. |
| 6 |
|
| */ |
| 7 |
|
| #ifndef INCLUDE_bitvec_h__ |
| 8 |
|
| #define INCLUDE_bitvec_h__ |
| 9 |
|
| |
| 10 |
|
| #include "common.h" |
| 11 |
|
| |
| 12 |
|
| /* |
| 13 |
|
| * This is a silly little fixed length bit vector type that will store |
| 14 |
|
| * vectors of 64 bits or less directly in the structure and allocate |
| 15 |
|
| * memory for vectors longer than 64 bits. You can use the two versions |
| 16 |
|
| * transparently through the API and avoid heap allocation completely when |
| 17 |
|
| * using a short bit vector as a result. |
| 18 |
|
| */ |
| 19 |
|
| typedef struct { |
| 20 |
|
| size_t length; |
| 21 |
|
| union { |
| 22 |
|
| uint64_t *words; |
| 23 |
|
| uint64_t bits; |
| 24 |
|
| } u; |
| 25 |
|
| } git_bitvec; |
| 26 |
|
| |
| 27 |
11 |
| GIT_INLINE(int) git_bitvec_init(git_bitvec *bv, size_t capacity) |
| 28 |
|
| { |
| 29 |
11 |
| memset(bv, 0x0, sizeof(*bv)); |
| 30 |
|
| |
| 31 |
11 |
▶ | if (capacity >= 64) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (capacity >= 64) { |
0 |
11 |
11 |
|
| 32 |
0 |
| bv->length = (capacity / 64) + 1; |
| 33 |
0 |
| bv->u.words = git__calloc(bv->length, sizeof(uint64_t)); |
| 34 |
0 |
▶ | if (!bv->u.words) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bv->u.words) |
0 |
0 |
0 |
|
| 35 |
0 |
| return -1; |
| 36 |
|
| } |
| 37 |
|
| |
| 38 |
11 |
| return 0; |
| 39 |
|
| } |
| 40 |
|
| |
| 41 |
|
| #define GIT_BITVEC_MASK(BIT) ((uint64_t)1 << (BIT % 64)) |
| 42 |
|
| #define GIT_BITVEC_WORD(BV, BIT) (BV->length ? &BV->u.words[BIT / 64] : &BV->u.bits) |
| 43 |
|
| |
| 44 |
9 |
| GIT_INLINE(void) git_bitvec_set(git_bitvec *bv, size_t bit, bool on) |
| 45 |
|
| { |
| 46 |
9 |
▶ | uint64_t *word = GIT_BITVEC_WORD(bv, bit); |
Branch: 1 branch
| Branch | True | False | Total |
| uint64_t *word = GIT_BITVEC_WORD(bv, bit); |
0 |
9 |
9 |
|
| 47 |
9 |
| uint64_t mask = GIT_BITVEC_MASK(bit); |
| 48 |
|
| |
| 49 |
9 |
▶ | if (on) |
Branch: 1 branch
| Branch | True | False | Total |
| if (on) |
9 |
0 |
9 |
|
| 50 |
9 |
| *word |= mask; |
| 51 |
|
| else |
| 52 |
0 |
| *word &= ~mask; |
| 53 |
9 |
| } |
| 54 |
|
| |
| 55 |
23 |
| GIT_INLINE(bool) git_bitvec_get(git_bitvec *bv, size_t bit) |
| 56 |
|
| { |
| 57 |
23 |
▶ | uint64_t *word = GIT_BITVEC_WORD(bv, bit); |
Branch: 1 branch
| Branch | True | False | Total |
| uint64_t *word = GIT_BITVEC_WORD(bv, bit); |
0 |
23 |
23 |
|
| 58 |
23 |
| return (*word & GIT_BITVEC_MASK(bit)) != 0; |
| 59 |
|
| } |
| 60 |
|
| |
| 61 |
|
| GIT_INLINE(void) git_bitvec_clear(git_bitvec *bv) |
| 62 |
|
| { |
| 63 |
|
| if (!bv->length) |
| 64 |
|
| bv->u.bits = 0; |
| 65 |
|
| else |
| 66 |
|
| memset(bv->u.words, 0x0, bv->length * sizeof(uint64_t)); |
| 67 |
|
| } |
| 68 |
|
| |
| 69 |
11 |
| GIT_INLINE(void) git_bitvec_free(git_bitvec *bv) |
| 70 |
|
| { |
| 71 |
11 |
▶ | if (bv->length) |
Branch: 1 branch
| Branch | True | False | Total |
| if (bv->length) |
0 |
11 |
11 |
|
| 72 |
0 |
| git__free(bv->u.words); |
| 73 |
11 |
| } |
| 74 |
|
| |
| 75 |
|
| #endif |