1 /***********************************************************************
2 * Software License Agreement (BSD License)
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.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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 *************************************************************************/
31 /***********************************************************************
32 * Author: Vincent Rabaud
33 *************************************************************************/
35 #ifndef OPENCV_FLANN_DYNAMIC_BITSET_H_
36 #define OPENCV_FLANN_DYNAMIC_BITSET_H_
38 #ifndef FLANN_USE_BOOST
39 # define FLANN_USE_BOOST 0
41 //#define FLANN_USE_BOOST 1
43 #include <boost/dynamic_bitset.hpp>
44 typedef boost::dynamic_bitset<> DynamicBitset;
53 /** Class re-implementing the boost version of it
54 * This helps not depending on boost, it also does not do the bound checks
55 * and has a way to reset a block for speed
60 /** @param default constructor
66 /** @param only constructor we use in our code
67 * @param the size of the bitset (in bits)
69 DynamicBitset(size_t sz)
75 /** Sets all the bits to 0
79 std::fill(bitset_.begin(), bitset_.end(), 0);
82 /** @brief checks if the bitset is empty
83 * @return true if the bitset is empty
87 return bitset_.empty();
90 /** @param set all the bits to 0
94 std::fill(bitset_.begin(), bitset_.end(), 0);
97 /** @brief set one bit to 0
100 void reset(size_t index)
102 bitset_[index / cell_bit_size_] &= ~(size_t(1) << (index % cell_bit_size_));
105 /** @brief sets a specific bit to 0, and more bits too
106 * This function is useful when resetting a given set of bits so that the
107 * whole bitset ends up being 0: if that's the case, we don't care about setting
111 void reset_block(size_t index)
113 bitset_[index / cell_bit_size_] = 0;
116 /** @param resize the bitset so that it contains at least size bits
119 void resize(size_t sz)
122 bitset_.resize(sz / cell_bit_size_ + 1);
125 /** @param set a bit to true
126 * @param index the index of the bit to set to 1
128 void set(size_t index)
130 bitset_[index / cell_bit_size_] |= size_t(1) << (index % cell_bit_size_);
133 /** @param gives the number of contained bits
140 /** @param check if a bit is set
141 * @param index the index of the bit to check
142 * @return true if the bit is set
144 bool test(size_t index) const
146 return (bitset_[index / cell_bit_size_] & (size_t(1) << (index % cell_bit_size_))) != 0;
150 std::vector<size_t> bitset_;
152 static const unsigned int cell_bit_size_ = CHAR_BIT * sizeof(size_t);
155 } // namespace cvflann
159 #endif // OPENCV_FLANN_DYNAMIC_BITSET_H_