summaryrefslogtreecommitdiff
path: root/src/common/scratch_buffer.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/scratch_buffer.h')
-rw-r--r--src/common/scratch_buffer.h46
1 files changed, 26 insertions, 20 deletions
diff --git a/src/common/scratch_buffer.h b/src/common/scratch_buffer.h
index 6fe907953..d5961b020 100644
--- a/src/common/scratch_buffer.h
+++ b/src/common/scratch_buffer.h
@@ -5,7 +5,6 @@
5 5
6#include <iterator> 6#include <iterator>
7 7
8#include "common/concepts.h"
9#include "common/make_unique_for_overwrite.h" 8#include "common/make_unique_for_overwrite.h"
10 9
11namespace Common { 10namespace Common {
@@ -19,15 +18,22 @@ namespace Common {
19template <typename T> 18template <typename T>
20class ScratchBuffer { 19class ScratchBuffer {
21public: 20public:
22 using iterator = T*;
23 using const_iterator = const T*;
24 using value_type = T;
25 using element_type = T; 21 using element_type = T;
26 using iterator_category = std::contiguous_iterator_tag; 22 using value_type = T;
23 using size_type = size_t;
24 using difference_type = std::ptrdiff_t;
25 using pointer = T*;
26 using const_pointer = const T*;
27 using reference = T&;
28 using const_reference = const T&;
29 using iterator = pointer;
30 using const_iterator = const_pointer;
31 using iterator_category = std::random_access_iterator_tag;
32 using iterator_concept = std::contiguous_iterator_tag;
27 33
28 ScratchBuffer() = default; 34 ScratchBuffer() = default;
29 35
30 explicit ScratchBuffer(size_t initial_capacity) 36 explicit ScratchBuffer(size_type initial_capacity)
31 : last_requested_size{initial_capacity}, buffer_capacity{initial_capacity}, 37 : last_requested_size{initial_capacity}, buffer_capacity{initial_capacity},
32 buffer{Common::make_unique_for_overwrite<T[]>(initial_capacity)} {} 38 buffer{Common::make_unique_for_overwrite<T[]>(initial_capacity)} {}
33 39
@@ -39,7 +45,7 @@ public:
39 45
40 /// This will only grow the buffer's capacity if size is greater than the current capacity. 46 /// This will only grow the buffer's capacity if size is greater than the current capacity.
41 /// The previously held data will remain intact. 47 /// The previously held data will remain intact.
42 void resize(size_t size) { 48 void resize(size_type size) {
43 if (size > buffer_capacity) { 49 if (size > buffer_capacity) {
44 auto new_buffer = Common::make_unique_for_overwrite<T[]>(size); 50 auto new_buffer = Common::make_unique_for_overwrite<T[]>(size);
45 std::move(buffer.get(), buffer.get() + buffer_capacity, new_buffer.get()); 51 std::move(buffer.get(), buffer.get() + buffer_capacity, new_buffer.get());
@@ -51,7 +57,7 @@ public:
51 57
52 /// This will only grow the buffer's capacity if size is greater than the current capacity. 58 /// This will only grow the buffer's capacity if size is greater than the current capacity.
53 /// The previously held data will be destroyed if a reallocation occurs. 59 /// The previously held data will be destroyed if a reallocation occurs.
54 void resize_destructive(size_t size) { 60 void resize_destructive(size_type size) {
55 if (size > buffer_capacity) { 61 if (size > buffer_capacity) {
56 buffer_capacity = size; 62 buffer_capacity = size;
57 buffer = Common::make_unique_for_overwrite<T[]>(buffer_capacity); 63 buffer = Common::make_unique_for_overwrite<T[]>(buffer_capacity);
@@ -59,43 +65,43 @@ public:
59 last_requested_size = size; 65 last_requested_size = size;
60 } 66 }
61 67
62 [[nodiscard]] T* data() noexcept { 68 [[nodiscard]] pointer data() noexcept {
63 return buffer.get(); 69 return buffer.get();
64 } 70 }
65 71
66 [[nodiscard]] const T* data() const noexcept { 72 [[nodiscard]] const_pointer data() const noexcept {
67 return buffer.get(); 73 return buffer.get();
68 } 74 }
69 75
70 [[nodiscard]] T* begin() noexcept { 76 [[nodiscard]] iterator begin() noexcept {
71 return data(); 77 return data();
72 } 78 }
73 79
74 [[nodiscard]] const T* begin() const noexcept { 80 [[nodiscard]] const_iterator begin() const noexcept {
75 return data(); 81 return data();
76 } 82 }
77 83
78 [[nodiscard]] T* end() noexcept { 84 [[nodiscard]] iterator end() noexcept {
79 return data() + last_requested_size; 85 return data() + last_requested_size;
80 } 86 }
81 87
82 [[nodiscard]] const T* end() const noexcept { 88 [[nodiscard]] const_iterator end() const noexcept {
83 return data() + last_requested_size; 89 return data() + last_requested_size;
84 } 90 }
85 91
86 [[nodiscard]] T& operator[](size_t i) { 92 [[nodiscard]] reference operator[](size_type i) {
87 return buffer[i]; 93 return buffer[i];
88 } 94 }
89 95
90 [[nodiscard]] const T& operator[](size_t i) const { 96 [[nodiscard]] const_reference operator[](size_type i) const {
91 return buffer[i]; 97 return buffer[i];
92 } 98 }
93 99
94 [[nodiscard]] size_t size() const noexcept { 100 [[nodiscard]] size_type size() const noexcept {
95 return last_requested_size; 101 return last_requested_size;
96 } 102 }
97 103
98 [[nodiscard]] size_t capacity() const noexcept { 104 [[nodiscard]] size_type capacity() const noexcept {
99 return buffer_capacity; 105 return buffer_capacity;
100 } 106 }
101 107
@@ -106,8 +112,8 @@ public:
106 } 112 }
107 113
108private: 114private:
109 size_t last_requested_size{}; 115 size_type last_requested_size{};
110 size_t buffer_capacity{}; 116 size_type buffer_capacity{};
111 std::unique_ptr<T[]> buffer{}; 117 std::unique_ptr<T[]> buffer{};
112}; 118};
113 119