GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Table.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_DATA_TABLE_HPP
5#define GREM_CORE_DATA_TABLE_HPP
6
8
13
14#include <algorithm> // std::min, std::copy, std::move, std::equal, std::lexicographical_compare
15#include <cstddef> // std::size_t, std::ptrdiff_t
16#include <iterator> // std::iterator_traits, std::random_access_iterator_tag, std::reverse_iterator
17#include <limits> // std::numeric_limits
18#include <memory> // std::allocator, std::allocator_traits, std::to_address
19#include <memory_resource> // std::pmr::polymorphic_allocator
20#include <stdexcept> // std::out_of_range, std::length_error
21#include <type_traits> // std::is_..._v, std::remove_..._t
22#include <utility> // std::move, std::forward, std::exchange, std::swap, std::tuple_element_t, std::as_const
23
24namespace grem {
25
26template <typename Row, typename Allocator = std::allocator<Row>>
27class Table;
28
29namespace detail {
30
31template <typename Row, typename Allocator>
32class TableIterator;
33
34template <typename... Ts, typename Allocator>
35class TableIterator<Tuple<Ts...>, Allocator> {
36public:
37 static_assert(sizeof...(Ts) > 0);
38
39 using difference_type = std::ptrdiff_t;
40 using value_type = Tuple<Ts...>;
41 using reference = Tuple<Ts&...>;
42 using iterator_category = std::random_access_iterator_tag;
43
44 struct pointer {
45 reference ref;
46
47 [[nodiscard]] constexpr reference* operator->() noexcept {
48 return &ref;
49 }
50 };
51
52 TableIterator() = default;
53
54 constexpr TableIterator(Table<value_type, Allocator>* table, std::size_t rowIndex) noexcept
55 : table(table)
56 , rowIndex(rowIndex) {}
57
58 constexpr operator TableIterator<const value_type, Allocator>() const noexcept {
59 return TableIterator<const value_type, Allocator>{table, rowIndex};
60 }
61
62 [[nodiscard]] constexpr reference operator*() const {
63 return [&]<std::size_t... Indices>(std::index_sequence<Indices...>) {
64 return grem::tie(*(get<Indices>(table->columns) + rowIndex)...);
65 }(std::make_index_sequence<sizeof...(Ts)>{});
66 }
67
68 [[nodiscard]] constexpr pointer operator->() const {
69 return pointer{**this};
70 }
71
72 [[nodiscard]] constexpr reference operator[](difference_type n) const {
73 return *(*this + n);
74 }
75
76 constexpr TableIterator& operator++() {
77 ++rowIndex;
78 return *this;
79 }
80
81 constexpr TableIterator& operator--() {
82 --rowIndex;
83 return *this;
84 }
85
86 constexpr TableIterator operator++(int) {
87 return TableIterator{table, rowIndex++};
88 }
89
90 constexpr TableIterator operator--(int) {
91 return TableIterator{table, rowIndex--};
92 }
93
94 constexpr TableIterator& operator+=(difference_type n) {
95 rowIndex += static_cast<std::size_t>(n);
96 return *this;
97 }
98
99 constexpr TableIterator& operator-=(difference_type n) {
100 rowIndex -= static_cast<std::size_t>(n);
101 return *this;
102 }
103
104 [[nodiscard]] friend constexpr TableIterator operator+(TableIterator a, difference_type b) {
105 return TableIterator{a.table, a.rowIndex + static_cast<std::size_t>(b)};
106 }
107
108 [[nodiscard]] friend constexpr TableIterator operator+(difference_type a, TableIterator b) {
109 return TableIterator{b.table, static_cast<std::size_t>(a) + b.rowIndex};
110 }
111
112 [[nodiscard]] friend constexpr TableIterator operator-(TableIterator a, difference_type b) {
113 return TableIterator{a.table, a.rowIndex - static_cast<std::size_t>(b)};
114 }
115
116 [[nodiscard]] friend constexpr difference_type operator-(TableIterator a, TableIterator b) {
117 GREM_ASSERT(a.table == b.table);
118 return static_cast<difference_type>(a.rowIndex - b.rowIndex);
119 }
120
121 [[nodiscard]] friend constexpr bool operator==(TableIterator a, TableIterator b) {
122 GREM_ASSERT(a.table == b.table);
123 return a.rowIndex == b.rowIndex;
124 }
125
126 [[nodiscard]] friend constexpr auto operator<=>(TableIterator a, TableIterator b) {
127 GREM_ASSERT(a.table == b.table);
128 return a.rowIndex <=> b.rowIndex;
129 }
130
131private:
132 Table<value_type, Allocator>* table;
133 std::size_t rowIndex;
134};
135
136template <typename... Ts, typename Allocator>
137class TableIterator<const Tuple<Ts...>, Allocator> {
138public:
139 using difference_type = std::ptrdiff_t;
140 using value_type = Tuple<Ts...>;
141 using reference = Tuple<const Ts&...>;
142 using iterator_category = std::random_access_iterator_tag;
143
144 struct pointer {
145 reference ref;
146
147 [[nodiscard]] constexpr reference* operator->() noexcept {
148 return &ref;
149 }
150 };
151
152 TableIterator() = default;
153
154 constexpr TableIterator(const Table<value_type, Allocator>* table, std::size_t rowIndex) noexcept
155 : table(table)
156 , rowIndex(rowIndex) {}
157
158 [[nodiscard]] constexpr reference operator*() const {
159 return [&]<std::size_t... Indices>(std::index_sequence<Indices...>) {
160 return grem::tie(std::as_const(*(get<Indices>(table->columns) + rowIndex))...);
161 }(std::make_index_sequence<sizeof...(Ts)>{});
162 }
163
164 [[nodiscard]] constexpr pointer operator->() const {
165 return pointer{**this};
166 }
167
168 [[nodiscard]] constexpr reference operator[](difference_type n) const {
169 return *(*this + n);
170 }
171
172 constexpr TableIterator& operator++() {
173 ++rowIndex;
174 return *this;
175 }
176
177 constexpr TableIterator& operator--() {
178 --rowIndex;
179 return *this;
180 }
181
182 constexpr TableIterator operator++(int) {
183 return TableIterator{table, rowIndex++};
184 }
185
186 constexpr TableIterator operator--(int) {
187 return TableIterator{table, rowIndex--};
188 }
189
190 constexpr TableIterator& operator+=(difference_type n) {
191 rowIndex += static_cast<std::size_t>(n);
192 return *this;
193 }
194
195 constexpr TableIterator& operator-=(difference_type n) {
196 rowIndex -= static_cast<std::size_t>(n);
197 return *this;
198 }
199
200 [[nodiscard]] friend constexpr TableIterator operator+(TableIterator a, difference_type b) {
201 return TableIterator{a.table, a.rowIndex + static_cast<std::size_t>(b)};
202 }
203
204 [[nodiscard]] friend constexpr TableIterator operator+(difference_type a, TableIterator b) {
205 return TableIterator{b.table, static_cast<std::size_t>(a) + b.rowIndex};
206 }
207
208 [[nodiscard]] friend constexpr TableIterator operator-(TableIterator a, difference_type b) {
209 return TableIterator{a.table, a.rowIndex - static_cast<std::size_t>(b)};
210 }
211
212 [[nodiscard]] friend constexpr difference_type operator-(TableIterator a, TableIterator b) {
213 GREM_ASSERT(a.table == b.table);
214 return static_cast<difference_type>(a.rowIndex - b.rowIndex);
215 }
216
217 [[nodiscard]] friend constexpr bool operator==(TableIterator a, TableIterator b) {
218 GREM_ASSERT(a.table == b.table);
219 return a.rowIndex == b.rowIndex;
220 }
221
222 [[nodiscard]] friend constexpr auto operator<=>(TableIterator a, TableIterator b) {
223 GREM_ASSERT(a.table == b.table);
224 return a.rowIndex <=> b.rowIndex;
225 }
226
227private:
228 const Table<value_type, Allocator>* table;
229 std::size_t rowIndex;
230};
231
232} // namespace detail
233
234template <typename... Ts, typename Allocator>
235class Table<Tuple<Ts...>, Allocator> {
236public:
237 using value_type = Tuple<Ts...>;
238 using allocator_type = Allocator;
239 using size_type = std::size_t;
240 using difference_type = std::ptrdiff_t;
241 using reference = Tuple<Ts&...>;
242 using const_reference = Tuple<const Ts&...>;
243 using iterator = detail::TableIterator<value_type, Allocator>;
244 using const_iterator = detail::TableIterator<const value_type, Allocator>;
245 using pointer = typename iterator::pointer;
246 using const_pointer = typename const_iterator::pointer;
247 using reverse_iterator = std::reverse_iterator<iterator>;
248 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
249
250 constexpr Table() = default;
251
252 constexpr explicit Table(const Allocator& allocator) noexcept
253 : allocator(allocator) {}
254
255 constexpr Table(const Table& other)
256 : Table(other, std::allocator_traits<Allocator>::select_on_container_copy_construction(other.get_allocator())) {}
257
258 constexpr Table(const Table& other, const Allocator& allocator)
259 : Table(allocator) {
260 assign(other);
261 }
262
263 constexpr Table(Table&& other) noexcept
264 : rowCount(std::exchange(other.rowCount, size_type{0}))
265 , rowCapacity(std::exchange(other.rowCapacity, size_type{0}))
266 , columns(std::exchange(other.columns, {}))
267 , allocator(other.get_allocator()) {}
268
269 constexpr Table(Table&& other, const Allocator& allocator) // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
270 : Table(allocator) {
271 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
272 if (allocator != other.get_allocator()) {
273 assign(std::move(other));
274 other.clear();
275 return;
276 }
277 }
278 rowCount = std::exchange(other.rowCount, size_type{0});
279 rowCapacity = std::exchange(other.rowCapacity, size_type{0});
280 columns = std::exchange(other.columns, {});
281 }
282
283 constexpr ~Table() {
284 reset();
285 }
286
287 constexpr Table& operator=(const Table& other) {
288 if (this == &other) {
289 return *this;
290 }
291 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_copy_assignment::value) {
292 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
293 if (allocator != other.get_allocator()) {
294 reset();
295 }
296 }
297 allocator = other.get_allocator();
298 }
299 assign(other);
300 return *this;
301 }
302
303 constexpr Table& operator=(Table&& other) noexcept(
304 std::allocator_traits<Allocator>::is_always_equal::value) { // NOLINT(cppcoreguidelines-noexcept-move-operations, performance-noexcept-move-constructor)
305 if (this == &other) {
306 return *this;
307 }
308 if constexpr (!std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value) {
309 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
310 if (allocator != other.get_allocator()) {
311 assign(std::move(other));
312 other.clear();
313 return *this;
314 }
315 }
316 }
317 reset();
318 rowCount = std::exchange(other.rowCount, size_type{0});
319 rowCapacity = std::exchange(other.rowCapacity, size_type{0});
320 columns = std::exchange(other.columns, {});
321 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value) {
322 allocator = other.get_allocator();
323 }
324 return *this;
325 }
326
327 [[nodiscard]] constexpr allocator_type get_allocator() const noexcept {
328 return allocator;
329 }
330
331 template <typename T>
332 [[nodiscard]] constexpr T& get(size_type rowIndex) {
333 using ColumnPointer = typename std::allocator_traits<typename std::allocator_traits<Allocator>::template rebind_alloc<T>>::pointer;
334 return columns.template get<ColumnPointer>()[rowIndex];
335 }
336
337 template <typename T>
338 [[nodiscard]] constexpr const T& get(size_type rowIndex) const {
339 using ColumnPointer = typename std::allocator_traits<typename std::allocator_traits<Allocator>::template rebind_alloc<T>>::pointer;
340 return columns.template get<ColumnPointer>()[rowIndex];
341 }
342
343 template <std::size_t ColumnIndex>
344 [[nodiscard]] constexpr decltype(auto) get(size_type rowIndex) {
345 return columns.template get<ColumnIndex>()[rowIndex];
346 }
347
348 template <std::size_t ColumnIndex>
349 [[nodiscard]] constexpr decltype(auto) get(size_type rowIndex) const {
350 return columns.template get<ColumnIndex>()[rowIndex];
351 }
352
353 template <typename T>
354 [[nodiscard]] constexpr Span<T> column() {
355 using ColumnPointer = typename std::allocator_traits<typename std::allocator_traits<Allocator>::template rebind_alloc<T>>::pointer;
356 return Span<T>{columns.template get<ColumnPointer>(), rowCount};
357 }
358
359 template <typename T>
360 [[nodiscard]] constexpr Span<const T> column() const {
361 using ColumnPointer = typename std::allocator_traits<typename std::allocator_traits<Allocator>::template rebind_alloc<T>>::pointer;
362 return Span<const T>{columns.template get<ColumnPointer>(), rowCount};
363 }
364
365 template <std::size_t ColumnIndex>
366 [[nodiscard]] constexpr auto column() {
367 using T = std::remove_cvref_t<decltype(*columns.template get<ColumnIndex>())>;
368 return Span<T>{columns.template get<ColumnIndex>(), rowCount};
369 }
370
371 template <std::size_t ColumnIndex>
372 [[nodiscard]] constexpr auto column() const {
373 using T = std::remove_cvref_t<decltype(*columns.template get<ColumnIndex>())>;
374 return Span<const T>{columns.template get<ColumnIndex>(), rowCount};
375 }
376
377 [[nodiscard]] constexpr reference at(size_type pos) {
378 if (pos >= size()) {
379 throw std::out_of_range{"pos >= size()"};
380 }
381 return (*this)[pos];
382 }
383
384 [[nodiscard]] constexpr const_reference at(size_type pos) const {
385 if (pos >= size()) {
386 throw std::out_of_range{"pos >= size()"};
387 }
388 return (*this)[pos];
389 }
390
391 [[nodiscard]] constexpr reference operator[](size_type pos) {
392 GREM_ASSERT(pos < size());
393 return *(begin() + static_cast<difference_type>(pos));
394 }
395
396 [[nodiscard]] constexpr const_reference operator[](size_type pos) const {
397 GREM_ASSERT(pos < size());
398 return *(begin() + static_cast<difference_type>(pos));
399 }
400
401 [[nodiscard]] constexpr reference front() {
402 GREM_ASSERT(!empty());
403 return *begin();
404 }
405
406 [[nodiscard]] constexpr const_reference front() const {
407 GREM_ASSERT(!empty());
408 return *begin();
409 }
410
411 [[nodiscard]] constexpr reference back() {
412 GREM_ASSERT(!empty());
413 return *(end() - 1);
414 }
415
416 [[nodiscard]] constexpr const_reference back() const {
417 GREM_ASSERT(!empty());
418 return *(end() - 1);
419 }
420
421 [[nodiscard]] constexpr iterator begin() noexcept {
422 return iterator{this, 0};
423 }
424
425 [[nodiscard]] constexpr const_iterator begin() const noexcept {
426 return const_iterator{this, 0};
427 }
428
429 [[nodiscard]] constexpr const_iterator cbegin() const noexcept {
430 return begin();
431 }
432
433 [[nodiscard]] constexpr iterator end() noexcept {
434 return iterator{this, rowCount};
435 }
436
437 [[nodiscard]] constexpr const_iterator end() const noexcept {
438 return const_iterator{this, rowCount};
439 }
440
441 [[nodiscard]] constexpr const_iterator cend() const noexcept {
442 return end();
443 }
444
445 [[nodiscard]] constexpr reverse_iterator rbegin() noexcept {
446 return reverse_iterator{end()};
447 }
448
449 [[nodiscard]] constexpr const_reverse_iterator rbegin() const noexcept {
450 return const_reverse_iterator{end()};
451 }
452
453 [[nodiscard]] constexpr const_reverse_iterator crbegin() const noexcept {
454 return rbegin();
455 }
456
457 [[nodiscard]] constexpr reverse_iterator rend() noexcept {
458 return reverse_iterator{begin()};
459 }
460
461 [[nodiscard]] constexpr const_reverse_iterator rend() const noexcept {
463 }
464
465 [[nodiscard]] constexpr const_reverse_iterator crend() const noexcept {
466 return rend();
467 }
468
469 [[nodiscard]] constexpr bool empty() const noexcept {
470 return size() == 0;
471 }
472
473 [[nodiscard]] constexpr size_type size() const noexcept {
474 return rowCount;
475 }
476
477 [[nodiscard]] constexpr size_type max_size() const noexcept {
478 return std::min({
479 static_cast<size_type>(std::numeric_limits<difference_type>::max()),
480 static_cast<size_type>(std::numeric_limits<size_type>::max() / std::max({sizeof(Ts)...})),
481 static_cast<size_type>(std::allocator_traits<Allocator>::max_size(allocator)),
482 });
483 }
484
485 constexpr void reserve(size_type newRowCapacity) {
486 if (newRowCapacity <= capacity()) {
487 return;
488 }
489 if (newRowCapacity > max_size()) {
490 throw std::length_error{"Maximum capacity exceeded."};
491 }
492 Table newTable{allocator};
493 newTable.rowCapacity = newRowCapacity;
494 meta::forEachIndex<sizeof...(Ts)>([&](auto columnIndex) -> void {
495 using T = std::tuple_element_t<columnIndex, value_type>;
496 using ColumnAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<T>;
497 ColumnAllocator columnAllocator = allocator;
498 newTable.columns.template get<columnIndex>() = std::allocator_traits<ColumnAllocator>::allocate(columnAllocator, newRowCapacity);
499 });
500 newTable.assign(std::move(*this));
501 *this = std::move(newTable);
502 }
503
504 [[nodiscard]] constexpr size_type capacity() const noexcept {
505 return rowCapacity;
506 }
507
508 constexpr void shrink_to_fit() {
509 if (size() < capacity()) {
510 *this = Table{*this};
511 }
512 }
513
514 constexpr void clear() noexcept {
515 meta::forEachIndex<sizeof...(Ts)>([&](auto columnIndex) -> void {
516 using T = std::tuple_element_t<columnIndex, value_type>;
517 if constexpr (!std::is_trivially_destructible_v<T>) {
518 using ColumnAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<T>;
519 ColumnAllocator columnAllocator = allocator;
520 for (size_type rowIndex = rowCount; rowIndex-- > 0;) {
521 std::allocator_traits<ColumnAllocator>::destroy(columnAllocator, std::to_address(columns.template get<columnIndex>()) + rowIndex);
522 }
523 }
524 });
525 rowCount = 0;
526 }
527
528 template <typename... Us>
529 constexpr void push_back(const Tuple<Us...>& value) requires(sizeof...(Us) == sizeof...(Ts)) {
530 if (size() >= max_size()) {
531 throw std::length_error{"size() >= max_size()"};
532 }
533 if (size() >= capacity()) {
534 GREM_ASSERT(size() == capacity());
535 reserve(std::max(capacity() + capacity() / 2, size_type{1}));
536 }
538 meta::forEachIndex<sizeof...(Ts)>([&](auto columnIndex) -> void {
539 using T = std::tuple_element_t<columnIndex, value_type>;
540 using ColumnAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<T>;
541 ColumnAllocator columnAllocator = allocator;
542 try {
543 std::allocator_traits<ColumnAllocator>::construct(columnAllocator, std::to_address(columns.template get<columnIndex>()) + rowCount,
544 value.template get<columnIndex>());
545 } catch (...) {
546 meta::forEachIndex<columnIndex>([&](auto otherColumnIndex) -> void {
547 using OtherT = std::tuple_element_t<otherColumnIndex, value_type>;
548 if constexpr (!std::is_trivially_destructible_v<OtherT>) {
549 using OtherColumnAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<OtherT>;
550 OtherColumnAllocator otherColumnAllocator = allocator;
551 std::allocator_traits<OtherColumnAllocator>::destroy(otherColumnAllocator, std::to_address(columns.template get<otherColumnIndex>()) + rowCount);
552 }
553 });
554 throw;
555 }
556 });
557 ++rowCount;
558 }
559
560 template <typename... Us>
561 constexpr void push_back(Tuple<Us...>&& value) requires(sizeof...(Us) == sizeof...(Ts)) { // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
562 if (size() >= max_size()) {
563 throw std::length_error{"size() >= max_size()"};
564 }
565 if (size() >= capacity()) {
566 GREM_ASSERT(size() == capacity());
567 reserve(std::max(capacity() + capacity() / 2, size_type{32}));
568 }
570 meta::forEachIndex<sizeof...(Ts)>([&](auto columnIndex) -> void {
571 using T = std::tuple_element_t<columnIndex, value_type>;
572 using ColumnAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<T>;
573 ColumnAllocator columnAllocator = allocator;
574 try {
575 std::allocator_traits<ColumnAllocator>::construct(columnAllocator, std::to_address(columns.template get<columnIndex>()) + rowCount,
576 std::move(value.template get<columnIndex>()));
577 } catch (...) {
578 meta::forEachIndex<columnIndex>([&](auto otherColumnIndex) -> void {
579 using OtherT = std::tuple_element_t<otherColumnIndex, value_type>;
580 if constexpr (!std::is_trivially_destructible_v<OtherT>) {
581 using OtherColumnAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<OtherT>;
582 OtherColumnAllocator otherColumnAllocator = allocator;
583 std::allocator_traits<OtherColumnAllocator>::destroy(otherColumnAllocator, std::to_address(columns.template get<otherColumnIndex>()) + rowCount);
584 }
585 });
586 throw;
587 }
588 });
589 ++rowCount;
590 }
591
592 template <typename... Args>
593 constexpr reference emplace_back(Args&&... args) {
594 push_back(Tuple<Ts...>(std::forward<Args>(args)...));
595 return back();
596 }
597
598 constexpr void pop_back() {
599 GREM_ASSERT(!empty());
600 --rowCount;
601 meta::forEachIndex<sizeof...(Ts)>([&](auto columnIndex) -> void {
602 using T = std::tuple_element_t<columnIndex, value_type>;
603 if constexpr (!std::is_trivially_destructible_v<T>) {
604 using ColumnAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<T>;
605 ColumnAllocator columnAllocator = allocator;
606 std::allocator_traits<ColumnAllocator>::destroy(columnAllocator, std::to_address(columns.template get<columnIndex>()) + rowCount);
607 }
608 });
609 }
610
611 constexpr void resize(size_type newRowCount) {
612 if (newRowCount < size()) {
613 for (size_type i = size() - newRowCount; i-- > 0;) {
614 pop_back();
615 }
616 } else {
617 reserve(newRowCount);
618 meta::forEachIndex<sizeof...(Ts)>([&](auto columnIndex) -> void {
619 using T = std::tuple_element_t<columnIndex, value_type>;
620 if constexpr (!std::is_trivially_default_constructible_v<T>) {
621 using ColumnAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<T>;
622 ColumnAllocator columnAllocator = allocator;
623 size_type rowIndex = rowCount;
624 try {
625 while (rowIndex < newRowCount) {
626 std::allocator_traits<ColumnAllocator>::construct(columnAllocator, std::to_address(columns.template get<columnIndex>()) + rowIndex);
627 ++rowIndex;
628 }
629 } catch (...) {
630 while (rowIndex-- > rowCount) {
631 std::allocator_traits<ColumnAllocator>::destroy(columnAllocator, std::to_address(columns.template get<columnIndex>()) + rowIndex);
632 }
633 meta::forEachIndex<columnIndex>([&](auto otherColumnIndex) -> void {
634 using OtherT = std::tuple_element_t<otherColumnIndex, value_type>;
635 if constexpr (!std::is_trivially_destructible_v<OtherT>) {
636 using OtherColumnAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<OtherT>;
637 OtherColumnAllocator otherColumnAllocator = allocator;
638 for (size_type rowIndex = newRowCount; rowIndex-- > rowCount;) {
639 std::allocator_traits<OtherColumnAllocator>::destroy(otherColumnAllocator,
640 std::to_address(columns.template get<otherColumnIndex>()) + rowIndex);
641 }
642 }
643 });
644 throw;
645 }
646 }
647 });
648 rowCount = newRowCount;
649 }
650 }
651
652 constexpr void swap(Table& other) noexcept(std::allocator_traits<Allocator>::is_always_equal::value) { // NOLINT(cppcoreguidelines-noexcept-swap, performance-noexcept-swap)
653 if (this == &other) {
654 return;
655 }
656 using std::swap;
657 swap(rowCount, other.rowCount);
658 swap(rowCapacity, other.rowCapacity);
659 swap(columns, other.columns);
660 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_swap::value) {
661 swap(allocator, other.allocator);
662 } else if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
663 GREM_ASSERT(allocator == other.allocator);
664 }
665 }
666
667 [[nodiscard]] constexpr bool operator==(const Table& other) const {
668 return std::equal(begin(), end(), other.begin(), other.end());
669 }
670
671 [[nodiscard]] constexpr bool operator<(const Table& other) const {
672 return std::lexicographical_compare(begin(), end(), other.begin(), other.end());
673 }
674
675 [[nodiscard]] constexpr bool operator<=(const Table& other) const {
676 return !(other < *this);
677 }
678
679 [[nodiscard]] constexpr bool operator>(const Table& other) const {
680 return other < *this;
681 }
682
683 [[nodiscard]] constexpr bool operator>=(const Table& other) const {
684 return !(*this < other);
685 }
686
687 friend constexpr void swap(Table& a, Table& b) noexcept(noexcept(a.swap(b))) { // NOLINT(cppcoreguidelines-noexcept-swap, performance-noexcept-swap)
688 a.swap(b);
689 }
690
691private:
692 friend iterator;
693 friend const_iterator;
694
695 constexpr void reset() noexcept {
696 clear();
697 meta::forEachIndex<sizeof...(Ts)>([&](auto columnIndex) -> void {
698 using T = std::tuple_element_t<columnIndex, value_type>;
699 using ColumnAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<T>;
700 ColumnAllocator columnAllocator = allocator;
701 if (columns.template get<columnIndex>()) {
702 std::allocator_traits<ColumnAllocator>::deallocate(columnAllocator, columns.template get<columnIndex>(), rowCapacity);
703 columns.template get<columnIndex>() = nullptr;
704 }
705 });
706 rowCapacity = 0;
707 }
708
709 constexpr void assign(const Table& other) {
710 const size_type newRowCount = other.rowCount;
711 if constexpr ((std::is_default_constructible_v<Ts> && ...) && (std::is_copy_assignable_v<Ts> && ...)) {
712 resize(newRowCount);
713 meta::forEachIndex<sizeof...(Ts)>([&](auto columnIndex) -> void {
714 const auto* const column = other.columns.template get<columnIndex>();
715 std::copy(column, column + newRowCount, columns.template get<columnIndex>());
716 });
717 } else {
718 if constexpr ((std::is_copy_assignable_v<Ts> && ...)) {
719 if (newRowCount <= rowCount) {
720 while (rowCount > newRowCount) {
721 pop_back();
722 }
723 meta::forEachIndex<sizeof...(Ts)>([&](auto columnIndex) -> void {
724 const auto* const column = other.columns.template get<columnIndex>();
725 std::copy(column, column + newRowCount, columns.template get<columnIndex>());
726 });
727 return;
728 }
729 }
730 clear();
731 reserve(newRowCount);
732 meta::forEachIndex<sizeof...(Ts)>([&](auto columnIndex) -> void {
733 using T = std::tuple_element_t<columnIndex, value_type>;
734 using ColumnAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<T>;
735 ColumnAllocator columnAllocator = allocator;
736 size_type rowIndex = 0;
737 try {
738 while (rowIndex < newRowCount) {
739 std::allocator_traits<ColumnAllocator>::construct(columnAllocator, std::to_address(columns.template get<columnIndex>()) + rowIndex,
740 *(other.columns.template get<columnIndex>() + rowIndex));
741 ++rowIndex;
742 }
743 } catch (...) {
744 while (rowIndex-- > 0) {
745 std::allocator_traits<ColumnAllocator>::destroy(columnAllocator, std::to_address(columns.template get<columnIndex>()) + rowIndex);
746 }
747 meta::forEachIndex<columnIndex>([&](auto otherColumnIndex) -> void {
748 using OtherT = std::tuple_element_t<otherColumnIndex, value_type>;
749 if constexpr (!std::is_trivially_destructible_v<OtherT>) {
750 using OtherColumnAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<OtherT>;
751 OtherColumnAllocator otherColumnAllocator = allocator;
752 for (size_type rowIndex = newRowCount; rowIndex-- > 0;) {
753 std::allocator_traits<OtherColumnAllocator>::destroy(otherColumnAllocator, std::to_address(columns.template get<otherColumnIndex>()) + rowIndex);
754 }
755 }
756 });
757 throw;
758 }
759 });
760 rowCount = newRowCount;
761 }
762 }
763
764 constexpr void assign(Table&& other) { // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
765 const size_type newRowCount = other.rowCount;
766 if constexpr ((std::is_default_constructible_v<Ts> && ...) && (std::is_move_assignable_v<Ts> && ...)) {
767 resize(newRowCount);
768 meta::forEachIndex<sizeof...(Ts)>([&](auto columnIndex) -> void {
769 const auto* const column = other.columns.template get<columnIndex>();
770 std::move(column, column + newRowCount, columns.template get<columnIndex>());
771 });
772 } else {
773 if constexpr ((std::is_move_assignable_v<Ts> && ...)) {
774 if (newRowCount <= rowCount) {
775 while (rowCount > newRowCount) {
776 pop_back();
777 }
778 meta::forEachIndex<sizeof...(Ts)>([&](auto columnIndex) -> void {
779 const auto* const column = other.columns.template get<columnIndex>();
780 std::move(column, column + newRowCount, columns.template get<columnIndex>());
781 });
782 return;
783 }
784 }
785 clear();
786 reserve(newRowCount);
787 meta::forEachIndex<sizeof...(Ts)>([&](auto columnIndex) -> void {
788 using T = std::tuple_element_t<columnIndex, value_type>;
789 using ColumnAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<T>;
790 ColumnAllocator columnAllocator = allocator;
791 size_type rowIndex = 0;
792 try {
793 while (rowIndex < newRowCount) {
794 std::allocator_traits<ColumnAllocator>::construct(columnAllocator, std::to_address(columns.template get<columnIndex>()) + rowIndex,
795 std::move(*(other.columns.template get<columnIndex>() + rowIndex)));
796 ++rowIndex;
797 }
798 } catch (...) {
799 while (rowIndex-- > 0) {
800 std::allocator_traits<ColumnAllocator>::destroy(columnAllocator, std::to_address(columns.template get<columnIndex>()) + rowIndex);
801 }
802 meta::forEachIndex<columnIndex>([&](auto otherColumnIndex) -> void {
803 using OtherT = std::tuple_element_t<otherColumnIndex, value_type>;
804 if constexpr (!std::is_trivially_destructible_v<OtherT>) {
805 using OtherColumnAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<OtherT>;
806 OtherColumnAllocator otherColumnAllocator = allocator;
807 for (size_type rowIndex = newRowCount; rowIndex-- > 0;) {
808 std::allocator_traits<OtherColumnAllocator>::destroy(otherColumnAllocator, std::to_address(columns.template get<otherColumnIndex>()) + rowIndex);
809 }
810 }
811 });
812 throw;
813 }
814 });
815 rowCount = newRowCount;
816 }
817 }
818
819 size_type rowCount = 0;
820 size_type rowCapacity = 0;
821 Tuple<typename std::allocator_traits<typename std::allocator_traits<Allocator>::template rebind_alloc<Ts>>::pointer...> columns{};
822 [[no_unique_address]] Allocator allocator;
823};
824
825template <std::size_t Index, typename Row, typename Allocator>
826[[nodiscard]] constexpr decltype(auto) get(Table<Row, Allocator>& t, std::size_t rowIndex) {
827 return t.template get<Index>(rowIndex);
828}
829
830template <std::size_t Index, typename Row, typename Allocator>
831[[nodiscard]] constexpr decltype(auto) get(const Table<Row, Allocator>& t, std::size_t rowIndex) {
832 return t.template get<Index>(rowIndex);
833}
834
835template <typename T, typename Row, typename Allocator>
836[[nodiscard]] constexpr T& get(Table<Row, Allocator>& t, std::size_t rowIndex) {
837 return t.template get<T>(rowIndex);
838}
839
840template <typename T, typename Row, typename Allocator>
841[[nodiscard]] constexpr const T& get(const Table<Row, Allocator>& t, std::size_t rowIndex) {
842 return t.template get<T>(rowIndex);
843}
844
845template <typename T, typename Row, typename Allocator>
846[[nodiscard]] constexpr Span<T> column(Table<Row, Allocator>& t) {
847 return t.template column<T>();
848}
849
850template <typename T, typename Row, typename Allocator>
851[[nodiscard]] constexpr Span<const T> column(const Table<Row, Allocator>& t) {
852 return t.template column<T>();
853}
854
855template <std::size_t ColumnIndex, typename Row, typename Allocator>
856[[nodiscard]] constexpr auto column(Table<Row, Allocator>& t) {
857 return t.template column<ColumnIndex>();
858}
859
860template <std::size_t ColumnIndex, typename Row, typename Allocator>
861[[nodiscard]] constexpr auto column(const Table<Row, Allocator>& t) {
862 return t.template column<ColumnIndex>();
863}
864
865} // namespace grem
866
867namespace grem::pmr {
868
869template <typename Row>
871
872} // namespace grem::pmr
873
874#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Definition Span.hpp:146
std::reverse_iterator< iterator > reverse_iterator
Definition Table.hpp:247
constexpr Table & operator=(Table &&other) noexcept(std::allocator_traits< Allocator >::is_always_equal::value)
Definition Table.hpp:303
constexpr const_reference back() const
Definition Table.hpp:416
constexpr reverse_iterator rend() noexcept
Definition Table.hpp:457
constexpr reference emplace_back(Args &&... args)
Definition Table.hpp:593
constexpr allocator_type get_allocator() const noexcept
Definition Table.hpp:327
Tuple< Ts... > value_type
Definition Table.hpp:237
constexpr Table & operator=(const Table &other)
Definition Table.hpp:287
constexpr const_reference operator[](size_type pos) const
Definition Table.hpp:396
constexpr const_reverse_iterator rbegin() const noexcept
Definition Table.hpp:449
constexpr bool empty() const noexcept
Definition Table.hpp:469
constexpr void push_back(const Tuple< Us... > &value)
Definition Table.hpp:529
Allocator allocator_type
Definition Table.hpp:238
constexpr const_reverse_iterator crend() const noexcept
Definition Table.hpp:465
detail::TableIterator< const value_type, Allocator > const_iterator
Definition Table.hpp:244
constexpr decltype(auto) get(size_type rowIndex) const
Definition Table.hpp:349
constexpr Span< const T > column() const
Definition Table.hpp:360
constexpr size_type capacity() const noexcept
Definition Table.hpp:504
constexpr const_reference at(size_type pos) const
Definition Table.hpp:384
detail::TableIterator< value_type, Allocator > iterator
Definition Table.hpp:243
constexpr T & get(size_type rowIndex)
Definition Table.hpp:332
Tuple< const Ts &... > const_reference
Definition Table.hpp:242
constexpr bool operator>=(const Table &other) const
Definition Table.hpp:683
constexpr Table(const Table &other)
Definition Table.hpp:255
constexpr bool operator<=(const Table &other) const
Definition Table.hpp:675
Tuple< Ts &... > reference
Definition Table.hpp:241
std::size_t size_type
Definition Table.hpp:239
constexpr Table(Table &&other, const Allocator &allocator)
Definition Table.hpp:269
friend constexpr void swap(Table &a, Table &b) noexcept(noexcept(a.swap(b)))
Definition Table.hpp:687
constexpr void resize(size_type newRowCount)
Definition Table.hpp:611
constexpr void clear() noexcept
Definition Table.hpp:514
constexpr reference front()
Definition Table.hpp:401
constexpr bool operator>(const Table &other) const
Definition Table.hpp:679
constexpr Table(const Table &other, const Allocator &allocator)
Definition Table.hpp:258
constexpr Table(Table &&other) noexcept
Definition Table.hpp:263
constexpr const_iterator end() const noexcept
Definition Table.hpp:437
std::ptrdiff_t difference_type
Definition Table.hpp:240
constexpr const_iterator cbegin() const noexcept
Definition Table.hpp:429
constexpr void reserve(size_type newRowCapacity)
Definition Table.hpp:485
constexpr void push_back(Tuple< Us... > &&value)
Definition Table.hpp:561
constexpr void pop_back()
Definition Table.hpp:598
constexpr iterator end() noexcept
Definition Table.hpp:433
constexpr const_reverse_iterator crbegin() const noexcept
Definition Table.hpp:453
constexpr ~Table()
Definition Table.hpp:283
constexpr decltype(auto) get(size_type rowIndex)
Definition Table.hpp:344
constexpr bool operator==(const Table &other) const
Definition Table.hpp:667
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition Table.hpp:248
constexpr reference back()
Definition Table.hpp:411
constexpr size_type max_size() const noexcept
Definition Table.hpp:477
constexpr reference operator[](size_type pos)
Definition Table.hpp:391
constexpr const_iterator begin() const noexcept
Definition Table.hpp:425
constexpr iterator begin() noexcept
Definition Table.hpp:421
typename const_iterator::pointer const_pointer
Definition Table.hpp:246
constexpr reference at(size_type pos)
Definition Table.hpp:377
constexpr const_iterator cend() const noexcept
Definition Table.hpp:441
constexpr void swap(Table &other) noexcept(std::allocator_traits< Allocator >::is_always_equal::value)
Definition Table.hpp:652
constexpr void shrink_to_fit()
Definition Table.hpp:508
constexpr auto column()
Definition Table.hpp:366
constexpr Table(const Allocator &allocator) noexcept
Definition Table.hpp:252
constexpr size_type size() const noexcept
Definition Table.hpp:473
constexpr bool operator<(const Table &other) const
Definition Table.hpp:671
constexpr auto column() const
Definition Table.hpp:372
constexpr reverse_iterator rbegin() noexcept
Definition Table.hpp:445
constexpr const T & get(size_type rowIndex) const
Definition Table.hpp:338
typename iterator::pointer pointer
Definition Table.hpp:245
constexpr const_reverse_iterator rend() const noexcept
Definition Table.hpp:461
constexpr Span< T > column()
Definition Table.hpp:354
constexpr const_reference front() const
Definition Table.hpp:406
Definition Table.hpp:27
Definition Tuple.hpp:210
Definition concepts.hpp:106
@ T
't'
Definition keyboard.hpp:90
constexpr void forEachIndex(auto fn)
Execute a function for each index in the sequence from 0 up to, but not including,...
Definition metaprogramming.hpp:229
Definition Allocation.hpp:554
grem::Table< Row, std::pmr::polymorphic_allocator< Row > > Table
Definition Table.hpp:870
Definition aliases.hpp:9
constexpr tuple_element_t< Index, Pair< T1, T2 > > & get(Pair< T1, T2 > &t) noexcept
Definition Pair.hpp:202
constexpr Offset2D & operator+=(Offset2D &a, Offset2D b) noexcept
Definition extents.hpp:646
constexpr Tuple< Ts &... > tie(Ts &... ts)
Definition Tuple.hpp:451
constexpr Offset2D operator-(Offset2D a) noexcept
Definition extents.hpp:664
constexpr bool operator==(const Indirect< A, AllocatorA > &a, const Indirect< B, AllocatorB > &b)
Definition Indirect.hpp:246
Tuple(Ts...) -> Tuple< Ts... >
constexpr Offset2D operator*(Offset2D a, int32_t b) noexcept
Definition extents.hpp:692
constexpr Offset2D & operator-=(Offset2D &a, Extent2D b) noexcept
Definition extents.hpp:658
constexpr decltype(auto) column(Registry< T, ElementID, Container > &r)
Definition Registry.hpp:698
constexpr std::compare_three_way_result_t< A, B > operator<=>(const Indirect< A, AllocatorA > &a, const Indirect< B, AllocatorB > &b)
Definition Indirect.hpp:273
constexpr auto operator+(const ConstantString< CharT, LengthA > &a, const ConstantString< CharT, LengthB > &b)
Definition ConstantString.hpp:656