9 #ifndef LIBPMEMOBJ_CPP_ARRAY_HPP
10 #define LIBPMEMOBJ_CPP_ARRAY_HPP
14 #include <initializer_list>
23 #include <libpmemobj/base.h>
56 template <
typename T, std::
size_t N>
59 template <
typename Y, std::
size_t M>
60 struct standard_array_traits {
66 struct standard_array_traits<Y, 0> {
67 struct _alignment_struct {
71 struct alignas(_alignment_struct) type {
72 char _data[
sizeof(_alignment_struct)];
78 using pointer = value_type *;
79 using const_pointer =
const value_type *;
80 using reference = value_type &;
81 using const_reference =
const value_type &;
83 using const_iterator = const_pointer;
84 using size_type = std::size_t;
85 using difference_type = std::ptrdiff_t;
86 using reverse_iterator = std::reverse_iterator<iterator>;
87 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
92 typename standard_array_traits<T, N>::type _data;
112 #if __cplusplus > 201703L
119 array(std::initializer_list<T> list)
121 if constexpr (N > 0) {
123 for (
auto &v : list) {
148 auto pop = _get_pool();
155 this, 1, POBJ_XADD_ASSUME_INITIALIZED);
156 std::copy(other.
cbegin(), other.
cend(), _get_data());
179 auto pop = _get_pool();
186 this, 1, POBJ_XADD_ASSUME_INITIALIZED);
188 &other, 1, POBJ_XADD_ASSUME_INITIALIZED);
189 std::move(other._get_data(), other._get_data() +
size(),
207 throw std::out_of_range(
"array::at");
210 POBJ_XADD_ASSUME_INITIALIZED);
212 return _get_data()[n];
221 at(size_type n)
const
224 throw std::out_of_range(
"array::at");
226 return _get_data()[n];
238 throw std::out_of_range(
"array::const_at");
240 return _get_data()[n];
253 POBJ_XADD_ASSUME_INITIALIZED);
255 return _get_data()[n];
264 return _get_data()[n];
278 POBJ_XADD_ASSUME_INITIALIZED);
330 return const_iterator(_get_data());
339 return const_iterator(_get_data());
348 return const_iterator(_get_data() +
size());
357 return const_iterator(_get_data() +
size());
369 return reverse_iterator(
iterator(_get_data() +
size()));
381 return reverse_iterator(
iterator(_get_data()));
387 const_reverse_iterator
390 return const_reverse_iterator(
cend());
396 const_reverse_iterator
399 return const_reverse_iterator(
cend());
405 const_reverse_iterator
408 return const_reverse_iterator(
cbegin());
414 const_reverse_iterator
417 return const_reverse_iterator(
cbegin());
430 POBJ_XADD_ASSUME_INITIALIZED);
431 return _get_data()[0];
444 POBJ_XADD_ASSUME_INITIALIZED);
445 return _get_data()[
size() - 1];
454 return _get_data()[0];
463 return _get_data()[0];
472 return _get_data()[
size() - 1];
481 return _get_data()[
size() - 1];
499 throw std::out_of_range(
"array::range");
502 POBJ_XADD_ASSUME_INITIALIZED);
504 return {_get_data() + start, _get_data() + start + n};
524 range(size_type start, size_type n, size_type snapshot_size)
527 throw std::out_of_range(
"array::range");
529 if (snapshot_size > n)
533 _get_data() + start, n,
536 _get_data() + start, n,
552 range(size_type start, size_type n)
const
555 throw std::out_of_range(
"array::range");
557 return {const_iterator(_get_data() + start),
558 const_iterator(_get_data() + start + n)};
573 crange(size_type start, size_type n)
const
576 throw std::out_of_range(
"array::crange");
578 return {const_iterator(_get_data() + start),
579 const_iterator(_get_data() + start + n)};
619 auto pop = _get_pool();
623 this, 1, POBJ_XADD_ASSUME_INITIALIZED);
624 std::fill(_get_data(), _get_data() +
size(), value);
635 template <std::
size_t Size = N>
636 typename std::enable_if<Size != 0>::type
643 auto pop = _get_pool();
650 this, 1, POBJ_XADD_ASSUME_INITIALIZED);
652 &other, 1, POBJ_XADD_ASSUME_INITIALIZED);
654 std::swap_ranges(_get_data(), _get_data() +
size(),
662 template <std::
size_t Size = N>
663 typename std::enable_if<Size == 0>::type
666 static_assert(!std::is_const<T>::value,
667 "cannot swap zero-sized array of type 'const T'");
674 template <std::
size_t Size = N>
675 typename std::enable_if<Size != 0, T *>::type
684 template <std::
size_t Size = N>
685 typename std::enable_if<Size != 0, const T *>::type
695 template <std::
size_t Size = N>
696 typename std::enable_if<Size == 0, T *>::type
699 return reinterpret_cast<T *
>(&this->_data);
705 template <std::
size_t Size = N>
706 typename std::enable_if<Size == 0, const T *>::type
709 return reinterpret_cast<const T *
>(&this->_data);
728 template <
typename T, std::
size_t N>
739 template <
typename T, std::
size_t N>
743 return !(lhs == rhs);
750 template <
typename T, std::
size_t N>
754 return std::lexicographical_compare(lhs.
cbegin(), lhs.
cend(),
762 template <
typename T, std::
size_t N>
773 template <
typename T, std::
size_t N>
784 template <
typename T, std::
size_t N>
795 template <
typename T, std::
size_t N>
796 typename pmem::obj::array<T, N>::const_iterator
806 template <
typename T, std::
size_t N>
807 typename pmem::obj::array<T, N>::const_iterator
817 template <
typename T, std::
size_t N>
818 typename pmem::obj::array<T, N>::const_reverse_iterator
828 template <
typename T, std::
size_t N>
829 typename pmem::obj::array<T, N>::const_reverse_iterator
839 template <
typename T, std::
size_t N>
850 template <
typename T, std::
size_t N>
851 typename pmem::obj::array<T, N>::const_iterator
861 template <
typename T, std::
size_t N>
872 template <
typename T, std::
size_t N>
873 typename pmem::obj::array<T, N>::const_iterator
883 template <
typename T, std::
size_t N>
884 typename pmem::obj::array<T, N>::reverse_iterator
894 template <
typename T, std::
size_t N>
895 typename pmem::obj::array<T, N>::const_reverse_iterator
905 template <
typename T, std::
size_t N>
906 typename pmem::obj::array<T, N>::reverse_iterator
916 template <
typename T, std::
size_t N>
917 typename pmem::obj::array<T, N>::const_reverse_iterator
927 template <
typename T,
size_t N>
938 template <
size_t I,
typename T,
size_t N>
943 "Index out of bounds in std::get<> (pmem::obj::array)");
951 template <
size_t I,
typename T,
size_t N>
956 "Index out of bounds in std::get<> (pmem::obj::array)");
957 return std::move(a.at(I));
964 template <
size_t I,
typename T,
size_t N>
969 "Index out of bounds in std::get<> (pmem::obj::array)");
977 template <
size_t I,
typename T,
size_t N>
982 "Index out of bounds in std::get<> (pmem::obj::array)");
983 return std::move(a.at(I));
static void run(obj::pool_base &pool, std::function< void()> tx, Locks &... locks)
Execute a closure-like transaction and lock locks.
Definition: transaction.hpp:810
Provides interface to access sequence of objects.
Definition: slice.hpp:61
Commonly used functionality.
Iterators for contiguous persistent containers.
void conditional_add_to_tx(const T *that, std::size_t count=1, uint64_t flags=0)
Conditionally add 'count' objects to a transaction.
Definition: common.hpp:176
pool_base pool_by_vptr(const T *that)
Retrieve pool handle for the given pointer.
Definition: utils.hpp:32
Persistent memory namespace.
Definition: allocation_flag.hpp:15
Persistent smart pointer.
Convenience extensions for the resides on pmem property template.
Interface to access sequence of objects.
Default non-const iterator which adds element to a transaction on every access.
Definition: contiguous_iterator.hpp:331
Non-const iterator which adds elements to a transaction in a bulk.
Definition: contiguous_iterator.hpp:192
Persistent container with std::array compatible interface.
Definition: array.hpp:57
reverse_iterator rbegin()
Returns a reverse iterator to the beginning.
Definition: array.hpp:367
reference at(size_type n)
Access element at specific index and add it to a transaction.
Definition: array.hpp:204
slice< range_snapshotting_iterator > range(size_type start, size_type n, size_type snapshot_size)
Returns slice.
Definition: array.hpp:524
constexpr size_type max_size() const noexcept
Returns the maximum size of the array.
Definition: array.hpp:595
pmem::obj::array< T, N >::reverse_iterator rbegin(pmem::obj::array< T, N > &a)
Non-member rbegin.
Definition: array.hpp:885
void fill(const_reference value)
Fills array with specified value inside internal transaction.
Definition: array.hpp:617
slice< const_iterator > crange(size_type start, size_type n) const
Returns const slice.
Definition: array.hpp:573
pmem::obj::array< T, N >::const_iterator end(const pmem::obj::array< T, N > &a)
Non-member end.
Definition: array.hpp:874
const T & get(const pmem::obj::array< T, N > &a) noexcept
Non-member get function.
Definition: array.hpp:966
const_reference front() const
Access the first element.
Definition: array.hpp:452
iterator begin()
Returns an iterator to the beginning.
Definition: array.hpp:307
iterator end()
Returns an iterator to the end.
Definition: array.hpp:319
const_reference at(size_type n) const
Access element at specific index.
Definition: array.hpp:221
pmem::obj::array< T, N >::iterator begin(pmem::obj::array< T, N > &a)
Non-member begin.
Definition: array.hpp:841
array(array &&)=default
Defaulted move constructor.
const_reverse_iterator crend() const noexcept
Returns a const reverse iterator to the beginning.
Definition: array.hpp:415
const_iterator end() const noexcept
Returns a const iterator to the end.
Definition: array.hpp:346
pmem::obj::array< T, N >::iterator end(pmem::obj::array< T, N > &a)
Non-member end.
Definition: array.hpp:863
pmem::obj::array< T, N >::const_reverse_iterator crend(const pmem::obj::array< T, N > &a)
Non-member crend.
Definition: array.hpp:830
array & operator=(array &&other)
Move assignment operator - perform move assignment from other pmem::obj::array.
Definition: array.hpp:173
bool operator!=(const array< T, N > &lhs, const array< T, N > &rhs)
Non-member not-equal operator.
Definition: array.hpp:741
const_reverse_iterator rend() const noexcept
Returns a const reverse iterator to the end.
Definition: array.hpp:406
const T * data() const noexcept
Returns const raw pointer to the underlying data.
Definition: array.hpp:286
pmem::obj::array< T, N >::const_iterator cbegin(const pmem::obj::array< T, N > &a)
Non-member cbegin.
Definition: array.hpp:797
bool operator<=(const array< T, N > &lhs, const array< T, N > &rhs)
Non-member less or equal operator.
Definition: array.hpp:786
pmem::obj::array< T, N >::reverse_iterator rend(pmem::obj::array< T, N > &a)
Non-member rend.
Definition: array.hpp:907
void swap(pmem::obj::array< T, N > &lhs, pmem::obj::array< T, N > &rhs)
Non-member swap function.
Definition: array.hpp:929
pmem::obj::array< T, N >::const_iterator begin(const pmem::obj::array< T, N > &a)
Non-member begin.
Definition: array.hpp:852
array(const array &)=default
Defaulted copy constructor.
reference operator[](size_type n)
Access element at specific index and add it to a transaction.
Definition: array.hpp:250
pmem::obj::array< T, N >::const_reverse_iterator rbegin(const pmem::obj::array< T, N > &a)
Non-member rbegin.
Definition: array.hpp:896
const_reverse_iterator rbegin() const noexcept
Returns a const reverse iterator to the beginning.
Definition: array.hpp:388
T & get(pmem::obj::array< T, N > &a)
Non-member get function.
Definition: array.hpp:940
bool operator==(const array< T, N > &lhs, const array< T, N > &rhs)
Non-member equal operator.
Definition: array.hpp:730
T * data()
Returns raw pointer to the underlying data and adds entire array to a transaction.
Definition: array.hpp:275
array & operator=(const array &other)
Copy assignment operator - perform assignment from other pmem::obj::array.
Definition: array.hpp:142
const_reverse_iterator crbegin() const noexcept
Returns a const reverse iterator to the beginning.
Definition: array.hpp:397
bool operator>(const array< T, N > &lhs, const array< T, N > &rhs)
Non-member greater than operator.
Definition: array.hpp:764
slice< pointer > range(size_type start, size_type n)
Returns slice and snapshots requested range.
Definition: array.hpp:496
const_reference const_at(size_type n) const
Access element at specific index.
Definition: array.hpp:235
const_reference back() const
Access the last element.
Definition: array.hpp:470
std::enable_if< Size !=0 >::type swap(array &other)
Swaps content with other array's content inside internal transaction.
Definition: array.hpp:637
bool operator<(const array< T, N > &lhs, const array< T, N > &rhs)
Non-member less than operator.
Definition: array.hpp:752
const_reference cback() const
Access the last element.
Definition: array.hpp:479
reference back()
Access the last element and add this element to a transaction.
Definition: array.hpp:441
const_iterator cend() const noexcept
Returns a const iterator to the end.
Definition: array.hpp:355
const_reference cfront() const
Access the first element.
Definition: array.hpp:461
const_iterator cbegin() const noexcept
Returns const iterator to the beginning.
Definition: array.hpp:337
const T && get(const pmem::obj::array< T, N > &&a) noexcept
Non-member get function.
Definition: array.hpp:979
const_iterator begin() const noexcept
Returns const iterator to the beginning.
Definition: array.hpp:328
reference front()
Access the first element and add this element to a transaction.
Definition: array.hpp:427
constexpr bool empty() const noexcept
Checks whether array is empty.
Definition: array.hpp:604
const T * cdata() const noexcept
Returns const raw pointer to the underlying data.
Definition: array.hpp:295
array()=default
Defaulted constructor.
pmem::obj::array< T, N >::const_reverse_iterator crbegin(const pmem::obj::array< T, N > &a)
Non-member crbegin.
Definition: array.hpp:819
constexpr size_type size() const noexcept
Returns size of the array.
Definition: array.hpp:586
bool operator>=(const array< T, N > &lhs, const array< T, N > &rhs)
Non-member greater or equal operator.
Definition: array.hpp:775
const_reference operator[](size_type n) const
Access element at specific index.
Definition: array.hpp:262
pmem::obj::array< T, N >::const_reverse_iterator rend(const pmem::obj::array< T, N > &a)
Non-member rend.
Definition: array.hpp:918
pmem::obj::array< T, N >::const_iterator cend(const pmem::obj::array< T, N > &a)
Non-member cend.
Definition: array.hpp:808
T && get(pmem::obj::array< T, N > &&a)
Non-member get function.
Definition: array.hpp:953
std::enable_if< Size==0 >::type swap(array &other)
Swap for zero-sized array.
Definition: array.hpp:664
slice< const_iterator > range(size_type start, size_type n) const
Returns const slice.
Definition: array.hpp:552
reverse_iterator rend()
Returns a reverse iterator to the end.
Definition: array.hpp:379
C++ pmemobj transactions.