proxygen
folly::padded::Adaptor< Container > Class Template Reference

#include <Padded.h>

Public Types

typedef Container::value_type Node
 
typedef Node::value_type value_type
 
typedef value_typereference
 
typedef const value_typeconst_reference
 
typedef Iterator< typename Container::iterator > iterator
 
typedef Iterator< typename Container::const_iterator > const_iterator
 
typedef const_iterator::difference_type difference_type
 
typedef Container::size_type size_type
 

Public Member Functions

 Adaptor ()
 
 Adaptor (Container c, size_t lastCount=Node::kElementCount)
 
 Adaptor (size_t n, const value_type &value=value_type())
 
 Adaptor (const Adaptor &)=default
 
Adaptoroperator= (const Adaptor &)=default
 
 Adaptor (Adaptor &&other) noexcept
 
Adaptoroperator= (Adaptor &&other)
 
const_iterator cbegin () const
 
const_iterator cend () const
 
const_iterator begin () const
 
const_iterator end () const
 
iterator begin ()
 
iterator end ()
 
void swap (Adaptor &other)
 
bool empty () const
 
size_type size () const
 
size_type max_size () const
 
const value_typefront () const
 
value_typefront ()
 
const value_typeback () const
 
value_typeback ()
 
template<typename... Args>
void emplace_back (Args &&...args)
 
void push_back (value_type x)
 
void pop_back ()
 
void clear ()
 
void reserve (size_type n)
 
size_type capacity () const
 
const value_typeoperator[] (size_type idx) const
 
value_typeoperator[] (size_type idx)
 
std::pair< Container, size_t > move ()
 
std::pair< const Container &, size_t > peek () const
 
void padToFullNode (const value_type &padValue)
 

Static Public Attributes

static constexpr size_t kElementsPerNode = Node::kElementCount
 

Private Member Functions

value_typeallocate_back ()
 

Static Private Member Functions

static Node fullNode (const value_type &value)
 

Private Attributes

Container c_
 
size_t lastCount_
 

Detailed Description

template<class Container>
class folly::padded::Adaptor< Container >

Adaptor around a STL sequence container.

Converts a sequence of Node into a sequence of its underlying elements (with enough functionality to make it useful, although it's not fully compatible with the STL containre requiremenets, see below).

Provides iterators (of the same category as those of the underlying container), size(), front(), back(), push_back(), pop_back(), and const / non-const versions of operator[] (if the underlying container supports them). Does not provide push_front() / pop_front() or arbitrary insert / emplace / erase. Also provides reserve() / capacity() if supported by the underlying container.

Yes, it's called Adaptor, not Adapter, as that's the name used by the STL and by boost. Deal with it.

Internally, we hold a container of Node and the number of elements in the last block. We don't keep empty blocks, so the number of elements in the last block is always between 1 and Node::kElementCount (inclusive). (this is true if the container is empty as well to make push_back() simpler, see the implementation of the size() method for details).

Definition at line 372 of file Padded.h.

Member Typedef Documentation

template<class Container>
typedef Iterator<typename Container::const_iterator> folly::padded::Adaptor< Container >::const_iterator

Definition at line 379 of file Padded.h.

template<class Container>
typedef const value_type& folly::padded::Adaptor< Container >::const_reference

Definition at line 377 of file Padded.h.

template<class Container>
typedef const_iterator::difference_type folly::padded::Adaptor< Container >::difference_type

Definition at line 380 of file Padded.h.

template<class Container>
typedef Iterator<typename Container::iterator> folly::padded::Adaptor< Container >::iterator

Definition at line 378 of file Padded.h.

template<class Container>
typedef Container::value_type folly::padded::Adaptor< Container >::Node

Definition at line 374 of file Padded.h.

template<class Container>
typedef value_type& folly::padded::Adaptor< Container >::reference

Definition at line 376 of file Padded.h.

template<class Container>
typedef Container::size_type folly::padded::Adaptor< Container >::size_type

Definition at line 381 of file Padded.h.

template<class Container>
typedef Node::value_type folly::padded::Adaptor< Container >::value_type

Definition at line 375 of file Padded.h.

Constructor & Destructor Documentation

template<class Container>
folly::padded::Adaptor< Container >::Adaptor ( )
inline

Definition at line 385 of file Padded.h.

385 : lastCount_(Node::kElementCount) {}
template<class Container>
folly::padded::Adaptor< Container >::Adaptor ( Container  c,
size_t  lastCount = Node::kElementCount 
)
inlineexplicit

Definition at line 386 of file Padded.h.

387  : c_(std::move(c)), lastCount_(lastCount) {}
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
char c
template<class Container>
folly::padded::Adaptor< Container >::Adaptor ( size_t  n,
const value_type value = value_type() 
)
inlineexplicit

Definition at line 388 of file Padded.h.

389  : c_(Node::nodeCount(n), fullNode(value)) {
390  const auto count = n % Node::kElementCount;
391  lastCount_ = count != 0 ? count : Node::kElementCount;
392  }
static Node fullNode(const value_type &value)
Definition: Padded.h:549
int * count
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
template<class Container>
folly::padded::Adaptor< Container >::Adaptor ( const Adaptor< Container > &  )
default
template<class Container>
folly::padded::Adaptor< Container >::Adaptor ( Adaptor< Container > &&  other)
inlinenoexcept

Definition at line 396 of file Padded.h.

397  : c_(std::move(other.c_)), lastCount_(other.lastCount_) {
398  other.lastCount_ = Node::kElementCount;
399  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567

Member Function Documentation

template<class Container>
value_type* folly::padded::Adaptor< Container >::allocate_back ( )
inlineprivate

Definition at line 541 of file Padded.h.

541  {
542  if (lastCount_ == Node::kElementCount) {
544  lastCount_ = 0;
545  }
546  return &c_.back().data()[lastCount_++];
547  }
decltype(auto) padded_emplace_back_or_push_back(Container &container, Args &&...args)
Definition: Padded.h:179
template<class Container>
const value_type& folly::padded::Adaptor< Container >::back ( ) const
inline

Definition at line 465 of file Padded.h.

465  {
466  assert(!empty());
467  return c_.back().data()[lastCount_ - 1];
468  }
bool empty() const
Definition: Padded.h:441
template<class Container>
value_type& folly::padded::Adaptor< Container >::back ( )
inline

Definition at line 469 of file Padded.h.

469  {
470  assert(!empty());
471  return c_.back().data()[lastCount_ - 1];
472  }
bool empty() const
Definition: Padded.h:441
template<class Container>
const_iterator folly::padded::Adaptor< Container >::begin ( ) const
inline

Definition at line 420 of file Padded.h.

420  {
421  return cbegin();
422  }
const_iterator cbegin() const
Definition: Padded.h:410
template<class Container>
iterator folly::padded::Adaptor< Container >::begin ( )
inline

Definition at line 426 of file Padded.h.

426  {
427  return iterator(c_.begin());
428  }
Iterator< typename Container::iterator > iterator
Definition: Padded.h:378
template<class Container>
size_type folly::padded::Adaptor< Container >::capacity ( ) const
inline

Definition at line 501 of file Padded.h.

501  {
502  return c_.capacity() * Node::kElementCount;
503  }
template<class Container>
const_iterator folly::padded::Adaptor< Container >::cbegin ( ) const
inline

Definition at line 410 of file Padded.h.

410  {
411  return const_iterator(c_.begin());
412  }
Iterator< typename Container::const_iterator > const_iterator
Definition: Padded.h:379
template<class Container>
const_iterator folly::padded::Adaptor< Container >::cend ( ) const
inline

Definition at line 413 of file Padded.h.

413  {
414  auto it = const_iterator(c_.end());
415  if (lastCount_ != Node::kElementCount) {
416  it -= (Node::kElementCount - lastCount_);
417  }
418  return it;
419  }
Iterator< typename Container::const_iterator > const_iterator
Definition: Padded.h:379
template<class Container>
void folly::padded::Adaptor< Container >::clear ( )
inline

Definition at line 491 of file Padded.h.

491  {
492  c_.clear();
493  lastCount_ = Node::kElementCount;
494  }
template<class Container>
template<typename... Args>
void folly::padded::Adaptor< Container >::emplace_back ( Args &&...  args)
inline

Definition at line 475 of file Padded.h.

475  {
476  new (allocate_back()) value_type(std::forward<Args>(args)...);
477  }
value_type * allocate_back()
Definition: Padded.h:541
Node::value_type value_type
Definition: Padded.h:375
template<class Container>
bool folly::padded::Adaptor< Container >::empty ( ) const
inline

Definition at line 441 of file Padded.h.

441  {
442  return c_.empty();
443  }
template<class Container>
const_iterator folly::padded::Adaptor< Container >::end ( ) const
inline

Definition at line 423 of file Padded.h.

423  {
424  return cend();
425  }
const_iterator cend() const
Definition: Padded.h:413
template<class Container>
iterator folly::padded::Adaptor< Container >::end ( )
inline

Definition at line 429 of file Padded.h.

429  {
430  auto it = iterator(c_.end());
431  if (lastCount_ != Node::kElementCount) {
432  it -= difference_type(Node::kElementCount - lastCount_);
433  }
434  return it;
435  }
Iterator< typename Container::iterator > iterator
Definition: Padded.h:378
const_iterator::difference_type difference_type
Definition: Padded.h:380
template<class Container>
const value_type& folly::padded::Adaptor< Container >::front ( ) const
inline

Definition at line 456 of file Padded.h.

456  {
457  assert(!empty());
458  return c_.front().data()[0];
459  }
bool empty() const
Definition: Padded.h:441
template<class Container>
value_type& folly::padded::Adaptor< Container >::front ( )
inline

Definition at line 460 of file Padded.h.

460  {
461  assert(!empty());
462  return c_.front().data()[0];
463  }
bool empty() const
Definition: Padded.h:441
template<class Container>
static Node folly::padded::Adaptor< Container >::fullNode ( const value_type value)
inlinestaticprivate

Definition at line 549 of file Padded.h.

549  {
550  Node n;
551  std::fill(n.data(), n.data() + kElementsPerNode, value);
552  return n;
553  }
static constexpr size_t kElementsPerNode
Definition: Padded.h:383
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
template<class Container>
size_type folly::padded::Adaptor< Container >::max_size ( ) const
inline

Definition at line 448 of file Padded.h.

448  {
449  return (
450  (c_.max_size() <=
451  std::numeric_limits<size_type>::max() / Node::kElementCount)
452  ? c_.max_size() * Node::kElementCount
454  }
LogLevel max
Definition: LogLevel.cpp:31
template<class Container>
std::pair<Container, size_t> folly::padded::Adaptor< Container >::move ( )
inline

Return the underlying container and number of elements in the last block, and clear *this. Useful when you want to process the data as Nodes (again) and want to avoid copies.

Definition at line 517 of file Padded.h.

517  {
518  std::pair<Container, size_t> p(std::move(c_), lastCount_);
519  lastCount_ = Node::kElementCount;
520  return std::move(p);
521  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
template<class Container>
Adaptor& folly::padded::Adaptor< Container >::operator= ( const Adaptor< Container > &  )
default
template<class Container>
Adaptor& folly::padded::Adaptor< Container >::operator= ( Adaptor< Container > &&  other)
inline

Definition at line 400 of file Padded.h.

400  {
401  if (this != &other) {
402  c_ = std::move(other.c_);
403  lastCount_ = other.lastCount_;
404  other.lastCount_ = Node::kElementCount;
405  }
406  return *this;
407  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
template<class Container>
const value_type& folly::padded::Adaptor< Container >::operator[] ( size_type  idx) const
inline

Definition at line 505 of file Padded.h.

505  {
506  return c_[idx / Node::kElementCount].data()[idx % Node::kElementCount];
507  }
template<class Container>
value_type& folly::padded::Adaptor< Container >::operator[] ( size_type  idx)
inline

Definition at line 508 of file Padded.h.

508  {
509  return c_[idx / Node::kElementCount].data()[idx % Node::kElementCount];
510  }
template<class Container>
void folly::padded::Adaptor< Container >::padToFullNode ( const value_type padValue)
inline

Definition at line 531 of file Padded.h.

531  {
532  // the if is necessary because c_ may be empty so we can't call c_.back()
533  if (lastCount_ != Node::kElementCount) {
534  auto last = c_.back().data();
535  std::fill(last + lastCount_, last + Node::kElementCount, padValue);
536  lastCount_ = Node::kElementCount;
537  }
538  }
template<class Container>
std::pair<const Container&, size_t> folly::padded::Adaptor< Container >::peek ( ) const
inline

Return a const reference to the underlying container and the current number of elements in the last block.

Definition at line 527 of file Padded.h.

527  {
528  return std::make_pair(std::cref(c_), lastCount_);
529  }
template<class Container>
void folly::padded::Adaptor< Container >::pop_back ( )
inline

Definition at line 483 of file Padded.h.

483  {
484  assert(!empty());
485  if (--lastCount_ == 0) {
486  c_.pop_back();
487  lastCount_ = Node::kElementCount;
488  }
489  }
bool empty() const
Definition: Padded.h:441
template<class Container>
void folly::padded::Adaptor< Container >::push_back ( value_type  x)
inline

Definition at line 479 of file Padded.h.

479  {
481  }
Definition: InvokeTest.cpp:58
void emplace_back(Args &&...args)
Definition: Padded.h:475
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
template<class Container>
void folly::padded::Adaptor< Container >::reserve ( size_type  n)
inline

Definition at line 496 of file Padded.h.

496  {
497  assert(n >= 0);
498  c_.reserve(Node::nodeCount(n));
499  }
template<class Container>
size_type folly::padded::Adaptor< Container >::size ( ) const
inline

Definition at line 444 of file Padded.h.

444  {
445  return (
446  c_.empty() ? 0 : (c_.size() - 1) * Node::kElementCount + lastCount_);
447  }
template<class Container>
void folly::padded::Adaptor< Container >::swap ( Adaptor< Container > &  other)
inline

Definition at line 436 of file Padded.h.

436  {
437  using std::swap;
438  swap(c_, other.c_);
439  swap(lastCount_, other.lastCount_);
440  }
void swap(Adaptor &other)
Definition: Padded.h:436
void swap(SwapTrackingAlloc< T > &, SwapTrackingAlloc< T > &)
Definition: F14TestUtil.h:414

Member Data Documentation

template<class Container>
Container folly::padded::Adaptor< Container >::c_
private

Definition at line 554 of file Padded.h.

Referenced by folly::padded::Adaptor< IntNodeVec >::swap().

template<class Container>
constexpr size_t folly::padded::Adaptor< Container >::kElementsPerNode = Node::kElementCount
static

Definition at line 383 of file Padded.h.

template<class Container>
size_t folly::padded::Adaptor< Container >::lastCount_
private

Definition at line 555 of file Padded.h.

Referenced by folly::padded::Adaptor< IntNodeVec >::swap().


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