/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * BitIterator * Wrapper around an iterator over an integral type that iterates * over its underlying bits in MSb to LSb order * * findFirstSet(BitIterator begin, BitIterator end) * return a BitIterator pointing to the first 1 bit in [begin, end), or * end if all bits in [begin, end) are 0 */ #pragma once #include <cassert> #include <cinttypes> #include <cstdint> #include <cstring> #include <iterator> #include <limits> #include <type_traits> #include <boost/iterator/iterator_adaptor.hpp> #include <folly/Portability.h> #include <folly/container/detail/BitIteratorDetail.h> #include <folly/lang/Bits.h> namespace folly { /** * Fast bit iteration facility. */ template <class BaseIter> class BitIterator; template <class BaseIter> BitIterator<BaseIter> findFirstSet( BitIterator<BaseIter>, BitIterator<BaseIter>); /** * Wrapper around an iterator over an integer type that iterates * over its underlying bits in LSb to MSb order. * * BitIterator models the same iterator concepts as the base iterator. */ template <class BaseIter> class BitIterator : public bititerator_detail::BitIteratorBase<BaseIter>::type { … }; /** * Helper function, so you can write * auto bi = makeBitIterator(container.begin()); */ template <class BaseIter> BitIterator<BaseIter> makeBitIterator(const BaseIter& iter) { … } /** * Find first bit set in a range of bit iterators. * 4.5x faster than the obvious std::find(begin, end, true); */ template <class BaseIter> BitIterator<BaseIter> findFirstSet( BitIterator<BaseIter> begin, BitIterator<BaseIter> end) { … } } // namespace folly