PageRenderTime 83ms CodeModel.GetById 50ms RepoModel.GetById 1ms app.codeStats 0ms

/mysql-5.5.25a/sql/sql_bitmap.h

#
C Header | 137 lines | 110 code | 7 blank | 20 comment | 12 complexity | 2b8e1e25a2540e72a4f63e96f87a69e5 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. /* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  12. /*
  13. Implementation of a bitmap type.
  14. The idea with this is to be able to handle any constant number of bits but
  15. also be able to use 32 or 64 bits bitmaps very efficiently
  16. */
  17. #ifndef SQL_BITMAP_INCLUDED
  18. #define SQL_BITMAP_INCLUDED
  19. #include <my_sys.h>
  20. #include <my_bitmap.h>
  21. template <uint default_width> class Bitmap
  22. {
  23. MY_BITMAP map;
  24. uint32 buffer[(default_width+31)/32];
  25. public:
  26. Bitmap() { init(); }
  27. Bitmap(const Bitmap& from) { *this=from; }
  28. explicit Bitmap(uint prefix_to_set) { init(prefix_to_set); }
  29. void init() { bitmap_init(&map, buffer, default_width, 0); }
  30. void init(uint prefix_to_set) { init(); set_prefix(prefix_to_set); }
  31. uint length() const { return default_width; }
  32. Bitmap& operator=(const Bitmap& map2)
  33. {
  34. init();
  35. memcpy(buffer, map2.buffer, sizeof(buffer));
  36. return *this;
  37. }
  38. void set_bit(uint n) { bitmap_set_bit(&map, n); }
  39. void clear_bit(uint n) { bitmap_clear_bit(&map, n); }
  40. void set_prefix(uint n) { bitmap_set_prefix(&map, n); }
  41. void set_all() { bitmap_set_all(&map); }
  42. void clear_all() { bitmap_clear_all(&map); }
  43. void intersect(Bitmap& map2) { bitmap_intersect(&map, &map2.map); }
  44. void intersect(ulonglong map2buff)
  45. {
  46. MY_BITMAP map2;
  47. bitmap_init(&map2, (uint32 *)&map2buff, sizeof(ulonglong)*8, 0);
  48. bitmap_intersect(&map, &map2);
  49. }
  50. /* Use highest bit for all bits above sizeof(ulonglong)*8. */
  51. void intersect_extended(ulonglong map2buff)
  52. {
  53. intersect(map2buff);
  54. if (map.n_bits > sizeof(ulonglong) * 8)
  55. bitmap_set_above(&map, sizeof(ulonglong),
  56. test(map2buff & (LL(1) << (sizeof(ulonglong) * 8 - 1))));
  57. }
  58. void subtract(Bitmap& map2) { bitmap_subtract(&map, &map2.map); }
  59. void merge(Bitmap& map2) { bitmap_union(&map, &map2.map); }
  60. my_bool is_set(uint n) const { return bitmap_is_set(&map, n); }
  61. my_bool is_prefix(uint n) const { return bitmap_is_prefix(&map, n); }
  62. my_bool is_clear_all() const { return bitmap_is_clear_all(&map); }
  63. my_bool is_set_all() const { return bitmap_is_set_all(&map); }
  64. my_bool is_subset(const Bitmap& map2) const { return bitmap_is_subset(&map, &map2.map); }
  65. my_bool is_overlapping(const Bitmap& map2) const { return bitmap_is_overlapping(&map, &map2.map); }
  66. my_bool operator==(const Bitmap& map2) const { return bitmap_cmp(&map, &map2.map); }
  67. char *print(char *buf) const
  68. {
  69. char *s=buf;
  70. const uchar *e=(uchar *)buffer, *b=e+sizeof(buffer)-1;
  71. while (!*b && b>e)
  72. b--;
  73. if ((*s=_dig_vec_upper[*b >> 4]) != '0')
  74. s++;
  75. *s++=_dig_vec_upper[*b & 15];
  76. while (--b>=e)
  77. {
  78. *s++=_dig_vec_upper[*b >> 4];
  79. *s++=_dig_vec_upper[*b & 15];
  80. }
  81. *s=0;
  82. return buf;
  83. }
  84. ulonglong to_ulonglong() const
  85. {
  86. if (sizeof(buffer) >= 8)
  87. return uint8korr(buffer);
  88. DBUG_ASSERT(sizeof(buffer) >= 4);
  89. return (ulonglong) uint4korr(buffer);
  90. }
  91. };
  92. template <> class Bitmap<64>
  93. {
  94. ulonglong map;
  95. public:
  96. Bitmap<64>() { }
  97. explicit Bitmap<64>(uint prefix_to_set) { set_prefix(prefix_to_set); }
  98. void init() { }
  99. void init(uint prefix_to_set) { set_prefix(prefix_to_set); }
  100. uint length() const { return 64; }
  101. void set_bit(uint n) { map|= ((ulonglong)1) << n; }
  102. void clear_bit(uint n) { map&= ~(((ulonglong)1) << n); }
  103. void set_prefix(uint n)
  104. {
  105. if (n >= length())
  106. set_all();
  107. else
  108. map= (((ulonglong)1) << n)-1;
  109. }
  110. void set_all() { map=~(ulonglong)0; }
  111. void clear_all() { map=(ulonglong)0; }
  112. void intersect(Bitmap<64>& map2) { map&= map2.map; }
  113. void intersect(ulonglong map2) { map&= map2; }
  114. void intersect_extended(ulonglong map2) { map&= map2; }
  115. void subtract(Bitmap<64>& map2) { map&= ~map2.map; }
  116. void merge(Bitmap<64>& map2) { map|= map2.map; }
  117. my_bool is_set(uint n) const { return test(map & (((ulonglong)1) << n)); }
  118. my_bool is_prefix(uint n) const { return map == (((ulonglong)1) << n)-1; }
  119. my_bool is_clear_all() const { return map == (ulonglong)0; }
  120. my_bool is_set_all() const { return map == ~(ulonglong)0; }
  121. my_bool is_subset(const Bitmap<64>& map2) const { return !(map & ~map2.map); }
  122. my_bool is_overlapping(const Bitmap<64>& map2) const { return (map & map2.map)!= 0; }
  123. my_bool operator==(const Bitmap<64>& map2) const { return map == map2.map; }
  124. char *print(char *buf) const { longlong2str(map,buf,16); return buf; }
  125. ulonglong to_ulonglong() const { return map; }
  126. };
  127. #endif /* SQL_BITMAP_INCLUDED */