ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
RangeMinimum.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File RangeMinimum.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
84/*--------------------------------------------------------------------------*/
85/*----------------------------- DEFINITIONS --------------------------------*/
86/*--------------------------------------------------------------------------*/
87
88#ifndef __ADS_RangeMinimum
89 #define __ADS_RangeMinimum
90/* self-identification: #endif constant */
91
92/*--------------------------------------------------------------------------*/
93/*------------------------------ INCLUDES ----------------------------------*/
94/*--------------------------------------------------------------------------*/
95
96#include "ADS/Contract.h"
97
98#include <algorithm>
99#include <bit>
100#include <cstddef>
101#include <cstdint>
102#include <functional>
103#include <limits>
104#include <utility>
105#include <vector>
106
107/*--------------------------------------------------------------------------*/
108/*--------------------------- NAMESPACE ------------------------------------*/
109/*--------------------------------------------------------------------------*/
110
111namespace ADS
112{
113
137template< class T , class Compare = std::less< T > >
138[[nodiscard]] std::vector< std::size_t >
139cartesianTree( const std::vector< T > & A , Compare less = {} );
140
141/*--------------------------------------------------------------------------*/
142/*--------------------------- CLASS SparseTable ----------------------------*/
143/*--------------------------------------------------------------------------*/
144
146template< class T , class Compare = std::less< T > >
148{
149
150/*--------------------------------------------------------------------------*/
151 public:
153 SparseTable( void ) = default;
154
173 explicit SparseTable( std::vector< T > A , Compare less = {} );
174
190 [[nodiscard]] std::size_t query( std::size_t i , std::size_t j ) const;
191
193 [[nodiscard]] std::size_t size( void ) const { return( f_A.size() ); }
194
197 [[nodiscard]] const T & operator[]( std::size_t i ) const;
198
199/*------------------------- THE STANDARD INTERFACE -------------------------*/
200
201 // a constant view of the array, random access
202
203 using value_type = T;
204 using size_type = std::size_t;
205 using difference_type = std::ptrdiff_t;
206 using const_reference = const T &;
208 using const_iterator = typename std::vector< T >::const_iterator;
211
213 [[nodiscard]] const_iterator begin( void ) const { return( f_A.begin() ); }
215 [[nodiscard]] const_iterator end( void ) const { return( f_A.end() ); }
217 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
219 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
220
221/*--------------------------------------------------------------------------*/
222 private:
224 [[nodiscard]] std::size_t better( std::size_t a , std::size_t b ) const;
225
226 std::vector< T > f_A;
227 std::vector< std::vector< std::size_t > > f_M;
228 Compare f_less;
229
230 }; // end( class SparseTable )
231
232/*--------------------------------------------------------------------------*/
233/*--------------------- CLASS PlusMinusOneRangeMinimum ---------------------*/
234/*--------------------------------------------------------------------------*/
235
238{
239
240/*--------------------------------------------------------------------------*/
241 public:
243 PlusMinusOneRangeMinimum( void ) = default;
244
267 explicit PlusMinusOneRangeMinimum( std::vector< long > D );
268
292 [[nodiscard]] std::size_t query( std::size_t i , std::size_t j ) const;
293
295 [[nodiscard]] std::size_t size( void ) const { return( f_D.size() ); }
296
298 [[nodiscard]] std::size_t blockSize( void ) const { return( f_d ); }
299
300/*--------------------------------------------------------------------------*/
301 private:
304 [[nodiscard]] std::size_t inBlock( std::size_t k , std::size_t a ,
305 std::size_t b ) const;
306
307 std::vector< long > f_D;
308 std::size_t f_d = 1;
309 std::vector< std::uint32_t > f_sig;
310 std::vector< std::uint8_t > f_table;
311 std::vector< std::size_t > f_blockMin;
312 SparseTable< long > f_blocks;
313
314 }; // end( class PlusMinusOneRangeMinimum )
315
316/*--------------------------------------------------------------------------*/
317/*--------------------------- CLASS RangeMinimum ---------------------------*/
318/*--------------------------------------------------------------------------*/
319
322template< class T , class Compare = std::less< T > >
324{
325
326/*--------------------------------------------------------------------------*/
327 public:
329 RangeMinimum( void ) = default;
330
345 explicit RangeMinimum( std::vector< T > A , Compare less = {} );
346
363 [[nodiscard]] std::size_t query( std::size_t i , std::size_t j ) const;
364
366 [[nodiscard]] std::size_t size( void ) const { return( f_A.size() ); }
367
370 [[nodiscard]] const T & operator[]( std::size_t i ) const;
371
374 [[nodiscard]] const std::vector< std::size_t > & eulerTour( void ) const {
375 return( f_E );
376 }
377
378/*------------------------- THE STANDARD INTERFACE -------------------------*/
379
380 // a constant view of the array, random access
381
382 using value_type = T;
383 using size_type = std::size_t;
384 using difference_type = std::ptrdiff_t;
385 using const_reference = const T &;
387 using const_iterator = typename std::vector< T >::const_iterator;
390
392 [[nodiscard]] const_iterator begin( void ) const { return( f_A.begin() ); }
394 [[nodiscard]] const_iterator end( void ) const { return( f_A.end() ); }
396 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
398 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
399
400/*--------------------------------------------------------------------------*/
401 private:
402 std::vector< T > f_A;
403 std::vector< std::size_t > f_E;
404 std::vector< std::size_t > f_first;
406 Compare f_less;
407
408 }; // end( class RangeMinimum )
409
410/*--------------------------------------------------------------------------*/
411
412} // end( namespace ADS )
413
414/*--------------------------------------------------------------------------*/
415/*-------------------------- IMPLEMENTATION --------------------------------*/
416/*--------------------------------------------------------------------------*/
417
418#include "ADS/search/impl/RangeMinimum.tpp"
419
420/*--------------------------------------------------------------------------*/
421
422#endif /* RangeMinimum.h included */
423
424/*--------------------------------------------------------------------------*/
425/*------------------------ End File RangeMinimum.h -------------------------*/
426/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
the range minima of an array whose adjacent items differ by 1
Definition RangeMinimum.h:238
PlusMinusOneRangeMinimum(void)=default
an empty structure
std::size_t blockSize(void) const
the number of items in a block
Definition RangeMinimum.h:298
PlusMinusOneRangeMinimum(std::vector< long > D)
the blocks, their sparse table and the table of the signatures,
std::size_t size(void) const
the number of the items
Definition RangeMinimum.h:295
std::size_t query(std::size_t i, std::size_t j) const
the place of the leftmost minimum of ,
typename std::vector< T >::const_iterator const_iterator
an iterator on the items
Definition RangeMinimum.h:387
const T & operator[](std::size_t i) const
the item of place
std::size_t size(void) const
the number of the items
Definition RangeMinimum.h:366
const_iterator cbegin(void) const
the first item
Definition RangeMinimum.h:396
const_iterator begin(void) const
the first item
Definition RangeMinimum.h:392
RangeMinimum(std::vector< T > A, Compare less={})
the structure of ,
const T & const_reference
a reference to an item
Definition RangeMinimum.h:385
std::ptrdiff_t difference_type
the type of the distances
Definition RangeMinimum.h:384
std::size_t query(std::size_t i, std::size_t j) const
the place of the leftmost minimum of ,
const_reference reference
the same, constant
Definition RangeMinimum.h:386
const_iterator end(void) const
past the last item
Definition RangeMinimum.h:394
std::size_t size_type
the type of the sizes
Definition RangeMinimum.h:383
T value_type
the type of the items
Definition RangeMinimum.h:382
RangeMinimum(void)=default
an empty structure
const_iterator iterator
the same, constant
Definition RangeMinimum.h:389
const_iterator cend(void) const
past the last item
Definition RangeMinimum.h:398
const std::vector< std::size_t > & eulerTour(void) const
the nodes of the Euler tour of the Cartesian tree, of them
Definition RangeMinimum.h:374
typename std::vector< T >::const_iterator const_iterator
an iterator on the items
Definition RangeMinimum.h:208
const_reference reference
the same, constant
Definition RangeMinimum.h:207
SparseTable(void)=default
an empty table
const_iterator cend(void) const
past the last item
Definition RangeMinimum.h:219
const T & operator[](std::size_t i) const
the item of place
SparseTable(std::vector< T > A, Compare less={})
the table of ,
const_iterator end(void) const
past the last item
Definition RangeMinimum.h:215
const_iterator begin(void) const
the first item
Definition RangeMinimum.h:213
const_iterator iterator
the same, constant
Definition RangeMinimum.h:210
std::size_t size(void) const
the number of the items
Definition RangeMinimum.h:193
std::ptrdiff_t difference_type
the type of the distances
Definition RangeMinimum.h:205
std::size_t query(std::size_t i, std::size_t j) const
the place of the leftmost minimum of ,
const T & const_reference
a reference to an item
Definition RangeMinimum.h:206
std::size_t size_type
the type of the sizes
Definition RangeMinimum.h:204
T value_type
the type of the items
Definition RangeMinimum.h:203
const_iterator cbegin(void) const
the first item
Definition RangeMinimum.h:217
std::vector< std::size_t > cartesianTree(const std::vector< T > &A, Compare less={})
the parent of each item of in its Cartesian tree, the root (the leftmost minimum) having ; equal ite...
Definition ADT.h:96