35 namespace sortedArrayManipulation
45 UNSORTED_NO_DUPLICATES,
46 SORTED_WITH_DUPLICATES,
47 UNSORTED_WITH_DUPLICATES
56 {
return desc == SORTED_UNIQUE || desc == SORTED_WITH_DUPLICATES; }
64 {
return desc == SORTED_UNIQUE || desc == UNSORTED_NO_DUPLICATES; }
71 template<
typename T >
84 std::ptrdiff_t
const nToAdd )
95 void insert( std::ptrdiff_t
const pos )
105 void set( std::ptrdiff_t
const pos,
106 std::ptrdiff_t
const valuePos )
122 void insert( std::ptrdiff_t
const nLeftToInsert,
123 std::ptrdiff_t
const valuePos,
124 std::ptrdiff_t
const pos,
125 std::ptrdiff_t
const prevPos )
138 void remove( std::ptrdiff_t
const pos )
150 void remove( std::ptrdiff_t
const nRemoved,
151 std::ptrdiff_t
const curPos,
152 std::ptrdiff_t
const nextPos )
165 template<
typename T >
175 bool operator() ( T
const & lhs, T
const & rhs )
const 176 {
return lhs < rhs; }
184 template<
typename T >
194 bool operator() ( T
const & lhs, T
const & rhs )
const 195 {
return lhs > rhs; }
208 template<
typename RandomAccessIterator,
211 RandomAccessIterator
const last,
212 Compare && comp=Compare() )
215 if( last - first > internal::INTROSORT_THRESHOLD )
217 internal::introsortLoop( first, last, comp );
220 internal::insertionSort( first, last - first, comp );
222 std::sort( first, last, std::forward< Compare >( comp ) );
238 template<
typename RandomAccessIteratorA,
239 typename RandomAccessIteratorB,
242 RandomAccessIteratorB dataFirst, Compare && comp=Compare() )
244 std::ptrdiff_t
const size = valueLast - valueFirst;
245 internal::DualIterator< RandomAccessIteratorA, RandomAccessIteratorB > dualIter( valueFirst, dataFirst );
247 auto dualCompare = dualIter.createComparator( std::forward< Compare >( comp ) );
249 if( size > internal::INTROSORT_THRESHOLD )
251 internal::introsortLoop( dualIter, dualIter + size, dualCompare );
254 internal::insertionSort( dualIter, size, dualCompare );
267 template< typename ITER, typename Compare=less< typename std::iterator_traits< ITER >::value_type > >
269 bool isSorted( ITER first, ITER
const last, Compare && comp=Compare() )
278 while( next != last )
280 if( comp( *next, *first ) )
305 template< typename ITER, typename Compare=less< typename std::iterator_traits< ITER >::value_type > >
316 std::ptrdiff_t numUnique = 1;
328 for(; next != last; ++next )
330 if( comp( *first, *next ) )
336 *first = std::move( *next );
354 template< typename ITER, typename Compare=less< typename std::iterator_traits< ITER >::value_type > >
356 std::ptrdiff_t
makeSortedUnique( ITER
const first, ITER
const last, Compare && comp=Compare() )
372 template< typename ITER, typename Compare=less< typename std::iterator_traits< ITER >::value_type > >
383 while( next != last )
385 if( comp( *next, *first ) || !comp( *first, *next ) )
409 template<
typename T,
typename Compare=less< T > >
411 std::ptrdiff_t
find( T
const *
const LVARRAY_RESTRICT ptr,
412 std::ptrdiff_t
const size,
414 Compare && comp=Compare() )
420 std::ptrdiff_t lower = 0;
421 std::ptrdiff_t upper = size;
422 while( lower != upper )
424 std::ptrdiff_t
const guess = (lower + upper) / 2;
425 if( comp( ptr[guess], value ) )
449 template<
typename T,
typename Compare=less< T > >
451 bool contains( T
const *
const LVARRAY_RESTRICT ptr,
452 std::ptrdiff_t
const size,
454 Compare && comp=Compare() )
456 std::ptrdiff_t
const pos =
find( ptr, size, value, std::forward< Compare >( comp ) );
457 return (pos != size) && (ptr[pos] == value);
471 template<
typename T,
typename CALLBACKS >
473 bool remove( T *
const LVARRAY_RESTRICT ptr,
474 std::ptrdiff_t
const size,
476 CALLBACKS && callBacks )
482 std::ptrdiff_t
const index =
find( ptr, size, value );
485 if( index == size || ptr[index] != value )
492 callBacks.remove( index );
511 template<
typename T,
typename ITER,
typename CALLBACKS=CallBacks< T > >
513 std::ptrdiff_t
remove( T *
const LVARRAY_RESTRICT ptr,
514 std::ptrdiff_t
const size,
517 CALLBACKS && callBacks=CALLBACKS() )
526 ITER valueToRemove = first;
527 std::ptrdiff_t removalPosition = 0;
528 for(; valueToRemove != last; ++valueToRemove )
530 removalPosition +=
find( ptr + removalPosition, size - removalPosition, *valueToRemove );
533 if( removalPosition == size )
538 if( ptr[ removalPosition ] == *valueToRemove )
545 if( valueToRemove == last )
551 std::ptrdiff_t nRemoved = 0;
552 for(; valueToRemove != last; )
555 ITER nextValueToRemove = valueToRemove;
557 std::ptrdiff_t nextRemovalPosition = removalPosition;
558 for(; nextValueToRemove != last; ++nextValueToRemove )
561 nextRemovalPosition +=
find( ptr + nextRemovalPosition, size - nextRemovalPosition, *nextValueToRemove );
564 if( nextRemovalPosition == size )
566 nextValueToRemove = last;
571 if( ptr[ nextRemovalPosition ] == *nextValueToRemove )
577 if( nextValueToRemove == last )
579 nextRemovalPosition = size;
585 callBacks.remove( nRemoved, removalPosition, nextRemovalPosition );
587 valueToRemove = nextValueToRemove;
588 removalPosition = nextRemovalPosition;
591 if( removalPosition == size )
598 for( std::ptrdiff_t i = size - nRemoved; i < size; ++i )
618 template<
typename T,
typename CALLBACKS=CallBacks< T > >
620 bool insert( T *
const LVARRAY_RESTRICT ptr,
621 std::ptrdiff_t
const size,
623 CALLBACKS && callBacks=CALLBACKS() )
629 std::ptrdiff_t insertPos = std::ptrdiff_t( -1 );
630 if( size == 0 || value < ptr[0] )
634 else if( ptr[size - 1] < value )
641 insertPos =
find( ptr, size, value );
645 if( insertPos != size && ptr[insertPos] == value )
647 callBacks.incrementSize( ptr, 0 );
652 T *
const newPtr = callBacks.incrementSize( ptr, 1 );
654 callBacks.insert( insertPos );
674 template<
typename T,
typename ITER,
typename CALLBACKS=CallBacks< T > >
676 std::ptrdiff_t
insert( T *
const LVARRAY_RESTRICT ptr,
677 std::ptrdiff_t
const size,
680 CALLBACKS && callBacks=CALLBACKS() )
692 T *
const newPtr = callBacks.incrementSize( ptr, numInserted );
695 for( ITER iter = first; iter != last; ++iter )
697 new ( newPtr + numInserted ) T( *iter );
698 callBacks.set( numInserted, numInserted );
706 std::ptrdiff_t nVals = 0;
707 std::ptrdiff_t nToInsert = 0;
708 std::ptrdiff_t upperBound = size;
710 while( iter != first )
714 upperBound =
find( ptr, upperBound, *iter );
715 if( upperBound == size || ptr[upperBound] != *iter )
722 T *
const newPtr = callBacks.incrementSize( ptr, nToInsert );
731 std::ptrdiff_t currentIndex = nVals - 1;
732 std::ptrdiff_t nLeftToInsert = nToInsert;
735 while( iter != first )
738 std::ptrdiff_t
const pos =
find( newPtr, upperBound, *iter );
741 if( pos != upperBound && newPtr[pos] == *iter )
749 new ( newPtr + pos + nLeftToInsert - 1 ) T( *iter );
750 callBacks.insert( nLeftToInsert, currentIndex, pos, upperBound );
#define LVARRAY_UNUSED_VARIABLE(X)
Mark X as an unused variable, used to silence compiler warnings.
#define LVARRAY_ASSERT(EXP)
Assert EXP is true with no message.
constexpr std::iterator_traits< ITER >::difference_type iterDistance(ITER first, ITER const last, std::input_iterator_tag)
constexpr bool isSorted(Description const desc)
bool isSortedUnique(ITER first, ITER const last, Compare &&comp=Compare())
void insert(std::ptrdiff_t const nLeftToInsert, std::ptrdiff_t const valuePos, std::ptrdiff_t const pos, std::ptrdiff_t const prevPos)
Callback signaling that the that the given value was inserted at the given position. Further information is provided in order to make the insertion efficient.
void shiftUp(T *const LVARRAY_RESTRICT ptr, std::ptrdiff_t const size, std::ptrdiff_t const index, std::ptrdiff_t const n)
Shift the values in the array at or above the given position up by the given amount. New uninitialized values take their place.
constexpr std::enable_if< std::is_signed< INDEX_TYPE >::value, bool >::type isPositive(INDEX_TYPE const i)
Contains helper functions for the sortedArrayManipulation routines. Much of this was adapted from stl...
This class provides a no-op callbacks interface for the ArrayManipulation sorted routines.
std::ptrdiff_t find(T const *const LVARRAY_RESTRICT ptr, std::ptrdiff_t const size, T const &value, Compare &&comp=Compare())
void emplace(T *const LVARRAY_RESTRICT ptr, std::ptrdiff_t const size, std::ptrdiff_t const index, ARGS &&... args)
Insert into the array constructing the new value in place.
std::ptrdiff_t makeSortedUnique(ITER const first, ITER const last, Compare &&comp=Compare())
Sort and remove duplicates from the array, duplicates aren't destroyed but they're moved out of...
void insert(std::ptrdiff_t const pos)
Callback signaling that a value was inserted at the given position.
Contains functions for manipulating a contiguous array of values.
bool contains(T const *const LVARRAY_RESTRICT ptr, std::ptrdiff_t const size, T const &value, Compare &&comp=Compare())
Contains a bunch of macro definitions.
T * incrementSize(T *const curPtr, std::ptrdiff_t const nToAdd)
Callback signaling that the size of the array has increased.
constexpr bool isUnique(Description const desc)
This class operates as functor similar to std::less.
void erase(T *const LVARRAY_RESTRICT ptr, std::ptrdiff_t const size, std::ptrdiff_t const index, std::ptrdiff_t const n=1)
Shift the values in the array at or above the given position down by the given amount overwriting the...
void makeSorted(RandomAccessIterator const first, RandomAccessIterator const last, Compare &&comp=Compare())
Sort the given values in place using the given comparator.
This class operates as functor similar to std::greater.
void shiftDown(T *const LVARRAY_RESTRICT ptr, std::ptrdiff_t const size, std::ptrdiff_t const index, std::ptrdiff_t const n)
Shift the values in the array at or above the given position down by the given amount overwriting the...
#define DISABLE_HD_WARNING
Disable host device warnings.
void dualSort(RandomAccessIteratorA valueFirst, RandomAccessIteratorA valueLast, RandomAccessIteratorB dataFirst, Compare &&comp=Compare())
Sort the given values in place using the given comparator and perform the same operations on the data...
std::ptrdiff_t removeDuplicates(ITER first, ITER const last, Compare &&comp=Compare())
Remove duplicates from the array, duplicates aren't destroyed but they're moved out of...
Description
Describes an as some combination of sorted/unsorted and unique/with duplicates.
#define LVARRAY_HOST_DEVICE
Mark a function for both host and device usage.