MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SparseBitmask.hpp
1 /*
2  Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #ifndef NDB_SPARSE_BITMASK_H
19 #define NDB_SPARSE_BITMASK_H
20 
21 #include <ndb_global.h>
22 #include <util/Vector.hpp>
23 #include <util/BaseString.hpp>
24 
26  unsigned m_max_size;
27  Vector<unsigned> m_vec;
28 
29 public:
30  STATIC_CONST( NotFound = (unsigned)-1 );
31 
32  SparseBitmask(unsigned max_size = NotFound - 1) : m_max_size(max_size) {}
33 
34  unsigned max_size() const { return m_max_size; }
35 
36  /* Set bit n */
37  void set(unsigned n) {
38  assert(n <= m_max_size);
39 
40  unsigned i = m_vec.size();
41  while (i > 0)
42  {
43  const unsigned j = m_vec[i-1];
44  if (n == j)
45  return; // Bit n already set
46 
47  if (j < n)
48  break;
49  i--;
50  }
51 
52  m_vec.push(n, i);
53  }
54 
55  /* Get bit n */
56  bool get(unsigned n) const {
57  assert(n <= m_max_size);
58 
59  for (unsigned i = 0; i < m_vec.size(); i++)
60  {
61  const unsigned j = m_vec[i];
62  if (j < n)
63  continue;
64 
65  return (j == n);
66  }
67  return false;
68  }
69 
70  /* Clear bit n */
71  int clear(unsigned n) {
72  assert(n <= m_max_size);
73  for (unsigned i = 0; i < m_vec.size(); i++)
74  {
75  const unsigned j = m_vec[i];
76  if (j != n)
77  continue;
78 
79  m_vec.erase(i);
80  return 1;
81  }
82  return 0;
83  }
84 
85  /* Clear all bits */
86  void clear(void) {
87  m_vec.clear();
88  }
89 
90  /* Find first bit >= n */
91  unsigned find(unsigned n) const {
92  for (unsigned i = 0; i < m_vec.size(); i++)
93  {
94  const unsigned j = m_vec[i];
95  if (j >= n)
96  return j;
97  }
98  return NotFound;
99  }
100 
101  /* Number of bits set */
102  unsigned count() const { return m_vec.size(); }
103 
104  bool isclear() const { return count() == 0; }
105 
106  unsigned getBitNo(unsigned n) const {
107  assert(n < m_vec.size());
108  return m_vec[n];
109  }
110 
111  void print(void) const {
112  for (unsigned i = 0; i < m_vec.size(); i++)
113  {
114  const unsigned j = m_vec[i];
115  printf("[%u]: %u\n", i, j);
116  }
117  }
118 
119  bool equal(const SparseBitmask& obj) const {
120  if (obj.count() != count())
121  return false;
122 
123  for (unsigned i = 0; i<count(); i++)
124  if (!obj.get(m_vec[i]))
125  return false;
126 
127  return true;
128  }
129 
130  bool overlaps(const SparseBitmask& obj) const {
131  for (unsigned i = 0; i<count(); i++)
132  if (!obj.get(m_vec[i]))
133  return true;
134 
135  for (unsigned i = 0; i<obj.count(); i++)
136  if (!get(obj.getBitNo(i)))
137  return true;
138  return false;
139  }
140 
141  BaseString str() const {
142  BaseString tmp;
143  const char* sep="";
144  for (unsigned i = 0; i<m_vec.size(); i++)
145  {
146  tmp.appfmt("%s%u", sep, m_vec[i]);
147  sep=",";
148  }
149  return tmp;
150  }
151 };
152 
153 #endif