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