dynamic_bitset.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /***********************************************************************
  2. * Software License Agreement (BSD License)
  3. *
  4. * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
  5. * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
  6. *
  7. * THE BSD LICENSE
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  21. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *************************************************************************/
  30. /***********************************************************************
  31. * Author: Vincent Rabaud
  32. *************************************************************************/
  33. #ifndef OPENCV_FLANN_DYNAMIC_BITSET_H_
  34. #define OPENCV_FLANN_DYNAMIC_BITSET_H_
  35. #ifndef FLANN_USE_BOOST
  36. # define FLANN_USE_BOOST 0
  37. #endif
  38. //#define FLANN_USE_BOOST 1
  39. #if FLANN_USE_BOOST
  40. #include <boost/dynamic_bitset.hpp>
  41. typedef boost::dynamic_bitset<> DynamicBitset;
  42. #else
  43. #include <limits.h>
  44. #include "dist.h"
  45. namespace cvflann {
  46. /** Class re-implementing the boost version of it
  47. * This helps not depending on boost, it also does not do the bound checks
  48. * and has a way to reset a block for speed
  49. */
  50. class DynamicBitset
  51. {
  52. public:
  53. /** default constructor
  54. */
  55. DynamicBitset() : size_(0)
  56. {
  57. }
  58. /** only constructor we use in our code
  59. * @param sz the size of the bitset (in bits)
  60. */
  61. DynamicBitset(size_t sz)
  62. {
  63. resize(sz);
  64. reset();
  65. }
  66. /** Sets all the bits to 0
  67. */
  68. void clear()
  69. {
  70. std::fill(bitset_.begin(), bitset_.end(), 0);
  71. }
  72. /** @brief checks if the bitset is empty
  73. * @return true if the bitset is empty
  74. */
  75. bool empty() const
  76. {
  77. return bitset_.empty();
  78. }
  79. /** set all the bits to 0
  80. */
  81. void reset()
  82. {
  83. std::fill(bitset_.begin(), bitset_.end(), 0);
  84. }
  85. /** @brief set one bit to 0
  86. * @param index
  87. */
  88. void reset(size_t index)
  89. {
  90. bitset_[index / cell_bit_size_] &= ~(size_t(1) << (index % cell_bit_size_));
  91. }
  92. /** @brief sets a specific bit to 0, and more bits too
  93. * This function is useful when resetting a given set of bits so that the
  94. * whole bitset ends up being 0: if that's the case, we don't care about setting
  95. * other bits to 0
  96. * @param index
  97. */
  98. void reset_block(size_t index)
  99. {
  100. bitset_[index / cell_bit_size_] = 0;
  101. }
  102. /** resize the bitset so that it contains at least sz bits
  103. * @param sz
  104. */
  105. void resize(size_t sz)
  106. {
  107. size_ = sz;
  108. bitset_.resize(sz / cell_bit_size_ + 1);
  109. }
  110. /** set a bit to true
  111. * @param index the index of the bit to set to 1
  112. */
  113. void set(size_t index)
  114. {
  115. bitset_[index / cell_bit_size_] |= size_t(1) << (index % cell_bit_size_);
  116. }
  117. /** gives the number of contained bits
  118. */
  119. size_t size() const
  120. {
  121. return size_;
  122. }
  123. /** check if a bit is set
  124. * @param index the index of the bit to check
  125. * @return true if the bit is set
  126. */
  127. bool test(size_t index) const
  128. {
  129. return (bitset_[index / cell_bit_size_] & (size_t(1) << (index % cell_bit_size_))) != 0;
  130. }
  131. private:
  132. std::vector<size_t> bitset_;
  133. size_t size_;
  134. static const unsigned int cell_bit_size_ = CHAR_BIT * sizeof(size_t);
  135. };
  136. } // namespace cvflann
  137. #endif
  138. #endif // OPENCV_FLANN_DYNAMIC_BITSET_H_