ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SlidingAggregate.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------- File SlidingAggregate.h ------------------------*/
3/*--------------------------------------------------------------------------*/
55/*--------------------------------------------------------------------------*/
56/*----------------------------- DEFINITIONS --------------------------------*/
57/*--------------------------------------------------------------------------*/
58
59#ifndef __ADS_SlidingAggregate
60 #define __ADS_SlidingAggregate
61/* self-identification: #endif constant */
62
63/*--------------------------------------------------------------------------*/
64/*------------------------------ INCLUDES ----------------------------------*/
65/*--------------------------------------------------------------------------*/
66
67#include "ADS/Algebra.h"
68#include "ADS/Contract.h"
69#include "ADS/Portable.h"
70
71#include <algorithm>
72#include <concepts>
73#include <cstddef>
74#include <cstdint>
75#include <deque>
76#include <memory>
77#include <type_traits>
78#include <utility>
79#include <vector>
80
81/*--------------------------------------------------------------------------*/
82/*--------------------------- NAMESPACE ------------------------------------*/
83/*--------------------------------------------------------------------------*/
84
85namespace ADS
86{
87
88/*--------------------------------------------------------------------------*/
89/*------------------------------ THE POLICIES ------------------------------*/
90/*--------------------------------------------------------------------------*/
91
95{ };
96
100{ };
101
109
111namespace detail
112{
113
115template< class G , class P >
116constexpr window_kind windowKindOf( void )
117{
118 if constexpr( std::is_same_v< P , WorstCase > )
119 return( window_kind::real_time );
120 else if constexpr( Group< G > )
122 else if constexpr( Selective< G > &&
123 std::equality_comparable< typename G::Value > )
125 else
126 return( window_kind::two_stacks );
127 }
128
130template< class G >
131class SubtractWindow
132{
133 public:
134 using Value = typename G::Value;
135 [[nodiscard]] std::size_t size( void ) const { return( f_q.size() ); }
136 [[nodiscard]] const Value & front( void ) const { return( f_q.front() ); }
137 [[nodiscard]] Value aggregate( void ) const { return( f_s ); }
138 void push( const Value & x );
139 Value pop( void );
140 void clear( void ) {
141 f_q.clear();
142 f_s = G::identity();
143 f_since = 0;
144 }
145 template< class F >
146 void forEach( F && f ) const {
147 for( const auto & x : f_q )
148 f( x );
149 }
150 std::size_t f_period = 4096;
151 private:
152 std::deque< Value > f_q;
153 Value f_s = G::identity();
154 std::size_t f_since = 0;
155 };
156
159template< class G >
160class DequeWindow
161{
162 public:
163 using Value = typename G::Value;
164 [[nodiscard]] std::size_t size( void ) const { return( f_q.size() ); }
165 [[nodiscard]] const Value & front( void ) const { return( f_q.front() ); }
166 [[nodiscard]] Value aggregate( void ) const {
167 return( f_c.empty() ? G::identity() : f_c.front().second );
168 }
169 void push( const Value & x );
170 Value pop( void );
171 void clear( void ) {
172 f_q.clear();
173 f_c.clear();
174 }
175 template< class F >
176 void forEach( F && f ) const {
177 for( const auto & x : f_q )
178 f( x );
179 }
180
181 private:
182 std::deque< Value > f_q;
184 std::deque< std::pair< std::uint64_t , Value > > f_c;
185 std::uint64_t f_head = 0;
186 };
187
190template< class G >
191class StacksWindow
192{
193 public:
194 using Value = typename G::Value;
195 [[nodiscard]] std::size_t size( void ) const { return( f_q.size() ); }
196 [[nodiscard]] const Value & front( void ) const { return( f_q.front() ); }
197 [[nodiscard]] Value aggregate( void ) const {
198 return( f_f.empty() ? f_b : G::combine( f_f.back() , f_b ) );
199 }
200 void push( const Value & x ) {
201 f_q.push_back( x );
202 f_b = G::combine( f_b , x );
203 }
204 Value pop( void );
205 void clear( void ) {
206 f_q.clear();
207 f_f.clear();
208 f_b = G::identity();
209 }
210 template< class F >
211 void forEach( F && f ) const {
212 for( const auto & x : f_q )
213 f( x );
214 }
215
216 private:
217 std::deque< Value > f_q;
219 std::vector< Value > f_f;
220 Value f_b = G::identity();
221 };
222
226template< class Item , class Summary >
227class CellArray
228{
229 public:
231 CellArray( void ) = default;
232
234 explicit CellArray( std::size_t c )
235 : f_a( c ? std::allocator< Cell >().allocate( c ) : nullptr ) ,
236 f_cap( c ) { }
237
239 CellArray( CellArray && o ) noexcept { swap( o ); }
240
242 CellArray & operator=( CellArray && o ) noexcept {
243 swap( o );
244 return( *this );
245 }
246
248 ~CellArray() {
249 for( std::size_t i = f_lo ; i < f_hi ; ++i )
250 std::destroy_at( f_a + i );
251 if( f_a )
252 std::allocator< Cell >().deallocate( f_a , f_cap );
253 }
254
256 void swap( CellArray & o ) noexcept {
257 std::swap( f_a , o.f_a );
258 std::swap( f_cap , o.f_cap );
259 std::swap( f_lo , o.f_lo );
260 std::swap( f_hi , o.f_hi );
261 }
262
264 [[nodiscard]] std::size_t size( void ) const { return( f_hi - f_lo ); }
265
267 [[nodiscard]] std::size_t capacity( void ) const { return( f_cap ); }
268
270 [[nodiscard]] std::size_t lo( void ) const { return( f_lo ); }
271
273 [[nodiscard]] std::size_t hi( void ) const { return( f_hi ); }
274
276 [[nodiscard]] Item & at( std::size_t i ) { return( f_a[ i ].item ); }
277
279 [[nodiscard]] const Item & at( std::size_t i ) const {
280 return( f_a[ i ].item );
281 }
282
284 [[nodiscard]] const Summary & summary( std::size_t i ) const {
285 return( f_a[ i ].s );
286 }
287
289 template< class U , class S >
290 void push( U && x , S && s ) {
291 ADS_PRE( f_hi < f_cap );
292 std::construct_at( f_a + f_hi , Cell{ Item( std::forward< U >( x ) ) ,
293 Summary( std::forward< S >( s ) ) } );
294 ++f_hi;
295 }
296
298 void pop( void ) {
299 ADS_PRE( f_hi > f_lo );
300 std::destroy_at( f_a + --f_hi );
301 }
302
304 void popBottom( void ) {
305 ADS_PRE( f_hi > f_lo );
306 std::destroy_at( f_a + f_lo++ );
307 }
308
309 private:
311 struct Cell
312 {
313 Item item;
314 Summary s;
315 };
316
317 Cell * f_a = nullptr;
318 std::size_t f_cap = 0;
319 std::size_t f_lo = 0;
320 std::size_t f_hi = 0;
321 };
322
329template< class Link >
330class RealTimeRotation
331{
332 public:
333 using Item = typename Link::Item;
334 using Array = CellArray< Item , typename Link::Summary >;
335
337 explicit RealTimeRotation( Link l = Link() )
338 : f_R( 1 ) , f_link( std::move( l ) ) { }
339
341 RealTimeRotation( const RealTimeRotation & o )
342 : f_R( 1 ) , f_link( o.f_link ) {
343 o.forEach( [ & ]( const Item & x ) { push( x ); } );
344 }
345
347 RealTimeRotation( RealTimeRotation && o ) noexcept
348 : f_R( 1 ) , f_link( o.f_link ) {
349 swap( o );
350 }
351
353 RealTimeRotation & operator=( const RealTimeRotation & o ) {
354 RealTimeRotation c( o );
355 swap( c );
356 return( *this );
357 }
358
360 RealTimeRotation & operator=( RealTimeRotation && o ) noexcept {
361 swap( o );
362 return( *this );
363 }
364
366 void swap( RealTimeRotation & o ) noexcept;
367
369 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
370
372 [[nodiscard]] const Item & front( void ) const {
373 return( f_F.at( f_F.hi() - 1 ) );
374 }
375
377 [[nodiscard]] decltype( auto ) aggregate( void ) const {
378 return( f_link.aggregate( *this ) );
379 }
380
382 void push( const Item & x );
383
385 Item pop( void );
386
388 void clear( void ) {
389 RealTimeRotation e( f_link );
390 swap( e );
391 }
392
394 template< class F >
395 void forEach( F && f ) const;
396
397 Array f_F;
398 Array f_R;
399 Array f_Rf;
400 Array f_N;
401 std::size_t f_r0 = 0;
402 std::size_t f_i = 0;
403 std::size_t f_n = 0;
404 bool f_rot = false;
405 ADS_NO_UNIQUE_ADDRESS Link f_link;
406
407 private:
409 void startIfDue( void );
410
412 void steps( int k );
413 };
414
417template< class G >
418struct AggLink
419{
420 using Item = typename G::Value;
421 using Summary = typename G::Value;
422
424 template< class A >
425 static void pushFront( A & a , Item x ) {
426 Item s = a.size() > 0 ? G::combine( x , a.summary( a.hi() - 1 ) ) : x;
427 a.push( std::move( x ) , std::move( s ) );
428 }
429
431 template< class A >
432 static void pushBack( A & a , Item x ) {
433 Item s = a.size() > 0 ? G::combine( a.summary( a.hi() - 1 ) , x ) : x;
434 a.push( std::move( x ) , std::move( s ) );
435 }
436
438 template< class Q >
439 static Item aggregate( const Q & q );
440 };
441
444template< class G >
445using RealTimeWindow = RealTimeRotation< AggLink< G > >;
446
448template< class G , window_kind K >
449struct window_of
450{
451 using type = RealTimeWindow< G >;
452 };
453
454template< class G >
455struct window_of< G , window_kind::subtract_on_evict >
456{
457 using type = SubtractWindow< G >;
458 };
459
460template< class G >
461struct window_of< G , window_kind::monotone_deque >
462{
463 using type = DequeWindow< G >;
464 };
465
466template< class G >
467struct window_of< G , window_kind::two_stacks >
468{
469 using type = StacksWindow< G >;
470 };
471
472} // end( namespace detail )
474
475/*--------------------------------------------------------------------------*/
476/*------------------------- CLASS SlidingAggregate -------------------------*/
477/*--------------------------------------------------------------------------*/
478
482template< Monoid G , class Policy = Amortized >
484{
485
486 static_assert( std::is_same_v< Policy , Amortized > ||
487 std::is_same_v< Policy , WorstCase > ,
488 "ADS: the policy of a window is Amortized or WorstCase" );
489
490/*--------------------------------------------------------------------------*/
491 public:
492 using Value = typename G::Value;
493 using Item = Value;
494 using Algebra = G;
495
497 static constexpr window_kind kind = detail::windowKindOf< G , Policy >();
498
500 [[nodiscard]] bool empty( void ) const { return( f_w.size() == 0 ); }
501
503 [[nodiscard]] std::size_t size( void ) const { return( f_w.size() ); }
504
520 void enqueue( const Value & x ) {
521 [[maybe_unused]] const std::size_t n = size();
522 f_w.push( x );
523 ADS_POST( size() == n + 1 );
524 }
525
547 Value dequeue( void ) {
548 ADS_PRE( ! empty() );
549 [[maybe_unused]] const std::size_t n = size();
550 Value x = f_w.pop();
551 ADS_POST( size() == n - 1 );
552 return( x );
553 }
554
557 [[nodiscard]] const Value & top( void ) const {
558 ADS_PRE( ! empty() );
559 return( f_w.front() );
560 }
561
564 [[nodiscard]] Value aggregate( void ) const { return( f_w.aggregate() ); }
565
567 template< class F >
568 void forEach( F && f ) const {
569 f_w.forEach( std::forward< F >( f ) );
570 }
571
573 void clear( void ) { f_w.clear(); }
574
577 [[nodiscard]] std::size_t period( void ) const
579 {
580 return( f_w.f_period );
581 }
582
585 void period( std::size_t p )
587 {
588 ADS_PRE( p >= 1 );
589 f_w.f_period = p;
590 }
591
592/*--------------------------------------------------------------------------*/
593 private:
594 typename detail::window_of< G , kind >::type f_w;
595
596 }; // end( class SlidingAggregate )
597
598/*--------------------------------------------------------------------------*/
599
600} // end( namespace ADS )
601
602/*--------------------------------------------------------------------------*/
603/*-------------------------- IMPLEMENTATION --------------------------------*/
604/*--------------------------------------------------------------------------*/
605
606#include "ADS/queue/impl/SlidingAggregate.tpp"
607
608/*--------------------------------------------------------------------------*/
609
610#endif /* SlidingAggregate.h included */
611
612/*--------------------------------------------------------------------------*/
613/*---------------------- End File SlidingAggregate.h -----------------------*/
614/*--------------------------------------------------------------------------*/
The algebra of the aggregates, beyond the book: the concepts by which a structure that keeps a combin...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
a QUEUE of values of the monoid G with the aggregate of its values, each operation in time,...
Definition SlidingAggregate.h:484
Value dequeue(void)
removes the item at the front and returns it
Definition SlidingAggregate.h:547
const Value & top(void) const
the item at the front
Definition SlidingAggregate.h:557
static constexpr window_kind kind
the algorithm chosen by the algebra and the policy
Definition SlidingAggregate.h:497
void forEach(F &&f) const
f( x ) on the items from the front to the back,
Definition SlidingAggregate.h:568
void enqueue(const Value &x)
at the back
Definition SlidingAggregate.h:520
Value aggregate(void) const
the aggregate of the items from the front to the back, the identity if there is none
Definition SlidingAggregate.h:564
void period(std::size_t p)
sets the period of the rebuilding to
Definition SlidingAggregate.h:585
G Algebra
their algebra
Definition SlidingAggregate.h:494
std::size_t period(void) const
the removals after which a group of floating point values is rebuilt from its items (subtract-on-evic...
Definition SlidingAggregate.h:577
bool empty(void) const
true if there is no item
Definition SlidingAggregate.h:500
void clear(void)
removes all the items
Definition SlidingAggregate.h:573
std::size_t size(void) const
the number of items
Definition SlidingAggregate.h:503
typename G::Value Value
the values
Definition SlidingAggregate.h:492
Value Item
the items of the QUEUE
Definition SlidingAggregate.h:493
#define ADS_POST(cond)
a postcondition, checked from level 2
Definition Contract.h:124
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
#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
GrB_Type type(void)
the type of GraphBLAS of the numbers T: GrB_BOOL, the integers of 8 to 64 bits with or without sign,...
Definition GraphBLAS.h:128
window_kind
the algorithm of a sliding window [see SlidingAggregate]
Definition SlidingAggregate.h:103
@ subtract_on_evict
Definition SlidingAggregate.h:104
@ monotone_deque
Definition SlidingAggregate.h:105
@ real_time
Definition SlidingAggregate.h:107
@ two_stacks
Definition SlidingAggregate.h:106
Definition ADT.h:96
the policy of a sliding window whose operations cost amortized time, by the algorithm that suits the...
Definition SlidingAggregate.h:95
the policy of a sliding window whose operations cost time in the worst case
Definition SlidingAggregate.h:100