/*
This file is a part of libcds - Concurrent Data Structures library
- (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
+ (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
Source code repo: http://github.com/khizmax/libcds/
Download: http://sourceforge.net/projects/libcds/files/
-
+
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef CDSLIB_OPT_BUFFER_H
/// Get item \p i
value_type& operator []( size_t i )
{
- assert( i < capacity() );
+ assert( i < capacity());
return m_buffer[i].v;
}
/// Get item \p i, const version
const value_type& operator []( size_t i ) const
{
- assert( i < capacity() );
+ assert( i < capacity());
return m_buffer[i].v;
}
/// Zeroize the buffer
void zeroize()
{
- memset( m_buffer, 0, capacity() * sizeof(m_buffer[0]) );
+ memset( m_buffer, 0, capacity() * sizeof(m_buffer[0]));
}
/// Returns pointer to buffer array
{
return &( m_buffer[0].v );
}
+
+ /// Returns <tt> idx % capacity() </tt>
+ /**
+ If the buffer size is a power of two, binary arithmethics is used
+ instead of modulo arithmetics
+ */
+ size_t mod( size_t idx )
+ {
+ static_if( c_bExp2 )
+ return idx & ( capacity() - 1 );
+ else
+ return idx % capacity();
+ }
+
+ //@cond
+ template <typename I>
+ typename std::enable_if< sizeof(I) != sizeof(size_t), size_t >::type mod( I idx )
+ {
+ static_if( c_bExp2 )
+ return static_cast<size_t>( idx & static_cast<I>( capacity() - 1 ));
+ else
+ return static_cast<size_t>( idx % capacity());
+ }
+ //@endcond
};
/// Static initialized buffer
/// Get item \p i
value_type& operator []( size_t i )
{
- assert( i < capacity() );
+ assert( i < capacity());
return m_buffer[i];
}
/// Get item \p i, const version
const value_type& operator []( size_t i ) const
{
- assert( i < capacity() );
+ assert( i < capacity());
return m_buffer[i];
}
/// Zeroize the buffer
void zeroize()
{
- memset( m_buffer, 0, capacity() * sizeof(m_buffer[0]) );
+ memset( m_buffer, 0, capacity() * sizeof(m_buffer[0]));
}
/// Returns pointer to buffer array
{
return m_buffer;
}
+
+ /// Returns <tt> idx % capacity() </tt>
+ /**
+ If the buffer size is a power of two, binary arithmethics is used
+ instead of modulo arithmetics
+ */
+ size_t mod( size_t idx )
+ {
+ static_if( c_bExp2 )
+ return idx & ( capacity() - 1 );
+ else
+ return idx % capacity();
+ }
+
+ //@cond
+ template <typename I>
+ typename std::enable_if< sizeof( I ) != sizeof( size_t ), size_t >::type mod( I idx )
+ {
+ static_if( c_bExp2 )
+ return static_cast<size_t>( idx & static_cast<I>( capacity() - 1 ));
+ else
+ return static_cast<size_t>( idx % capacity());
+ }
+ //@endcond
};
/// Dynamically allocated uninitialized buffer
/// Get item \p i
value_type& operator []( size_t i )
{
- assert( i < capacity() );
+ assert( i < capacity());
return m_buffer[i];
}
/// Get item \p i, const version
const value_type& operator []( size_t i ) const
{
- assert( i < capacity() );
+ assert( i < capacity());
return m_buffer[i];
}
/// Zeroize the buffer
void zeroize()
{
- memset( m_buffer, 0, capacity() * sizeof(m_buffer[0]) );
+ memset( m_buffer, 0, capacity() * sizeof(m_buffer[0]));
}
/// Returns pointer to buffer array
{
return m_buffer;
}
+
+ /// Returns <tt> idx % capacity() </tt>
+ /**
+ If the buffer size is a power of two, binary arithmethics is used
+ instead of modulo arithmetics
+ */
+ size_t mod( size_t idx )
+ {
+ static_if ( c_bExp2 )
+ return idx & ( capacity() - 1 );
+ else
+ return idx % capacity();
+ }
+
+ //@cond
+ template <typename I>
+ typename std::enable_if< sizeof( I ) != sizeof( size_t ), size_t >::type mod( I idx )
+ {
+ static_if ( c_bExp2 )
+ return static_cast<size_t>( idx & static_cast<I>( capacity() - 1 ));
+ else
+ return static_cast<size_t>( idx % capacity());
+ }
+ //@endcond
};
/// Get item \p i
value_type& operator []( size_t i )
{
- assert( i < capacity() );
+ assert( i < capacity());
return m_buffer[i];
}
/// Get item \p i, const version
const value_type& operator []( size_t i ) const
{
- assert( i < capacity() );
+ assert( i < capacity());
return m_buffer[i];
}
/// Zeroize the buffer
void zeroize()
{
- memset( m_buffer, 0, capacity() * sizeof(m_buffer[0]) );
+ memset( m_buffer, 0, capacity() * sizeof(m_buffer[0]));
}
/// Returns pointer to buffer array
{
return m_buffer;
}
+
+ /// Returns <tt> idx % capacity() </tt>
+ /**
+ If the buffer size is a power of two, binary arithmethics is used
+ instead of modulo arithmetics
+ */
+ size_t mod( size_t idx )
+ {
+ static_if( c_bExp2 )
+ return idx & ( capacity() - 1 );
+ else
+ return idx % capacity();
+ }
+
+ //@cond
+ template <typename I>
+ typename std::enable_if< sizeof( I ) != sizeof( size_t ), size_t >::type mod( I idx )
+ {
+ static_if( c_bExp2 )
+ return static_cast<size_t>( idx & static_cast<I>( capacity() - 1 ));
+ else
+ return static_cast<size_t>( idx % capacity());
+ }
+ //@endcond
};
} // namespace v