Skip to content

Commit d7473c2

Browse files
committed
Added operator() and toSlice to SortedArrayView.
1 parent 613ae98 commit d7473c2

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

src/SortedArray.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ class SortedArray : protected SortedArrayView< T, INDEX_TYPE, BUFFER_TYPE >
168168
return ParentClass::toViewConst();
169169
}
170170

171+
using ParentClass::toSlice;
172+
171173
///@}
172174

173175
/**
@@ -198,6 +200,7 @@ class SortedArray : protected SortedArrayView< T, INDEX_TYPE, BUFFER_TYPE >
198200
///@{
199201

200202
using ParentClass::operator[];
203+
using ParentClass::operator();
201204

202205
/**
203206
* @copydoc SortedArrayView::data

src/SortedArrayView.hpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#pragma once
1414

1515
// Source includes
16+
#include "ArraySlice.hpp"
1617
#include "bufferManipulation.hpp"
1718
#include "sortedArrayManipulation.hpp"
1819

@@ -129,7 +130,7 @@ class SortedArrayView
129130
///@}
130131

131132
/**
132-
* @name SortedArrayView creation methods
133+
* @name SortedArrayView and ArraySlice creation methods
133134
*/
134135
///@{
135136

@@ -149,6 +150,27 @@ class SortedArrayView
149150
toViewConst() const
150151
{ return toView(); }
151152

153+
/**
154+
* @return Return an ArraySlice representing this SortedArrayView.
155+
*/
156+
LVARRAY_HOST_DEVICE constexpr inline
157+
ArraySlice< T const, 1, 0, INDEX_TYPE > toSlice() const &
158+
{ return ArraySlice< T const, 1, 0, INDEX_TYPE >( data(), &m_size, nullptr ); }
159+
160+
/**
161+
* @brief Overload for rvalues that raises a compilation error when used.
162+
* @return A null ArraySlice.
163+
* @note This cannot be called on a rvalue since the @c ArraySlice would
164+
* contain pointers to the size of the current @c SortedArrayView that is
165+
* about to be destroyed. This overload prevents that from happening.
166+
*/
167+
LVARRAY_HOST_DEVICE constexpr inline
168+
ArraySlice< T const, 1, 0, INDEX_TYPE > toSlice() const &&
169+
{
170+
static_assert( typeManipulation::always_true< T >, "Cannot call toSlice on an rvalue." );
171+
return ArraySlice< T const, 1, 0, INDEX_TYPE >( nullptr, nullptr, nullptr );
172+
}
173+
152174
///@}
153175

154176
/**
@@ -205,6 +227,17 @@ class SortedArrayView
205227
return data()[ i ];
206228
}
207229

230+
/**
231+
* @return Return the value at position @p i .
232+
* @param i the index of the value to access.
233+
*/
234+
LVARRAY_HOST_DEVICE CONSTEXPR_WITHOUT_BOUNDS_CHECK inline
235+
T const & operator()( INDEX_TYPE const i ) const
236+
{
237+
SORTEDARRAY_CHECK_BOUNDS( i );
238+
return data()[ i ];
239+
}
240+
208241
/**
209242
* @return Return a pointer to the values.
210243
*/

unitTests/testSortedArray.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,34 @@ class SortedArrayTest : public ::testing::Test
3939
*/
4040
void compareToReference() const
4141
{
42-
ASSERT_EQ( m_set.size(), m_ref.size() );
43-
ASSERT_EQ( m_set.empty(), m_ref.empty() );
42+
EXPECT_EQ( m_set.size(), m_ref.size() );
43+
EXPECT_EQ( m_set.empty(), m_ref.empty() );
4444
if( m_set.empty() )
4545
{
46-
ASSERT_EQ( m_set.size(), 0 );
46+
EXPECT_EQ( m_set.size(), 0 );
4747
return;
4848
}
4949

5050
T const * ptr = m_set.data();
51+
ArraySlice< T const, 1, 0, INDEX_TYPE > const slice = m_set.toSlice();
5152
typename std::set< T >::const_iterator it = m_ref.begin();
5253
for( int i = 0; i < m_set.size(); ++i )
5354
{
54-
ASSERT_EQ( m_set[ i ], *it );
55-
ASSERT_EQ( ptr[ i ], *it );
55+
EXPECT_EQ( m_set[ i ], *it );
56+
EXPECT_EQ( m_set( i ), *it );
57+
EXPECT_EQ( ptr[ i ], *it );
58+
EXPECT_EQ( slice[ i ], *it );
5659
++it;
5760
}
61+
62+
it = m_ref.begin();
63+
for( T const & val : m_set )
64+
{
65+
EXPECT_EQ( val, *it );
66+
++it;
67+
}
68+
69+
EXPECT_EQ( it, m_ref.end() );
5870
}
5971

6072
#define COMPARE_TO_REFERENCE { SCOPED_TRACE( "" ); compareToReference(); \

0 commit comments

Comments
 (0)