11// -----------------------------------------------------------
22//
33// Copyright (c) 2001-2002 Chuck Allison and Jeremy Siek
4- // Copyright (c) 2003-2006, 2008, 2025 Gennaro Prota
4+ // Copyright (c) 2003-2006, 2008, 2025-2026 Gennaro Prota
55// Copyright (c) 2014 Ahmed Charles
66//
77// Copyright (c) 2014 Glen Joseph Fernandes
1919#include " boost/assert.hpp"
2020#include " boost/core/bit.hpp"
2121#include " boost/core/no_exceptions_support.hpp"
22- #include " boost/dynamic_bitset/detail/lowest_bit .hpp"
22+ #include " boost/dynamic_bitset/detail/lowest_highest_bit .hpp"
2323#include " boost/functional/hash/hash.hpp"
2424#include " boost/throw_exception.hpp"
2525#include < algorithm>
@@ -1450,6 +1450,23 @@ dynamic_bitset< Block, AllocatorOrContainer >::find_first_zero( size_type pos )
14501450 : zero_pos;
14511451}
14521452
1453+ template < typename Block, typename AllocatorOrContainer >
1454+ BOOST_DYNAMIC_BITSET_CONSTEXPR20 typename dynamic_bitset< Block, AllocatorOrContainer >::size_type
1455+ dynamic_bitset< Block, AllocatorOrContainer >::find_last_one() const
1456+ {
1457+ size_type result = npos;
1458+
1459+ size_type i = num_blocks ();
1460+ while ( i > 0 ) {
1461+ -- i;
1462+ if ( m_not_empty ( m_bits[ i ] ) ) {
1463+ result = i * bits_per_block + detail::highest_bit ( m_bits[ i ] );
1464+ break ;
1465+ }
1466+ }
1467+ return result;
1468+ }
1469+
14531470template < typename Block, typename AllocatorOrContainer >
14541471BOOST_DYNAMIC_BITSET_CONSTEXPR20 typename dynamic_bitset< Block, AllocatorOrContainer >::size_type
14551472dynamic_bitset< Block, AllocatorOrContainer >::find_next( size_type pos ) const
@@ -1466,6 +1483,39 @@ dynamic_bitset< Block, AllocatorOrContainer >::find_next_one( size_type pos ) co
14661483 : find_first_one ( pos + 1 );
14671484}
14681485
1486+ template < typename Block, typename AllocatorOrContainer >
1487+ BOOST_DYNAMIC_BITSET_CONSTEXPR20 typename dynamic_bitset< Block, AllocatorOrContainer >::size_type
1488+ dynamic_bitset< Block, AllocatorOrContainer >::find_previous_one( size_type pos ) const
1489+ {
1490+ if ( pos == 0 || empty () ) {
1491+ return npos;
1492+ }
1493+
1494+ if ( pos >= size () ) {
1495+ return find_last_one ();
1496+ }
1497+
1498+ const size_type blk = block_index ( pos );
1499+ const int ind = bit_index ( pos );
1500+ // mask out bits from ind upwards
1501+ Block back = m_bits[ blk ] & ( ( Block ( 1 ) << ind ) - 1 );
1502+ bool found = m_not_empty ( back );
1503+ size_type i = blk;
1504+ if ( ! found ) {
1505+ while ( i > 0 ) {
1506+ -- i;
1507+ back = m_bits[ i ];
1508+ if ( m_not_empty ( back ) ) {
1509+ found = true ;
1510+ break ;
1511+ }
1512+ }
1513+ }
1514+ return found
1515+ ? i * bits_per_block + detail::highest_bit ( back )
1516+ : npos;
1517+ }
1518+
14691519template < typename Block, typename AllocatorOrContainer >
14701520BOOST_DYNAMIC_BITSET_CONSTEXPR20 typename dynamic_bitset< Block, AllocatorOrContainer >::size_type
14711521dynamic_bitset< Block, AllocatorOrContainer >::find_next_off( size_type pos ) const
0 commit comments