Skip to content

Commit 83813ec

Browse files
committed
deque fix some issues with missed back_block
1 parent 9defa04 commit 83813ec

3 files changed

Lines changed: 73 additions & 5 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <fast_io.h>
2+
#include <fast_io_driver/timer.h>
3+
#include <fast_io_dsal/deque.h>
4+
5+
int main()
6+
{
7+
fast_io::timer tm(u8"fast_io::deque");
8+
fast_io::deque<std::size_t> deq;
9+
constexpr std::size_t n{100000000};
10+
{
11+
fast_io::timer tm1(u8"push_front");
12+
for (std::size_t i{}; i != n; ++i)
13+
{
14+
deq.push_front(i);
15+
}
16+
}
17+
::std::size_t sum{};
18+
{
19+
fast_io::timer tm1(u8"loop");
20+
for (auto const e : deq)
21+
{
22+
sum += e;
23+
}
24+
}
25+
::fast_io::io::perrln("sum=",sum);
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <fast_io.h>
2+
#include <fast_io_driver/timer.h>
3+
#include <deque>
4+
5+
int main()
6+
{
7+
fast_io::timer tm(u8"std::deque");
8+
std::deque<std::size_t> deq;
9+
constexpr std::size_t n{100000000};
10+
{
11+
fast_io::timer tm1(u8"push_front");
12+
for (std::size_t i{}; i != n; ++i)
13+
{
14+
deq.push_front(i);
15+
}
16+
}
17+
::std::size_t sum{};
18+
{
19+
fast_io::timer tm1(u8"loop");
20+
for (auto const e : deq)
21+
{
22+
sum += e;
23+
}
24+
}
25+
::fast_io::io::perrln("sum=",sum);
26+
}

include/fast_io_dsal/impl/deque.h

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ struct deque_iterator
222222

223223
inline constexpr deque_iterator &operator--() noexcept
224224
{
225-
if (itercontent.curr_ptr == itercontent.begin_ptr) [[unlikely]]
225+
if (itercontent.begin_ptr == itercontent.curr_ptr) [[unlikely]]
226226
{
227227
itercontent.curr_ptr = (itercontent.begin_ptr = (*--itercontent.controller_ptr)) + ::fast_io::containers::details::deque_block_size<sizeof(value_type)>;
228228
}
@@ -1145,6 +1145,8 @@ class deque FAST_IO_TRIVIALLY_RELOCATABLE_IF_ELIGIBLE
11451145
run_destroy destroyer(__builtin_addressof(this->controller));
11461146
auto dq_back_backup{this->controller.back_block};
11471147
this->controller.back_block = this->controller.front_block;
1148+
auto dq_back_end_ptr_backup{this->controller.back_end_ptr};
1149+
this->controller.back_end_ptr = this->controller.front_end_ptr;
11481150
pointer lastblockbegin;
11491151
if (front_controller_ptr == back_controller_ptr)
11501152
{
@@ -1166,7 +1168,9 @@ class deque FAST_IO_TRIVIALLY_RELOCATABLE_IF_ELIGIBLE
11661168
{
11671169
pointer blockptr{*it};
11681170
::fast_io::freestanding::uninitialized_copy_n(blockptr, block_size, *destit);
1169-
this->controller.back_block = {destit, blockptr, blockptr, blockptr + block_size};
1171+
auto new_curr_ptr{blockptr + block_size};
1172+
this->controller.back_block = {blockptr, new_curr_ptr, destit};
1173+
this->controller.back_end_ptr = new_curr_ptr;
11701174
++destit;
11711175
}
11721176
lastblockbegin = fromcontroller.back_block.begin_ptr;
@@ -1177,6 +1181,7 @@ class deque FAST_IO_TRIVIALLY_RELOCATABLE_IF_ELIGIBLE
11771181
fromcontroller.back_block.curr_ptr, dq_back_backup.begin_ptr);
11781182

11791183
this->controller.back_block = dq_back_backup;
1184+
this->controller.back_end_ptr = dq_back_end_ptr_backup;
11801185
destroyer.thiscontroller = nullptr;
11811186
}
11821187
}
@@ -1186,6 +1191,8 @@ class deque FAST_IO_TRIVIALLY_RELOCATABLE_IF_ELIGIBLE
11861191

11871192
auto dq_back_backup{controller.back_block};
11881193
controller.back_block = controller.front_block;
1194+
auto dq_back_end_ptr_backup{controller.back_end_ptr};
1195+
controller.back_end_ptr = controller.back_begin_ptr;
11891196

11901197
auto front_controller_ptr{controller.front_block.controller_ptr};
11911198
auto back_controller_ptr{controller.back_block.controller_ptr};
@@ -1203,12 +1210,15 @@ class deque FAST_IO_TRIVIALLY_RELOCATABLE_IF_ELIGIBLE
12031210
{
12041211
T *blockptr{*it};
12051212
::fast_io::freestanding::uninitialized_default_construct(blockptr, blockptr + block_size);
1206-
this->controller.back_block = {it, blockptr, blockptr + block_size, blockptr + block_size};
1213+
auto new_curr_ptr{blockptr + block_size};
1214+
this->controller.back_block = {blockptr, new_curr_ptr, it};
1215+
this->controller.back_end_ptr = new_curr_ptr;
12071216
}
12081217
lastblockbegin = dq_back_backup.begin_ptr;
12091218
}
12101219
::fast_io::freestanding::uninitialized_default_construct(lastblockbegin, dq_back_backup.curr_ptr);
12111220
this->controller.back_block = dq_back_backup;
1221+
this->controller.back_end_ptr = dq_back_end_ptr_backup;
12121222
des.thiscontroller = nullptr;
12131223
}
12141224

@@ -1270,9 +1280,12 @@ class deque FAST_IO_TRIVIALLY_RELOCATABLE_IF_ELIGIBLE
12701280
this->init_blocks_common<false>(static_cast<::std::size_t>(dist));
12711281

12721282
auto dq_back_backup{this->controller.back_block};
1283+
this->controller.back_block = this->controller.front_block;
1284+
auto dq_back_end_ptr_backup{controller.back_end_ptr};
1285+
controller.back_end_ptr = controller.back_begin_ptr;
1286+
12731287
auto front_controller_ptr{controller.front_block.controller_ptr};
12741288
auto back_controller_ptr{controller.back_block.controller_ptr};
1275-
this->controller.back_block = this->controller.front_block;
12761289

12771290
T *lastblockbegin;
12781291
if (front_controller_ptr == back_controller_ptr)
@@ -1285,7 +1298,9 @@ class deque FAST_IO_TRIVIALLY_RELOCATABLE_IF_ELIGIBLE
12851298
{
12861299
T *blockptr{*it};
12871300
first = ::fast_io::containers::details::uninitialized_copy_n_for_deque(first, block_size, blockptr).from;
1288-
this->controller.back_block = {it, blockptr, blockptr + block_size, blockptr + block_size};
1301+
auto new_curr_ptr{blockptr + block_size};
1302+
this->controller.back_block = {blockptr, new_curr_ptr, it};
1303+
this->controller.back_end_ptr = new_curr_ptr;
12891304
}
12901305
lastblockbegin = dq_back_backup.begin_ptr;
12911306
}
@@ -1294,6 +1309,7 @@ class deque FAST_IO_TRIVIALLY_RELOCATABLE_IF_ELIGIBLE
12941309
static_cast<::std::size_t>(dq_back_backup.curr_ptr - lastblockbegin),
12951310
lastblockbegin);
12961311
this->controller.back_block = dq_back_backup;
1312+
this->controller.back_end_ptr = dq_back_end_ptr_backup;
12971313
}
12981314
else
12991315
{

0 commit comments

Comments
 (0)