Export of internal Abseil changes

--
38bc0644e17bf9fe4d78d3db92cd06f585b99ba7 by Andy Soffer <asoffer@google.com>:

Change benchmark to be cc_binary instead of cc_test, and fix a bug in the zipf_distribution benchmark in which arguments were passed in the wrong order.

PiperOrigin-RevId: 262227159

--
3b5411d8f285a758a1713f7ef0dbfa3518f2b38b by CJ Johnson <johnsoncj@google.com>:

Updates Simple<*>() overload to match the name schema of the others

PiperOrigin-RevId: 262211217

--
0cb6812cb8b6e3bf0386b9354189ffcf46c4c094 by Andy Soffer <asoffer@google.com>:

Removing period in trailing namespace comments.

PiperOrigin-RevId: 262210952

--
c903feae3a881be81adf37e9fccd558ee3ed1e64 by CJ Johnson <johnsoncj@google.com>:

This is a cleanup on the public header of InlinedVector to be more presentable

PiperOrigin-RevId: 262207691

--
9a94384dc79cdcf38f6153894f337ebb744e2d76 by Tom Manshreck <shreck@google.com>:

Fix incorrect doc on operator()[] for flat_hash_set

PiperOrigin-RevId: 262206962

--
17e88ee10b727af82c04f8150b6d246eaac836cb by Derek Mauro <dmauro@google.com>:

Fix gcc-5 build error

PiperOrigin-RevId: 262198236
GitOrigin-RevId: 38bc0644e17bf9fe4d78d3db92cd06f585b99ba7
Change-Id: I77cababa47ba3ee8b6cebb2c2cfc9f60a331f6b7
This commit is contained in:
Abseil Team 2019-08-07 15:25:26 -07:00 committed by CJ Johnson
parent b49b8d16b6
commit 8efba58a3b
9 changed files with 248 additions and 259 deletions

View file

@ -55,9 +55,9 @@ struct FlatHashSetPolicy;
// following notable differences: // following notable differences:
// //
// * Requires keys that are CopyConstructible // * Requires keys that are CopyConstructible
// * Supports heterogeneous lookup, through `find()`, `operator[]()` and // * Supports heterogeneous lookup, through `find()` and `insert()`, provided
// `insert()`, provided that the set is provided a compatible heterogeneous // that the set is provided a compatible heterogeneous hashing function and
// hashing function and equality operator. // equality operator.
// * Invalidates any references and pointers to elements within the table after // * Invalidates any references and pointers to elements within the table after
// `rehash()`. // `rehash()`.
// * Contains a `capacity()` member function indicating the number of element // * Contains a `capacity()` member function indicating the number of element

View file

@ -66,8 +66,7 @@ namespace absl {
// designed to cover the same API footprint as covered by `std::vector`. // designed to cover the same API footprint as covered by `std::vector`.
template <typename T, size_t N, typename A = std::allocator<T>> template <typename T, size_t N, typename A = std::allocator<T>>
class InlinedVector { class InlinedVector {
static_assert( static_assert(N > 0, "`absl::InlinedVector` requires an inlined capacity.");
N > 0, "InlinedVector cannot be instantiated with `0` inlined elements.");
using Storage = inlined_vector_internal::Storage<T, N, A>; using Storage = inlined_vector_internal::Storage<T, N, A>;
using rvalue_reference = typename Storage::rvalue_reference; using rvalue_reference = typename Storage::rvalue_reference;
@ -84,7 +83,6 @@ class InlinedVector {
template <typename Iterator> template <typename Iterator>
using EnableIfAtLeastForwardIterator = absl::enable_if_t< using EnableIfAtLeastForwardIterator = absl::enable_if_t<
inlined_vector_internal::IsAtLeastForwardIterator<Iterator>::value>; inlined_vector_internal::IsAtLeastForwardIterator<Iterator>::value>;
template <typename Iterator> template <typename Iterator>
using DisableIfAtLeastForwardIterator = absl::enable_if_t< using DisableIfAtLeastForwardIterator = absl::enable_if_t<
!inlined_vector_internal::IsAtLeastForwardIterator<Iterator>::value>; !inlined_vector_internal::IsAtLeastForwardIterator<Iterator>::value>;
@ -110,7 +108,7 @@ class InlinedVector {
// Creates an empty inlined vector with a value-initialized allocator. // Creates an empty inlined vector with a value-initialized allocator.
InlinedVector() noexcept(noexcept(allocator_type())) : storage_() {} InlinedVector() noexcept(noexcept(allocator_type())) : storage_() {}
// Creates an empty inlined vector with a specified allocator. // Creates an empty inlined vector with a copy of `alloc`.
explicit InlinedVector(const allocator_type& alloc) noexcept explicit InlinedVector(const allocator_type& alloc) noexcept
: storage_(alloc) {} : storage_(alloc) {}
@ -128,7 +126,7 @@ class InlinedVector {
storage_.Initialize(CopyValueAdapter(v), n); storage_.Initialize(CopyValueAdapter(v), n);
} }
// Creates an inlined vector of copies of the values in `list`. // Creates an inlined vector with copies of the elements of `list`.
InlinedVector(std::initializer_list<value_type> list, InlinedVector(std::initializer_list<value_type> list,
const allocator_type& alloc = allocator_type()) const allocator_type& alloc = allocator_type())
: InlinedVector(list.begin(), list.end(), alloc) {} : InlinedVector(list.begin(), list.end(), alloc) {}
@ -136,7 +134,7 @@ class InlinedVector {
// Creates an inlined vector with elements constructed from the provided // Creates an inlined vector with elements constructed from the provided
// forward iterator range [`first`, `last`). // forward iterator range [`first`, `last`).
// //
// NOTE: The `enable_if` prevents ambiguous interpretation between a call to // NOTE: the `enable_if` prevents ambiguous interpretation between a call to
// this constructor with two integral arguments and a call to the above // this constructor with two integral arguments and a call to the above
// `InlinedVector(size_type, const_reference)` constructor. // `InlinedVector(size_type, const_reference)` constructor.
template <typename ForwardIterator, template <typename ForwardIterator,
@ -158,11 +156,12 @@ class InlinedVector {
std::copy(first, last, std::back_inserter(*this)); std::copy(first, last, std::back_inserter(*this));
} }
// Creates a copy of an `other` inlined vector using `other`'s allocator. // Creates an inlined vector by copying the contents of `other` using
// `other`'s allocator.
InlinedVector(const InlinedVector& other) InlinedVector(const InlinedVector& other)
: InlinedVector(other, *other.storage_.GetAllocPtr()) {} : InlinedVector(other, *other.storage_.GetAllocPtr()) {}
// Creates a copy of an `other` inlined vector using a specified allocator. // Creates an inlined vector by copying the contents of `other` using `alloc`.
InlinedVector(const InlinedVector& other, const allocator_type& alloc) InlinedVector(const InlinedVector& other, const allocator_type& alloc)
: storage_(alloc) { : storage_(alloc) {
if (IsMemcpyOk::value && !other.storage_.GetIsAllocated()) { if (IsMemcpyOk::value && !other.storage_.GetIsAllocated()) {
@ -173,67 +172,66 @@ class InlinedVector {
} }
} }
// Creates an inlined vector by moving in the contents of an `other` inlined // Creates an inlined vector by moving in the contents of `other` without
// vector without performing any allocations. If `other` contains allocated // allocating. If `other` contains allocated memory, the newly-created inlined
// memory, the newly-created instance will take ownership of that memory // vector will take ownership of that memory. However, if `other` does not
// (leaving `other` empty). However, if `other` does not contain allocated // contain allocated memory, the newly-created inlined vector will perform
// memory (i.e. is inlined), the new inlined vector will perform element-wise // element-wise move construction of the contents of `other`.
// move construction of `other`'s elements.
// //
// NOTE: since no allocation is performed for the inlined vector in either // NOTE: since no allocation is performed for the inlined vector in either
// case, the `noexcept(...)` specification depends on whether moving the // case, the `noexcept(...)` specification depends on whether moving the
// underlying objects can throw. We assume: // underlying objects can throw. It is assumed assumed that...
// a) Move constructors should only throw due to allocation failure. // a) move constructors should only throw due to allocation failure.
// b) If `value_type`'s move constructor allocates, it uses the same // b) if `value_type`'s move constructor allocates, it uses the same
// allocation function as the `InlinedVector`'s allocator. Thus, the move // allocation function as the inlined vector's allocator.
// constructor is non-throwing if the allocator is non-throwing or // Thus, the move constructor is non-throwing if the allocator is non-throwing
// `value_type`'s move constructor is specified as `noexcept`. // or `value_type`'s move constructor is specified as `noexcept`.
InlinedVector(InlinedVector&& other) noexcept( InlinedVector(InlinedVector&& other) noexcept(
absl::allocator_is_nothrow<allocator_type>::value || absl::allocator_is_nothrow<allocator_type>::value ||
std::is_nothrow_move_constructible<value_type>::value) std::is_nothrow_move_constructible<value_type>::value)
: storage_(*other.storage_.GetAllocPtr()) { : storage_(*other.storage_.GetAllocPtr()) {
if (IsMemcpyOk::value) { if (IsMemcpyOk::value) {
storage_.MemcpyFrom(other.storage_); storage_.MemcpyFrom(other.storage_);
other.storage_.SetInlinedSize(0); other.storage_.SetInlinedSize(0);
} else if (other.storage_.GetIsAllocated()) { } else if (other.storage_.GetIsAllocated()) {
storage_.SetAllocatedData(other.storage_.GetAllocatedData(), storage_.SetAllocatedData(other.storage_.GetAllocatedData(),
other.storage_.GetAllocatedCapacity()); other.storage_.GetAllocatedCapacity());
storage_.SetAllocatedSize(other.storage_.GetSize()); storage_.SetAllocatedSize(other.storage_.GetSize());
other.storage_.SetInlinedSize(0); other.storage_.SetInlinedSize(0);
} else { } else {
IteratorValueAdapter<MoveIterator> other_values( IteratorValueAdapter<MoveIterator> other_values(
MoveIterator(other.storage_.GetInlinedData())); MoveIterator(other.storage_.GetInlinedData()));
inlined_vector_internal::ConstructElements( inlined_vector_internal::ConstructElements(
storage_.GetAllocPtr(), storage_.GetInlinedData(), &other_values, storage_.GetAllocPtr(), storage_.GetInlinedData(), &other_values,
other.storage_.GetSize()); other.storage_.GetSize());
storage_.SetInlinedSize(other.storage_.GetSize()); storage_.SetInlinedSize(other.storage_.GetSize());
} }
} }
// Creates an inlined vector by moving in the contents of an `other` inlined // Creates an inlined vector by moving in the contents of `other` with a copy
// vector, performing allocations with the specified `alloc` allocator. If // of `alloc`.
// `other`'s allocator is not equal to `alloc` and `other` contains allocated
// memory, this move constructor will create a new allocation.
// //
// NOTE: since allocation is performed in this case, this constructor can // NOTE: if `other`'s allocator is not equal to `alloc`, even if `other`
// only be `noexcept` if the specified allocator is also `noexcept`. If this // contains allocated memory, this move constructor will still allocate. Since
// is the case, or if `other` contains allocated memory, this constructor // allocation is performed, this constructor can only be `noexcept` if the
// performs element-wise move construction of its contents. // specified allocator is also `noexcept`.
//
// Only in the case where `other`'s allocator is equal to `alloc` and `other`
// contains allocated memory will the newly created inlined vector take
// ownership of `other`'s allocated memory.
InlinedVector(InlinedVector&& other, const allocator_type& alloc) noexcept( InlinedVector(InlinedVector&& other, const allocator_type& alloc) noexcept(
absl::allocator_is_nothrow<allocator_type>::value) absl::allocator_is_nothrow<allocator_type>::value)
: storage_(alloc) { : storage_(alloc) {
if (IsMemcpyOk::value) { if (IsMemcpyOk::value) {
storage_.MemcpyFrom(other.storage_); storage_.MemcpyFrom(other.storage_);
other.storage_.SetInlinedSize(0); other.storage_.SetInlinedSize(0);
} else if ((*storage_.GetAllocPtr() == *other.storage_.GetAllocPtr()) && } else if ((*storage_.GetAllocPtr() == *other.storage_.GetAllocPtr()) &&
other.storage_.GetIsAllocated()) { other.storage_.GetIsAllocated()) {
storage_.SetAllocatedData(other.storage_.GetAllocatedData(), storage_.SetAllocatedData(other.storage_.GetAllocatedData(),
other.storage_.GetAllocatedCapacity()); other.storage_.GetAllocatedCapacity());
storage_.SetAllocatedSize(other.storage_.GetSize()); storage_.SetAllocatedSize(other.storage_.GetSize());
other.storage_.SetInlinedSize(0); other.storage_.SetInlinedSize(0);
} else { } else {
storage_.Initialize( storage_.Initialize(
@ -250,7 +248,7 @@ class InlinedVector {
// `InlinedVector::empty()` // `InlinedVector::empty()`
// //
// Checks if the inlined vector has no elements. // Returns whether the inlined vector contains no elements.
bool empty() const noexcept { return !size(); } bool empty() const noexcept { return !size(); }
// `InlinedVector::size()` // `InlinedVector::size()`
@ -260,23 +258,23 @@ class InlinedVector {
// `InlinedVector::max_size()` // `InlinedVector::max_size()`
// //
// Returns the maximum number of elements the vector can hold. // Returns the maximum number of elements the inlined vector can hold.
size_type max_size() const noexcept { size_type max_size() const noexcept {
// One bit of the size storage is used to indicate whether the inlined // One bit of the size storage is used to indicate whether the inlined
// vector is allocated. As a result, the maximum size of the container that // vector contains allocated memory. As a result, the maximum size that the
// we can express is half of the max for `size_type`. // inlined vector can express is half of the max for `size_type`.
return (std::numeric_limits<size_type>::max)() / 2; return (std::numeric_limits<size_type>::max)() / 2;
} }
// `InlinedVector::capacity()` // `InlinedVector::capacity()`
// //
// Returns the number of elements that can be stored in the inlined vector // Returns the number of elements that could be stored in the inlined vector
// without requiring a reallocation of underlying memory. // without requiring a reallocation.
// //
// NOTE: For most inlined vectors, `capacity()` should equal the template // NOTE: for most inlined vectors, `capacity()` should be equal to the
// parameter `N`. For inlined vectors which exceed this capacity, they // template parameter `N`. For inlined vectors which exceed this capacity,
// will no longer be inlined and `capacity()` will equal its capacity on the // they will no longer be inlined and `capacity()` will equal the capactity of
// allocated heap. // the allocated memory.
size_type capacity() const noexcept { size_type capacity() const noexcept {
return storage_.GetIsAllocated() ? storage_.GetAllocatedCapacity() return storage_.GetIsAllocated() ? storage_.GetAllocatedCapacity()
: storage_.GetInlinedCapacity(); : storage_.GetInlinedCapacity();
@ -284,56 +282,68 @@ class InlinedVector {
// `InlinedVector::data()` // `InlinedVector::data()`
// //
// Returns a `pointer` to elements of the inlined vector. This pointer can be // Returns a `pointer` to the elements of the inlined vector. This pointer
// used to access and modify the contained elements. // can be used to access and modify the contained elements.
// Only results within the range [`0`, `size()`) are defined. //
// NOTE: only elements within [`data()`, `data() + size()`) are valid.
pointer data() noexcept { pointer data() noexcept {
return storage_.GetIsAllocated() ? storage_.GetAllocatedData() return storage_.GetIsAllocated() ? storage_.GetAllocatedData()
: storage_.GetInlinedData(); : storage_.GetInlinedData();
} }
// Overload of `InlinedVector::data()` to return a `const_pointer` to elements // Overload of `InlinedVector::data()` that returns a `const_pointer` to the
// of the inlined vector. This pointer can be used to access (but not modify) // elements of the inlined vector. This pointer can be used to access but not
// the contained elements. // modify the contained elements.
//
// NOTE: only elements within [`data()`, `data() + size()`) are valid.
const_pointer data() const noexcept { const_pointer data() const noexcept {
return storage_.GetIsAllocated() ? storage_.GetAllocatedData() return storage_.GetIsAllocated() ? storage_.GetAllocatedData()
: storage_.GetInlinedData(); : storage_.GetInlinedData();
} }
// `InlinedVector::operator[]()` // `InlinedVector::operator[](...)`
//
// Returns a `reference` to the `i`th element of the inlined vector using the
// array operator.
reference operator[](size_type i) {
assert(i < size());
return data()[i];
}
// Overload of `InlinedVector::operator[]()` to return a `const_reference` to
// the `i`th element of the inlined vector.
const_reference operator[](size_type i) const {
assert(i < size());
return data()[i];
}
// `InlinedVector::at()`
// //
// Returns a `reference` to the `i`th element of the inlined vector. // Returns a `reference` to the `i`th element of the inlined vector.
reference operator[](size_type i) {
assert(i < size());
return data()[i];
}
// Overload of `InlinedVector::operator[](...)` that returns a
// `const_reference` to the `i`th element of the inlined vector.
const_reference operator[](size_type i) const {
assert(i < size());
return data()[i];
}
// `InlinedVector::at(...)`
//
// Returns a `reference` to the `i`th element of the inlined vector.
//
// NOTE: if `i` is not within the required range of `InlinedVector::at(...)`,
// in both debug and non-debug builds, `std::out_of_range` will be thrown.
reference at(size_type i) { reference at(size_type i) {
if (ABSL_PREDICT_FALSE(i >= size())) { if (ABSL_PREDICT_FALSE(i >= size())) {
base_internal::ThrowStdOutOfRange( base_internal::ThrowStdOutOfRange(
"`InlinedVector::at(size_type)` failed bounds check"); "`InlinedVector::at(size_type)` failed bounds check");
} }
return data()[i]; return data()[i];
} }
// Overload of `InlinedVector::at()` to return a `const_reference` to the // Overload of `InlinedVector::at(...)` that returns a `const_reference` to
// `i`th element of the inlined vector. // the `i`th element of the inlined vector.
//
// NOTE: if `i` is not within the required range of `InlinedVector::at(...)`,
// in both debug and non-debug builds, `std::out_of_range` will be thrown.
const_reference at(size_type i) const { const_reference at(size_type i) const {
if (ABSL_PREDICT_FALSE(i >= size())) { if (ABSL_PREDICT_FALSE(i >= size())) {
base_internal::ThrowStdOutOfRange( base_internal::ThrowStdOutOfRange(
"`InlinedVector::at(size_type) const` failed bounds check"); "`InlinedVector::at(size_type) const` failed bounds check");
} }
return data()[i]; return data()[i];
} }
@ -342,13 +352,15 @@ class InlinedVector {
// Returns a `reference` to the first element of the inlined vector. // Returns a `reference` to the first element of the inlined vector.
reference front() { reference front() {
assert(!empty()); assert(!empty());
return at(0); return at(0);
} }
// Overload of `InlinedVector::front()` returns a `const_reference` to the // Overload of `InlinedVector::front()` that returns a `const_reference` to
// first element of the inlined vector. // the first element of the inlined vector.
const_reference front() const { const_reference front() const {
assert(!empty()); assert(!empty());
return at(0); return at(0);
} }
@ -357,13 +369,15 @@ class InlinedVector {
// Returns a `reference` to the last element of the inlined vector. // Returns a `reference` to the last element of the inlined vector.
reference back() { reference back() {
assert(!empty()); assert(!empty());
return at(size() - 1); return at(size() - 1);
} }
// Overload of `InlinedVector::back()` to return a `const_reference` to the // Overload of `InlinedVector::back()` that returns a `const_reference` to the
// last element of the inlined vector. // last element of the inlined vector.
const_reference back() const { const_reference back() const {
assert(!empty()); assert(!empty());
return at(size() - 1); return at(size() - 1);
} }
@ -372,7 +386,7 @@ class InlinedVector {
// Returns an `iterator` to the beginning of the inlined vector. // Returns an `iterator` to the beginning of the inlined vector.
iterator begin() noexcept { return data(); } iterator begin() noexcept { return data(); }
// Overload of `InlinedVector::begin()` to return a `const_iterator` to // Overload of `InlinedVector::begin()` that returns a `const_iterator` to
// the beginning of the inlined vector. // the beginning of the inlined vector.
const_iterator begin() const noexcept { return data(); } const_iterator begin() const noexcept { return data(); }
@ -381,7 +395,7 @@ class InlinedVector {
// Returns an `iterator` to the end of the inlined vector. // Returns an `iterator` to the end of the inlined vector.
iterator end() noexcept { return data() + size(); } iterator end() noexcept { return data() + size(); }
// Overload of `InlinedVector::end()` to return a `const_iterator` to the // Overload of `InlinedVector::end()` that returns a `const_iterator` to the
// end of the inlined vector. // end of the inlined vector.
const_iterator end() const noexcept { return data() + size(); } const_iterator end() const noexcept { return data() + size(); }
@ -400,7 +414,7 @@ class InlinedVector {
// Returns a `reverse_iterator` from the end of the inlined vector. // Returns a `reverse_iterator` from the end of the inlined vector.
reverse_iterator rbegin() noexcept { return reverse_iterator(end()); } reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
// Overload of `InlinedVector::rbegin()` to return a // Overload of `InlinedVector::rbegin()` that returns a
// `const_reverse_iterator` from the end of the inlined vector. // `const_reverse_iterator` from the end of the inlined vector.
const_reverse_iterator rbegin() const noexcept { const_reverse_iterator rbegin() const noexcept {
return const_reverse_iterator(end()); return const_reverse_iterator(end());
@ -411,7 +425,7 @@ class InlinedVector {
// Returns a `reverse_iterator` from the beginning of the inlined vector. // Returns a `reverse_iterator` from the beginning of the inlined vector.
reverse_iterator rend() noexcept { return reverse_iterator(begin()); } reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
// Overload of `InlinedVector::rend()` to return a `const_reverse_iterator` // Overload of `InlinedVector::rend()` that returns a `const_reverse_iterator`
// from the beginning of the inlined vector. // from the beginning of the inlined vector.
const_reverse_iterator rend() const noexcept { const_reverse_iterator rend() const noexcept {
return const_reverse_iterator(begin()); return const_reverse_iterator(begin());
@ -430,71 +444,75 @@ class InlinedVector {
// `InlinedVector::get_allocator()` // `InlinedVector::get_allocator()`
// //
// Returns a copy of the allocator of the inlined vector. // Returns a copy of the inlined vector's allocator.
allocator_type get_allocator() const { return *storage_.GetAllocPtr(); } allocator_type get_allocator() const { return *storage_.GetAllocPtr(); }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// InlinedVector Member Mutators // InlinedVector Member Mutators
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// `InlinedVector::operator=()` // `InlinedVector::operator=(...)`
// //
// Replaces the contents of the inlined vector with copies of the elements in // Replaces the elements of the inlined vector with copies of the elements of
// the provided `std::initializer_list`. // `list`.
InlinedVector& operator=(std::initializer_list<value_type> list) { InlinedVector& operator=(std::initializer_list<value_type> list) {
assign(list.begin(), list.end()); assign(list.begin(), list.end());
return *this; return *this;
} }
// Overload of `InlinedVector::operator=()` to replace the contents of the // Overload of `InlinedVector::operator=(...)` that replaces the elements of
// inlined vector with the contents of `other`. // the inlined vector with copies of the elements of `other`.
InlinedVector& operator=(const InlinedVector& other) { InlinedVector& operator=(const InlinedVector& other) {
if (ABSL_PREDICT_TRUE(this != std::addressof(other))) { if (ABSL_PREDICT_TRUE(this != std::addressof(other))) {
const_pointer other_data = other.data(); const_pointer other_data = other.data();
assign(other_data, other_data + other.size()); assign(other_data, other_data + other.size());
} }
return *this; return *this;
} }
// Overload of `InlinedVector::operator=()` to replace the contents of the // Overload of `InlinedVector::operator=(...)` that moves the elements of
// inlined vector with the contents of `other`. // `other` into the inlined vector.
// //
// NOTE: As a result of calling this overload, `other` may be empty or it's // NOTE: as a result of calling this overload, `other` is left in a valid but
// contents may be left in a moved-from state. // unspecified state.
InlinedVector& operator=(InlinedVector&& other) { InlinedVector& operator=(InlinedVector&& other) {
if (ABSL_PREDICT_FALSE(this == std::addressof(other))) return *this; if (ABSL_PREDICT_TRUE(this != std::addressof(other))) {
if (IsMemcpyOk::value || other.storage_.GetIsAllocated()) {
inlined_vector_internal::DestroyElements(storage_.GetAllocPtr(), data(),
size());
storage_.DeallocateIfAllocated();
storage_.MemcpyFrom(other.storage_);
if (IsMemcpyOk::value || other.storage_.GetIsAllocated()) { other.storage_.SetInlinedSize(0);
inlined_vector_internal::DestroyElements(storage_.GetAllocPtr(), data(), } else {
size()); storage_.Assign(IteratorValueAdapter<MoveIterator>(
storage_.DeallocateIfAllocated(); MoveIterator(other.storage_.GetInlinedData())),
storage_.MemcpyFrom(other.storage_); other.size());
other.storage_.SetInlinedSize(0); }
} else {
storage_.Assign(IteratorValueAdapter<MoveIterator>(
MoveIterator(other.storage_.GetInlinedData())),
other.size());
} }
return *this; return *this;
} }
// `InlinedVector::assign()` // `InlinedVector::assign(...)`
// //
// Replaces the contents of the inlined vector with `n` copies of `v`. // Replaces the contents of the inlined vector with `n` copies of `v`.
void assign(size_type n, const_reference v) { void assign(size_type n, const_reference v) {
storage_.Assign(CopyValueAdapter(v), n); storage_.Assign(CopyValueAdapter(v), n);
} }
// Overload of `InlinedVector::assign()` to replace the contents of the // Overload of `InlinedVector::assign(...)` that replaces the contents of the
// inlined vector with copies of the values in the provided // inlined vector with copies of the elements of `list`.
// `std::initializer_list`.
void assign(std::initializer_list<value_type> list) { void assign(std::initializer_list<value_type> list) {
assign(list.begin(), list.end()); assign(list.begin(), list.end());
} }
// Overload of `InlinedVector::assign()` to replace the contents of the // Overload of `InlinedVector::assign(...)` to replace the contents of the
// inlined vector with the forward iterator range [`first`, `last`). // inlined vector with the range [`first`, `last`).
//
// NOTE: this overload is for iterators that are "forward" category or better.
template <typename ForwardIterator, template <typename ForwardIterator,
EnableIfAtLeastForwardIterator<ForwardIterator>* = nullptr> EnableIfAtLeastForwardIterator<ForwardIterator>* = nullptr>
void assign(ForwardIterator first, ForwardIterator last) { void assign(ForwardIterator first, ForwardIterator last) {
@ -502,8 +520,10 @@ class InlinedVector {
std::distance(first, last)); std::distance(first, last));
} }
// Overload of `InlinedVector::assign()` to replace the contents of the // Overload of `InlinedVector::assign(...)` to replace the contents of the
// inlined vector with the input iterator range [`first`, `last`). // inlined vector with the range [`first`, `last`).
//
// NOTE: this overload is for iterators that are "input" category.
template <typename InputIterator, template <typename InputIterator,
DisableIfAtLeastForwardIterator<InputIterator>* = nullptr> DisableIfAtLeastForwardIterator<InputIterator>* = nullptr>
void assign(InputIterator first, InputIterator last) { void assign(InputIterator first, InputIterator last) {
@ -517,36 +537,39 @@ class InlinedVector {
std::copy(first, last, std::back_inserter(*this)); std::copy(first, last, std::back_inserter(*this));
} }
// `InlinedVector::resize()` // `InlinedVector::resize(...)`
// //
// Resizes the inlined vector to contain `n` elements. If `n` is smaller than // Resizes the inlined vector to contain `n` elements.
// the inlined vector's current size, extra elements are destroyed. If `n` is //
// larger than the initial size, new elements are value-initialized. // NOTE: if `n` is smaller than `size()`, extra elements are destroyed. If `n`
// is larger than `size()`, new elements are value-initialized.
void resize(size_type n) { storage_.Resize(DefaultValueAdapter(), n); } void resize(size_type n) { storage_.Resize(DefaultValueAdapter(), n); }
// Overload of `InlinedVector::resize()` to resize the inlined vector to // Overload of `InlinedVector::resize(...)` that resizes the inlined vector to
// contain `n` elements where, if `n` is larger than `size()`, the new values // contain `n` elements.
// will be copy-constructed from `v`. //
// NOTE: if `n` is smaller than `size()`, extra elements are destroyed. If `n`
// is larger than `size()`, new elements are copied-constructed from `v`.
void resize(size_type n, const_reference v) { void resize(size_type n, const_reference v) {
storage_.Resize(CopyValueAdapter(v), n); storage_.Resize(CopyValueAdapter(v), n);
} }
// `InlinedVector::insert()` // `InlinedVector::insert(...)`
// //
// Copies `v` into `pos`, returning an `iterator` pointing to the newly // Inserts a copy of `v` at `pos`, returning an `iterator` to the newly
// inserted element. // inserted element.
iterator insert(const_iterator pos, const_reference v) { iterator insert(const_iterator pos, const_reference v) {
return emplace(pos, v); return emplace(pos, v);
} }
// Overload of `InlinedVector::insert()` for moving `v` into `pos`, returning // Overload of `InlinedVector::insert(...)` that inserts `v` at `pos` using
// an iterator pointing to the newly inserted element. // move semantics, returning an `iterator` to the newly inserted element.
iterator insert(const_iterator pos, rvalue_reference v) { iterator insert(const_iterator pos, rvalue_reference v) {
return emplace(pos, std::move(v)); return emplace(pos, std::move(v));
} }
// Overload of `InlinedVector::insert()` for inserting `n` contiguous copies // Overload of `InlinedVector::insert(...)` that inserts `n` contiguous copies
// of `v` starting at `pos`. Returns an `iterator` pointing to the first of // of `v` starting at `pos`, returning an `iterator` pointing to the first of
// the newly inserted elements. // the newly inserted elements.
iterator insert(const_iterator pos, size_type n, const_reference v) { iterator insert(const_iterator pos, size_type n, const_reference v) {
assert(pos >= begin()); assert(pos >= begin());
@ -560,19 +583,18 @@ class InlinedVector {
} }
} }
// Overload of `InlinedVector::insert()` for copying the contents of the // Overload of `InlinedVector::insert(...)` that inserts copies of the
// `std::initializer_list` into the vector starting at `pos`. Returns an // elements of `list` starting at `pos`, returning an `iterator` pointing to
// `iterator` pointing to the first of the newly inserted elements. // the first of the newly inserted elements.
iterator insert(const_iterator pos, std::initializer_list<value_type> list) { iterator insert(const_iterator pos, std::initializer_list<value_type> list) {
return insert(pos, list.begin(), list.end()); return insert(pos, list.begin(), list.end());
} }
// Overload of `InlinedVector::insert()` for inserting elements constructed // Overload of `InlinedVector::insert(...)` that inserts the range [`first`,
// from the forward iterator range [`first`, `last`). Returns an `iterator` // `last`) starting at `pos`, returning an `iterator` pointing to the first
// pointing to the first of the newly inserted elements. // of the newly inserted elements.
// //
// NOTE: The `enable_if` is intended to disambiguate the two three-argument // NOTE: this overload is for iterators that are "forward" category or better.
// overloads of `insert()`.
template <typename ForwardIterator, template <typename ForwardIterator,
EnableIfAtLeastForwardIterator<ForwardIterator>* = nullptr> EnableIfAtLeastForwardIterator<ForwardIterator>* = nullptr>
iterator insert(const_iterator pos, ForwardIterator first, iterator insert(const_iterator pos, ForwardIterator first,
@ -588,9 +610,11 @@ class InlinedVector {
} }
} }
// Overload of `InlinedVector::insert()` for inserting elements constructed // Overload of `InlinedVector::insert(...)` that inserts the range [`first`,
// from the input iterator range [`first`, `last`). Returns an `iterator` // `last`) starting at `pos`, returning an `iterator` pointing to the first
// pointing to the first of the newly inserted elements. // of the newly inserted elements.
//
// NOTE: this overload is for iterators that are "input" category.
template <typename InputIterator, template <typename InputIterator,
DisableIfAtLeastForwardIterator<InputIterator>* = nullptr> DisableIfAtLeastForwardIterator<InputIterator>* = nullptr>
iterator insert(const_iterator pos, InputIterator first, InputIterator last) { iterator insert(const_iterator pos, InputIterator first, InputIterator last) {
@ -605,10 +629,10 @@ class InlinedVector {
return iterator(data() + index); return iterator(data() + index);
} }
// `InlinedVector::emplace()` // `InlinedVector::emplace(...)`
// //
// Constructs and inserts an object in the inlined vector at the given `pos`, // Constructs and inserts an element using `args...` in the inlined vector at
// returning an `iterator` pointing to the newly emplaced element. // `pos`, returning an `iterator` pointing to the newly emplaced element.
template <typename... Args> template <typename... Args>
iterator emplace(const_iterator pos, Args&&... args) { iterator emplace(const_iterator pos, Args&&... args) {
assert(pos >= begin()); assert(pos >= begin());
@ -621,30 +645,29 @@ class InlinedVector {
1); 1);
} }
// `InlinedVector::emplace_back()` // `InlinedVector::emplace_back(...)`
// //
// Constructs and appends a new element to the end of the inlined vector, // Constructs and inserts an element using `args...` in the inlined vector at
// returning a `reference` to the emplaced element. // `end()`, returning a `reference` to the newly emplaced element.
template <typename... Args> template <typename... Args>
reference emplace_back(Args&&... args) { reference emplace_back(Args&&... args) {
return storage_.EmplaceBack(std::forward<Args>(args)...); return storage_.EmplaceBack(std::forward<Args>(args)...);
} }
// `InlinedVector::push_back()` // `InlinedVector::push_back(...)`
// //
// Appends a copy of `v` to the end of the inlined vector. // Inserts a copy of `v` in the inlined vector at `end()`.
void push_back(const_reference v) { static_cast<void>(emplace_back(v)); } void push_back(const_reference v) { static_cast<void>(emplace_back(v)); }
// Overload of `InlinedVector::push_back()` for moving `v` into a newly // Overload of `InlinedVector::push_back(...)` for inserting `v` at `end()`
// appended element. // using move semantics.
void push_back(rvalue_reference v) { void push_back(rvalue_reference v) {
static_cast<void>(emplace_back(std::move(v))); static_cast<void>(emplace_back(std::move(v)));
} }
// `InlinedVector::pop_back()` // `InlinedVector::pop_back()`
// //
// Destroys the element at the end of the inlined vector and shrinks the size // Destroys the element at `back()`, reducing the size by `1`.
// by `1` (unless the inlined vector is empty, in which case this is a no-op).
void pop_back() noexcept { void pop_back() noexcept {
assert(!empty()); assert(!empty());
@ -652,12 +675,12 @@ class InlinedVector {
storage_.SubtractSize(1); storage_.SubtractSize(1);
} }
// `InlinedVector::erase()` // `InlinedVector::erase(...)`
// //
// Erases the element at `pos` of the inlined vector, returning an `iterator` // Erases the element at `pos`, returning an `iterator` pointing to where the
// pointing to the first element following the erased element. // erased element was located.
// //
// NOTE: May return the end iterator, which is not dereferencable. // NOTE: may return `end()`, which is not dereferencable.
iterator erase(const_iterator pos) { iterator erase(const_iterator pos) {
assert(pos >= begin()); assert(pos >= begin());
assert(pos < end()); assert(pos < end());
@ -665,10 +688,11 @@ class InlinedVector {
return storage_.Erase(pos, pos + 1); return storage_.Erase(pos, pos + 1);
} }
// Overload of `InlinedVector::erase()` for erasing all elements in the // Overload of `InlinedVector::erase(...)` that erases every element in the
// range [`from`, `to`) in the inlined vector. Returns an `iterator` pointing // range [`from`, `to`), returning an `iterator` pointing to where the first
// to the first element following the range erased or the end iterator if `to` // erased element was located.
// was the end iterator. //
// NOTE: may return `end()`, which is not dereferencable.
iterator erase(const_iterator from, const_iterator to) { iterator erase(const_iterator from, const_iterator to) {
assert(from >= begin()); assert(from >= begin());
assert(from <= to); assert(from <= to);
@ -683,8 +707,8 @@ class InlinedVector {
// `InlinedVector::clear()` // `InlinedVector::clear()`
// //
// Destroys all elements in the inlined vector, sets the size of `0` and // Destroys all elements in the inlined vector, setting the size to `0` and
// deallocates the heap allocation if the inlined vector was allocated. // deallocating any held memory.
void clear() noexcept { void clear() noexcept {
inlined_vector_internal::DestroyElements(storage_.GetAllocPtr(), data(), inlined_vector_internal::DestroyElements(storage_.GetAllocPtr(), data(),
size()); size());
@ -692,37 +716,31 @@ class InlinedVector {
storage_.SetInlinedSize(0); storage_.SetInlinedSize(0);
} }
// `InlinedVector::reserve()` // `InlinedVector::reserve(...)`
// //
// Enlarges the underlying representation of the inlined vector so it can hold // Ensures that there is enough room for at least `n` elements.
// at least `n` elements. This method does not change `size()` or the actual
// contents of the vector.
//
// NOTE: If `n` does not exceed `capacity()`, `reserve()` will have no
// effects. Otherwise, `reserve()` will reallocate, performing an n-time
// element-wise move of everything contained.
void reserve(size_type n) { storage_.Reserve(n); } void reserve(size_type n) { storage_.Reserve(n); }
// `InlinedVector::shrink_to_fit()` // `InlinedVector::shrink_to_fit()`
// //
// Reduces memory usage by freeing unused memory. After this call, calls to // Reduces memory usage by freeing unused memory. After being called, calls to
// `capacity()` will be equal to `max(N, size())`. // `capacity()` will be equal to `max(N, size())`.
// //
// If `size() <= N` and the elements are currently stored on the heap, they // If `size() <= N` and the inlined vector contains allocated memory, the
// will be moved to the inlined storage and the heap memory will be // elements will all be moved to the inlined space and the allocated memory
// deallocated. // will be deallocated.
// //
// If `size() > N` and `size() < capacity()` the elements will be moved to a // If `size() > N` and `size() < capacity()`, the elements will be moved to a
// smaller heap allocation. // smaller allocation.
void shrink_to_fit() { void shrink_to_fit() {
if (storage_.GetIsAllocated()) { if (storage_.GetIsAllocated()) {
storage_.ShrinkToFit(); storage_.ShrinkToFit();
} }
} }
// `InlinedVector::swap()` // `InlinedVector::swap(...)`
// //
// Swaps the contents of this inlined vector with the contents of `other`. // Swaps the contents of the inlined vector with `other`.
void swap(InlinedVector& other) { void swap(InlinedVector& other) {
if (ABSL_PREDICT_TRUE(this != std::addressof(other))) { if (ABSL_PREDICT_TRUE(this != std::addressof(other))) {
storage_.Swap(std::addressof(other.storage_)); storage_.Swap(std::addressof(other.storage_));
@ -740,93 +758,86 @@ class InlinedVector {
// InlinedVector Non-Member Functions // InlinedVector Non-Member Functions
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// `swap()` // `swap(...)`
// //
// Swaps the contents of two inlined vectors. This convenience function // Swaps the contents of two inlined vectors.
// simply calls `InlinedVector::swap()`.
template <typename T, size_t N, typename A> template <typename T, size_t N, typename A>
void swap(absl::InlinedVector<T, N, A>& a, void swap(absl::InlinedVector<T, N, A>& a,
absl::InlinedVector<T, N, A>& b) noexcept(noexcept(a.swap(b))) { absl::InlinedVector<T, N, A>& b) noexcept(noexcept(a.swap(b))) {
a.swap(b); a.swap(b);
} }
// `operator==()` // `operator==(...)`
// //
// Tests the equivalency of the contents of two inlined vectors. // Tests for value-equality of two inlined vectors.
template <typename T, size_t N, typename A> template <typename T, size_t N, typename A>
bool operator==(const absl::InlinedVector<T, N, A>& a, bool operator==(const absl::InlinedVector<T, N, A>& a,
const absl::InlinedVector<T, N, A>& b) { const absl::InlinedVector<T, N, A>& b) {
auto a_data = a.data(); auto a_data = a.data();
auto a_size = a.size();
auto b_data = b.data(); auto b_data = b.data();
auto b_size = b.size(); return absl::equal(a_data, a_data + a.size(), b_data, b_data + b.size());
return absl::equal(a_data, a_data + a_size, b_data, b_data + b_size);
} }
// `operator!=()` // `operator!=(...)`
// //
// Tests the inequality of the contents of two inlined vectors. // Tests for value-inequality of two inlined vectors.
template <typename T, size_t N, typename A> template <typename T, size_t N, typename A>
bool operator!=(const absl::InlinedVector<T, N, A>& a, bool operator!=(const absl::InlinedVector<T, N, A>& a,
const absl::InlinedVector<T, N, A>& b) { const absl::InlinedVector<T, N, A>& b) {
return !(a == b); return !(a == b);
} }
// `operator<()` // `operator<(...)`
// //
// Tests whether the contents of one inlined vector are less than the contents // Tests whether the value of an inlined vector is less than the value of
// of another through a lexicographical comparison operation. // another inlined vector using a lexicographical comparison algorithm.
template <typename T, size_t N, typename A> template <typename T, size_t N, typename A>
bool operator<(const absl::InlinedVector<T, N, A>& a, bool operator<(const absl::InlinedVector<T, N, A>& a,
const absl::InlinedVector<T, N, A>& b) { const absl::InlinedVector<T, N, A>& b) {
auto a_data = a.data(); auto a_data = a.data();
auto a_size = a.size();
auto b_data = b.data(); auto b_data = b.data();
auto b_size = b.size(); return std::lexicographical_compare(a_data, a_data + a.size(), b_data,
return std::lexicographical_compare(a_data, a_data + a_size, b_data, b_data + b.size());
b_data + b_size);
} }
// `operator>()` // `operator>(...)`
// //
// Tests whether the contents of one inlined vector are greater than the // Tests whether the value of an inlined vector is greater than the value of
// contents of another through a lexicographical comparison operation. // another inlined vector using a lexicographical comparison algorithm.
template <typename T, size_t N, typename A> template <typename T, size_t N, typename A>
bool operator>(const absl::InlinedVector<T, N, A>& a, bool operator>(const absl::InlinedVector<T, N, A>& a,
const absl::InlinedVector<T, N, A>& b) { const absl::InlinedVector<T, N, A>& b) {
return b < a; return b < a;
} }
// `operator<=()` // `operator<=(...)`
// //
// Tests whether the contents of one inlined vector are less than or equal to // Tests whether the value of an inlined vector is less than or equal to the
// the contents of another through a lexicographical comparison operation. // value of another inlined vector using a lexicographical comparison algorithm.
template <typename T, size_t N, typename A> template <typename T, size_t N, typename A>
bool operator<=(const absl::InlinedVector<T, N, A>& a, bool operator<=(const absl::InlinedVector<T, N, A>& a,
const absl::InlinedVector<T, N, A>& b) { const absl::InlinedVector<T, N, A>& b) {
return !(b < a); return !(b < a);
} }
// `operator>=()` // `operator>=(...)`
// //
// Tests whether the contents of one inlined vector are greater than or equal to // Tests whether the value of an inlined vector is greater than or equal to the
// the contents of another through a lexicographical comparison operation. // value of another inlined vector using a lexicographical comparison algorithm.
template <typename T, size_t N, typename A> template <typename T, size_t N, typename A>
bool operator>=(const absl::InlinedVector<T, N, A>& a, bool operator>=(const absl::InlinedVector<T, N, A>& a,
const absl::InlinedVector<T, N, A>& b) { const absl::InlinedVector<T, N, A>& b) {
return !(a < b); return !(a < b);
} }
// `AbslHashValue()` // `AbslHashValue(...)`
// //
// Provides `absl::Hash` support for `absl::InlinedVector`. You do not normally // Provides `absl::Hash` support for `absl::InlinedVector`. It is uncommon to
// call this function directly. // call this directly.
template <typename H, typename TheT, size_t TheN, typename TheA> template <typename H, typename T, size_t N, typename A>
H AbslHashValue(H h, const absl::InlinedVector<TheT, TheN, TheA>& a) { H AbslHashValue(H h, const absl::InlinedVector<T, N, A>& a) {
auto a_data = a.data(); auto size = a.size();
auto a_size = a.size(); return H::combine(H::combine_contiguous(std::move(h), a.data(), size), size);
return H::combine(H::combine_contiguous(std::move(h), a_data, a_size),
a_size);
} }
} // namespace absl } // namespace absl

View file

@ -71,14 +71,12 @@ template <typename AllocatorType, typename ValueType, typename ValueAdapter,
typename SizeType> typename SizeType>
void ConstructElements(AllocatorType* alloc_ptr, ValueType* construct_first, void ConstructElements(AllocatorType* alloc_ptr, ValueType* construct_first,
ValueAdapter* values_ptr, SizeType construct_size) { ValueAdapter* values_ptr, SizeType construct_size) {
// If any construction fails, all completed constructions are rolled back.
for (SizeType i = 0; i < construct_size; ++i) { for (SizeType i = 0; i < construct_size; ++i) {
ABSL_INTERNAL_TRY { ABSL_INTERNAL_TRY {
values_ptr->ConstructNext(alloc_ptr, construct_first + i); values_ptr->ConstructNext(alloc_ptr, construct_first + i);
} }
ABSL_INTERNAL_CATCH_ANY { ABSL_INTERNAL_CATCH_ANY {
inlined_vector_internal::DestroyElements(alloc_ptr, construct_first, i); inlined_vector_internal::DestroyElements(alloc_ptr, construct_first, i);
ABSL_INTERNAL_RETHROW; ABSL_INTERNAL_RETHROW;
} }
} }
@ -171,6 +169,12 @@ class AllocationTransaction {
explicit AllocationTransaction(AllocatorType* alloc_ptr) explicit AllocationTransaction(AllocatorType* alloc_ptr)
: alloc_data_(*alloc_ptr, nullptr) {} : alloc_data_(*alloc_ptr, nullptr) {}
~AllocationTransaction() {
if (DidAllocate()) {
AllocatorTraits::deallocate(GetAllocator(), GetData(), GetCapacity());
}
}
AllocationTransaction(const AllocationTransaction&) = delete; AllocationTransaction(const AllocationTransaction&) = delete;
void operator=(const AllocationTransaction&) = delete; void operator=(const AllocationTransaction&) = delete;
@ -185,12 +189,6 @@ class AllocationTransaction {
return GetData(); return GetData();
} }
~AllocationTransaction() {
if (DidAllocate()) {
AllocatorTraits::deallocate(GetAllocator(), GetData(), GetCapacity());
}
}
private: private:
container_internal::CompressedTuple<AllocatorType, pointer> alloc_data_; container_internal::CompressedTuple<AllocatorType, pointer> alloc_data_;
size_type capacity_ = 0; size_type capacity_ = 0;
@ -205,9 +203,21 @@ class ConstructionTransaction {
explicit ConstructionTransaction(AllocatorType* alloc_ptr) explicit ConstructionTransaction(AllocatorType* alloc_ptr)
: alloc_data_(*alloc_ptr, nullptr) {} : alloc_data_(*alloc_ptr, nullptr) {}
~ConstructionTransaction() {
if (DidConstruct()) {
inlined_vector_internal::DestroyElements(std::addressof(GetAllocator()),
GetData(), GetSize());
}
}
ConstructionTransaction(const ConstructionTransaction&) = delete; ConstructionTransaction(const ConstructionTransaction&) = delete;
void operator=(const ConstructionTransaction&) = delete; void operator=(const ConstructionTransaction&) = delete;
AllocatorType& GetAllocator() { return alloc_data_.template get<0>(); }
pointer& GetData() { return alloc_data_.template get<1>(); }
size_type& GetSize() { return size_; }
bool DidConstruct() { return GetData() != nullptr; }
template <typename ValueAdapter> template <typename ValueAdapter>
void Construct(pointer data, ValueAdapter* values_ptr, size_type size) { void Construct(pointer data, ValueAdapter* values_ptr, size_type size) {
inlined_vector_internal::ConstructElements(std::addressof(GetAllocator()), inlined_vector_internal::ConstructElements(std::addressof(GetAllocator()),
@ -220,18 +230,7 @@ class ConstructionTransaction {
GetSize() = 0; GetSize() = 0;
} }
~ConstructionTransaction() {
if (GetData() != nullptr) {
inlined_vector_internal::DestroyElements(std::addressof(GetAllocator()),
GetData(), GetSize());
}
}
private: private:
AllocatorType& GetAllocator() { return alloc_data_.template get<0>(); }
pointer& GetData() { return alloc_data_.template get<1>(); }
size_type& GetSize() { return size_; }
container_internal::CompressedTuple<AllocatorType, pointer> alloc_data_; container_internal::CompressedTuple<AllocatorType, pointer> alloc_data_;
size_type size_ = 0; size_type size_ = 0;
}; };
@ -345,6 +344,7 @@ class Storage {
void SubtractSize(size_type count) { void SubtractSize(size_type count) {
assert(count <= GetSize()); assert(count <= GetSize());
GetSizeAndIsAllocated() -= count << 1; GetSizeAndIsAllocated() -= count << 1;
} }
@ -533,22 +533,14 @@ auto Storage<T, N, A>::Resize(ValueAdapter values, size_type new_size) -> void {
if (new_size > storage_view.capacity) { if (new_size > storage_view.capacity) {
size_type new_capacity = ComputeCapacity(storage_view.capacity, new_size); size_type new_capacity = ComputeCapacity(storage_view.capacity, new_size);
pointer new_data = allocation_tx.Allocate(new_capacity); pointer new_data = allocation_tx.Allocate(new_capacity);
// Construct new objects in `new_data`
construct_loop = {new_data + storage_view.size, construct_loop = {new_data + storage_view.size,
new_size - storage_view.size}; new_size - storage_view.size};
// Move all existing objects into `new_data`
move_construct_loop = {new_data, storage_view.size}; move_construct_loop = {new_data, storage_view.size};
// Destroy all existing objects in `storage_view.data`
destroy_loop = {storage_view.data, storage_view.size}; destroy_loop = {storage_view.data, storage_view.size};
} else if (new_size > storage_view.size) { } else if (new_size > storage_view.size) {
// Construct new objects in `storage_view.data`
construct_loop = {storage_view.data + storage_view.size, construct_loop = {storage_view.data + storage_view.size,
new_size - storage_view.size}; new_size - storage_view.size};
} else { } else {
// Destroy end `storage_view.size - new_size` objects in `storage_view.data`
destroy_loop = {storage_view.data + new_size, storage_view.size - new_size}; destroy_loop = {storage_view.data + new_size, storage_view.size - new_size};
} }
@ -797,8 +789,6 @@ auto Storage<T, N, A>::ShrinkToFit() -> void {
&move_values, storage_view.size); &move_values, storage_view.size);
} }
ABSL_INTERNAL_CATCH_ANY { ABSL_INTERNAL_CATCH_ANY {
// Writing to inlined data will trample on the existing state, thus it needs
// to be restored when a construction fails.
SetAllocatedData(storage_view.data, storage_view.capacity); SetAllocatedData(storage_view.data, storage_view.capacity);
ABSL_INTERNAL_RETHROW; ABSL_INTERNAL_RETHROW;
} }
@ -822,13 +812,8 @@ auto Storage<T, N, A>::Swap(Storage* other_storage_ptr) -> void {
assert(this != other_storage_ptr); assert(this != other_storage_ptr);
if (GetIsAllocated() && other_storage_ptr->GetIsAllocated()) { if (GetIsAllocated() && other_storage_ptr->GetIsAllocated()) {
// Both are allocated, thus we can swap the allocations at the top level.
swap(data_.allocated, other_storage_ptr->data_.allocated); swap(data_.allocated, other_storage_ptr->data_.allocated);
} else if (!GetIsAllocated() && !other_storage_ptr->GetIsAllocated()) { } else if (!GetIsAllocated() && !other_storage_ptr->GetIsAllocated()) {
// Both are inlined, thus element-wise swap up to smaller size, then move
// the remaining elements.
Storage* small_ptr = this; Storage* small_ptr = this;
Storage* large_ptr = other_storage_ptr; Storage* large_ptr = other_storage_ptr;
if (small_ptr->GetSize() > large_ptr->GetSize()) swap(small_ptr, large_ptr); if (small_ptr->GetSize() > large_ptr->GetSize()) swap(small_ptr, large_ptr);
@ -850,11 +835,6 @@ auto Storage<T, N, A>::Swap(Storage* other_storage_ptr) -> void {
large_ptr->GetInlinedData() + small_ptr->GetSize(), large_ptr->GetInlinedData() + small_ptr->GetSize(),
large_ptr->GetSize() - small_ptr->GetSize()); large_ptr->GetSize() - small_ptr->GetSize());
} else { } else {
// One is allocated and the other is inlined, thus we first move the
// elements from the inlined instance to the inlined space in the allocated
// instance and then we can finish by having the other vector take on the
// allocation.
Storage* allocated_ptr = this; Storage* allocated_ptr = this;
Storage* inlined_ptr = other_storage_ptr; Storage* inlined_ptr = other_storage_ptr;
if (!allocated_ptr->GetIsAllocated()) swap(allocated_ptr, inlined_ptr); if (!allocated_ptr->GetIsAllocated()) swap(allocated_ptr, inlined_ptr);
@ -872,8 +852,6 @@ auto Storage<T, N, A>::Swap(Storage* other_storage_ptr) -> void {
&move_values, inlined_ptr->GetSize()); &move_values, inlined_ptr->GetSize());
} }
ABSL_INTERNAL_CATCH_ANY { ABSL_INTERNAL_CATCH_ANY {
// Writing to inlined data will trample on the existing state, thus it
// needs to be restored when a construction fails.
allocated_ptr->SetAllocatedData(allocated_storage_view.data, allocated_ptr->SetAllocatedData(allocated_storage_view.data,
allocated_storage_view.capacity); allocated_storage_view.capacity);
ABSL_INTERNAL_RETHROW; ABSL_INTERNAL_RETHROW;
@ -887,7 +865,6 @@ auto Storage<T, N, A>::Swap(Storage* other_storage_ptr) -> void {
allocated_storage_view.capacity); allocated_storage_view.capacity);
} }
// All cases swap the size, `is_allocated` boolean and the allocator.
swap(GetSizeAndIsAllocated(), other_storage_ptr->GetSizeAndIsAllocated()); swap(GetSizeAndIsAllocated(), other_storage_ptr->GetSizeAndIsAllocated());
swap(*GetAllocPtr(), *other_storage_ptr->GetAllocPtr()); swap(*GetAllocPtr(), *other_storage_ptr->GetAllocPtr());
} }

View file

@ -368,9 +368,9 @@ BENCHMARK_TAGS = [
] ]
# Benchmarks for various methods / test utilities # Benchmarks for various methods / test utilities
cc_test( cc_binary(
name = "benchmarks", name = "benchmarks",
size = "small", testonly = 1,
srcs = [ srcs = [
"benchmarks.cc", "benchmarks.cc",
], ],

View file

@ -25,7 +25,6 @@
#include <type_traits> #include <type_traits>
#include <vector> #include <vector>
#include "benchmark/benchmark.h"
#include "absl/base/macros.h" #include "absl/base/macros.h"
#include "absl/meta/type_traits.h" #include "absl/meta/type_traits.h"
#include "absl/random/bernoulli_distribution.h" #include "absl/random/bernoulli_distribution.h"
@ -40,6 +39,7 @@
#include "absl/random/uniform_int_distribution.h" #include "absl/random/uniform_int_distribution.h"
#include "absl/random/uniform_real_distribution.h" #include "absl/random/uniform_real_distribution.h"
#include "absl/random/zipf_distribution.h" #include "absl/random/zipf_distribution.h"
#include "benchmark/benchmark.h"
namespace { namespace {
@ -221,12 +221,12 @@ void BM_Poisson(benchmark::State& state) {
BM_Dist<Engine, Dist>(state, a); BM_Dist<Engine, Dist>(state, a);
} }
template <typename Engine, typename Dist, int V = 1, int Q = 2> template <typename Engine, typename Dist, int Q = 2, int V = 1>
void BM_Zipf(benchmark::State& state) { void BM_Zipf(benchmark::State& state) {
using value_type = typename Dist::result_type; using value_type = typename Dist::result_type;
volatile double v = V;
volatile double q = Q; volatile double q = Q;
BM_Dist<Engine, Dist>(state, std::numeric_limits<value_type>::max(), v, q); volatile double v = V;
BM_Dist<Engine, Dist>(state, std::numeric_limits<value_type>::max(), q, v);
} }
template <typename Engine, typename Dist> template <typename Engine, typename Dist>
@ -333,8 +333,8 @@ void BM_Thread(benchmark::State& state) {
absl::log_uniform_int_distribution<int64_t>); \ absl::log_uniform_int_distribution<int64_t>); \
BENCHMARK_TEMPLATE(BM_Dist, Engine, std::geometric_distribution<int64_t>); \ BENCHMARK_TEMPLATE(BM_Dist, Engine, std::geometric_distribution<int64_t>); \
BENCHMARK_TEMPLATE(BM_Zipf, Engine, absl::zipf_distribution<uint64_t>); \ BENCHMARK_TEMPLATE(BM_Zipf, Engine, absl::zipf_distribution<uint64_t>); \
BENCHMARK_TEMPLATE(BM_Zipf, Engine, absl::zipf_distribution<uint64_t>, 3, \ BENCHMARK_TEMPLATE(BM_Zipf, Engine, absl::zipf_distribution<uint64_t>, 2, \
2); \ 3); \
BENCHMARK_TEMPLATE(BM_Bernoulli, Engine, std::bernoulli_distribution, \ BENCHMARK_TEMPLATE(BM_Bernoulli, Engine, std::bernoulli_distribution, \
257305); \ 257305); \
BENCHMARK_TEMPLATE(BM_Bernoulli, Engine, absl::bernoulli_distribution, \ BENCHMARK_TEMPLATE(BM_Bernoulli, Engine, absl::bernoulli_distribution, \

View file

@ -437,6 +437,6 @@ IntType Zipf(URBG&& urbg, // NOLINT(runtime/references)
distribution_t, format_t>(&urbg, hi, q, v); distribution_t, format_t>(&urbg, hi, q, v);
} }
} // namespace absl. } // namespace absl
#endif // ABSL_RANDOM_DISTRIBUTIONS_H_ #endif // ABSL_RANDOM_DISTRIBUTIONS_H_

View file

@ -28,7 +28,8 @@
#define ABSL_EXPECT_DEATH_IF_SUPPORTED(statement, regex) \ #define ABSL_EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
EXPECT_DEATH_IF_SUPPORTED(statement, ".*") EXPECT_DEATH_IF_SUPPORTED(statement, ".*")
#else #else
#define ABSL_EXPECT_DEATH_IF_SUPPORTED EXPECT_DEATH_IF_SUPPORTED #define ABSL_EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
EXPECT_DEATH_IF_SUPPORTED(statement, regex)
#endif #endif
namespace { namespace {

View file

@ -264,6 +264,6 @@ std::basic_istream<CharT, Traits>& operator>>(
return is; return is;
} }
} // namespace absl. } // namespace absl
#endif // ABSL_RANDOM_ZIPF_DISTRIBUTION_H_ #endif // ABSL_RANDOM_ZIPF_DISTRIBUTION_H_

View file

@ -47,7 +47,7 @@ namespace absl {
// integer type. If any errors are encountered, this function returns `false`, // integer type. If any errors are encountered, this function returns `false`,
// leaving `out` in an unspecified state. // leaving `out` in an unspecified state.
template <typename int_type> template <typename int_type>
ABSL_MUST_USE_RESULT bool SimpleAtoi(absl::string_view s, int_type* out); ABSL_MUST_USE_RESULT bool SimpleAtoi(absl::string_view str, int_type* out);
// SimpleAtof() // SimpleAtof()
// //
@ -180,8 +180,8 @@ ABSL_MUST_USE_RESULT bool safe_strtoi_base(absl::string_view s, int_type* out,
// preceded by ASCII whitespace, with a value in the range of the corresponding // preceded by ASCII whitespace, with a value in the range of the corresponding
// integer type. // integer type.
template <typename int_type> template <typename int_type>
ABSL_MUST_USE_RESULT bool SimpleAtoi(absl::string_view s, int_type* out) { ABSL_MUST_USE_RESULT bool SimpleAtoi(absl::string_view str, int_type* out) {
return numbers_internal::safe_strtoi_base(s, out, 10); return numbers_internal::safe_strtoi_base(str, out, 10);
} }
} // namespace absl } // namespace absl