2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <type_traits>
21 #include <boost/iterator/iterator_adaptor.hpp>
25 template <class BaseIter> class BitIterator;
27 namespace bititerator_detail {
29 // Reference to a bit.
30 // Templatize on both parent reference and value types to capture
31 // const-ness correctly and to work with the case where Ref is a
32 // reference-like type (not T&), just like our BitReference here.
33 template <class Ref, class Value>
36 BitReference(Ref r, size_t bit) : ref_(r), bit_(bit) { }
38 /* implicit */ operator bool() const {
39 return ref_ & (one_ << bit_);
42 BitReference& operator=(bool b) {
52 ref_ |= (one_ << bit_);
56 ref_ &= ~(one_ << bit_);
60 ref_ ^= (one_ << bit_);
64 // shortcut to avoid writing static_cast everywhere
65 const static Value one_ = 1;
71 template <class BaseIter>
72 struct BitIteratorBase {
73 static_assert(std::is_integral<typename BaseIter::value_type>::value,
74 "BitIterator may only be used with integral types");
75 typedef boost::iterator_adaptor<
76 BitIterator<BaseIter>, // Derived
79 boost::use_default, // CategoryOrTraversal
80 bititerator_detail::BitReference<
81 typename std::iterator_traits<BaseIter>::reference,
82 typename std::iterator_traits<BaseIter>::value_type
88 } // namespace bititerator_detail