MySQL  8.0.19
Source Code Documentation
ut0bitset.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3 Copyright (c) 1994, 2019, Oracle and/or its affiliates. All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8 
9 This program is also distributed with certain software (including but not
10 limited to OpenSSL) that is licensed under separate terms, as designated in a
11 particular file or component or in included license documentation. The authors
12 of MySQL hereby grant you an additional permission to link the program and
13 your derivative works with the separately licensed software that they have
14 included with MySQL.
15 
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19 for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 
25 *****************************************************************************/
26 
27 /** @file include/ut0bitset.h
28  Utilities for bitset operations
29 
30  Created 11/05/2018 Bin Su
31  ***********************************************************************/
32 
33 #ifndef ut0bitset_h
34 #define ut0bitset_h
35 
36 /** A simple bitset wrapper class, whose size can be specified
37 after the object has been defined */
38 class Bitset {
39  public:
40  /** Constructor */
41  Bitset() : m_bitset(nullptr), m_size(0) {}
42 
43  /** Destructor */
44  ~Bitset() {}
45 
46  /** Initialize the bitset with a byte array and size
47  @param[in] bitset byte array for this bitset
48  @param[in] size size of the byte array */
49  void init(byte *bitset, size_t size) {
50  m_bitset = bitset;
51  m_size = size;
52  m_capacity = size;
53  }
54 
55  /** Copy a byte array and size to current bitmap
56  @param[in] bitset byte array for this bitset
57  @param[in] size size of the byte array */
58  void copy(const byte *bitset, size_t size) {
59  ut_ad(m_capacity >= size);
60  memcpy(m_bitset, bitset, size);
61  m_size = size;
62  }
63 
64  /** Set the specified bit to the value 'bit'
65  @param[in] pos specified bit
66  @param[in] v true or false */
67  void set(size_t pos, bool v = true) {
68  ut_ad(pos / 8 < m_size);
69  m_bitset[pos / 8] &= ~(0x1 << (pos & 0x7));
70  m_bitset[pos / 8] |= (static_cast<uint>(v) << (pos & 0x7));
71  }
72 
73  /** Set all bits to true */
74  void set() { memset(m_bitset, 0xFF, m_size); }
75 
76  /** Set all bits to false */
77  void reset() { memset(m_bitset, 0, m_size); }
78 
79  /** Test if the specified bit is set or not
80  @param[in] pos the specified bit
81  @return True if this bit is set, otherwise false */
82  bool test(size_t pos) const {
83  ut_ad(pos / 8 < m_size);
84  return ((m_bitset[pos / 8] >> (pos & 0x7)) & 0x1);
85  }
86 
87  /** Get the size of current bitset
88  @return the size of the bitset */
89  size_t size() const { return (m_size); }
90 
91  /** Get the capacity of current bitset
92  @return the capacity of the bitset */
93  size_t capacity() const { return (m_capacity); }
94 
95  /** Get the bitset, don't allow to modify it!
96  @return current bitset */
97  const byte *bitset() const { return (m_bitset); }
98 
99  /** Set current bitset with specified one. Current bitset should have
100  called init() to allocate its own bitmap memory which should be big
101  enough for the assignment.
102  @param[in] from set the bitset from this one
103  @return current bitset object */
104  Bitset &operator=(const Bitset &from) {
105  ut_ad(m_capacity >= from.m_size);
106  memcpy(m_bitset, from.m_bitset, from.m_size);
107  m_size = from.m_size;
108 
109  return (*this);
110  }
111 
112  private:
113  /** Bitset bytes */
114  byte *m_bitset;
115 
116  /** Bitset size in bytes */
117  size_t m_size;
118 
119  /** Bitset capacity, could be bigger than m_size if one smaller bitmap
120  has been assigned to it, after a copy() called */
121  size_t m_capacity;
122 };
123 
124 #endif
Bitset::size
size_t size() const
Get the size of current bitset.
Definition: ut0bitset.h:89
ut_ad
#define ut_ad(EXPR)
Debug assertion.
Definition: ut0dbg.h:65
Bitset::bitset
const byte * bitset() const
Get the bitset, don't allow to modify it!
Definition: ut0bitset.h:97
pos
char * pos
Definition: do_ctype.cc:76
Bitset::m_size
size_t m_size
Bitset size in bytes.
Definition: ut0bitset.h:117
Bitset::m_bitset
byte * m_bitset
Bitset bytes.
Definition: ut0bitset.h:114
Bitset::capacity
size_t capacity() const
Get the capacity of current bitset.
Definition: ut0bitset.h:93
Bitset::set
void set(size_t pos, bool v=true)
Set the specified bit to the value 'bit'.
Definition: ut0bitset.h:67
uint
unsigned int uint
Definition: uca-dump.cc:29
Bitset::init
void init(byte *bitset, size_t size)
Initialize the bitset with a byte array and size.
Definition: ut0bitset.h:49
Bitset
A simple bitset wrapper class, whose size can be specified after the object has been defined.
Definition: ut0bitset.h:38
Bitset::copy
void copy(const byte *bitset, size_t size)
Copy a byte array and size to current bitmap.
Definition: ut0bitset.h:58
Bitset::m_capacity
size_t m_capacity
Bitset capacity, could be bigger than m_size if one smaller bitmap has been assigned to it,...
Definition: ut0bitset.h:121
Bitset::~Bitset
~Bitset()
Destructor.
Definition: ut0bitset.h:44
Bitset::test
bool test(size_t pos) const
Test if the specified bit is set or not.
Definition: ut0bitset.h:82
Bitset::operator=
Bitset & operator=(const Bitset &from)
Set current bitset with specified one.
Definition: ut0bitset.h:104
Bitset::reset
void reset()
Set all bits to false.
Definition: ut0bitset.h:77
Bitset::Bitset
Bitset()
Constructor.
Definition: ut0bitset.h:41
Bitset::set
void set()
Set all bits to true.
Definition: ut0bitset.h:74