dynamic_bitset.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. //! @cond IGNORED
  36. #ifndef FLANN_USE_BOOST
  37. # define FLANN_USE_BOOST 0
  38. #endif
  39. //#define FLANN_USE_BOOST 1
  40. #if FLANN_USE_BOOST
  41. #include <boost/dynamic_bitset.hpp>
  42. typedef boost::dynamic_bitset<> DynamicBitset;
  43. #else
  44. #include <limits.h>
  45. #include "dist.h"
  46. namespace cvflann {
  47. /** Class re-implementing the boost version of it
  48. * This helps not depending on boost, it also does not do the bound checks
  49. * and has a way to reset a block for speed
  50. */
  51. class DynamicBitset
  52. {
  53. public:
  54. /** default constructor
  55. */
  56. DynamicBitset() : size_(0)
  57. {
  58. }
  59. /** only constructor we use in our code
  60. * @param sz the size of the bitset (in bits)
  61. */
  62. DynamicBitset(size_t sz)
  63. {
  64. resize(sz);
  65. reset();
  66. }
  67. /** Sets all the bits to 0
  68. */
  69. void clear()
  70. {
  71. std::fill(bitset_.begin(), bitset_.end(), 0);
  72. }
  73. /** @brief checks if the bitset is empty
  74. * @return true if the bitset is empty
  75. */
  76. bool empty() const
  77. {
  78. return bitset_.empty();
  79. }
  80. /** set all the bits to 0
  81. */
  82. void reset()
  83. {
  84. std::fill(bitset_.begin(), bitset_.end(), 0);
  85. }
  86. /** @brief set one bit to 0
  87. * @param index
  88. */
  89. void reset(size_t index)
  90. {
  91. bitset_[index / cell_bit_size_] &= ~(size_t(1) << (index % cell_bit_size_));
  92. }
  93. /** @brief sets a specific bit to 0, and more bits too
  94. * This function is useful when resetting a given set of bits so that the
  95. * whole bitset ends up being 0: if that's the case, we don't care about setting
  96. * other bits to 0
  97. * @param index
  98. */
  99. void reset_block(size_t index)
  100. {
  101. bitset_[index / cell_bit_size_] = 0;
  102. }
  103. /** resize the bitset so that it contains at least sz bits
  104. * @param sz
  105. */
  106. void resize(size_t sz)
  107. {
  108. size_ = sz;
  109. bitset_.resize(sz / cell_bit_size_ + 1);
  110. }
  111. /** set a bit to true
  112. * @param index the index of the bit to set to 1
  113. */
  114. void set(size_t index)
  115. {
  116. bitset_[index / cell_bit_size_] |= size_t(1) << (index % cell_bit_size_);
  117. }
  118. /** gives the number of contained bits
  119. */
  120. size_t size() const
  121. {
  122. return size_;
  123. }
  124. /** check if a bit is set
  125. * @param index the index of the bit to check
  126. * @return true if the bit is set
  127. */
  128. bool test(size_t index) const
  129. {
  130. return (bitset_[index / cell_bit_size_] & (size_t(1) << (index % cell_bit_size_))) != 0;
  131. }
  132. private:
  133. std::vector<size_t> bitset_;
  134. size_t size_;
  135. static const unsigned int cell_bit_size_ = CHAR_BIT * sizeof(size_t);
  136. };
  137. } // namespace cvflann
  138. #endif
  139. //! @endcond
  140. #endif // OPENCV_FLANN_DYNAMIC_BITSET_H_