20 using allocator_type = std::allocator<T>;
21 using size_type = std::size_t;
23 using allocator_traits = std::allocator_traits<std::allocator<T>>;
25 using pointer =
typename allocator_traits::pointer;
26 using const_pointer =
typename allocator_traits::const_pointer;
28 using iterator = pointer;
29 using const_iterator = const_pointer;
31 using reverse_iterator = std::reverse_iterator<iterator>;
32 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
36 , ptr_{size ? allocator_traits::allocate(alloc_, size) :
nullptr} {}
40 swap(size_, other.size_);
41 swap(capacity_, other.capacity_);
42 swap(alloc_, other.alloc_);
43 swap(ptr_, other.ptr_);
47 : capacity_{other.size_}
48 , ptr_{other.ptr_ ? allocator_traits::allocate(alloc_, other.size_)
50 for (
auto i = other.begin(), end = other.end(); i != end; ++i) {
73 allocator_traits::deallocate(alloc_, ptr_, capacity_);
76 template <
typename... Ts>
77 void construct(Ts&&... ts) {
78 allocator_traits::construct(
79 alloc_, ptr_ + size_, std::forward<Ts>(ts)...);
83 void destroy() noexcept {
85 allocator_traits::destroy(alloc_, ptr_ + size_);
88 size_type size()
const {
return size_; }
89 size_type capacity()
const {
return capacity_; }
91 iterator begin() {
return ptr_; }
92 const_iterator begin()
const {
return ptr_; }
93 const_iterator cbegin()
const {
return ptr_; }
95 iterator end() {
return ptr_ + size_; }
96 const_iterator end()
const {
return ptr_ + size_; }
97 const_iterator cend()
const {
return ptr_ + size_; }
101 size_type capacity_{0};
102 allocator_type alloc_{};
106 template <
typename T>
113 template <
typename T>
117 using value_type =
typename impl_type::value_type;
118 using allocator_type =
typename impl_type::allocator_type;
119 using size_type =
typename impl_type::size_type;
120 using difference_type = std::ptrdiff_t;
122 using reference = value_type&;
123 using const_reference =
const value_type&;
125 using pointer =
typename impl_type::pointer;
126 using const_pointer =
typename impl_type::const_pointer;
128 using iterator = pointer;
129 using const_iterator = const_pointer;
131 using reverse_iterator = std::reverse_iterator<iterator>;
132 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
136 swap(impl_, other.impl_);
139 template <
typename It>
140 iterator insert(const_iterator pos, It first, It last) {
141 const auto n = std::distance(first, last);
142 const auto c = impl_.capacity();
143 const auto new_size = impl_.size() + n;
144 const auto old_pos = std::distance(impl_.cbegin(), pos);
148 const auto old_end = impl_.end();
149 while (impl_.size() < new_size) {
153 const auto r = impl_.begin() + old_pos;
154 std::move_backward(r, old_end, impl_.begin() + new_size);
155 std::copy(first, last, r);
162 impl_type v{std::max(impl_.size() + n, impl_.capacity() * 2 + 1)};
164 for (
auto i = impl_.begin(); i != pos; ++i) {
165 v.construct(std::move(*i));
167 for (
auto i = first; i != last; ++i) {
170 for (
auto i = pos; i != impl_.end(); ++i) {
171 v.construct(std::move(*i));
175 return impl_.begin() + old_pos;
178 iterator erase(const_iterator begin, const_iterator end) {
180 const auto p = impl_.begin() + std::distance(impl_.cbegin(), begin);
181 std::move(impl_.begin() + std::distance(impl_.cbegin(), end),
185 const auto num = std::distance(begin, end);
186 for (
auto i = 0u; i != num; ++i) {
192 void reserve(size_type new_cap) {
193 if (new_cap > impl_.capacity()) {
196 impl_type v{std::max(new_cap, impl_.capacity() * 2 + 1)};
198 for (
auto i = impl_.begin(), end = impl_.end(); i != end; ++i) {
199 v.construct(std::move(*i));
206 pointer data() {
return impl_.begin(); }
207 const_pointer data()
const {
return impl_.cbegin(); }
209 size_type size()
const {
return impl_.size(); }
210 size_type capacity()
const {
return impl_.capacity(); }
212 iterator begin() {
return impl_.begin(); }
213 const_iterator begin()
const {
return impl_.begin(); }
214 const_iterator cbegin()
const {
return impl_.begin(); }
216 iterator end() {
return impl_.end(); }
217 const_iterator end()
const {
return impl_.end(); }
218 const_iterator cend()
const {
return impl_.end(); }
224 template <
class T,
class Comp = std::less<T>>
231 : comp_{std::move(comp)} {}
233 auto begin() {
return data_.begin(); }
234 const auto begin()
const {
return data_.begin(); }
235 const auto cbegin()
const {
return data_.cbegin(); }
237 auto end() {
return data_.end(); }
238 const auto end()
const {
return data_.end(); }
239 const auto cend()
const {
return data_.cend(); }
241 auto find(
const T& t)
const {
242 const auto it = std::lower_bound(data_.begin(), data_.end(), t, comp_);
243 if (it != data_.end() && values_equal(t, *it)) {
250 const auto it = std::lower_bound(data_.begin(), data_.end(), t, comp_);
251 if (it != data_.end() && values_equal(t, *it)) {
252 return std::make_pair(it,
false);
254 return std::make_pair(data_.insert(it, &t, &t + 1),
true);
257 constexpr
auto key_comp()
const {
return comp_; }
258 constexpr
auto value_comp()
const {
return comp_; }
260 auto size()
const {
return data_.size(); }
263 constexpr
bool values_not_equal(
const T& a,
const T& b)
const {
264 return comp_(a, b) || comp_(b, a);
266 constexpr
bool values_equal(
const T& a,
const T& b)
const {
267 return !values_not_equal(a, b);
Definition: recursive_vector.h:114
Definition: capsule_base.h:9
Definition: recursive_vector.h:17
Definition: recursive_vector.h:225