Commit 6f7b20b8 authored by Martin Martin's avatar Martin Martin Committed by Facebook Github Bot

clang-format folly::small_vector. No functional change.

Summary: clang-format folly::small_vector.  No functional change.

Reviewed By: Orvid

Differential Revision: D5808357

fbshipit-source-id: d2ee831e25c60778c699214b875635c22854832d
parent 943afd98
......@@ -85,15 +85,13 @@ class small_vector;
namespace detail {
/*
/*
* Move a range to a range of uninitialized memory. Assumes the
* ranges don't overlap.
*/
template <class T>
typename std::enable_if<
!FOLLY_IS_TRIVIALLY_COPYABLE(T)
>::type
moveToUninitialized(T* first, T* last, T* out) {
template <class T>
typename std::enable_if<!FOLLY_IS_TRIVIALLY_COPYABLE(T)>::type
moveToUninitialized(T* first, T* last, T* out) {
std::size_t idx = 0;
try {
for (; first != last; ++first, ++idx) {
......@@ -110,25 +108,23 @@ namespace detail {
}
throw;
}
}
}
// Specialization for trivially copyable types.
template <class T>
typename std::enable_if<
FOLLY_IS_TRIVIALLY_COPYABLE(T)
>::type
moveToUninitialized(T* first, T* last, T* out) {
// Specialization for trivially copyable types.
template <class T>
typename std::enable_if<FOLLY_IS_TRIVIALLY_COPYABLE(T)>::type
moveToUninitialized(T* first, T* last, T* out) {
std::memmove(out, first, (last - first) * sizeof *first);
}
}
/*
/*
* Move a range to a range of uninitialized memory. Assumes the
* ranges don't overlap. Inserts an element at out + pos using emplaceFunc().
* out will contain (end - begin) + 1 elements on success and none on failure.
* If emplaceFunc() throws [begin, end) is unmodified.
*/
template <class T, class Size, class EmplaceFunc>
void moveToUninitializedEmplace(
template <class T, class Size, class EmplaceFunc>
void moveToUninitializedEmplace(
T* begin,
T* end,
T* out,
......@@ -155,19 +151,17 @@ namespace detail {
}
throw;
}
}
}
/*
/*
* Move objects in memory to the right into some uninitialized
* memory, where the region overlaps. This doesn't just use
* std::move_backward because move_backward only works if all the
* memory is initialized to type T already.
*/
template <class T>
typename std::enable_if<
!FOLLY_IS_TRIVIALLY_COPYABLE(T)
>::type
moveObjectsRight(T* first, T* lastConstructed, T* realLast) {
template <class T>
typename std::enable_if<!FOLLY_IS_TRIVIALLY_COPYABLE(T)>::type
moveObjectsRight(T* first, T* lastConstructed, T* realLast) {
if (lastConstructed == realLast) {
return;
}
......@@ -197,25 +191,23 @@ namespace detail {
}
throw;
}
}
}
// Specialization for trivially copyable types. The call to
// std::move_backward here will just turn into a memmove. (TODO:
// change to std::is_trivially_copyable when that works.)
template <class T>
typename std::enable_if<
FOLLY_IS_TRIVIALLY_COPYABLE(T)
>::type
moveObjectsRight(T* first, T* lastConstructed, T* realLast) {
// Specialization for trivially copyable types. The call to
// std::move_backward here will just turn into a memmove. (TODO:
// change to std::is_trivially_copyable when that works.)
template <class T>
typename std::enable_if<FOLLY_IS_TRIVIALLY_COPYABLE(T)>::type
moveObjectsRight(T* first, T* lastConstructed, T* realLast) {
std::move_backward(first, lastConstructed, realLast);
}
}
/*
/*
* Populate a region of memory using `op' to construct elements. If
* anything throws, undo what we did.
*/
template <class T, class Function>
void populateMemForward(T* mem, std::size_t n, Function const& op) {
template <class T, class Function>
void populateMemForward(T* mem, std::size_t n, Function const& op) {
std::size_t idx = 0;
try {
for (size_t i = 0; i < n; ++i) {
......@@ -228,10 +220,10 @@ namespace detail {
}
throw;
}
}
}
template <class SizeType, bool ShouldUseHeap>
struct IntegralSizePolicy {
template <class SizeType, bool ShouldUseHeap>
struct IntegralSizePolicy {
typedef SizeType InternalSizeType;
IntegralSizePolicy() : size_(0) {}
......@@ -271,13 +263,12 @@ namespace detail {
private:
static SizeType const kExternMask =
kShouldUseHeap ? SizeType(1) << (sizeof(SizeType) * 8 - 1)
: 0;
kShouldUseHeap ? SizeType(1) << (sizeof(SizeType) * 8 - 1) : 0;
SizeType size_;
};
};
/*
/*
* If you're just trying to use this class, ignore everything about
* this next small_vector_base class thing.
*
......@@ -288,51 +279,49 @@ namespace detail {
*
* Apologies for all the black magic.
*/
namespace mpl = boost::mpl;
template <
namespace mpl = boost::mpl;
template <
class Value,
std::size_t RequestedMaxInline,
class InPolicyA,
class InPolicyB,
class InPolicyC>
struct small_vector_base {
typedef mpl::vector<InPolicyA,InPolicyB,InPolicyC> PolicyList;
struct small_vector_base {
typedef mpl::vector<InPolicyA, InPolicyB, InPolicyC> PolicyList;
/*
* Determine the size type
*/
typedef typename mpl::filter_view<
PolicyList,
boost::is_integral<mpl::placeholders::_1>
>::type Integrals;
boost::is_integral<mpl::placeholders::_1>>::type Integrals;
typedef typename mpl::eval_if<
mpl::empty<Integrals>,
mpl::identity<std::size_t>,
mpl::front<Integrals>
>::type SizeType;
mpl::front<Integrals>>::type SizeType;
static_assert(std::is_unsigned<SizeType>::value,
static_assert(
std::is_unsigned<SizeType>::value,
"Size type should be an unsigned integral type");
static_assert(mpl::size<Integrals>::value == 0 ||
mpl::size<Integrals>::value == 1,
static_assert(
mpl::size<Integrals>::value == 0 || mpl::size<Integrals>::value == 1,
"Multiple size types specified in small_vector<>");
/*
* Determine whether we should allow spilling to the heap or not.
*/
typedef typename mpl::count<
PolicyList,small_vector_policy::NoHeap
>::type HasNoHeap;
typedef typename mpl::count<PolicyList, small_vector_policy::NoHeap>::type
HasNoHeap;
static_assert(HasNoHeap::value == 0 || HasNoHeap::value == 1,
static_assert(
HasNoHeap::value == 0 || HasNoHeap::value == 1,
"Multiple copies of small_vector_policy::NoHeap "
"supplied; this is probably a mistake");
/*
* Make the real policy base classes.
*/
typedef IntegralSizePolicy<SizeType,!HasNoHeap::value>
ActualSizePolicy;
typedef IntegralSizePolicy<SizeType, !HasNoHeap::value> ActualSizePolicy;
/*
* Now inherit from them all. This is done in such a convoluted
......@@ -340,27 +329,26 @@ namespace detail {
* types to keep sizeof(small_vector<>) minimal.
*/
typedef boost::totally_ordered1<
small_vector<Value,RequestedMaxInline,InPolicyA,InPolicyB,InPolicyC>,
ActualSizePolicy
> type;
};
small_vector<Value, RequestedMaxInline, InPolicyA, InPolicyB, InPolicyC>,
ActualSizePolicy>
type;
};
template <class T>
T* pointerFlagSet(T* p) {
template <class T>
T* pointerFlagSet(T* p) {
return reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(p) | 1);
}
template <class T>
bool pointerFlagGet(T* p) {
}
template <class T>
bool pointerFlagGet(T* p) {
return reinterpret_cast<uintptr_t>(p) & 1;
}
template <class T>
T* pointerFlagClear(T* p) {
return reinterpret_cast<T*>(
reinterpret_cast<uintptr_t>(p) & ~uintptr_t(1));
}
inline void* shiftPointer(void* p, size_t sizeBytes) {
}
template <class T>
T* pointerFlagClear(T* p) {
return reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(p) & ~uintptr_t(1));
}
inline void* shiftPointer(void* p, size_t sizeBytes) {
return static_cast<char*>(p) + sizeBytes;
}
}
}
//////////////////////////////////////////////////////////////////////
......@@ -371,14 +359,15 @@ template <
class PolicyA = void,
class PolicyB = void,
class PolicyC = void>
class small_vector
: public detail::small_vector_base<
Value,RequestedMaxInline,PolicyA,PolicyB,PolicyC
>::type
{
typedef typename detail::small_vector_base<
Value,RequestedMaxInline,PolicyA,PolicyB,PolicyC
>::type BaseType;
class small_vector : public detail::small_vector_base<
Value,
RequestedMaxInline,
PolicyA,
PolicyB,
PolicyC>::type {
typedef typename detail::
small_vector_base<Value, RequestedMaxInline, PolicyA, PolicyB, PolicyC>::
type BaseType;
typedef typename BaseType::InternalSizeType InternalSizeType;
/*
......@@ -417,12 +406,13 @@ class small_vector
this->setSize(n);
}
small_vector(small_vector&& o)
noexcept(std::is_nothrow_move_constructible<Value>::value) {
small_vector(small_vector&& o) noexcept(
std::is_nothrow_move_constructible<Value>::value) {
if (o.isExtern()) {
swap(o);
} else {
std::uninitialized_copy(std::make_move_iterator(o.begin()),
std::uninitialized_copy(
std::make_move_iterator(o.begin()),
std::make_move_iterator(o.end()),
begin());
this->setSize(o.size());
......@@ -466,7 +456,9 @@ class small_vector
small_vector& operator=(small_vector&& o) {
// TODO: optimization:
// if both are internal, use move assignment where possible
if (this == &o) return *this;
if (this == &o) {
return *this;
}
clear();
swap(o);
return *this;
......@@ -485,18 +477,38 @@ class small_vector
: BaseType::policyMaxSize();
}
size_type size() const { return this->doSize(); }
bool empty() const { return !size(); }
size_type size() const {
return this->doSize();
}
bool empty() const {
return !size();
}
iterator begin() { return data(); }
iterator end() { return data() + size(); }
const_iterator begin() const { return data(); }
const_iterator end() const { return data() + size(); }
const_iterator cbegin() const { return begin(); }
const_iterator cend() const { return end(); }
iterator begin() {
return data();
}
iterator end() {
return data() + size();
}
const_iterator begin() const {
return data();
}
const_iterator end() const {
return data() + size();
}
const_iterator cbegin() const {
return begin();
}
const_iterator cend() const {
return end();
}
reverse_iterator rbegin() { return reverse_iterator(end()); }
reverse_iterator rend() { return reverse_iterator(begin()); }
reverse_iterator rbegin() {
return reverse_iterator(end());
}
reverse_iterator rend() {
return reverse_iterator(begin());
}
const_reverse_iterator rbegin() const {
return const_reverse_iterator(end());
......@@ -506,8 +518,12 @@ class small_vector
return const_reverse_iterator(begin());
}
const_reverse_iterator crbegin() const { return rbegin(); }
const_reverse_iterator crend() const { return rend(); }
const_reverse_iterator crbegin() const {
return rbegin();
}
const_reverse_iterator crend() const {
return rend();
}
/*
* Usually one of the simplest functions in a Container-like class
......@@ -602,9 +618,8 @@ class small_vector
return;
}
makeSize(sz);
detail::populateMemForward(begin() + size(), sz - size(),
[&] (void* p) { new (p) value_type(); }
);
detail::populateMemForward(
begin() + size(), sz - size(), [&](void* p) { new (p) value_type(); });
this->setSize(sz);
}
......@@ -614,9 +629,8 @@ class small_vector
return;
}
makeSize(sz);
detail::populateMemForward(begin() + size(), sz - size(),
[&] (void* p) { new (p) value_type(v); }
);
detail::populateMemForward(
begin() + size(), sz - size(), [&](void* p) { new (p) value_type(v); });
this->setSize(sz);
}
......@@ -722,14 +736,12 @@ class small_vector
offset);
this->setSize(this->size() + 1);
} else {
detail::moveObjectsRight(data() + offset,
data() + size(),
data() + size() + 1);
detail::moveObjectsRight(
data() + offset, data() + size(), data() + size() + 1);
this->setSize(size() + 1);
data()[offset] = std::move(t);
}
return begin() + offset;
}
iterator insert(const_iterator p, value_type const& t) {
......@@ -741,9 +753,8 @@ class small_vector
iterator insert(const_iterator pos, size_type n, value_type const& val) {
auto offset = pos - begin();
makeSize(size() + n);
detail::moveObjectsRight(data() + offset,
data() + size(),
data() + size() + n);
detail::moveObjectsRight(
data() + offset, data() + size(), data() + size() + n);
this->setSize(size() + n);
std::generate_n(begin() + offset, n, [&] { return val; });
return begin() + offset;
......@@ -769,7 +780,9 @@ class small_vector
}
iterator erase(const_iterator q1, const_iterator q2) {
if (q1 == q2) return unconst(q1);
if (q1 == q2) {
return unconst(q1);
}
std::move(unconst(q2), end(), unconst(q1));
for (auto it = (end() - std::distance(q1, q2)); it != end(); ++it) {
it->~value_type();
......@@ -797,10 +810,22 @@ class small_vector
insert(end(), n, t);
}
reference front() { assert(!empty()); return *begin(); }
reference back() { assert(!empty()); return *(end() - 1); }
const_reference front() const { assert(!empty()); return *begin(); }
const_reference back() const { assert(!empty()); return *(end() - 1); }
reference front() {
assert(!empty());
return *begin();
}
reference back() {
assert(!empty());
return *(end() - 1);
}
const_reference front() const {
assert(!empty());
return *begin();
}
const_reference back() const {
assert(!empty());
return *(end() - 1);
}
reference operator[](size_type i) {
assert(i < size());
......@@ -836,7 +861,7 @@ class small_vector
template <class It>
iterator insertImpl(iterator pos, It first, It last, std::false_type) {
typedef typename std::iterator_traits<It>::iterator_category categ;
if (std::is_same<categ,std::input_iterator_tag>::value) {
if (std::is_same<categ, std::input_iterator_tag>::value) {
auto offset = pos - begin();
while (first != last) {
pos = insert(pos, *first++);
......@@ -848,16 +873,15 @@ class small_vector
auto distance = std::distance(first, last);
auto offset = pos - begin();
makeSize(size() + distance);
detail::moveObjectsRight(data() + offset,
data() + size(),
data() + size() + distance);
detail::moveObjectsRight(
data() + offset, data() + size(), data() + size() + distance);
this->setSize(size() + distance);
std::copy_n(first, distance, begin() + offset);
return begin() + offset;
}
iterator insertImpl(iterator pos, size_type n, const value_type& val,
std::true_type) {
iterator
insertImpl(iterator pos, size_type n, const value_type& val, std::true_type) {
// The true_type means this should call the size_t,value_type
// overload. (See insert().)
return insert(pos, n, val);
......@@ -869,7 +893,7 @@ class small_vector
template <class It>
void constructImpl(It first, It last, std::false_type) {
typedef typename std::iterator_traits<It>::iterator_category categ;
if (std::is_same<categ,std::input_iterator_tag>::value) {
if (std::is_same<categ, std::input_iterator_tag>::value) {
// With iterators that only allow a single pass, we can't really
// do anything sane here.
while (first != last) {
......@@ -882,9 +906,8 @@ class small_vector
makeSize(distance);
this->setSize(distance);
try {
detail::populateMemForward(data(), distance,
[&] (void* p) { new (p) value_type(*first++); }
);
detail::populateMemForward(
data(), distance, [&](void* p) { new (p) value_type(*first++); });
} catch (...) {
if (this->isExtern()) {
u.freeHeap();
......@@ -972,9 +995,8 @@ class small_vector
assert(!detail::pointerFlagGet(newh));
value_type* newp = static_cast<value_type*>(
heapifyCapacity ?
detail::shiftPointer(newh, kHeapifyCapacitySize) :
newh);
heapifyCapacity ? detail::shiftPointer(newh, kHeapifyCapacitySize)
: newh);
try {
if (insert) {
......@@ -1051,37 +1073,32 @@ class small_vector
#else
typedef typename std::aligned_storage<
sizeof(value_type) * MaxInline,
alignof(value_type)
>::type InlineStorageDataType;
alignof(value_type)>::type InlineStorageDataType;
#endif
typedef typename std::conditional<
sizeof(value_type) * MaxInline != 0,
InlineStorageDataType,
void*
>::type InlineStorageType;
void*>::type InlineStorageType;
static bool const kHasInlineCapacity =
sizeof(HeapPtrWithCapacity) < sizeof(InlineStorageType);
// This value should we multiple of word size.
static size_t const kHeapifyCapacitySize = sizeof(
typename std::aligned_storage<
sizeof(InternalSizeType),
alignof(value_type)
>::type);
typename std::
aligned_storage<sizeof(InternalSizeType), alignof(value_type)>::type);
// Threshold to control capacity heapifying.
static size_t const kHeapifyCapacityThreshold =
100 * kHeapifyCapacitySize;
static size_t const kHeapifyCapacityThreshold = 100 * kHeapifyCapacitySize;
typedef typename std::conditional<
kHasInlineCapacity,
HeapPtrWithCapacity,
HeapPtr
>::type PointerType;
typedef typename std::
conditional<kHasInlineCapacity, HeapPtrWithCapacity, HeapPtr>::type
PointerType;
union Data {
explicit Data() { pdata_.heap_ = 0; }
explicit Data() {
pdata_.heap_ = 0;
}
PointerType pdata_;
InlineStorageType storage_;
......@@ -1128,8 +1145,9 @@ FOLLY_PACK_POP
// Basic guarantee only, or provides the nothrow guarantee iff T has a
// nothrow move or copy constructor.
template <class T, std::size_t MaxInline, class A, class B, class C>
void swap(small_vector<T,MaxInline,A,B,C>& a,
small_vector<T,MaxInline,A,B,C>& b) {
void swap(
small_vector<T, MaxInline, A, B, C>& a,
small_vector<T, MaxInline, A, B, C>& b) {
a.swap(b);
}
......@@ -1140,8 +1158,7 @@ namespace detail {
// Format support.
template <class T, size_t M, class A, class B, class C>
struct IndexableTraits<small_vector<T, M, A, B, C>>
: public IndexableTraitsSeq<small_vector<T, M, A, B, C>> {
};
: public IndexableTraitsSeq<small_vector<T, M, A, B, C>> {};
} // namespace detail
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment