59#ifndef __ADS_SlidingAggregate
60 #define __ADS_SlidingAggregate
115template<
class G ,
class P >
118 if constexpr( std::is_same_v< P , WorstCase > )
120 else if constexpr( Group< G > )
122 else if constexpr( Selective< G > &&
123 std::equality_comparable< typename G::Value > )
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 );
146 void forEach( F && f )
const {
147 for(
const auto & x : f_q )
150 std::size_t f_period = 4096;
152 std::deque< Value > f_q;
153 Value f_s = G::identity();
154 std::size_t f_since = 0;
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 );
169 void push(
const Value & x );
176 void forEach( F && f )
const {
177 for(
const auto & x : f_q )
182 std::deque< Value > f_q;
184 std::deque< std::pair< std::uint64_t , Value > > f_c;
185 std::uint64_t f_head = 0;
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 ) );
200 void push(
const Value & x ) {
202 f_b = G::combine( f_b , x );
211 void forEach( F && f )
const {
212 for(
const auto & x : f_q )
217 std::deque< Value > f_q;
219 std::vector< Value > f_f;
220 Value f_b = G::identity();
226template<
class Item ,
class Summary >
231 CellArray(
void ) =
default;
234 explicit CellArray( std::size_t c )
235 : f_a( c ? std::allocator< Cell >().allocate( c ) : nullptr ) ,
239 CellArray( CellArray && o )
noexcept { swap( o ); }
242 CellArray & operator=( CellArray && o )
noexcept {
249 for( std::size_t i = f_lo ; i < f_hi ; ++i )
250 std::destroy_at( f_a + i );
252 std::allocator< Cell >().deallocate( f_a , f_cap );
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 );
264 [[nodiscard]] std::size_t size(
void )
const {
return( f_hi - f_lo ); }
267 [[nodiscard]] std::size_t capacity(
void )
const {
return( f_cap ); }
270 [[nodiscard]] std::size_t lo(
void )
const {
return( f_lo ); }
273 [[nodiscard]] std::size_t hi(
void )
const {
return( f_hi ); }
276 [[nodiscard]] Item & at( std::size_t i ) {
return( f_a[ i ].item ); }
279 [[nodiscard]]
const Item & at( std::size_t i )
const {
280 return( f_a[ i ].item );
284 [[nodiscard]]
const Summary & summary( std::size_t i )
const {
285 return( f_a[ i ].s );
289 template<
class U ,
class S >
290 void push( U && x , S && s ) {
292 std::construct_at( f_a + f_hi , Cell{ Item( std::forward< U >( x ) ) ,
293 Summary( std::forward< S >( s ) ) } );
300 std::destroy_at( f_a + --f_hi );
304 void popBottom(
void ) {
306 std::destroy_at( f_a + f_lo++ );
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;
329template<
class Link >
330class RealTimeRotation
333 using Item =
typename Link::Item;
334 using Array = CellArray< Item , typename Link::Summary >;
337 explicit RealTimeRotation( Link l = Link() )
338 : f_R( 1 ) , f_link( std::move( l ) ) { }
341 RealTimeRotation(
const RealTimeRotation & o )
342 : f_R( 1 ) , f_link( o.f_link ) {
343 o.forEach( [ & ](
const Item & x ) { push( x ); } );
347 RealTimeRotation( RealTimeRotation && o ) noexcept
348 : f_R( 1 ) , f_link( o.f_link ) {
353 RealTimeRotation & operator=(
const RealTimeRotation & o ) {
354 RealTimeRotation c( o );
360 RealTimeRotation & operator=( RealTimeRotation && o )
noexcept {
366 void swap( RealTimeRotation & o )
noexcept;
369 [[nodiscard]] std::size_t size(
void )
const {
return( f_n ); }
372 [[nodiscard]]
const Item & front(
void )
const {
373 return( f_F.at( f_F.hi() - 1 ) );
377 [[nodiscard]]
decltype( auto ) aggregate(
void )
const {
378 return( f_link.aggregate( *
this ) );
382 void push(
const Item & x );
389 RealTimeRotation e( f_link );
395 void forEach( F && f )
const;
401 std::size_t f_r0 = 0;
409 void startIfDue(
void );
420 using Item =
typename G::Value;
421 using Summary =
typename G::Value;
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 ) );
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 ) );
439 static Item aggregate(
const Q & q );
445using RealTimeWindow = RealTimeRotation< AggLink< G > >;
448template<
class G , window_kind K >
451 using type = RealTimeWindow< G >;
457 using type = SubtractWindow< G >;
463 using type = DequeWindow< G >;
469 using type = StacksWindow< G >;
482template< Mono
id G ,
class Policy = Amortized >
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" );
500 [[nodiscard]]
bool empty(
void )
const {
return( f_w.size() == 0 ); }
503 [[nodiscard]] std::size_t
size(
void )
const {
return( f_w.size() ); }
521 [[maybe_unused]]
const std::size_t n =
size();
549 [[maybe_unused]]
const std::size_t n =
size();
559 return( f_w.front() );
569 f_w.forEach( std::forward< F >( f ) );
577 [[nodiscard]] std::size_t
period(
void )
const
580 return( f_w.f_period );
594 typename detail::window_of< G , kind >::type f_w;
606#include "ADS/queue/impl/SlidingAggregate.tpp"
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
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