Skip to content

Commit

Permalink
Add span::ssize(), size(nonstd::span), ssize(nonstd::span) (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmoene committed Feb 26, 2019
1 parent 9a7a7bd commit 71a8ead
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
23 changes: 23 additions & 0 deletions include/nonstd/span.hpp
Expand Up @@ -913,6 +913,11 @@ class span
return size_;
}

span_constexpr std::ptrdiff_t ssize() const span_noexcept
{
return static_cast<std::ptrdiff_t>( size_ );
}

span_constexpr index_type size_bytes() const span_noexcept
{
return size() * to_size( sizeof( element_type ) );
Expand Down Expand Up @@ -1210,6 +1215,20 @@ subspan( T & t, index_t offset, extent_t count = dynamic_extent ) -> decltype( m

#endif // span_FEATURE( NON_MEMBER_FIRST_LAST_SUB )

// 27.8 Container and view access [iterator.container]

template< class T, extent_t Extent /*= dynamic_extent*/ >
span_constexpr std::size_t size( span<T,Extent> const & spn )
{
return static_cast<std::size_t>( spn.size() );
}

template< class T, extent_t Extent /*= dynamic_extent*/ >
span_constexpr std::ptrdiff_t ssize( span<T,Extent> const & spn )
{
return static_cast<std::ptrdiff_t>( spn.size() );
}

} // namespace span_lite
} // namespace nonstd

Expand Down Expand Up @@ -1238,6 +1257,10 @@ using span_lite::as_writeable_bytes;
#if span_FEATURE( SAME )
using span_lite::same;
#endif

using span_lite::size;
using span_lite::ssize;

} // namespace nonstd

#endif // span_USES_STD_SPAN
Expand Down
42 changes: 42 additions & 0 deletions test/span.t.cpp
Expand Up @@ -973,6 +973,20 @@ CASE( "span<>: Allows to obtain the number of elements via size()" )
EXPECT( z.size() == index_type( 0 ) );
}

CASE( "span<>: Allows to obtain the number of elements via ssize()" )
{
int a[] = { 1, 2, 3, };
int b[] = { 1, 2, 3, 4, 5, };

span<int> z;
span<int> va( a );
span<int> vb( b );

EXPECT( va.ssize() == DIMENSION_OF( a ) );
EXPECT( vb.ssize() == DIMENSION_OF( b ) );
EXPECT( z.ssize() == 0 );
}

CASE( "span<>: Allows to obtain the number of bytes via size_bytes()" )
{
int a[] = { 1, 2, 3, };
Expand Down Expand Up @@ -1390,6 +1404,34 @@ CASE( "subspan(): Allows to create a sub span starting at a given offset" )
#endif
}

CASE( "size(): Allows to obtain the number of elements via size()" )
{
int a[] = { 1, 2, 3, };
int b[] = { 1, 2, 3, 4, 5, };

span<int> z;
span<int> va( a );
span<int> vb( b );

EXPECT( size( va ) == index_type( DIMENSION_OF( a ) ) );
EXPECT( size( vb ) == index_type( DIMENSION_OF( b ) ) );
EXPECT( size( z ) == index_type( 0 ) );
}

CASE( "ssize(): Allows to obtain the number of elements via ssize()" )
{
int a[] = { 1, 2, 3, };
int b[] = { 1, 2, 3, 4, 5, };

span<int> z;
span<int> va( a );
span<int> vb( b );

EXPECT( ssize( va ) == DIMENSION_OF( a ) );
EXPECT( ssize( vb ) == DIMENSION_OF( b ) );
EXPECT( ssize( z ) == 0 );
}

// Issues

#include <cassert>
Expand Down

0 comments on commit 71a8ead

Please sign in to comment.