ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
MaterializedAggregate.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------- File MaterializedAggregate.h ----------------------*/
3/*--------------------------------------------------------------------------*/
50/*--------------------------------------------------------------------------*/
51/*----------------------------- DEFINITIONS --------------------------------*/
52/*--------------------------------------------------------------------------*/
53
54#ifndef __ADS_MaterializedAggregate
55 #define __ADS_MaterializedAggregate
56/* self-identification: #endif constant */
57
58/*--------------------------------------------------------------------------*/
59/*------------------------------ INCLUDES ----------------------------------*/
60/*--------------------------------------------------------------------------*/
61
62#include "ADS/Algebra.h"
63#include "ADS/Contract.h"
64#include "ADS/Modification.h"
65#include "ADS/tree/JoinTree.h"
66
67#include <cmath>
68#include <concepts>
69#include <cstddef>
70#include <set>
71#include <type_traits>
72
73/*--------------------------------------------------------------------------*/
74/*--------------------------- NAMESPACE ------------------------------------*/
75/*--------------------------------------------------------------------------*/
76
77namespace ADS
78{
79
81namespace detail
82{
83
85struct AnyPair
86{
87 template< class A , class B >
88 void operator()( const A & , const B & ) const { }
89 };
90
92template< class S >
93concept pair_visitable = requires( const S & s )
94{ s.forEach( AnyPair() ); };
95
96} // end( namespace detail )
98
99/*--------------------------------------------------------------------------*/
100/*---------------------- CLASS MaterializedAggregate -----------------------*/
101/*--------------------------------------------------------------------------*/
102
106template< class G , std::size_t Period = 1024 >
109{
110
111/*--------------------------------------------------------------------------*/
112 public:
113 using Value = typename G::Value;
114
116 enum class Kind {
120 };
121
123 static constexpr Kind kind =
125 : ( Selective< G > && std::equality_comparable< Value > ) ? Kind::Selective
126 : Kind::Monoid;
127
129 MaterializedAggregate( void ) = default;
130
132 template< detail::pair_visitable S >
133 explicit MaterializedAggregate( const S & s ) {
134 bind( s );
135 }
136
139 template< detail::pair_visitable S >
140 void bind( const S & s );
141
142/*--------------------------------------------------------------------------*/
143/*------------------------------ THE EVENTS --------------------------------*/
144/*--------------------------------------------------------------------------*/
145
168 template< class H , class K , class V >
170
191 template< class H , class K , class V >
192 void on( const mod::Erased< H , K , V > & e ) noexcept;
193
216 template< class H , class V >
217 void on( const mod::Assigned< H , V > & e );
218
220 void on( const mod::Cleared & ) noexcept;
221
223 void on( const mod::Rebuilt & ) noexcept { f_stale = true; }
224
225/*--------------------------------------------------------------------------*/
226/*------------------------------ THE QUERIES -------------------------------*/
227/*--------------------------------------------------------------------------*/
228
248 [[nodiscard]] Value value( void ) const;
249
251 [[nodiscard]] bool stale( void ) const { return( f_stale ); }
252
254 [[nodiscard]] bool bound( void ) const { return( f_src != nullptr ); }
255
257 [[nodiscard]] std::size_t recomputations( void ) const {
258 return( f_recomputed );
259 }
260
264 template< detail::pair_visitable S >
265 [[nodiscard]] bool audit( const S & s ) const;
266
267/*--------------------------------------------------------------------------*/
268 private:
271 struct Before
272 {
273 bool operator()( const Value & x , const Value & y ) const {
274 return( ! ( x == y ) && ( G::combine( x , y ) == x ) );
275 }
276 };
277
279 struct NoSet
280 { };
281 using Set = std::conditional_t< kind == Kind::Selective ,
282 std::multiset< Value , Before > , NoSet >;
283
285 void changed( void ) noexcept;
286
288 void recompute( void ) const;
289
291 template< class S >
292 static Value fold( const S & s );
293
294 mutable Value f_a = G::identity();
295 ADS_NO_UNIQUE_ADDRESS mutable Set f_set;
296 mutable bool f_stale = false;
297 mutable std::size_t f_drift = 0;
298 mutable std::size_t f_recomputed = 0;
299 const void * f_src = nullptr;
301 void ( *f_redo )( const void * , const MaterializedAggregate & ) = nullptr;
302
303 }; // end( class MaterializedAggregate )
304
305/*--------------------------------------------------------------------------*/
306
307} // end( namespace ADS )
308
309/*--------------------------------------------------------------------------*/
310/*-------------------------- IMPLEMENTATION --------------------------------*/
311/*--------------------------------------------------------------------------*/
312
313#include "ADS/tree/impl/MaterializedAggregate.tpp"
314
315/*--------------------------------------------------------------------------*/
316
317#endif /* MaterializedAggregate.h included */
318
319/*--------------------------------------------------------------------------*/
320/*---------------------- End File MaterializedAggregate.h ------------------*/
321/*--------------------------------------------------------------------------*/
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 binary search trees balanced by join alone, and the augmented maps, beyond the book: JoinTree< K ...
The typed modifications, beyond the book: the vocabulary by which a structure tells the observers att...
MaterializedAggregate(void)=default
the identity, with no structure bound
bool audit(const S &s) const
whether the aggregate (if not stale) is that of : equal for exact values, within a relative tolerance...
Value value(void) const
the aggregate, recomputed from the structure bound if stale
void bind(const S &s)
bound, and the aggregate recomputed from it, in ( if selective)
Kind
how the events update the aggregate
Definition MaterializedAggregate.h:116
@ Group
by the value and its inverse
Definition MaterializedAggregate.h:117
@ Selective
by the multiset of the values
Definition MaterializedAggregate.h:118
static constexpr Kind kind
the kind of G
Definition MaterializedAggregate.h:123
void on(const mod::Assigned< H, V > &e)
the value of the key changed from to : , or the two values exchanged in the multiset,...
bool stale(void) const
whether the next value() recomputes
Definition MaterializedAggregate.h:251
void on(const mod::Cleared &) noexcept
the structure emptied: the identity
void on(const mod::Rebuilt &) noexcept
anything may have changed: the aggregate stale
Definition MaterializedAggregate.h:223
bool bound(void) const
whether a structure is bound
Definition MaterializedAggregate.h:254
MaterializedAggregate(const S &s)
the aggregate of , bound
Definition MaterializedAggregate.h:133
void on(const mod::Erased< H, K, V > &e) noexcept
the pair erased: , or the value taken out of the multiset, or the aggregate stale
typename G::Value Value
the type of the aggregate
Definition MaterializedAggregate.h:113
std::size_t recomputations(void) const
the recomputations made so far
Definition MaterializedAggregate.h:257
void on(const mod::Inserted< H, K, V > &e)
the pair inserted: , or the value added to the multiset
a group whose operation commutes
Definition Algebra.h:137
a monoid whose operation commutes, by the opt-in G::commutative
Definition Algebra.h:125
a monoid with the inverse G::inverse( a ), i.e., combine( inverse( a) , a ) and combine( a ,...
Definition Algebra.h:130
a monoid on G::Value: the identity G::identity() and the associative operation G::combine( a ,...
Definition Algebra.h:117
a monoid whose combine( a , b ) is always or , by the opt-in G::selective
Definition Algebra.h:142
Changed< Value, H, V > Assigned
the value of an item changed
Definition Modification.h:623
#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
Definition ADT.h:96
the structure emptied
Definition Modification.h:699
the item of handle , key and value erased, told before the removal outside a batch
Definition Modification.h:568
the item of handle , key and value inserted
Definition Modification.h:536
anything may have changed: forget and recompute
Definition Modification.h:708