~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_bitmap.h

  • Committer: Monty Taylor
  • Date: 2008-10-16 06:32:30 UTC
  • mto: (511.1.5 codestyle)
  • mto: This revision was merged to the branch mainline in revision 521.
  • Revision ID: monty@inaugust.com-20081016063230-4brxsra0qsmsg84q
Added -Wunused-macros.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
1
20
#ifndef _SQL_BITMAP_H_
2
21
#define _SQL_BITMAP_H_
3
 
/* Copyright (C) 2003 MySQL AB
4
 
 
5
 
   This program is free software; you can redistribute it and/or modify
6
 
   it under the terms of the GNU General Public License as published by
7
 
   the Free Software Foundation; version 2 of the License.
8
 
 
9
 
   This program is distributed in the hope that it will be useful,
10
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
   GNU General Public License for more details.
13
 
 
14
 
   You should have received a copy of the GNU General Public License
15
 
   along with this program; if not, write to the Free Software
16
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
17
22
 
18
23
/*
19
24
  Implementation of a bitmap type.
21
26
  also be able to use 32 or 64 bits bitmaps very efficiently
22
27
*/
23
28
 
24
 
#include <my_bitmap.h>
25
 
 
26
 
template <uint default_width> class Bitmap
 
29
/// TODO: OMG FIX THIS
 
30
 
 
31
#include <mysys/my_bitmap.h>
 
32
#include <drizzled/util/test.h>
 
33
 
 
34
template <uint32_t default_width> class Bitmap
27
35
{
28
36
  MY_BITMAP map;
29
 
  uint32 buffer[(default_width+31)/32];
 
37
  uint32_t buffer[(default_width+31)/32];
30
38
public:
31
 
  Bitmap() { init(); }
32
 
  Bitmap(const Bitmap& from) { *this=from; }
33
 
  explicit Bitmap(uint prefix_to_set) { init(prefix_to_set); }
 
39
  Bitmap() : map() { init(); }
 
40
  Bitmap(const Bitmap& from) : map() { *this=from; }
 
41
  explicit Bitmap(uint32_t prefix_to_set) : map(0) { init(prefix_to_set); }
34
42
  void init() { bitmap_init(&map, buffer, default_width, 0); }
35
 
  void init(uint prefix_to_set) { init(); set_prefix(prefix_to_set); }
36
 
  uint length() const { return default_width; }
 
43
  void init(uint32_t prefix_to_set) { init(); set_prefix(prefix_to_set); }
 
44
  uint32_t length() const { return default_width; }
37
45
  Bitmap& operator=(const Bitmap& map2)
38
46
  {
39
47
    init();
40
48
    memcpy(buffer, map2.buffer, sizeof(buffer));
41
49
    return *this;
42
50
  }
43
 
  void set_bit(uint n) { bitmap_set_bit(&map, n); }
44
 
  void clear_bit(uint n) { bitmap_clear_bit(&map, n); }
45
 
  void set_prefix(uint n) { bitmap_set_prefix(&map, n); }
 
51
  void set_bit(uint32_t n) { bitmap_set_bit(&map, n); }
 
52
  void clear_bit(uint32_t n) { bitmap_clear_bit(&map, n); }
 
53
  void set_prefix(uint32_t n) { bitmap_set_prefix(&map, n); }
46
54
  void set_all() { bitmap_set_all(&map); }
47
55
  void clear_all() { bitmap_clear_all(&map); }
48
56
  void intersect(Bitmap& map2) { bitmap_intersect(&map, &map2.map); }
49
57
  void intersect(uint64_t map2buff)
50
58
  {
51
59
    MY_BITMAP map2;
52
 
    bitmap_init(&map2, (uint32 *)&map2buff, sizeof(uint64_t)*8, 0);
 
60
    bitmap_init(&map2, (uint32_t *)&map2buff, sizeof(uint64_t)*8, 0);
53
61
    bitmap_intersect(&map, &map2);
54
62
  }
55
63
  /* Use highest bit for all bits above sizeof(uint64_t)*8. */
62
70
  }
63
71
  void subtract(Bitmap& map2) { bitmap_subtract(&map, &map2.map); }
64
72
  void merge(Bitmap& map2) { bitmap_union(&map, &map2.map); }
65
 
  my_bool is_set(uint n) const { return bitmap_is_set(&map, n); }
66
 
  my_bool is_set() const { return !bitmap_is_clear_all(&map); }
67
 
  my_bool is_prefix(uint n) const { return bitmap_is_prefix(&map, n); }
68
 
  my_bool is_clear_all() const { return bitmap_is_clear_all(&map); }
69
 
  my_bool is_set_all() const { return bitmap_is_set_all(&map); }
70
 
  my_bool is_subset(const Bitmap& map2) const { return bitmap_is_subset(&map, &map2.map); }
71
 
  my_bool is_overlapping(const Bitmap& map2) const { return bitmap_is_overlapping(&map, &map2.map); }
72
 
  my_bool operator==(const Bitmap& map2) const { return bitmap_cmp(&map, &map2.map); }
73
 
  my_bool operator!=(const Bitmap& map2) const { return !bitmap_cmp(&map, &map2.map); }
74
 
  Bitmap operator&=(uint n)
 
73
  bool is_set(uint32_t n) const { return bitmap_is_set(&map, n); }
 
74
  bool is_set() const { return !bitmap_is_clear_all(&map); }
 
75
  bool is_prefix(uint32_t n) const { return bitmap_is_prefix(&map, n); }
 
76
  bool is_clear_all() const { return bitmap_is_clear_all(&map); }
 
77
  bool is_set_all() const { return bitmap_is_set_all(&map); }
 
78
  bool is_subset(const Bitmap& map2) const { return bitmap_is_subset(&map, &map2.map); }
 
79
  bool is_overlapping(const Bitmap& map2) const { return bitmap_is_overlapping(&map, &map2.map); }
 
80
  bool operator==(const Bitmap& map2) const { return bitmap_cmp(&map, &map2.map); }
 
81
  bool operator!=(const Bitmap& map2) const { return !bitmap_cmp(&map, &map2.map); }
 
82
  Bitmap operator&=(uint32_t n)
75
83
  {
76
84
    if (bitmap_is_set(&map, n))
77
85
    {
87
95
    bitmap_intersect(&map, &map2.map);
88
96
    return *this;
89
97
  }
90
 
  Bitmap operator&(uint n)
 
98
  Bitmap operator&(uint32_t n)
91
99
  {
92
100
    Bitmap bm(*this);
93
101
    bm&= n;
99
107
    bm&= map2;
100
108
    return bm;
101
109
  }
102
 
  Bitmap operator|=(uint n)
 
110
  Bitmap operator|=(uint32_t n)
103
111
  {
104
112
    bitmap_set_bit(&map, n);
105
113
    return *this;
108
116
  {
109
117
    bitmap_union(&map, &map2.map);
110
118
  }
111
 
  Bitmap operator|(uint n)
 
119
  Bitmap operator|(uint32_t n)
112
120
  {
113
121
    Bitmap bm(*this);
114
122
    bm|= n;
129
137
  char *print(char *buf) const
130
138
  {
131
139
    char *s=buf;
132
 
    const uchar *e=(uchar *)buffer, *b=e+sizeof(buffer)-1;
 
140
    const unsigned char *e=(unsigned char *)buffer, *b=e+sizeof(buffer)-1;
133
141
    while (!*b && b>e)
134
142
      b--;
135
143
    if ((*s=_dig_vec_upper[*b >> 4]) != '0')
156
164
{
157
165
  uint64_t map;
158
166
public:
159
 
  Bitmap<64>() { map= 0; }
160
 
  explicit Bitmap<64>(uint prefix_to_set) { set_prefix(prefix_to_set); }
 
167
  Bitmap<64>() : map(0) { }
 
168
  explicit Bitmap<64>(uint32_t prefix_to_set) : map(0) { set_prefix(prefix_to_set); }
161
169
  void init() { }
162
 
  void init(uint prefix_to_set) { set_prefix(prefix_to_set); }
163
 
  uint length() const { return 64; }
164
 
  void set_bit(uint n) { map|= ((uint64_t)1) << n; }
165
 
  void clear_bit(uint n) { map&= ~(((uint64_t)1) << n); }
166
 
  void set_prefix(uint n)
 
170
  void init(uint32_t prefix_to_set) { set_prefix(prefix_to_set); }
 
171
  uint32_t length() const { return 64; }
 
172
  void set_bit(uint32_t n) { map|= ((uint64_t)1) << n; }
 
173
  void clear_bit(uint32_t n) { map&= ~(((uint64_t)1) << n); }
 
174
  void set_prefix(uint32_t n)
167
175
  {
168
176
    if (n >= length())
169
177
      set_all();
177
185
  void intersect_extended(uint64_t map2) { map&= map2; }
178
186
  void subtract(Bitmap<64>& map2) { map&= ~map2.map; }
179
187
  void merge(Bitmap<64>& map2) { map|= map2.map; }
180
 
  my_bool is_set(uint n) const { return test(map & (((uint64_t)1) << n)); }
181
 
  my_bool is_prefix(uint n) const { return map == (((uint64_t)1) << n)-1; }
182
 
  my_bool is_clear_all() const { return map == (uint64_t)0; }
183
 
  my_bool is_set_all() const { return map == ~(uint64_t)0; }
184
 
  my_bool is_subset(const Bitmap<64>& map2) const { return !(map & ~map2.map); }
185
 
  my_bool is_overlapping(const Bitmap<64>& map2) const { return (map & map2.map)!= 0; }
186
 
  my_bool operator==(const Bitmap<64>& map2) const { return map == map2.map; }
 
188
  bool is_set(uint32_t n) const { return test(map & (((uint64_t)1) << n)); }
 
189
  bool is_prefix(uint32_t n) const { return map == (((uint64_t)1) << n)-1; }
 
190
  bool is_clear_all() const { return map == (uint64_t)0; }
 
191
  bool is_set_all() const { return map == ~(uint64_t)0; }
 
192
  bool is_subset(const Bitmap<64>& map2) const { return !(map & ~map2.map); }
 
193
  bool is_overlapping(const Bitmap<64>& map2) const { return (map & map2.map)!= 0; }
 
194
  bool operator==(const Bitmap<64>& map2) const { return map == map2.map; }
187
195
  char *print(char *buf) const { int64_t2str(map,buf,16); return buf; }
188
196
  uint64_t to_uint64_t() const { return map; }
189
197
};
193
201
class Table_map_iterator
194
202
{
195
203
  uint64_t bmp;
196
 
  uint no;
 
204
  uint32_t no;
197
205
public:
198
206
  Table_map_iterator(uint64_t t) : bmp(t), no(0) {}
199
207
  int next_bit()
202
210
                                      2, 0, 1, 0, 
203
211
                                      3, 0, 1, 0,
204
212
                                      2, 0, 1, 0};
205
 
    uint bit;
 
213
    uint32_t bit;
206
214
    while ((bit= last_bit[bmp & 0xF]) == 32)
207
215
    {
208
216
      no += 4;