MySQL 8.0.31
Source Code Documentation
ut0bitset.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1994, 2022, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is also distributed with certain software (including but not
10limited to OpenSSL) that is licensed under separate terms, as designated in a
11particular file or component or in included license documentation. The authors
12of MySQL hereby grant you an additional permission to link the program and
13your derivative works with the separately licensed software that they have
14included with MySQL.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 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
37after the object has been defined */
38class Bitset {
39 public:
40 /** Constructor */
42
43 /** Destructor */
44 ~Bitset() = default;
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) {
51 m_size = 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) {
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 */
122};
123
124#endif
A simple bitset wrapper class, whose size can be specified after the object has been defined.
Definition: ut0bitset.h:38
Bitset()
Constructor.
Definition: ut0bitset.h:41
size_t m_size
Bitset size in bytes.
Definition: ut0bitset.h:117
bool test(size_t pos) const
Test if the specified bit is set or not.
Definition: ut0bitset.h:82
byte * m_bitset
Bitset bytes.
Definition: ut0bitset.h:114
size_t size() const
Get the size of current bitset.
Definition: ut0bitset.h:89
void copy(const byte *bitset, size_t size)
Copy a byte array and size to current bitmap.
Definition: ut0bitset.h:58
const byte * bitset() const
Get the bitset, don't allow to modify it!
Definition: ut0bitset.h:97
void init(byte *bitset, size_t size)
Initialize the bitset with a byte array and size.
Definition: ut0bitset.h:49
size_t capacity() const
Get the capacity of current bitset.
Definition: ut0bitset.h:93
Bitset & operator=(const Bitset &from)
Set current bitset with specified one.
Definition: ut0bitset.h:104
~Bitset()=default
Destructor.
void reset()
Set all bits to false.
Definition: ut0bitset.h:77
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
void set(size_t pos, bool v=true)
Set the specified bit to the value 'bit'.
Definition: ut0bitset.h:67
void set()
Set all bits to true.
Definition: ut0bitset.h:74
char * pos
Definition: do_ctype.cc:76
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:221
unsigned int uint
Definition: uca-dump.cc:29
#define ut_ad(EXPR)
Debug assertion.
Definition: ut0dbg.h:68