ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
StaticSearch.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File StaticSearch.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
99/*--------------------------------------------------------------------------*/
100/*----------------------------- DEFINITIONS --------------------------------*/
101/*--------------------------------------------------------------------------*/
102
103#ifndef __ADS_StaticSearch
104 #define __ADS_StaticSearch
105/* self-identification: #endif constant */
106
107/*--------------------------------------------------------------------------*/
108/*------------------------------ INCLUDES ----------------------------------*/
109/*--------------------------------------------------------------------------*/
110
111#include "ADS/Contract.h"
112#include "ADS/Portable.h"
114#include "ADS/memory/Prefetch.h"
115#include "ADS/search/CountLess.h"
116
117#include <algorithm>
118#include <cstddef>
119#include <functional>
120#include <limits>
121#include <memory>
122#include <new>
123#include <type_traits>
124#include <vector>
125
126
127/*--------------------------------------------------------------------------*/
128/*--------------------------- NAMESPACE ------------------------------------*/
129/*--------------------------------------------------------------------------*/
130
131namespace ADS
132{
133
155template< class T , class C = std::less<> >
156std::size_t branchlessLowerBound( const std::vector< T > & A , const T & v ,
157 C less = C() );
158
159/*--------------------------------------------------------------------------*/
160/*--------------------------- CLASS SPlusTree ------------------------------*/
161/*--------------------------------------------------------------------------*/
162
164template< class T , class Less = std::less< T > , std::size_t Bytes = 64 >
165 requires std::is_trivially_copyable_v< T >
167{
168
169/*--------------------------------------------------------------------------*/
170 public:
171 using Item = T;
172
174 static constexpr std::size_t B =
175 sizeof( T ) <= Bytes ? std::max< std::size_t >( Bytes / sizeof( T ) , 2 )
176 : 2;
177
179 SPlusTree( void ) = default;
180
201 explicit SPlusTree( const std::vector< T > & A , Less less = Less() );
202
204 SPlusTree( const SPlusTree & o );
205
207 SPlusTree( SPlusTree && o ) noexcept = default;
208
211
229 [[nodiscard]] std::size_t lowerBound( const T & v ) const;
230
236 [[nodiscard]] std::size_t upperBound( const T & v ) const;
237
239 [[nodiscard]] bool contains( const T & v ) const;
240
243 [[nodiscard]] const T & operator[]( std::size_t i ) const;
244
246 [[nodiscard]] std::size_t size( void ) const;
247
249 [[nodiscard]] bool empty( void ) const;
250
252 [[nodiscard]] int height( void ) const;
253
254/*--------------------------------------------------------------------------*/
255 private:
256 static constexpr bool FAST =
257 std::is_arithmetic_v< T > && ( std::is_same_v< Less , std::less< T > > ||
258 std::is_same_v< Less , std::less<> > );
259
260 [[nodiscard]] static constexpr T pad( void ) {
261 if constexpr( std::numeric_limits< T >::has_infinity )
262 return( std::numeric_limits< T >::infinity() );
263 else
264 return( std::numeric_limits< T >::max() );
265 }
266
269 [[nodiscard]] static T * allocate( std::size_t n );
270
271 struct Free
272 {
273 std::size_t n = 0;
274 void operator()( T * p ) const noexcept {
275 if( n * sizeof( T ) >= HUGE_BYTES )
277 else
278 ::operator delete( p , std::align_val_t( 64 ) );
279 }
280 };
281
282 static constexpr std::size_t HUGE_BYTES = std::size_t( 1 ) << 21;
283
286 template< bool Up >
287 [[nodiscard]] std::size_t count( const T * a , const T & v ,
288 std::size_t m ) const;
289
290 template< bool Up >
291 [[nodiscard]] std::size_t search( const T & v ) const;
292
293 void layout( const std::vector< T > & A );
294
295 std::vector< std::size_t > f_size;
296
297 std::unique_ptr< T[] , Free > f_a;
298 std::vector< std::size_t > f_off;
299 std::size_t f_items = 0;
300 std::size_t f_n = 0;
301 ADS_NO_UNIQUE_ADDRESS Less f_less;
302
303 }; // end( class SPlusTree )
304
305/*--------------------------------------------------------------------------*/
306
307} // end( namespace ADS )
308
309/*--------------------------------------------------------------------------*/
310/*-------------------------- IMPLEMENTATION --------------------------------*/
311/*--------------------------------------------------------------------------*/
312
313#include "ADS/search/impl/StaticSearch.tpp"
314
315/*--------------------------------------------------------------------------*/
316
317#endif /* StaticSearch.h included */
318
319/*--------------------------------------------------------------------------*/
320/*------------------------ End File StaticSearch.h -------------------------*/
321/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The count of the items of a short array smaller than a key, the search in a node of the B-trees in ma...
An allocator for the large blocks of a structure, beyond the book: a request of at least one huge pag...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
The prefetch of a line of the cache, beyond the book: ADS::prefetch( p ) asks the processor to bring ...
an allocator of objects of type T, on huge pages for the large blocks
Definition HugePageAllocator.h:169
void deallocate(T *p, std::size_t n) noexcept
the room of objects given back: kept by the thread if it is of 8 MB or more and the cache has room,...
Definition HugePageAllocator.h:221
std::size_t upperBound(const T &v) const
the first place of an item larger than , size() if none
std::size_t lowerBound(const T &v) const
the first place of an item not smaller than , size() if none
const T & operator[](std::size_t i) const
the item of place , in the sorted order
T Item
the type of the items
Definition StaticSearch.h:171
SPlusTree(SPlusTree &&o) noexcept=default
the tree , moved
std::size_t size(void) const
the number of the items
static constexpr std::size_t B
the items of a node, a line of cache
Definition StaticSearch.h:174
SPlusTree(const SPlusTree &o)
a copy of the tree
int height(void) const
the levels, the leaves included
SPlusTree(void)=default
an empty tree
bool empty(void) const
true if there are no items
SPlusTree & operator=(SPlusTree o) noexcept
the tree , assigned by a swap
SPlusTree(const std::vector< T > &A, Less less=Less())
the tree on the sorted items of , in
bool contains(const T &v) const
true if is an item, by lowerBound( v )
#define ADS_NO_UNIQUE_ADDRESS
an empty member that takes no storage, by the attribute that does so (the standard one is accepted bu...
Definition Portable.h:81
std::size_t branchlessLowerBound(const std::vector< T > &A, const T &v, C less=C())
the first place of an item not smaller than in the sorted , if none, by the binary search with no b...
Definition ADT.h:96