proxygen
folly::BitIterator< BaseIter > Class Template Reference

#include <BitIterator.h>

Inheritance diagram for folly::BitIterator< BaseIter >:

Public Member Functions

 BitIterator (const BaseIter &iter, size_t bitOff=0)
 
size_t bitOffset () const
 
void advanceToNextBlock ()
 
BitIteratoroperator= (const BaseIter &other)
 

Static Public Member Functions

static unsigned int bitsPerBlock ()
 

Private Types

typedef bititerator_detail::BitReference< typename std::iterator_traits< BaseIter >::reference, typename std::iterator_traits< BaseIter >::value_type > BitRef
 

Private Member Functions

void advanceInBlock (size_t n)
 
BitRef dereference () const
 
void advance (ssize_t n)
 
void increment ()
 
void decrement ()
 
bool equal (const BitIterator &other) const
 
ssize_t distance_to (const BitIterator &other) const
 

Private Attributes

size_t bitOffset_
 

Friends

class boost::iterator_core_access
 
BitIterator findFirstSet (BitIterator, BitIterator)
 

Detailed Description

template<class BaseIter>
class folly::BitIterator< BaseIter >

Fast bit iteration facility.

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.

Definition at line 52 of file BitIterator.h.

Member Typedef Documentation

template<class BaseIter>
typedef bititerator_detail::BitReference< typename std::iterator_traits<BaseIter>::reference, typename std::iterator_traits<BaseIter>::value_type> folly::BitIterator< BaseIter >::BitRef
private

Definition at line 106 of file BitIterator.h.

Constructor & Destructor Documentation

template<class BaseIter>
folly::BitIterator< BaseIter >::BitIterator ( const BaseIter &  iter,
size_t  bitOff = 0 
)
inlineexplicit

Construct a BitIterator that points at a given bit offset (default 0) in iter.

Definition at line 78 of file BitIterator.h.

References folly::BitIterator< BaseIter >::bitOffset_, and folly::BitIterator< BaseIter >::bitsPerBlock().

Referenced by folly::BitIterator< BaseIter >::operator=().

80  bitOffset_(bitOff) {
81  assert(bitOffset_ < bitsPerBlock());
82  }
static unsigned int bitsPerBlock()
Definition: BitIterator.h:69
boost::iterator_adaptor< BitIterator< BaseIter >, BaseIter, bool, boost::use_default, bititerator_detail::BitReference< typename std::iterator_traits< BaseIter >::reference, typename std::iterator_traits< BaseIter >::value_type >, ssize_t > type

Member Function Documentation

template<class BaseIter>
void folly::BitIterator< BaseIter >::advance ( ssize_t  n)
inlineprivate

Definition at line 117 of file BitIterator.h.

References folly::BitIterator< BaseIter >::bitOffset_, and folly::BitIterator< BaseIter >::bitsPerBlock().

117  {
118  size_t bpb = bitsPerBlock();
119  ssize_t blocks = n / ssize_t(bpb);
120  bitOffset_ += n % bpb;
121  if (bitOffset_ >= bpb) {
122  bitOffset_ -= bpb;
123  ++blocks;
124  }
125  this->base_reference() += blocks;
126  }
static unsigned int bitsPerBlock()
Definition: BitIterator.h:69
template<class BaseIter>
void folly::BitIterator< BaseIter >::advanceInBlock ( size_t  n)
inlineprivate

Definition at line 108 of file BitIterator.h.

References folly::BitIterator< BaseIter >::bitOffset_, and folly::BitIterator< BaseIter >::bitsPerBlock().

Referenced by folly::findFirstSet().

108  {
109  bitOffset_ += n;
110  assert(bitOffset_ < bitsPerBlock());
111  }
static unsigned int bitsPerBlock()
Definition: BitIterator.h:69
template<class BaseIter>
void folly::BitIterator< BaseIter >::advanceToNextBlock ( )
inline

Definition at line 88 of file BitIterator.h.

References folly::BitIterator< BaseIter >::bitOffset_.

Referenced by folly::findFirstSet(), and folly::BitIterator< BaseIter >::increment().

88  {
89  bitOffset_ = 0;
90  ++this->base_reference();
91  }
template<class BaseIter>
size_t folly::BitIterator< BaseIter >::bitOffset ( ) const
inline

Definition at line 84 of file BitIterator.h.

References folly::BitIterator< BaseIter >::bitOffset_.

Referenced by folly::findFirstSet().

84  {
85  return bitOffset_;
86  }
template<class BaseIter>
static unsigned int folly::BitIterator< BaseIter >::bitsPerBlock ( )
inlinestatic

Return the number of bits in an element of the underlying iterator.

Definition at line 69 of file BitIterator.h.

References type.

Referenced by folly::BitIterator< BaseIter >::advance(), folly::BitIterator< BaseIter >::advanceInBlock(), folly::BitIterator< BaseIter >::BitIterator(), folly::BitIterator< BaseIter >::decrement(), folly::BitIterator< BaseIter >::distance_to(), and folly::BitIterator< BaseIter >::increment().

69  {
70  return std::numeric_limits<typename std::make_unsigned<
71  typename std::iterator_traits<BaseIter>::value_type>::type>::digits;
72  }
PskType type
template<class BaseIter>
void folly::BitIterator< BaseIter >::decrement ( )
inlineprivate

Definition at line 134 of file BitIterator.h.

References folly::BitIterator< BaseIter >::bitOffset_, and folly::BitIterator< BaseIter >::bitsPerBlock().

134  {
135  if (bitOffset_-- == 0) {
136  bitOffset_ = bitsPerBlock() - 1;
137  --this->base_reference();
138  }
139  }
static unsigned int bitsPerBlock()
Definition: BitIterator.h:69
template<class BaseIter>
BitRef folly::BitIterator< BaseIter >::dereference ( ) const
inlineprivate

Definition at line 113 of file BitIterator.h.

References folly::BitIterator< BaseIter >::bitOffset_.

113  {
114  return BitRef(*this->base_reference(), bitOffset_);
115  }
bititerator_detail::BitReference< typename std::iterator_traits< BaseIter >::reference, typename std::iterator_traits< BaseIter >::value_type > BitRef
Definition: BitIterator.h:106
template<class BaseIter>
ssize_t folly::BitIterator< BaseIter >::distance_to ( const BitIterator< BaseIter > &  other) const
inlineprivate

Definition at line 147 of file BitIterator.h.

References folly::BitIterator< BaseIter >::bitOffset_, and folly::BitIterator< BaseIter >::bitsPerBlock().

147  {
148  return ssize_t(
149  (other.base_reference() - this->base_reference()) * bitsPerBlock() +
150  other.bitOffset_ - bitOffset_);
151  }
static unsigned int bitsPerBlock()
Definition: BitIterator.h:69
template<class BaseIter>
bool folly::BitIterator< BaseIter >::equal ( const BitIterator< BaseIter > &  other) const
inlineprivate

Definition at line 141 of file BitIterator.h.

References folly::BitIterator< BaseIter >::bitOffset_.

141  {
142  return (
143  bitOffset_ == other.bitOffset_ &&
144  this->base_reference() == other.base_reference());
145  }
template<class BaseIter>
void folly::BitIterator< BaseIter >::increment ( )
inlineprivate
template<class BaseIter>
BitIterator& folly::BitIterator< BaseIter >::operator= ( const BaseIter &  other)
inline

Definition at line 93 of file BitIterator.h.

References folly::BitIterator< BaseIter >::BitIterator().

93  {
94  this->~BitIterator();
95  new (this) BitIterator(other);
96  return *this;
97  }
BitIterator(const BaseIter &iter, size_t bitOff=0)
Definition: BitIterator.h:78

Friends And Related Function Documentation

template<class BaseIter>
friend class boost::iterator_core_access
friend

Definition at line 100 of file BitIterator.h.

template<class BaseIter>
BitIterator findFirstSet ( BitIterator< BaseIter >  begin,
BitIterator< BaseIter >  end 
)
friend

Find first bit set in a range of bit iterators. 4.5x faster than the obvious std::find(begin, end, true);

Definition at line 170 of file BitIterator.h.

Referenced by folly::findFirstSet().

172  {
173  // shortcut to avoid ugly static_cast<>
174  static const typename std::iterator_traits<BaseIter>::value_type one = 1;
175 
176  while (begin.base() != end.base()) {
177  typename std::iterator_traits<BaseIter>::value_type v = *begin.base();
178  // mask out the bits that don't matter (< begin.bitOffset)
179  v &= ~((one << begin.bitOffset()) - 1);
180  size_t firstSet = findFirstSet(v);
181  if (firstSet) {
182  --firstSet; // now it's 0-based
183  assert(firstSet >= begin.bitOffset());
184  begin.advanceInBlock(firstSet - begin.bitOffset());
185  return begin;
186  }
187  begin.advanceToNextBlock();
188  }
189 
190  // now begin points to the same block as end
191  if (end.bitOffset() != 0) { // assume end is dereferenceable
192  typename std::iterator_traits<BaseIter>::value_type v = *begin.base();
193  // mask out the bits that don't matter (< begin.bitOffset)
194  v &= ~((one << begin.bitOffset()) - 1);
195  // mask out the bits that don't matter (>= end.bitOffset)
196  v &= (one << end.bitOffset()) - 1;
197  size_t firstSet = findFirstSet(v);
198  if (firstSet) {
199  --firstSet; // now it's 0-based
200  assert(firstSet >= begin.bitOffset());
201  begin.advanceInBlock(firstSet - begin.bitOffset());
202  return begin;
203  }
204  }
205 
206  return end;
207 }
auto begin(TestAdlIterable &instance)
Definition: ForeachTest.cpp:56
auto end(TestAdlIterable &instance)
Definition: ForeachTest.cpp:62
friend BitIterator findFirstSet(BitIterator, BitIterator)
Definition: BitIterator.h:170

Member Data Documentation


The documentation for this class was generated from the following file: