Freeciv21
Develop your civilization from humble roots to a global empire
bitvector.cpp
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 
14 #include <QBitArray>
15 
16 // utility
17 #include "bitvector.h"
18 
19 /* bv_* - static bitvectors; used for data which where the length is
20  fixed (number of players; flags for enums; ...). They are
21  named bv_* and the macros BV_* are defined.
22  */
23 
31 bool bv_check_mask(const unsigned char *vec1, const unsigned char *vec2,
32  size_t size1, size_t size2)
33 {
34  size_t i;
35  fc_assert_ret_val(size1 == size2, false);
36 
37  for (i = 0; i < size1; i++) {
38  if ((vec1[0] & vec2[0]) != 0) {
39  return true;
40  }
41  vec1++;
42  vec2++;
43  }
44  return false;
45 }
46 
51 bool bv_are_equal(const unsigned char *vec1, const unsigned char *vec2,
52  size_t size1, size_t size2)
53 {
54  size_t i;
55  fc_assert_ret_val(size1 == size2, false);
56 
57  for (i = 0; i < size1; i++) {
58  if (vec1[0] != vec2[0]) {
59  return false;
60  }
61  vec1++;
62  vec2++;
63  }
64  return true;
65 }
66 
77 void bv_set_all_from(unsigned char *vec_to, const unsigned char *vec_from,
78  size_t size_to, size_t size_from)
79 {
80  size_t i;
81 
82  fc_assert_ret(size_to == size_from);
83 
84  for (i = 0; i < size_to; i++) {
85  vec_to[i] |= vec_from[i];
86  }
87 }
88 
99 void bv_clr_all_from(unsigned char *vec_to, const unsigned char *vec_from,
100  size_t size_to, size_t size_from)
101 {
102  size_t i;
103 
104  fc_assert_ret(size_to == size_from);
105 
106  for (i = 0; i < size_to; i++) {
107  vec_to[i] &= ~vec_from[i];
108  }
109 }
110 
111 // this is not very fast
112 bool is_any_set(QBitArray &ba)
113 {
114  for (int i = 0; i < ba.count(); i++) {
115  if (ba.testBit(i)) {
116  return true;
117  }
118  }
119  return false;
120 }
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
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
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(condition)
Definition: log.h:112
#define fc_assert_ret_val(condition, val)
Definition: log.h:114