~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_bitmap.h

  • Committer: Monty Taylor
  • Date: 2009-04-14 19:16:51 UTC
  • mto: (997.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 994.
  • Revision ID: mordred@inaugust.com-20090414191651-ltbww6hpqks8k7qk
Clarified instructions in README.

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
 
 
20
#ifndef _SQL_BITMAP_H_
 
21
#define _SQL_BITMAP_H_
 
22
 
 
23
/*
 
24
  Implementation of a bitmap type.
 
25
  The idea with this is to be able to handle any constant number of bits but
 
26
  also be able to use 32 or 64 bits bitmaps very efficiently
 
27
*/
 
28
 
 
29
/// TODO: OMG FIX THIS
 
30
 
 
31
#include <mysys/my_bitmap.h>
 
32
#include <drizzled/definitions.h>
 
33
#include <drizzled/util/test.h>
 
34
 
 
35
#include <bitset>
 
36
 
 
37
template <uint32_t default_width> class Bitmap
 
38
{
 
39
  MY_BITMAP map;
 
40
  uint32_t buffer[(default_width+31)/32];
 
41
public:
 
42
  Bitmap() : map() { init(); }
 
43
  Bitmap(const Bitmap& from) : map() { *this=from; }
 
44
  explicit Bitmap(uint32_t prefix_to_set) : map(0) { init(prefix_to_set); }
 
45
  void init() { bitmap_init(&map, buffer, default_width, 0); }
 
46
  void init(uint32_t prefix_to_set) { init(); set_prefix(prefix_to_set); }
 
47
  uint32_t length() const { return default_width; }
 
48
  Bitmap& operator=(const Bitmap& map2)
 
49
  {
 
50
    init();
 
51
    memcpy(buffer, map2.buffer, sizeof(buffer));
 
52
    return *this;
 
53
  }
 
54
  void set_bit(uint32_t n) { bitmap_set_bit(&map, n); }
 
55
  void clear_bit(uint32_t n) { bitmap_clear_bit(&map, n); }
 
56
  void set_prefix(uint32_t n) { bitmap_set_prefix(&map, n); }
 
57
  void set_all() { bitmap_set_all(&map); }
 
58
  void clear_all() { bitmap_clear_all(&map); }
 
59
  void intersect(Bitmap& map2) { bitmap_intersect(&map, &map2.map); }
 
60
  void intersect(uint64_t map2buff)
 
61
  {
 
62
    MY_BITMAP map2;
 
63
    bitmap_init(&map2, (uint32_t *)&map2buff, sizeof(uint64_t)*8, 0);
 
64
    bitmap_intersect(&map, &map2);
 
65
  }
 
66
  /* Use highest bit for all bits above sizeof(uint64_t)*8. */
 
67
  void intersect_extended(uint64_t map2buff)
 
68
  {
 
69
    intersect(map2buff);
 
70
    if (map.n_bits > sizeof(uint64_t) * 8)
 
71
      bitmap_set_above(&map, sizeof(uint64_t),
 
72
                       test(map2buff & (1 << (sizeof(uint64_t) * 8 - 1))));
 
73
  }
 
74
  void subtract(Bitmap& map2) { bitmap_subtract(&map, &map2.map); }
 
75
  void merge(Bitmap& map2) { bitmap_union(&map, &map2.map); }
 
76
  bool is_set(uint32_t n) const { return bitmap_is_set(&map, n); }
 
77
  bool is_set() const { return !bitmap_is_clear_all(&map); }
 
78
  bool is_prefix(uint32_t n) const { return bitmap_is_prefix(&map, n); }
 
79
  bool is_clear_all() const { return bitmap_is_clear_all(&map); }
 
80
  bool is_set_all() const { return bitmap_is_set_all(&map); }
 
81
  bool is_subset(const Bitmap& map2) const { return bitmap_is_subset(&map, &map2.map); }
 
82
  bool operator==(const Bitmap& map2) const { return bitmap_cmp(&map, &map2.map); }
 
83
  bool operator!=(const Bitmap& map2) const { return !bitmap_cmp(&map, &map2.map); }
 
84
  Bitmap operator&=(uint32_t n)
 
85
  {
 
86
    if (bitmap_is_set(&map, n))
 
87
    {
 
88
      bitmap_clear_all(&map);
 
89
      bitmap_set_bit(&map, n);
 
90
    }
 
91
    else
 
92
      bitmap_clear_all(&map);
 
93
    return *this;
 
94
  }
 
95
  Bitmap operator&=(const Bitmap& map2)
 
96
  {
 
97
    bitmap_intersect(&map, &map2.map);
 
98
    return *this;
 
99
  }
 
100
  Bitmap operator&(uint32_t n)
 
101
  {
 
102
    Bitmap bm(*this);
 
103
    bm&= n;
 
104
    return bm;
 
105
  }
 
106
  Bitmap operator&(const Bitmap& map2)
 
107
  {
 
108
    Bitmap bm(*this);
 
109
    bm&= map2;
 
110
    return bm;
 
111
  }
 
112
  Bitmap operator|=(uint32_t n)
 
113
  {
 
114
    bitmap_set_bit(&map, n);
 
115
    return *this;
 
116
  }
 
117
  Bitmap operator|=(const Bitmap& map2)
 
118
  {
 
119
    bitmap_union(&map, &map2.map);
 
120
  }
 
121
  Bitmap operator|(uint32_t n)
 
122
  {
 
123
    Bitmap bm(*this);
 
124
    bm|= n;
 
125
    return bm;
 
126
  }
 
127
  Bitmap operator|(const Bitmap& map2)
 
128
  {
 
129
    Bitmap bm(*this);
 
130
    bm|= map2;
 
131
    return bm;
 
132
  }
 
133
  Bitmap operator~()
 
134
  {
 
135
    Bitmap bm(*this);
 
136
    bitmap_invert(&bm.map);
 
137
    return bm;
 
138
  }
 
139
  char *print(char *buf) const
 
140
  {
 
141
    char *s=buf;
 
142
    const unsigned char *e=(unsigned char *)buffer, *b=e+sizeof(buffer)-1;
 
143
    while (!*b && b>e)
 
144
      b--;
 
145
    if ((*s=_dig_vec_upper[*b >> 4]) != '0')
 
146
        s++;
 
147
    *s++=_dig_vec_upper[*b & 15];
 
148
    while (--b>=e)
 
149
    {
 
150
      *s++=_dig_vec_upper[*b >> 4];
 
151
      *s++=_dig_vec_upper[*b & 15];
 
152
    }
 
153
    *s=0;
 
154
    return buf;
 
155
  }
 
156
  uint64_t to_uint64_t() const
 
157
  {
 
158
    if (sizeof(buffer) >= 8)
 
159
      return uint8korr(buffer);
 
160
    assert(sizeof(buffer) >= 4);
 
161
    return (uint64_t) uint4korr(buffer);
 
162
  }
 
163
};
 
164
 
 
165
template <> class Bitmap<64>
 
166
{
 
167
  uint64_t map;
 
168
public:
 
169
  Bitmap<64>() : map(0) { }
 
170
  explicit Bitmap<64>(uint32_t prefix_to_set) : map(0) { set_prefix(prefix_to_set); }
 
171
  void init() { }
 
172
  void init(uint32_t prefix_to_set) { set_prefix(prefix_to_set); }
 
173
  uint32_t length() const { return 64; }
 
174
  void set_bit(uint32_t n) { map|= ((uint64_t)1) << n; }
 
175
  void clear_bit(uint32_t n) { map&= ~(((uint64_t)1) << n); }
 
176
  void set_prefix(uint32_t n)
 
177
  {
 
178
    if (n >= length())
 
179
      set_all();
 
180
    else
 
181
      map= (((uint64_t)1) << n)-1;
 
182
  }
 
183
  void set_all() { map=~(uint64_t)0; }
 
184
  void clear_all() { map=(uint64_t)0; }
 
185
  void intersect(Bitmap<64>& map2) { map&= map2.map; }
 
186
  void intersect(uint64_t map2) { map&= map2; }
 
187
  void intersect_extended(uint64_t map2) { map&= map2; }
 
188
  void subtract(Bitmap<64>& map2) { map&= ~map2.map; }
 
189
  void merge(Bitmap<64>& map2) { map|= map2.map; }
 
190
  bool is_set(uint32_t n) const { return test(map & (((uint64_t)1) << n)); }
 
191
  bool is_prefix(uint32_t n) const { return map == (((uint64_t)1) << n)-1; }
 
192
  bool is_clear_all() const { return map == (uint64_t)0; }
 
193
  bool is_set_all() const { return map == ~(uint64_t)0; }
 
194
  bool is_subset(const Bitmap<64>& map2) const { return !(map & ~map2.map); }
 
195
  bool is_overlapping(const Bitmap<64>& map2) const { return (map & map2.map)!= 0; }
 
196
  bool operator==(const Bitmap<64>& map2) const { return map == map2.map; }
 
197
  char *print(char *buf) const { int64_t2str(map,buf,16); return buf; }
 
198
  uint64_t to_uint64_t() const { return map; }
 
199
};
 
200
 
 
201
 
 
202
typedef uint64_t table_map;          /* Used for table bits in join */
 
203
#if MAX_INDEXES <= 64
 
204
typedef Bitmap<64>  key_map;          /* Used for finding keys */
 
205
#else
 
206
typedef Bitmap<((MAX_INDEXES+7)/8*8)> key_map; /* Used for finding keys */
 
207
#endif
 
208
typedef uint32_t nesting_map;  /* Used for flags of nesting constructs */
 
209
 
 
210
/*
 
211
  Used to identify NESTED_JOIN structures within a join (applicable only to
 
212
  structures that have not been simplified away and embed more the one
 
213
  element)
 
214
*/
 
215
typedef uint64_t nested_join_map; /* Needed by sql_select.h and table.h */
 
216
 
 
217
/* useful constants */#
 
218
extern const key_map key_map_empty;
 
219
extern key_map key_map_full;          /* Should be threaded as const */
 
220
 
 
221
/*
 
222
 * Finds the first bit that is not set and sets
 
223
 * it.
 
224
 *
 
225
 * @param the bitmap to work with
 
226
 */
 
227
uint32_t setNextBit(std::bitset<MAX_FIELDS> &bitmap);
 
228
 
 
229
/*
 
230
 * Returns the position of the first bit in the
 
231
 * given bitmap which is not set. If every bit is set
 
232
 * in the bitmap, return MY_BIT_NONE.
 
233
 *
 
234
 * @param the bitmap to work with
 
235
 */
 
236
uint32_t getFirstBitPos(const std::bitset<MAX_FIELDS> &bitmap);
 
237
 
 
238
/*
 
239
 * Returns true if there is any overlapping bits between
 
240
 * the 2 given bitmaps.
 
241
 *
 
242
 * @param the first bitmap to work with
 
243
 * @param the second bitmap to work with
 
244
 */
 
245
bool isBitmapOverlapping(const std::bitset<MAX_FIELDS> *map1, const std::bitset<MAX_FIELDS> *map2);
 
246
 
 
247
#endif /* _SQL_BITMAP_H_ */