The world's most popular open source database
00001 /* Copyright (C) 2003 MySQL AB 00002 00003 This program is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU General Public License as published by 00005 the Free Software Foundation; either version 2 of the License, or 00006 (at your option) any later version. 00007 00008 This program is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 GNU General Public License for more details. 00012 00013 You should have received a copy of the GNU General Public License 00014 along with this program; if not, write to the Free Software 00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 00016 00017 /* 00018 Implementation of a bitmap type. 00019 The idea with this is to be able to handle any constant number of bits but 00020 also be able to use 32 or 64 bits bitmaps very efficiently 00021 */ 00022 00023 #include <my_bitmap.h> 00024 00025 template <uint default_width> class Bitmap 00026 { 00027 MY_BITMAP map; 00028 uint32 buffer[(default_width+31)/32]; 00029 public: 00030 Bitmap() { init(); } 00031 Bitmap(const Bitmap& from) { *this=from; } 00032 explicit Bitmap(uint prefix_to_set) { init(prefix_to_set); } 00033 void init() { bitmap_init(&map, buffer, default_width, 0); } 00034 void init(uint prefix_to_set) { init(); set_prefix(prefix_to_set); } 00035 uint length() const { return default_width; } 00036 Bitmap& operator=(const Bitmap& map2) 00037 { 00038 init(); 00039 memcpy(buffer, map2.buffer, sizeof(buffer)); 00040 return *this; 00041 } 00042 void set_bit(uint n) { bitmap_set_bit(&map, n); } 00043 void clear_bit(uint n) { bitmap_clear_bit(&map, n); } 00044 void set_prefix(uint n) { bitmap_set_prefix(&map, n); } 00045 void set_all() { bitmap_set_all(&map); } 00046 void clear_all() { bitmap_clear_all(&map); } 00047 void intersect(Bitmap& map2) { bitmap_intersect(&map, &map2.map); } 00048 void intersect(ulonglong map2buff) 00049 { 00050 MY_BITMAP map2; 00051 bitmap_init(&map2, (uint32 *)&map2buff, sizeof(ulonglong)*8, 0); 00052 bitmap_intersect(&map, &map2); 00053 } 00054 /* Use highest bit for all bits above sizeof(ulonglong)*8. */ 00055 void intersect_extended(ulonglong map2buff) 00056 { 00057 intersect(map2buff); 00058 if (map.n_bits > sizeof(ulonglong) * 8) 00059 bitmap_set_above(&map, sizeof(ulonglong), 00060 test(map2buff & (LL(1) << (sizeof(ulonglong) * 8 - 1)))); 00061 } 00062 void subtract(Bitmap& map2) { bitmap_subtract(&map, &map2.map); } 00063 void merge(Bitmap& map2) { bitmap_union(&map, &map2.map); } 00064 my_bool is_set(uint n) const { return bitmap_is_set(&map, n); } 00065 my_bool is_prefix(uint n) const { return bitmap_is_prefix(&map, n); } 00066 my_bool is_clear_all() const { return bitmap_is_clear_all(&map); } 00067 my_bool is_set_all() const { return bitmap_is_set_all(&map); } 00068 my_bool is_subset(const Bitmap& map2) const { return bitmap_is_subset(&map, &map2.map); } 00069 my_bool is_overlapping(const Bitmap& map2) const { return bitmap_is_overlapping(&map, map2.map); } 00070 my_bool operator==(const Bitmap& map2) const { return bitmap_cmp(&map, &map2.map); } 00071 char *print(char *buf) const 00072 { 00073 char *s=buf; 00074 const uchar *e=(uchar *)buffer, *b=e+sizeof(buffer)-1; 00075 while (!*b && b>e) 00076 b--; 00077 if ((*s=_dig_vec_upper[*b >> 4]) != '0') 00078 s++; 00079 *s++=_dig_vec_upper[*b & 15]; 00080 while (--b>=e) 00081 { 00082 *s++=_dig_vec_upper[*b >> 4]; 00083 *s++=_dig_vec_upper[*b & 15]; 00084 } 00085 *s=0; 00086 return buf; 00087 } 00088 ulonglong to_ulonglong() const 00089 { 00090 if (sizeof(buffer) >= 8) 00091 return uint8korr(buffer); 00092 DBUG_ASSERT(sizeof(buffer) >= 4); 00093 return (ulonglong) uint4korr(buffer); 00094 } 00095 }; 00096 00097 template <> class Bitmap<64> 00098 { 00099 ulonglong map; 00100 public: 00101 Bitmap<64>() { } 00102 #if defined(__NETWARE__) || defined(__MWERKS__) 00103 /* 00104 Metwork compiler gives error on Bitmap<64> 00105 Changed to Bitmap, since in this case also it will proper construct 00106 this class 00107 */ 00108 explicit Bitmap(uint prefix_to_set) { set_prefix(prefix_to_set); } 00109 #else 00110 explicit Bitmap<64>(uint prefix_to_set) { set_prefix(prefix_to_set); } 00111 #endif 00112 void init() { } 00113 void init(uint prefix_to_set) { set_prefix(prefix_to_set); } 00114 uint length() const { return 64; } 00115 void set_bit(uint n) { map|= ((ulonglong)1) << n; } 00116 void clear_bit(uint n) { map&= ~(((ulonglong)1) << n); } 00117 void set_prefix(uint n) 00118 { 00119 if (n >= length()) 00120 set_all(); 00121 else 00122 map= (((ulonglong)1) << n)-1; 00123 } 00124 void set_all() { map=~(ulonglong)0; } 00125 void clear_all() { map=(ulonglong)0; } 00126 void intersect(Bitmap<64>& map2) { map&= map2.map; } 00127 void intersect(ulonglong map2) { map&= map2; } 00128 void intersect_extended(ulonglong map2) { map&= map2; } 00129 void subtract(Bitmap<64>& map2) { map&= ~map2.map; } 00130 void merge(Bitmap<64>& map2) { map|= map2.map; } 00131 my_bool is_set(uint n) const { return test(map & (((ulonglong)1) << n)); } 00132 my_bool is_prefix(uint n) const { return map == (((ulonglong)1) << n)-1; } 00133 my_bool is_clear_all() const { return map == (ulonglong)0; } 00134 my_bool is_set_all() const { return map == ~(ulonglong)0; } 00135 my_bool is_subset(const Bitmap<64>& map2) const { return !(map & ~map2.map); } 00136 my_bool is_overlapping(const Bitmap<64>& map2) const { return (map & map2.map)!= 0; } 00137 my_bool operator==(const Bitmap<64>& map2) const { return map == map2.map; } 00138 char *print(char *buf) const { longlong2str(map,buf,16); return buf; } 00139 ulonglong to_ulonglong() const { return map; } 00140 }; 00141
1.4.7

