summaryrefslogtreecommitdiff
blob: ef6642562188139baaee2181d4e01c70670b8ea3 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/******************************************************************************
 **    Filename:    bitvec.h
 **    Purpose:     Routines for manipulating bit vectors
 **    Author:      Dan Johnson
 **
 **    (c) Copyright Hewlett-Packard Company, 1988.
 ** Licensed under the Apache License, Version 2.0 (the "License");
 ** you may not use this file except in compliance with the License.
 ** You may obtain a copy of the License at
 ** http://www.apache.org/licenses/LICENSE-2.0
 ** Unless required by applicable law or agreed to in writing, software
 ** distributed under the License is distributed on an "AS IS" BASIS,
 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 ** See the License for the specific language governing permissions and
 ** limitations under the License.
 ******************************************************************************/

#ifndef BITVEC_H
#define BITVEC_H

#include <cstddef>      // for size_t
#include <cstdint>      // for uint32_t

/*-----------------------------------------------------------------------------
          Include Files and Type Defines
-----------------------------------------------------------------------------*/

using BIT_VECTOR = uint32_t*;

//< no of bits in a BIT_VECTOR element
const size_t BITSINLONG = 8 * sizeof(uint32_t);

/*-----------------------------------------------------------------------------
          Public Function Prototypes
-----------------------------------------------------------------------------*/

static inline void zero_all_bits(BIT_VECTOR array, size_t length) {
  for (size_t index = 0; index < length; index++) {
    array[index] = 0;
  }
}

static inline void set_all_bits(BIT_VECTOR array, size_t length) {
  for (size_t index = 0; index < length; index++) {
    array[index] = ~0;
  }
}

static inline void copy_all_bits(BIT_VECTOR source, BIT_VECTOR dest, size_t length) {
  for (size_t index = 0; index < length; index++) {
    dest[index] = source[index];
  }
}

#define SET_BIT(array,bit) (array[bit/BITSINLONG]|=1<<(bit&(BITSINLONG-1)))

#define reset_bit(array,bit) (array[bit/BITSINLONG]&=~(1<<(bit&(BITSINLONG-1))))

#define test_bit(array,bit) (array[bit/BITSINLONG] & (1<<(bit&(BITSINLONG-1))))

static inline size_t WordsInVectorOfSize(size_t NumBits) {
  return (NumBits + BITSINLONG - 1) / BITSINLONG;
}

/**
 * This routine frees a bit vector.
 *
 * @param BitVector bit vector to be freed
 *
 */
static inline void FreeBitVector(BIT_VECTOR BitVector) {
  delete[] BitVector;
}

/*---------------------------------------------------------------------------*/
/**
 * Allocate and return a new bit vector large enough to
 * hold the specified number of bits.
 *
 * @param NumBits number of bits in new bit vector
 *
 * @return New bit vector.
 */
static inline BIT_VECTOR NewBitVector(size_t NumBits) {
  return new uint32_t[WordsInVectorOfSize(NumBits)];
}

#endif