Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template< typename T >
- struct Compare
- {
- int operator () ( const T& a, const T& b ) const
- {
- return a == b ? 0 : ( a < b ? -1 : 1 );
- }
- };
- template< typename T, class P = Compare< T >, class C = TArray< T > >
- struct TSorted
- {
- TSorted(){}
- void Add( const T& val )
- {
- size_t off = FindNext( val );
- cont.Insert( off, val );
- }
- INLINE bool Remove( const T& val )
- {
- size_t whr = Find( val );
- if( whr >= cont.Size() )
- return FALSE;
- Erase( whr );
- return TRUE;
- }
- INLINE void Erase( size_t at ) { cont.Erase( at, at ); }
- INLINE void Erase( size_t from, size_t to ) { cont.Erase( from, to ); }
- INLINE void Clear() { cont.Clear(); }
- size_t FindNext( const T& val ) const
- {
- int32_t min = 0, max = cont.Size() - 1;
- while( min <= max )
- {
- int32_t mid = ( min + max ) / 2;
- if( pred( val, cont[ mid ] ) < 0 )
- max = mid - 1;
- else
- min = mid + 1;
- }
- return MAX( min, max );
- }
- size_t Find( const T& val ) const
- {
- int32_t min = 0, max = cont.Size() - 1;
- while( min <= max )
- {
- int32_t mid = ( min + max ) / 2;
- int32_t chk = pred( val, cont[ mid ] );
- if( chk == 0 )
- return mid;
- else if( chk < 0 )
- max = mid - 1;
- else
- min = mid + 1;
- }
- return SIZE_MAX;
- }
- template< typename Tst >
- size_t Find( Tst& val ) const
- {
- int32_t min = 0, max = cont.Size() - 1;
- while( min <= max )
- {
- int32_t mid = ( min + max ) / 2;
- int32_t chk = val( cont[ mid ] );
- if( chk == 0 )
- return mid;
- else if( chk < 0 )
- max = mid - 1;
- else
- min = mid + 1;
- }
- return SIZE_MAX;
- }
- FINLINE T& At( size_t pos ){ return cont.At( pos ); }
- FINLINE const T& At( size_t pos ) const{ return cont.At( pos ); }
- FINLINE T& operator [] ( size_t pos ){ return cont[ pos ]; }
- FINLINE const T& operator [] ( size_t pos ) const{ return cont[ pos ]; }
- FINLINE size_t Size() const{ return cont.Size(); }
- P pred;
- C cont;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement