ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
IntervalTree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File IntervalTree.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
55/*--------------------------------------------------------------------------*/
56/*----------------------------- DEFINITIONS --------------------------------*/
57/*--------------------------------------------------------------------------*/
58
59#ifndef __ADS_IntervalTree
60 #define __ADS_IntervalTree
61/* self-identification: #endif constant */
62
63/*--------------------------------------------------------------------------*/
64/*------------------------------ INCLUDES ----------------------------------*/
65/*--------------------------------------------------------------------------*/
66
67#include "ADS/Contract.h"
69#include "ADS/tree/JoinTree.h"
70
71#include <concepts>
72#include <cstddef>
73#include <optional>
74#include <utility>
75#include <vector>
76
77/*--------------------------------------------------------------------------*/
78/*--------------------------- NAMESPACE ------------------------------------*/
79/*--------------------------------------------------------------------------*/
80
81namespace ADS
82{
83
84/*--------------------------------------------------------------------------*/
85/*-------------------------- CLASS IntervalTree ----------------------------*/
86/*--------------------------------------------------------------------------*/
87
89template< std::totally_ordered T >
91{
92
93/*--------------------------------------------------------------------------*/
94 public:
95 using Interval = std::pair< T , T >;
96
98 struct MaxEnd
99 {
101 using Value = std::optional< T >;
103 static Value identity( void ) { return( std::nullopt ); }
105 static Value from( const Interval & i , const std::size_t & ) {
106 return( i.second );
107 }
108
109 static Value combine( const Value & x , const Value & y ) {
110 if( ! x )
111 return( y );
112 if( ! y )
113 return( x );
114 return( *x < *y ? y : x );
115 }
116 };
117
119 IntervalTree( void ) = default;
120
138 explicit IntervalTree( std::vector< Interval > I );
139
142 template< Executor E >
143 IntervalTree( E & ex , std::vector< Interval > I );
144
157 void insert( const T & a , const T & b );
158
172 void remove( const T & a , const T & b );
173
175 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
176
194 [[nodiscard]] bool stabbed( const T & x ) const;
195
217 template< class F >
218 void stab( const T & x , F && f ) const;
219
231 [[nodiscard]] std::size_t count( const T & x ) const;
232
233/*--------------------------------------------------------------------------*/
234 private:
236
238 static std::vector< std::pair< Interval , std::size_t > >
239 counted( std::vector< Interval > & I );
240
241 Map f_map;
242 std::size_t f_n = 0;
243
244 }; // end( class IntervalTree )
245
246/*--------------------------------------------------------------------------*/
247
248} // end( namespace ADS )
249
250/*--------------------------------------------------------------------------*/
251/*-------------------------- IMPLEMENTATION --------------------------------*/
252/*--------------------------------------------------------------------------*/
253
254#include "ADS/geometry/impl/IntervalTree.tpp"
255
256/*--------------------------------------------------------------------------*/
257
258#endif /* IntervalTree.h included */
259
260/*--------------------------------------------------------------------------*/
261/*------------------------- End File IntervalTree.h ------------------------*/
262/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The layer of execution of the parallel algorithms of ADS, beyond the book: an executor runs the calls...
The binary search trees balanced by join alone, and the augmented maps, beyond the book: JoinTree< K ...
std::size_t count(const T &x) const
the intervals that contain , with their copies, by stab();
IntervalTree(std::vector< Interval > I)
the intervals of , sorted, counted and inserted by multiInsert()
bool stabbed(const T &x) const
whether an interval contains ,
void insert(const T &a, const T &b)
one more copy of ,
void remove(const T &a, const T &b)
one copy less of , if there,
IntervalTree(void)=default
no interval
void stab(const T &x, F &&f) const
f( a , b ) once per copy of each interval that contains , by increasing ; for distinct intervals re...
std::pair< T, T > Interval
Definition IntervalTree.h:95
std::size_t size(void) const
the intervals, with their copies
Definition IntervalTree.h:175
IntervalTree(E &ex, std::vector< Interval > I)
the intervals of , as above, on the threads of ex
a DICTIONARY on a binary search tree balanced by the join of B alone, augmented by G,...
Definition JoinTree.h:291
Definition ADT.h:96
the augmentation: the largest right end, nothing for no interval
Definition IntervalTree.h:99
std::optional< T > Value
the largest right end, or nothing
Definition IntervalTree.h:101
static Value from(const Interval &i, const std::size_t &)
the right end of the interval
Definition IntervalTree.h:105
static Value identity(void)
no interval, the identity of the maximum
Definition IntervalTree.h:103
static Value combine(const Value &x, const Value &y)
, nothing being smaller than any end
Definition IntervalTree.h:109