Freeciv21
Develop your civilization from humble roots to a global empire
bitvector.h
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 #pragma once
14 
15 #include <cstring> // memset
16 
17 class QBitArray;
18 
19 // utility
20 #include "log.h"
21 
22 // Yields TRUE iff the bit bit_no is set in val.
23 #define TEST_BIT(val, bit_no) \
24  (((val) & (1u << (bit_no))) == (1u << (bit_no)))
25 
26 // Static bitvectors.
27 #define _BV_BYTES(bits) ((((bits) -1) / 8) + 1)
28 #define _BV_BYTE_INDEX(bits) ((bits) / 8)
29 #define _BV_BITMASK(bit) (1u << ((bit) &0x7))
30 #ifdef FREECIV_DEBUG
31 #define _BV_ASSERT(bv, bit) \
32  fc_assert((bit) >= 0 && (bit) < (signed int) sizeof((bv).vec) * 8)
33 #else
34 #define _BV_ASSERT(bv, bit) (void) 0
35 #endif
36 
37 template <class BV> inline bool BV_ISSET(const BV &bv, int bit)
38 {
39  fc_assert_ret_val(bit >= 0 && bit < (signed int) sizeof(bv.vec) * 8,
40  false);
41  return (bv.vec[_BV_BYTE_INDEX(bit)] & _BV_BITMASK(bit)) != 0;
42 }
43 
44 #define BV_SET(bv, bit) \
45  do { \
46  _BV_ASSERT(bv, bit); \
47  (bv).vec[_BV_BYTE_INDEX(bit)] |= _BV_BITMASK(bit); \
48  } while (false)
49 #define BV_CLR(bv, bit) \
50  do { \
51  _BV_ASSERT(bv, bit); \
52  (bv).vec[_BV_BYTE_INDEX(bit)] &= ~_BV_BITMASK(bit); \
53  } while (false)
54 #define BV_SET_VAL(bv, bit, val) \
55  do { \
56  if (val) { \
57  BV_SET(bv, bit); \
58  } else { \
59  BV_CLR(bv, bit); \
60  } \
61  } while (false);
62 #define BV_CLR_ALL(bv) \
63  do { \
64  memset((bv).vec, 0, sizeof((bv).vec)); \
65  } while (false)
66 #define BV_SET_ALL(bv) \
67  do { \
68  memset((bv).vec, 0xff, sizeof((bv).vec)); \
69  } while (false)
70 
71 bool bv_check_mask(const unsigned char *vec1, const unsigned char *vec2,
72  size_t size1, size_t size2);
73 #define BV_CHECK_MASK(vec1, vec2) \
74  bv_check_mask((vec1).vec, (vec2).vec, sizeof((vec1).vec), \
75  sizeof((vec2).vec))
76 #define BV_ISSET_ANY(vec) BV_CHECK_MASK(vec, vec)
77 
78 bool bv_are_equal(const unsigned char *vec1, const unsigned char *vec2,
79  size_t size1, size_t size2);
80 #define BV_ARE_EQUAL(vec1, vec2) \
81  bv_are_equal((vec1).vec, (vec2).vec, sizeof((vec1).vec), \
82  sizeof((vec2).vec))
83 
84 void bv_set_all_from(unsigned char *vec_to, const unsigned char *vec_from,
85  size_t size_to, size_t size_from);
86 #define BV_SET_ALL_FROM(vec_to, vec_from) \
87  bv_set_all_from((vec_to).vec, (vec_from).vec, sizeof((vec_to).vec), \
88  sizeof((vec_from).vec))
89 
90 void bv_clr_all_from(unsigned char *vec_to, const unsigned char *vec_from,
91  size_t size_to, size_t size_from);
92 #define BV_CLR_ALL_FROM(vec_to, vec_from) \
93  bv_clr_all_from((vec_to).vec, (vec_from).vec, sizeof((vec_to).vec), \
94  sizeof((vec_from).vec))
95 
96 // Used to make a BV typedef. Such types are usually called "bv_foo".
97 #define BV_DEFINE(name, bits) \
98  typedef struct { \
99  unsigned char vec[_BV_BYTES(bits)]; \
100  } name
101 
102 bool is_any_set(QBitArray &ba);
bool bv_check_mask(const unsigned char *vec1, const unsigned char *vec2, size_t size1, size_t size2)
Return whether two vectors: vec1 and vec2 have common bits.
Definition: bitvector.cpp:31
void bv_clr_all_from(unsigned char *vec_to, const unsigned char *vec_from, size_t size_to, size_t size_from)
Clear everything that is true in vec_from in vec_to.
Definition: bitvector.cpp:99
#define _BV_BYTE_INDEX(bits)
Definition: bitvector.h:28
bool bv_are_equal(const unsigned char *vec1, const unsigned char *vec2, size_t size1, size_t size2)
Compares elements of two bitvectors.
Definition: bitvector.cpp:51
bool is_any_set(QBitArray &ba)
Definition: bitvector.cpp:112
bool BV_ISSET(const BV &bv, int bit)
Definition: bitvector.h:37
#define _BV_BITMASK(bit)
Definition: bitvector.h:29
void bv_set_all_from(unsigned char *vec_to, const unsigned char *vec_from, size_t size_to, size_t size_from)
Set everything that is true in vec_from in vec_to.
Definition: bitvector.cpp:77
#define fc_assert_ret_val(condition, val)
Definition: log.h:114