sparrow 0.3.0
Loading...
Searching...
No Matches
buffer.hpp
Go to the documentation of this file.
1// Copyright 2024 Man Group Operations Limited
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <algorithm>
18#include <concepts>
19#include <iterator>
20#include <memory>
21#include <ranges>
22#include <stdexcept>
23#include <type_traits>
24
29
30#if not defined(SPARROW_BUFFER_GROWTH_FACTOR)
31# define SPARROW_BUFFER_GROWTH_FACTOR 2
32#endif
33
34namespace sparrow
35{
43 template <class T>
45 {
46 protected:
47
49 using alloc_traits = std::allocator_traits<allocator_type>;
50 using pointer = typename alloc_traits::pointer;
51 using size_type = typename alloc_traits::size_type;
52
54 {
55 pointer p_begin = nullptr;
56 pointer p_end = nullptr;
58
59 buffer_data() = default;
60 constexpr buffer_data(buffer_data&&) noexcept;
61 constexpr buffer_data& operator=(buffer_data&&) noexcept;
62 };
63
64 buffer_base() = default;
65
66 template <allocator A>
67 constexpr buffer_base(const A& a) noexcept;
68
69 template <allocator A = allocator_type>
70 constexpr buffer_base(size_type n, const A& a = A());
71
72 template <allocator A = allocator_type>
73 constexpr buffer_base(pointer p, size_type n, const A& a = A());
74
75 ~buffer_base();
76
77 buffer_base(buffer_base&&) noexcept = default;
78
79 template <allocator A>
80 constexpr buffer_base(buffer_base&& rhs, const A& a);
81
82 [[nodiscard]] constexpr allocator_type& get_allocator() noexcept;
83 [[nodiscard]] constexpr const allocator_type& get_allocator() const noexcept;
84 [[nodiscard]] constexpr buffer_data& get_data() noexcept;
85 [[nodiscard]] constexpr const buffer_data& get_data() const noexcept;
86
87 constexpr pointer allocate(size_type n);
88 constexpr void deallocate(pointer p, size_type n);
89 constexpr void create_storage(size_type n);
90 constexpr void assign_storage(pointer p, size_type n, size_type cap);
91
92 private:
93
94 allocator_type m_alloc;
95 buffer_data m_data;
96 };
97
107 template <class T>
108 class buffer : private buffer_base<T>
109 {
110 using base_type = buffer_base<T>;
111 using alloc_traits = typename base_type::alloc_traits;
112
113 static_assert(
114 std::same_as<std::remove_cvref_t<T>, T>,
115 "buffer must have a non-const, non-volatile, non-reference value_type"
116 );
117
118 public:
119
121 using value_type = T;
124 using pointer = typename alloc_traits::pointer;
125 using const_pointer = typename alloc_traits::const_pointer;
126 using size_type = typename alloc_traits::size_type;
127 using difference_type = typename alloc_traits::difference_type;
130 using reverse_iterator = std::reverse_iterator<iterator>;
131 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
132
133 buffer() = default;
134
135 template <class A>
136 requires(not std::same_as<A, buffer<T>> and allocator<A>)
137 constexpr explicit buffer(const A& a)
138 : base_type(a)
139 {
140 }
141
142 template <allocator A = allocator_type>
143 constexpr explicit buffer(size_type n, const A& a = A());
144
145 template <allocator A = allocator_type>
146 constexpr buffer(size_type n, const value_type& v, const A& a = A());
147
148 template <allocator A = allocator_type>
149 constexpr buffer(pointer p, size_type n, const A& a = A());
150
151 template <allocator A = allocator_type>
152 constexpr buffer(std::initializer_list<value_type> init, const A& a = A());
153
154 template <class It, allocator A = allocator_type>
155 constexpr buffer(It first, It last, const A& a = A());
156
157 template <std::ranges::input_range Range, allocator A = allocator_type>
158 requires std::same_as<std::ranges::range_value_t<Range>, T>
159 constexpr buffer(const Range& range, const A& a = A());
160
161 ~buffer();
162
163 buffer(const buffer& rhs);
164
165 template <allocator A>
166 buffer(const buffer& rhs, const A& a);
167
168 buffer(buffer&& rhs) noexcept = default;
169
170 template <allocator A>
171 buffer(buffer&& rhs, const A& a);
172
173 constexpr buffer& operator=(const buffer& rhs);
174 constexpr buffer& operator=(buffer&& rhs);
175 constexpr buffer& operator=(std::initializer_list<value_type> init);
176
177 // Element access
178
179 [[nodiscard]] constexpr reference operator[](size_type i);
180 [[nodiscard]] constexpr const_reference operator[](size_type i) const;
181
182 [[nodiscard]] constexpr reference front();
183 [[nodiscard]] constexpr const_reference front() const;
184
185 [[nodiscard]] constexpr reference back();
186 [[nodiscard]] constexpr const_reference back() const;
187
188 // TODO: make this non template and make a buffer_caster class
189 template <class U = T>
190 [[nodiscard]] constexpr U* data() noexcept;
191
192 // TODO: make this non template and make a buffer_caster class
193 template <class U = T>
194 [[nodiscard]] constexpr const U* data() const noexcept;
195
196 // Iterators
197
198 [[nodiscard]] constexpr iterator begin() noexcept;
199 [[nodiscard]] constexpr iterator end() noexcept;
200
201 [[nodiscard]] constexpr const_iterator begin() const noexcept;
202 [[nodiscard]] constexpr const_iterator end() const noexcept;
203
204 [[nodiscard]] constexpr const_iterator cbegin() const noexcept;
205 [[nodiscard]] constexpr const_iterator cend() const noexcept;
206
207 [[nodiscard]] constexpr reverse_iterator rbegin() noexcept;
208 [[nodiscard]] constexpr reverse_iterator rend() noexcept;
209
210 [[nodiscard]] constexpr const_reverse_iterator rbegin() const noexcept;
211 [[nodiscard]] constexpr const_reverse_iterator rend() const noexcept;
212
213 [[nodiscard]] constexpr const_reverse_iterator crbegin() const noexcept;
214 [[nodiscard]] constexpr const_reverse_iterator crend() const noexcept;
215 [[nodiscard]]
216 // Capacity
217
218 [[nodiscard]] constexpr bool
219 empty() const noexcept;
220 [[nodiscard]] constexpr size_type capacity() const noexcept;
221 [[nodiscard]] constexpr size_type size() const noexcept;
222 [[nodiscard]] constexpr size_type max_size() const noexcept;
223 constexpr void reserve(size_type new_cap);
224 constexpr void shrink_to_fit();
225
226 // Modifiers
227
228 constexpr void clear();
229
230 constexpr iterator insert(const_iterator pos, const T& value);
231 constexpr iterator insert(const_iterator pos, T&& value);
232 constexpr iterator insert(const_iterator pos, size_type count, const T& value);
233 template <mpl::iterator_of_type<T> InputIt>
234 constexpr iterator insert(const_iterator pos, InputIt first, InputIt last);
235 template <std::ranges::input_range R>
236 requires std::same_as<std::ranges::range_value_t<R>, T>
237 constexpr iterator insert(const_iterator pos, R&& range);
238 constexpr iterator insert(const_iterator pos, std::initializer_list<T> ilist);
239
240 template <class... Args>
241 constexpr iterator emplace(const_iterator pos, Args&&... args);
242
243 constexpr iterator erase(const_iterator pos);
244 constexpr iterator erase(const_iterator first, const_iterator last);
245
246 constexpr void push_back(const T& value);
247 constexpr void push_back(T&& value);
248
249 constexpr void pop_back();
250
251 constexpr void resize(size_type new_size);
252 constexpr void resize(size_type new_size, const value_type& value);
253 constexpr void swap(buffer& rhs) noexcept;
254
255 private:
256
257 using base_type::get_allocator;
258 using base_type::get_data;
259
260 template <class F>
261 constexpr void resize_impl(size_type new_size, F&& initializer);
262
263 template <class It>
264 constexpr void assign_range_impl(It first, It last, std::forward_iterator_tag);
265
266 constexpr void erase_at_end(pointer p);
267
268 template <class It>
269 constexpr pointer allocate_and_copy(size_type n, It first, It last);
270
271 constexpr void reserve_with_growth_factor(size_type new_cap);
272
273 // The following methods are static because:
274 // - they accept an allocator argument, and do not depend on
275 // the state of buffer
276 // - taking an allocator argument instead of relying on get_allocator
277 // will make it easier to support allocators that propagate
278 // on copy / move, as these methods will be called with allocators
279 // from different instances of buffers.
280
281 static constexpr size_type check_init_length(size_type n, const allocator_type& a);
282
283 [[nodiscard]] static constexpr size_type max_size_impl(const allocator_type& a) noexcept;
284
285 [[nodiscard]] static constexpr pointer
286 default_initialize(pointer begin, size_type n, allocator_type& a);
287
288 static constexpr pointer
289 fill_initialize(pointer begin, size_type n, const value_type& v, allocator_type& a);
290
291 template <class It>
292 static constexpr pointer copy_initialize(It first, It last, pointer begin, allocator_type& a);
293
294 static constexpr void destroy(pointer first, pointer last, allocator_type& a);
295 };
296
297 template <class T>
298 constexpr bool operator==(const buffer<T>& lhs, const buffer<T>& rhs) noexcept;
299
300 /******************************
301 * buffer_base implementation *
302 ******************************/
303
304 template <class T>
305 constexpr buffer_base<T>::buffer_data::buffer_data(buffer_data&& rhs) noexcept
306 : p_begin(rhs.p_begin)
307 , p_end(rhs.p_end)
309 {
310 rhs.p_begin = nullptr;
311 rhs.p_end = nullptr;
312 rhs.p_storage_end = nullptr;
313 }
314
315 template <class T>
317 {
318 std::swap(p_begin, rhs.p_begin);
319 std::swap(p_end, rhs.p_end);
320 std::swap(p_storage_end, rhs.p_storage_end);
321 return *this;
322 }
323
324 template <class T>
325 template <allocator A>
326 constexpr buffer_base<T>::buffer_base(const A& a) noexcept
327 : m_alloc(a)
328 {
329 }
330
331 template <class T>
332 template <allocator A>
333 constexpr buffer_base<T>::buffer_base(size_type n, const A& a)
334 : m_alloc(a)
335 {
337 }
338
339 template <class T>
340 template <allocator A>
342 : m_alloc(a)
343 {
344 assign_storage(p, n, n);
345 }
346
347 template <class T>
349 {
350 deallocate(m_data.p_begin, static_cast<size_type>(m_data.p_storage_end - m_data.p_begin));
351 }
352
353 template <class T>
354 template <allocator A>
355 constexpr buffer_base<T>::buffer_base(buffer_base&& rhs, const A& a)
356 : m_alloc(a)
357 , m_data(std::move(rhs.m_data))
358 {
359 }
360
361 template <class T>
362 constexpr auto buffer_base<T>::get_allocator() noexcept -> allocator_type&
363 {
364 return m_alloc;
365 }
366
367 template <class T>
368 constexpr auto buffer_base<T>::get_allocator() const noexcept -> const allocator_type&
369 {
370 return m_alloc;
371 }
372
373 template <class T>
374 constexpr auto buffer_base<T>::get_data() noexcept -> buffer_data&
375 {
376 return m_data;
377 }
378
379 template <class T>
380 constexpr auto buffer_base<T>::get_data() const noexcept -> const buffer_data&
381 {
382 return m_data;
383 }
384
385 template <class T>
387 {
388 return alloc_traits::allocate(m_alloc, n);
389 }
390
391 template <class T>
393 {
394 alloc_traits::deallocate(m_alloc, p, n);
395 }
396
397 template <class T>
399 {
400 m_data.p_begin = allocate(n);
401 m_data.p_end = m_data.p_begin + n;
402 m_data.p_storage_end = m_data.p_begin + n;
403 }
404
405 template <class T>
407 {
408 SPARROW_ASSERT_TRUE(n <= cap);
409 m_data.p_begin = p;
410 m_data.p_end = p + n;
411 m_data.p_storage_end = p + cap;
412 }
413
414 /*************************
415 * buffer implementation *
416 *************************/
417
418 template <class T>
419 template <allocator A>
420 constexpr buffer<T>::buffer(size_type n, const A& a)
421 : base_type(check_init_length(n, a), a)
422 {
423 get_data().p_end = default_initialize(get_data().p_begin, n, get_allocator());
424 }
425
426 template <class T>
427 template <allocator A>
428 constexpr buffer<T>::buffer(size_type n, const value_type& v, const A& a)
429 : base_type(check_init_length(n, a), a)
430 {
431 get_data().p_end = fill_initialize(get_data().p_begin, n, v, get_allocator());
432 }
433
434 template <class T>
435 template <allocator A>
436 constexpr buffer<T>::buffer(pointer p, size_type n, const A& a)
437 : base_type(p, check_init_length(n, a), a)
438 {
439 }
440
441 template <class T>
442 template <allocator A>
443 constexpr buffer<T>::buffer(std::initializer_list<value_type> init, const A& a)
444 : base_type(check_init_length(init.size(), a), a)
445 {
446 get_data().p_end = copy_initialize(init.begin(), init.end(), get_data().p_begin, get_allocator());
447 }
448
449 template <class T>
450 template <class It, allocator A>
451 constexpr buffer<T>::buffer(It first, It last, const A& a)
452 : base_type(check_init_length(static_cast<size_type>(std::distance(first, last)), a), a)
453 {
454 get_data().p_end = copy_initialize(first, last, get_data().p_begin, get_allocator());
455 }
456
457 template <class T>
458 template <std::ranges::input_range Range, allocator A>
459 requires std::same_as<std::ranges::range_value_t<Range>, T>
460 constexpr buffer<T>::buffer(const Range& range, const A& a)
461 : base_type(check_init_length(static_cast<size_type>(std::ranges::size(range)), a), a)
462 {
463 get_data().p_end = copy_initialize(
464 std::ranges::begin(range),
465 std::ranges::end(range),
466 get_data().p_begin,
467 get_allocator()
468 );
469 }
470
471 template <class T>
473 {
474 destroy(get_data().p_begin, get_data().p_end, get_allocator());
475 }
476
477 template <class T>
479 : base_type(rhs.size(), rhs.get_allocator())
480 {
481 get_data().p_end = copy_initialize(rhs.begin(), rhs.end(), get_data().p_begin, get_allocator());
482 }
483
484 template <class T>
485 template <allocator A>
486 buffer<T>::buffer(const buffer& rhs, const A& a)
487 : base_type(rhs.size(), a)
488 {
489 get_data().p_end = copy_initialize(rhs.begin(), rhs.end(), get_data().p_begin, get_allocator());
490 }
491
492 template <class T>
493 template <allocator A>
494 buffer<T>::buffer(buffer&& rhs, const A& a)
495 : base_type(a)
496 {
497 if (rhs.get_allocator() == get_allocator())
498 {
499 get_data() = std::move(rhs.m_data);
500 }
501 else if (!rhs.empty())
502 {
503 this->create_storage(rhs.size());
504 get_data().p_end = copy_initialize(rhs.begin(), rhs.end(), get_data().p_begin, get_allocator());
505 rhs.clear();
506 }
507 }
508
509 template <class T>
510 constexpr buffer<T>& buffer<T>::operator=(const buffer& rhs)
511 {
512 if (std::addressof(rhs) != this)
513 {
514 // We assume that any_allocator never propagates on assign
515 assign_range_impl(rhs.get_data().p_begin, rhs.get_data().p_end, std::random_access_iterator_tag());
516 }
517 return *this;
518 }
519
520 template <class T>
522 {
523 if (get_allocator() == rhs.get_allocator())
524 {
525 get_data() = std::move(rhs.get_data());
526 }
527 else
528 {
529 assign_range_impl(
530 std::make_move_iterator(rhs.begin()),
531 std::make_move_iterator(rhs.end()),
532 std::random_access_iterator_tag()
533 );
534 rhs.clear();
535 }
536 return *this;
537 }
538
539 template <class T>
540 constexpr buffer<T>& buffer<T>::operator=(std::initializer_list<value_type> init)
541 {
542 assign_range_impl(
543 std::make_move_iterator(init.begin()),
544 std::make_move_iterator(init.end()),
545 std::random_access_iterator_tag()
546 );
547 return *this;
548 }
549
550 template <class T>
552 {
554 return get_data().p_begin[i];
555 }
556
557 template <class T>
559 {
561 return get_data().p_begin[i];
562 }
563
564 template <class T>
565 constexpr auto buffer<T>::front() -> reference
566 {
568 return *(get_data().p_begin);
569 }
570
571 template <class T>
572 constexpr auto buffer<T>::front() const -> const_reference
573 {
575 return *(get_data().p_begin);
576 }
577
578 template <class T>
579 constexpr auto buffer<T>::back() -> reference
580 {
582 return *(get_data().p_end - 1);
583 }
584
585 template <class T>
586 constexpr auto buffer<T>::back() const -> const_reference
587 {
589 return *(get_data().p_end - 1);
590 }
591
592 template <class T>
593 template <class U>
594 constexpr U* buffer<T>::data() noexcept
595 {
596#if defined(__GNUC__)
597# pragma GCC diagnostic push
598# pragma GCC diagnostic ignored "-Wcast-align"
599#endif
600 return reinterpret_cast<U*>(get_data().p_begin);
601#if defined(__GNUC__)
602# pragma GCC diagnostic pop
603#endif
604 }
605
606 template <class T>
607 template <class U>
608 constexpr const U* buffer<T>::data() const noexcept
609 {
610#if defined(__GNUC__)
611# pragma GCC diagnostic push
612# pragma GCC diagnostic ignored "-Wcast-align"
613#endif
614 return reinterpret_cast<U*>(get_data().p_begin);
615#if defined(__GNUC__)
616# pragma GCC diagnostic pop
617#endif
618 }
619
620 template <class T>
621 constexpr auto buffer<T>::begin() noexcept -> iterator
622 {
623 return make_pointer_iterator(get_data().p_begin);
624 }
625
626 template <class T>
627 constexpr auto buffer<T>::end() noexcept -> iterator
628 {
629 return make_pointer_iterator(get_data().p_end);
630 }
631
632 template <class T>
633 constexpr auto buffer<T>::begin() const noexcept -> const_iterator
634 {
635 return cbegin();
636 }
637
638 template <class T>
639 constexpr auto buffer<T>::end() const noexcept -> const_iterator
640 {
641 return cend();
642 }
643
644 template <class T>
645 constexpr auto buffer<T>::cbegin() const noexcept -> const_iterator
646 {
647 return make_pointer_iterator(const_pointer(get_data().p_begin));
648 }
649
650 template <class T>
651 constexpr auto buffer<T>::cend() const noexcept -> const_iterator
652 {
653 return make_pointer_iterator(const_pointer(get_data().p_end));
654 }
655
656 template <class T>
657 constexpr auto buffer<T>::rbegin() noexcept -> reverse_iterator
658 {
659 return reverse_iterator(end());
660 }
661
662 template <class T>
663 constexpr auto buffer<T>::rend() noexcept -> reverse_iterator
664 {
665 return reverse_iterator(begin());
666 }
667
668 template <class T>
669 constexpr auto buffer<T>::rbegin() const noexcept -> const_reverse_iterator
670 {
671 return crbegin();
672 }
673
674 template <class T>
675 constexpr auto buffer<T>::rend() const noexcept -> const_reverse_iterator
676 {
677 return crend();
678 }
679
680 template <class T>
681 constexpr auto buffer<T>::crbegin() const noexcept -> const_reverse_iterator
682 {
683 return const_reverse_iterator(end());
684 }
685
686 template <class T>
687 constexpr auto buffer<T>::crend() const noexcept -> const_reverse_iterator
688 {
690 }
691
692 template <class T>
693 constexpr bool buffer<T>::empty() const noexcept
694 {
695 return get_data().p_begin == get_data().p_end;
696 }
697
698 template <class T>
699 constexpr auto buffer<T>::capacity() const noexcept -> size_type
700 {
701 return static_cast<size_type>(get_data().p_storage_end - get_data().p_begin);
702 }
703
704 template <class T>
705 constexpr auto buffer<T>::size() const noexcept -> size_type
706 {
707 return static_cast<size_type>(get_data().p_end - get_data().p_begin);
708 }
709
710 template <class T>
711 constexpr auto buffer<T>::max_size() const noexcept -> size_type
712 {
713 return max_size_impl(get_allocator());
714 }
715
716 template <class T>
717 constexpr void buffer<T>::reserve(size_type new_cap)
718 {
719 if (new_cap > max_size())
720 {
721 throw std::length_error("buffer::reserve called with new_cap > max_size()");
722 }
723 if (new_cap > capacity())
724 {
725 const size_type old_size = size();
726 pointer tmp = allocate_and_copy(
727 new_cap,
728 std::make_move_iterator(get_data().p_begin),
729 std::make_move_iterator(get_data().p_end)
730 );
731 destroy(get_data().p_begin, get_data().p_end, get_allocator());
732 this->deallocate(
733 get_data().p_begin,
734 static_cast<size_type>(get_data().p_storage_end - get_data().p_begin)
735 );
736 this->assign_storage(tmp, old_size, new_cap);
737 }
738 }
739
740 template <class T>
741 constexpr void buffer<T>::reserve_with_growth_factor(size_type new_cap)
742 {
743 if (new_cap > capacity())
744 {
745 reserve(new_cap * SPARROW_BUFFER_GROWTH_FACTOR);
746 }
747 }
748
749 template <class T>
751 {
752 if (capacity() != size())
753 {
754 buffer(std::make_move_iterator(begin()), std::make_move_iterator(end()), get_allocator()).swap(*this);
755 }
756 }
757
758 template <class T>
759 constexpr void buffer<T>::clear()
760 {
761 erase_at_end(get_data().p_begin);
762 }
763
764 template <class T>
765 constexpr auto buffer<T>::insert(const_iterator pos, const T& value) -> iterator
766 {
767 SPARROW_ASSERT_TRUE(cbegin() <= pos);
768 SPARROW_ASSERT_TRUE(pos <= cend());
769 return emplace(pos, value);
770 }
771
772 template <class T>
773 constexpr auto buffer<T>::insert(const_iterator pos, T&& value) -> iterator
774 {
775 SPARROW_ASSERT_TRUE(cbegin() <= pos);
776 SPARROW_ASSERT_TRUE(pos <= cend());
777 return emplace(pos, std::move(value));
778 }
779
780 template <class T>
781 constexpr auto buffer<T>::insert(const_iterator pos, size_type count, const T& value) -> iterator
782 {
783 SPARROW_ASSERT_TRUE(cbegin() <= pos);
784 SPARROW_ASSERT_TRUE(pos <= cend());
785
786 const difference_type offset = std::distance(cbegin(), pos);
787 if (count != 0)
788 {
789 reserve_with_growth_factor(size() + count);
790 const iterator it = std::next(begin(), offset);
791 std::move_backward(it, end(), std::next(end(), static_cast<difference_type>(count)));
792 std::fill_n(it, count, value);
793 get_data().p_end += count;
794 }
795 return std::next(begin(), offset);
796 }
797
798 template <typename T>
799 struct is_move_iterator : std::false_type
800 {
801 };
802
803 template <typename Iterator>
804 struct is_move_iterator<std::move_iterator<Iterator>> : std::true_type
805 {
806 };
807
808 template <typename T>
810
811 template <class T>
812 template <mpl::iterator_of_type<T> InputIt>
813 constexpr auto buffer<T>::insert(const_iterator pos, InputIt first, InputIt last) -> iterator
814 {
815 SPARROW_ASSERT_TRUE(cbegin() <= pos && pos <= cend());
816 const difference_type num_elements = std::distance(first, last);
817 const size_type new_size = size() + static_cast<size_type>(num_elements);
818 const difference_type offset = std::distance(cbegin(), pos);
819 const size_type old_size = size();
820 reserve_with_growth_factor(new_size);
821 resize(new_size);
822 const iterator new_pos = std::next(begin(), offset);
823 const iterator end_it = std::next(begin(), static_cast<difference_type>(old_size));
824 std::move_backward(new_pos, end_it, end());
825 if constexpr (is_move_iterator_v<InputIt>)
826 {
827 std::uninitialized_move(first, last, new_pos);
828 }
829 else
830 {
831 std::uninitialized_copy(first, last, new_pos);
832 }
833 return new_pos;
834 }
835
836 template <class T>
837 template <std::ranges::input_range R>
838 requires std::same_as<std::ranges::range_value_t<R>, T>
839 constexpr auto buffer<T>::insert(const_iterator pos, R&& range) -> iterator
840 {
841 SPARROW_ASSERT_TRUE(cbegin() <= pos);
842 SPARROW_ASSERT_TRUE(pos <= cend());
843 return insert(pos, std::ranges::begin(range), std::ranges::end(range));
844 }
845
846 template <class T>
847 constexpr auto buffer<T>::insert(const_iterator pos, std::initializer_list<T> ilist) -> iterator
848 {
849 SPARROW_ASSERT_TRUE(cbegin() <= pos);
850 SPARROW_ASSERT_TRUE(pos <= cend());
851 return insert(pos, ilist.begin(), ilist.end());
852 }
853
854 template <class T>
855 template <class... Args>
857 {
858 SPARROW_ASSERT_TRUE(cbegin() <= pos);
859 SPARROW_ASSERT_TRUE(pos <= cend());
860 const difference_type offset = std::distance(cbegin(), pos);
861 reserve_with_growth_factor(size() + 1);
862 pointer p = get_data().p_begin + offset;
863 if (p != get_data().p_end)
864 {
865 alloc_traits::construct(get_allocator(), get_data().p_end, std::move(*(get_data().p_end - 1)));
866 std::move_backward(p, get_data().p_end - 1, get_data().p_end);
867 alloc_traits::construct(get_allocator(), p, std::forward<Args>(args)...);
868 }
869 else
870 {
871 alloc_traits::construct(get_allocator(), get_data().p_end, std::forward<Args>(args)...);
872 }
873 ++get_data().p_end;
874 return iterator(p);
875 }
876
877 template <class T>
879 {
880 SPARROW_ASSERT_TRUE(cbegin() <= pos);
881 SPARROW_ASSERT_TRUE(pos < cend());
882 return erase(pos, pos + 1);
883 }
884
885 template <class T>
887 {
888 SPARROW_ASSERT_TRUE(first < last);
889 SPARROW_ASSERT_TRUE(cbegin() <= first);
890 SPARROW_ASSERT_TRUE(last <= cend());
891 const difference_type offset = std::distance(cbegin(), first);
892 const difference_type len = std::distance(first, last);
893 pointer p = get_data().p_begin + offset;
894 erase_at_end(std::move(p + len, get_data().p_end, p));
895 return iterator(p);
896 }
897
898 template <class T>
899 constexpr void buffer<T>::push_back(const T& value)
900 {
901 emplace(cend(), value);
902 }
903
904 template <class T>
905 constexpr void buffer<T>::push_back(T&& value)
906 {
907 emplace(cend(), std::move(value));
908 }
909
910 template <class T>
911 constexpr void buffer<T>::pop_back()
912 {
914 destroy(get_allocator(), get_data().p_end - 1);
915 --get_data().p_end;
916 }
917
918 template <class T>
919 constexpr void buffer<T>::resize(size_type new_size)
920 {
921 resize_impl(
922 new_size,
923 [this](size_type nb_init)
924 {
925 get_data().p_end = default_initialize(get_data().p_end, nb_init, get_allocator());
926 }
927 );
928 }
929
930 template <class T>
931 constexpr void buffer<T>::resize(size_type new_size, const value_type& value)
932 {
933 resize_impl(
934 new_size,
935 [this, &value](size_type nb_init)
936 {
937 get_data().p_end = fill_initialize(get_data().p_end, nb_init, value, get_allocator());
938 }
939 );
940 }
941
942 template <class T>
943 constexpr void buffer<T>::swap(buffer& rhs) noexcept
944 {
945 std::swap(this->get_data(), rhs.get_data());
946 }
947
948 template <class T>
949 template <class F>
950 constexpr void buffer<T>::resize_impl(size_type new_size, F&& initializer)
951 {
952 if (new_size > size())
953 {
954 const std::size_t nb_init = new_size - size();
955 if (new_size <= capacity())
956 {
957 initializer(nb_init);
958 }
959 else
960 {
961 reserve(new_size);
962 initializer(nb_init);
963 }
964 }
965 else if (new_size < size())
966 {
967 erase_at_end(get_data().p_begin + new_size);
968 }
969 }
970
971 template <class T>
972 template <class It>
973 constexpr void buffer<T>::assign_range_impl(It first, It last, std::forward_iterator_tag)
974 {
975 const size_type sz = size();
976 const size_type len = static_cast<size_type>(std::distance(first, last));
977 if (len > capacity())
978 {
979 check_init_length(len, get_allocator());
980 pointer p = allocate_and_copy(len, first, last);
981 destroy(get_data().p_begin, get_data().p_end, get_allocator());
982 this->deallocate(get_data().p_begin, capacity());
983 this->assign_storage(p, len, len);
984 }
985 else if (sz >= len)
986 {
987 pointer p = std::copy(first, last, get_data().p_begin);
988 erase_at_end(p);
989 }
990 else
991 {
992 It mid = first;
993 std::advance(mid, sz);
994 std::copy(first, mid, get_data().p_begin);
995 get_data().p_end = copy_initialize(mid, last, get_data().p_end, get_allocator());
996 }
997 }
998
999 template <class T>
1000 constexpr void buffer<T>::erase_at_end(pointer p)
1001 {
1002 destroy(p, get_data().p_end, get_allocator());
1003 get_data().p_end = p;
1004 }
1005
1006 template <class T>
1007 template <class It>
1008 constexpr auto buffer<T>::allocate_and_copy(size_type n, It first, It last) -> pointer
1009 {
1010 pointer p = this->allocate(n);
1011 try
1012 {
1013 copy_initialize(first, last, p, get_allocator());
1014 }
1015 catch (...)
1016 {
1017 this->deallocate(p, n);
1018 throw;
1019 }
1020 return p;
1021 }
1022
1023 template <class T>
1024 constexpr auto buffer<T>::check_init_length(size_type n, const allocator_type& a) -> size_type
1025 {
1026 if (n > max_size_impl(a))
1027 {
1028 throw std::length_error("cannot create buffer larger than max_size()");
1029 }
1030 return n;
1031 }
1032
1033 template <class T>
1034 constexpr auto buffer<T>::max_size_impl(const allocator_type& a) noexcept -> size_type
1035 {
1036 const size_type diff_max = static_cast<size_type>(std::numeric_limits<difference_type>::max());
1037 const size_type alloc_max = std::allocator_traits<allocator_type>::max_size(a);
1038 return (std::min)(diff_max, alloc_max);
1039 }
1040
1041 template <class T>
1042 constexpr auto buffer<T>::default_initialize(pointer begin, size_type n, allocator_type& a) -> pointer
1043 {
1044 pointer current = begin;
1045 for (; n > 0; --n, ++current)
1046 {
1047 alloc_traits::construct(a, current);
1048 }
1049 return current;
1050 }
1051
1052 template <class T>
1053 constexpr auto
1054 buffer<T>::fill_initialize(pointer begin, size_type n, const value_type& v, allocator_type& a) -> pointer
1055 {
1056 pointer current = begin;
1057 for (; n > 0; --n, ++current)
1058 {
1059 alloc_traits::construct(a, current, v);
1060 }
1061 return current;
1062 }
1063
1064 template <class T>
1065 template <class It>
1066 constexpr auto buffer<T>::copy_initialize(It first, It last, pointer begin, allocator_type& a) -> pointer
1067 {
1068 pointer current = begin;
1069 for (; first != last; ++first, ++current)
1070 {
1071 alloc_traits::construct(a, current, *first);
1072 }
1073 return current;
1074 }
1075
1076 template <class T>
1077 constexpr void buffer<T>::destroy(pointer first, pointer last, allocator_type& a)
1078 {
1079 for (; first != last; ++first)
1080 {
1081 alloc_traits::destroy(a, first);
1082 }
1083 }
1084
1085 template <class T>
1086 constexpr bool operator==(const buffer<T>& lhs, const buffer<T>& rhs) noexcept
1087 {
1088 return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());
1089 }
1090}
#define SPARROW_BUFFER_GROWTH_FACTOR
Definition buffer.hpp:31
constexpr pointer allocate(size_type n)
Definition buffer.hpp:386
typename alloc_traits::pointer pointer
Definition buffer.hpp:50
any_allocator< T > allocator_type
Definition buffer.hpp:48
constexpr void create_storage(size_type n)
Definition buffer.hpp:398
constexpr void deallocate(pointer p, size_type n)
Definition buffer.hpp:392
constexpr buffer_data & get_data() noexcept
Definition buffer.hpp:374
constexpr void assign_storage(pointer p, size_type n, size_type cap)
Definition buffer.hpp:406
std::allocator_traits< allocator_type > alloc_traits
Definition buffer.hpp:49
typename alloc_traits::size_type size_type
Definition buffer.hpp:51
constexpr allocator_type & get_allocator() noexcept
Definition buffer.hpp:362
Object that owns a piece of contiguous memory.
Definition buffer.hpp:109
std::reverse_iterator< iterator > reverse_iterator
Definition buffer.hpp:130
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition buffer.hpp:131
typename alloc_traits::difference_type difference_type
Definition buffer.hpp:127
typename alloc_traits::size_type size_type
Definition buffer.hpp:126
constexpr void resize(size_type new_size)
Definition buffer.hpp:919
constexpr size_type max_size() const noexcept
Definition buffer.hpp:711
typename alloc_traits::const_pointer const_pointer
Definition buffer.hpp:125
constexpr iterator insert(const_iterator pos, const T &value)
Definition buffer.hpp:765
constexpr void shrink_to_fit()
Definition buffer.hpp:750
constexpr const_reverse_iterator crend() const noexcept
Definition buffer.hpp:687
constexpr U * data() noexcept
Definition buffer.hpp:594
pointer_iterator< const_pointer > const_iterator
Definition buffer.hpp:129
constexpr iterator erase(const_iterator pos)
Definition buffer.hpp:878
constexpr reference back()
Definition buffer.hpp:579
typename base_type::allocator_type allocator_type
Definition buffer.hpp:120
constexpr const_iterator cbegin() const noexcept
Definition buffer.hpp:645
const value_type & const_reference
Definition buffer.hpp:123
typename alloc_traits::pointer pointer
Definition buffer.hpp:124
constexpr buffer(const A &a)
Definition buffer.hpp:137
pointer_iterator< pointer > iterator
Definition buffer.hpp:128
constexpr const_iterator cend() const noexcept
Definition buffer.hpp:651
constexpr iterator begin() noexcept
Definition buffer.hpp:621
constexpr iterator end() noexcept
Definition buffer.hpp:627
constexpr reverse_iterator rend() noexcept
Definition buffer.hpp:663
buffer()=default
constexpr void clear()
Definition buffer.hpp:759
constexpr size_type size() const noexcept
Definition buffer.hpp:705
constexpr reference operator[](size_type i)
Definition buffer.hpp:551
constexpr void reserve(size_type new_cap)
Definition buffer.hpp:717
value_type & reference
Definition buffer.hpp:122
buffer(buffer &&rhs) noexcept=default
constexpr void pop_back()
Definition buffer.hpp:911
constexpr buffer & operator=(const buffer &rhs)
Definition buffer.hpp:510
constexpr void swap(buffer &rhs) noexcept
Definition buffer.hpp:943
constexpr void push_back(const T &value)
Definition buffer.hpp:899
constexpr const_reverse_iterator crbegin() const noexcept
Definition buffer.hpp:681
constexpr iterator emplace(const_iterator pos, Args &&... args)
Definition buffer.hpp:856
constexpr reference front()
Definition buffer.hpp:565
constexpr reverse_iterator rbegin() noexcept
Definition buffer.hpp:657
constexpr size_type capacity() const noexcept
Definition buffer.hpp:699
constexpr bool empty() const noexcept
Definition buffer.hpp:693
#define SPARROW_ASSERT_TRUE(expr__)
#define SPARROW_ASSERT_FALSE(expr__)
constexpr std::size_t size(typelist< T... >={})
Definition mp_utils.hpp:107
pointer_iterator< T * > make_pointer_iterator(T *t)
Definition iterator.hpp:497
SPARROW_API bool operator==(const array &lhs, const array &rhs)
Compares the content of two arrays.
constexpr bool is_move_iterator_v
Definition buffer.hpp:809
constexpr buffer_data & operator=(buffer_data &&) noexcept
Definition buffer.hpp:316