55#pragma once
66
77#include < concepts>
8+ #include < cstddef>
89#include < cstdint>
910#include < format>
1011#include < initializer_list>
@@ -29,6 +30,7 @@ namespace gl {
2930// / @todo Implement assign, and swap methods.
3031// / @todo Implement iterator-based insert, emplace and erase methods.
3132// / @todo Add `operator<<` overload for `std::ostream` and specialize `std::formatter`.
33+ // / @todo Use `std::ptrdiff_t` instead of `std::size_t` for offset values.
3234template <std::semiregular T>
3335class flat_jagged_vector {
3436public:
@@ -903,23 +905,24 @@ class flat_jagged_vector {
903905 requires std::convertible_to<std::ranges::range_reference_t <R>, value_type>
904906 void insert (size_type pos, R&& r) {
905907 const auto beg = this ->_offsets [pos];
908+ const auto beg_pos = static_cast <std::ptrdiff_t >(beg);
906909 const auto old_size = this ->_data .size ();
907910
908911 this ->_ensure_offset_capacity ();
909912
910913 if constexpr (std::ranges::contiguous_range<R>) {
911914 auto * ptr = std::ranges::data (r);
912915 const auto n = std::ranges::size (r);
913- this ->_data .insert (this ->_data .begin () + beg , ptr, ptr + n);
916+ this ->_data .insert (this ->_data .begin () + beg_pos , ptr, ptr + n);
914917 }
915918 else {
916919 this ->_data .insert (
917- this ->_data .begin () + beg , std::ranges::begin (r), std::ranges::end (r)
920+ this ->_data .begin () + beg_pos , std::ranges::begin (r), std::ranges::end (r)
918921 );
919922 }
920923
921924 const auto inserted = this ->_data .size () - old_size;
922- this ->_offsets .insert (this ->_offsets .begin () + pos, beg);
925+ this ->_offsets .insert (this ->_offsets .begin () + static_cast <std:: ptrdiff_t >( pos) , beg);
923926 for (size_type i = pos + 1uz; i < this ->_offsets .size (); i++)
924927 this ->_offsets [i] += inserted;
925928 }
@@ -949,12 +952,12 @@ class flat_jagged_vector {
949952 // / the erased segment in the underlying vector, $S$ is the number of segments after `pos`,
950953 // / and $L$ is the size of the erased segment. Erasing the **last** segment is $O(L)$.
951954 void erase (size_type pos) {
952- const auto start = this ->_offsets [pos];
953- const auto end = this ->_offsets [pos + 1uz];
954- const auto len = end - start;
955+ const auto start = static_cast <std:: ptrdiff_t >( this ->_offsets [pos]) ;
956+ const auto end = static_cast <std:: ptrdiff_t >( this ->_offsets [pos + 1uz]) ;
957+ const auto len = static_cast <size_type>( end - start) ;
955958
956959 this ->_data .erase (this ->_data .begin () + start, this ->_data .begin () + end);
957- this ->_offsets .erase (this ->_offsets .begin () + pos);
960+ this ->_offsets .erase (this ->_offsets .begin () + static_cast <std:: ptrdiff_t >( pos) );
958961 for (size_type i = pos; i < this ->_offsets .size (); i++)
959962 this ->_offsets [i] -= len;
960963 }
@@ -1019,7 +1022,8 @@ class flat_jagged_vector {
10191022 // / @note **Time Complexity:** Amortized $O(E + S)$ where $E$ is the number of elements after
10201023 // / the insertion point in the underlying vector, and $S$ is the number of segments after `seg`.
10211024 void insert (size_type seg, size_type pos, const value_type& value) {
1022- this ->_data .insert (this ->_data .begin () + this ->_offsets [seg] + pos, value);
1025+ const auto insert_pos = static_cast <std::ptrdiff_t >(this ->_offsets [seg] + pos);
1026+ this ->_data .insert (this ->_data .begin () + insert_pos, value);
10231027 for (size_type i = seg + 1uz; i < this ->_offsets .size (); i++)
10241028 this ->_offsets [i]++;
10251029 }
@@ -1037,9 +1041,8 @@ class flat_jagged_vector {
10371041 // / the insertion point in the underlying vector, and $S$ is the number of segments after `seg`.
10381042 template <class ... Args>
10391043 void emplace (size_type seg, size_type pos, Args&&... args) {
1040- this ->_data .emplace (
1041- this ->_data .begin () + this ->_offsets [seg] + pos, std::forward<Args>(args)...
1042- );
1044+ const auto insert_pos = static_cast <std::ptrdiff_t >(this ->_offsets [seg] + pos);
1045+ this ->_data .emplace (this ->_data .begin () + insert_pos, std::forward<Args>(args)...);
10431046 for (size_type i = seg + 1uz; i < this ->_offsets .size (); i++)
10441047 this ->_offsets [i]++;
10451048 }
@@ -1052,7 +1055,8 @@ class flat_jagged_vector {
10521055 // / @note **Time Complexity:** $O(E + S)$ where $E$ is the number of elements after the erased
10531056 // / position in the underlying vector, and $S$ is the number of segments after `seg`.
10541057 void erase (size_type seg, size_type pos) {
1055- this ->_data .erase (this ->_data .begin () + this ->_offsets [seg] + pos);
1058+ const auto erase_pos = static_cast <std::ptrdiff_t >(this ->_offsets [seg] + pos);
1059+ this ->_data .erase (this ->_data .begin () + erase_pos);
10561060 for (size_type i = seg + 1uz; i < this ->_offsets .size (); i++)
10571061 this ->_offsets [i]--;
10581062 }
@@ -1076,16 +1080,16 @@ class flat_jagged_vector {
10761080 const auto curr_count = this ->segment_size (seg);
10771081 if (n < curr_count) {
10781082 const auto diff = curr_count - n;
1079- const auto start = this ->_offsets [seg] + n;
1080- const auto end = this ->_offsets [seg + 1uz];
1083+ const auto start = static_cast <std:: ptrdiff_t >( this ->_offsets [seg] + n) ;
1084+ const auto end = static_cast <std:: ptrdiff_t >( this ->_offsets [seg + 1uz]) ;
10811085
10821086 this ->_data .erase (this ->_data .begin () + start, this ->_data .begin () + end);
10831087 for (size_type i = seg + 1uz; i < this ->_offsets .size (); i++)
10841088 this ->_offsets [i] -= diff;
10851089 }
10861090 else if (n > curr_count) {
10871091 const auto diff = n - curr_count;
1088- const auto pos = this ->_offsets [seg + 1uz];
1092+ const auto pos = static_cast <std:: ptrdiff_t >( this ->_offsets [seg + 1uz]) ;
10891093
10901094 this ->_data .insert (this ->_data .begin () + pos, diff, value_type ());
10911095 for (size_type i = seg + 1uz; i < this ->_offsets .size (); i++)
@@ -1113,16 +1117,16 @@ class flat_jagged_vector {
11131117 const auto curr_count = this ->segment_size (seg);
11141118 if (n < curr_count) {
11151119 const auto diff = curr_count - n;
1116- const auto start = this ->_offsets [seg] + n;
1117- const auto end = this ->_offsets [seg + 1uz];
1120+ const auto start = static_cast <std:: ptrdiff_t >( this ->_offsets [seg] + n) ;
1121+ const auto end = static_cast <std:: ptrdiff_t >( this ->_offsets [seg + 1uz]) ;
11181122
11191123 this ->_data .erase (this ->_data .begin () + start, this ->_data .begin () + end);
11201124 for (size_type i = seg + 1uz; i < this ->_offsets .size (); i++)
11211125 this ->_offsets [i] -= diff;
11221126 }
11231127 else if (n > curr_count) {
11241128 const auto diff = n - curr_count;
1125- const auto pos = this ->_offsets [seg + 1uz];
1129+ const auto pos = static_cast <std:: ptrdiff_t >( this ->_offsets [seg + 1uz]) ;
11261130
11271131 this ->_data .insert (this ->_data .begin () + pos, diff, value);
11281132 for (size_type i = seg + 1uz; i < this ->_offsets .size (); i++)
0 commit comments