File tree Expand file tree Collapse file tree 2 files changed +63
-1
lines changed
include/fast_io_dsal/impl
tests/0026.container/0014.bitvec Expand file tree Collapse file tree 2 files changed +63
-1
lines changed Original file line number Diff line number Diff line change @@ -85,7 +85,7 @@ class bitvec
8585 {
8686 current_capacity /= underlying_digits;
8787 }
88- constexpr ::std::size_t mxbyteshalf{max_size_bytes () >> 1 };
88+ constexpr ::std::size_t mxbyteshalf{max_size () >> 1 };
8989 if (mxbyteshalf < current_capacity)
9090 {
9191 ::fast_io::fast_terminate ();
@@ -700,6 +700,28 @@ class bitvec
700700 this ->destroy_bitvec ();
701701 this ->imp = {};
702702 }
703+
704+ private:
705+ inline static constexpr size_type bits_to_blocks (size_type bits) noexcept
706+ {
707+ if constexpr (underlying_digits == 8 )
708+ {
709+ return (bits + 7 ) >> 3 ; // ceil(bits / 8)
710+ }
711+ else
712+ {
713+ return (bits + (underlying_digits - 1 )) / underlying_digits;
714+ }
715+ }
716+
717+ public:
718+ constexpr void reserve (size_type n) noexcept
719+ {
720+ if (this ->imp .end_pos < n)
721+ {
722+ this ->grow_to_new_capacity (bits_to_blocks (n));
723+ }
724+ }
703725};
704726
705727} // namespace containers
Original file line number Diff line number Diff line change @@ -414,6 +414,45 @@ inline void test_bitvec_flip()
414414 ::fast_io::io::print (" bitvec flip test finished\n " );
415415}
416416
417+ inline void test_bitvec_reserve_and_push_back_unchecked ()
418+ {
419+ ::fast_io::io::perr (" === bitvec reserve + push_back_unchecked test ===\n " );
420+
421+ ::fast_io::bitvec bv;
422+
423+ // Reserve space for 5000 bits
424+ bv.reserve (5000 );
425+
426+ if (bv.capacity () < 5000 )
427+ {
428+ ::fast_io::io::panic (" ERROR: reserve did not increase capacity correctly\n " );
429+ }
430+
431+ // Now push bits WITHOUT triggering reallocation
432+ // Use push_back_unchecked since we know capacity is sufficient
433+ for (::std::size_t i{}; i != 5000u ; ++i)
434+ {
435+ bv.push_back_unchecked ((i & 1u ) != 0u );
436+ }
437+
438+ if (bv.size () != 5000u )
439+ {
440+ ::fast_io::io::panic (" ERROR: size mismatch after push_back_unchecked\n " );
441+ }
442+
443+ // Verify pattern
444+ for (::std::size_t i{}; i != 5000u ; ++i)
445+ {
446+ bool expected = (i & 1u ) != 0u ;
447+ if (bv.test (i) != expected)
448+ {
449+ ::fast_io::io::panicln (" ERROR: push_back_unchecked mismatch at index " , i);
450+ }
451+ }
452+
453+ ::fast_io::io::print (" bitvec reserve + push_back_unchecked test finished\n " );
454+ }
455+
417456int main ()
418457{
419458 test_bitvec_basic ();
@@ -424,6 +463,7 @@ int main()
424463 test_bitvec_copy_assignment ();
425464 test_bitvec_move ();
426465 test_bitvec_flip ();
466+ test_bitvec_reserve_and_push_back_unchecked ();
427467
428468 ::fast_io::io::print (" All bitvec tests finished\n " );
429469}
You can’t perform that action at this time.
0 commit comments