ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Fenwick.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*----------------------------- File Fenwick.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
61/*--------------------------------------------------------------------------*/
62/*----------------------------- DEFINITIONS --------------------------------*/
63/*--------------------------------------------------------------------------*/
64
65#ifndef __ADS_Fenwick
66 #define __ADS_Fenwick
67/* self-identification: #endif constant */
68
69/*--------------------------------------------------------------------------*/
70/*------------------------------ INCLUDES ----------------------------------*/
71/*--------------------------------------------------------------------------*/
72
73#include "ADS/Algebra.h"
74#include "ADS/Contract.h"
75#include "ADS/Modification.h"
76
77#include <concepts>
78#include <cstddef>
79#include <iterator>
80#include <type_traits>
81#include <vector>
82
83/*--------------------------------------------------------------------------*/
84/*--------------------------- NAMESPACE ------------------------------------*/
85/*--------------------------------------------------------------------------*/
86
87namespace ADS
88{
89
90/*--------------------------------------------------------------------------*/
91/*------------------------------ CLASS Fenwick -----------------------------*/
92/*--------------------------------------------------------------------------*/
93
95template< Monoid G >
97{
98
99/*--------------------------------------------------------------------------*/
100 public:
101 using Value = typename G::Value;
102 using Algebra = G;
103
105 explicit Fenwick( std::size_t n = 0 )
106 : f_a( n , G::identity() ) , f_t( n , G::identity() ) { }
107
110 template< std::input_iterator I >
111 Fenwick( I first , I last ) {
112 assign( first , last );
113 }
114
116 [[nodiscard]] std::size_t size( void ) const { return( f_a.size() ); }
117
119 [[nodiscard]] bool empty( void ) const { return( f_a.empty() ); }
120
123 [[nodiscard]] const Value & get( std::size_t i ) const {
124 ADS_PRE( i < f_a.size() );
125 return( f_a[ i ] );
126 }
127
143 [[nodiscard]] Value prefix( std::size_t r ) const;
144
148 [[nodiscard]] Value range( std::size_t l , std::size_t r ) const
149 requires Group< G >;
150
173 void set( std::size_t i , const Value & v );
174
177 void add( std::size_t i , const Value & d )
178 requires CommutativeMonoid< G >;
179
182 void push_back( const Value & v );
183
187 void pop_back( void ) {
188 ADS_PRE( ! f_a.empty() );
189 f_a.pop_back();
190 f_t.pop_back();
191 }
192
194 void clear( void ) noexcept {
195 f_a.clear();
196 f_t.clear();
197 f_since = 0;
198 }
199
202 template< std::input_iterator I >
203 void assign( I first , I last );
204
207 [[nodiscard]] std::size_t period( void ) const { return( f_period ); }
208
211 void period( std::size_t p ) {
212 ADS_PRE( p >= 1 );
213 f_period = p;
214 }
215
216/*--------------------------------------------------------------------------*/
217 private:
219 [[nodiscard]] Value node( std::size_t j ) const;
220
222 void build( void );
223
225 void counted( void );
226
228 static std::size_t low( std::size_t j ) { return( j & ( ~j + 1 ) ); }
229
230 std::vector< Value > f_a;
231 std::vector< Value > f_t;
232 std::size_t f_since = 0;
233 std::size_t f_period = 4096;
234
235 }; // end( class Fenwick )
236
237/*--------------------------------------------------------------------------*/
238/*-------------------------- CLASS FenwickObserver -------------------------*/
239/*--------------------------------------------------------------------------*/
240
244template< Monoid G >
246{
247
248 using Value = typename G::Value;
249
250/*--------------------------------------------------------------------------*/
251 public:
258
260 FenwickObserver( void ) = default;
261
263 template< class A >
264 requires requires( const A & a ) {
265 { a.values() } -> std::same_as< const std::vector< Value > & >;
266 }
267 explicit FenwickObserver( const A & a ) {
268 rebuild( a );
269 }
270
273 template< class A >
274 requires requires( const A & a ) {
275 { a.values() } -> std::same_as< const std::vector< Value > & >;
276 }
277 void rebuild( const A & a ) {
278 f_src = &a.values();
279 f_f.assign( f_src->begin() , f_src->end() );
280 f_stale = false;
281 }
282
284 void on( const Assign & e ) {
285 if( ! f_stale )
286 f_f.set( e.h , e.to );
287 }
288
290 void on( const Insert & e ) {
291 if( ! f_stale ) {
292 ADS_PRE( e.h == f_f.size() );
293 f_f.push_back( e.value );
294 }
295 }
296
298 void on( const Erase & ) noexcept {
299 if( ! f_stale && ! f_f.empty() )
300 f_f.pop_back();
301 }
302
304 void on( const mod::Cleared & ) noexcept {
305 f_f.clear();
306 f_stale = false;
307 }
308
310 void on( const mod::Rebuilt & ) noexcept { f_stale = true; }
311
313 void flush( void ) const {
314 if( f_stale ) {
315 ADS_PRE( f_src != nullptr );
316 f_f.assign( f_src->begin() , f_src->end() );
317 f_stale = false;
318 }
319 }
320
322 [[nodiscard]] std::size_t size( void ) const {
323 flush();
324 return( f_f.size() );
325 }
326
329 [[nodiscard]] Value prefix( std::size_t r ) const {
330 flush();
331 return( f_f.prefix( r ) );
332 }
333
336 [[nodiscard]] Value range( std::size_t l , std::size_t r ) const
337 requires Group< G >
338 {
339 flush();
340 return( f_f.range( l , r ) );
341 }
342
344 [[nodiscard]] const Fenwick< G > & tree( void ) const {
345 flush();
346 return( f_f );
347 }
348
351 template< class A >
352 [[nodiscard]] bool audit( const A & a ) const;
353
354/*--------------------------------------------------------------------------*/
355 private:
356 mutable Fenwick< G > f_f;
357 const std::vector< Value > * f_src = nullptr;
358 mutable bool f_stale = false;
359
360 }; // end( class FenwickObserver )
361
362/*--------------------------------------------------------------------------*/
363
364} // end( namespace ADS )
365
366/*--------------------------------------------------------------------------*/
367/*-------------------------- IMPLEMENTATION --------------------------------*/
368/*--------------------------------------------------------------------------*/
369
370#include "ADS/sequence/impl/Fenwick.tpp"
371
372/*--------------------------------------------------------------------------*/
373
374#endif /* Fenwick.h included */
375
376/*--------------------------------------------------------------------------*/
377/*--------------------------- End File Fenwick.h ---------------------------*/
378/*--------------------------------------------------------------------------*/
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 typed modifications, beyond the book: the vocabulary by which a structure tells the observers att...
void rebuild(const A &a)
the tree built again from the array , whose vector of values a.values() it reads from now on when Reb...
Definition Fenwick.h:277
mod::Erased< std::size_t, std::size_t, Value > Erase
the last value removed
Definition Fenwick.h:257
void on(const Erase &) noexcept
the last value removed
Definition Fenwick.h:298
bool audit(const A &a) const
whether the tree has the values of the array and, when they are exact, its prefixes are those of a s...
std::size_t size(void) const
the number of values
Definition Fenwick.h:322
void on(const Insert &e)
the value appended
Definition Fenwick.h:290
Value range(std::size_t l, std::size_t r) const
the range
Definition Fenwick.h:336
mod::Inserted< std::size_t, std::size_t, Value > Insert
a value appended
Definition Fenwick.h:255
Value prefix(std::size_t r) const
the prefix [see Fenwick::prefix()]
Definition Fenwick.h:329
FenwickObserver(const A &a)
the observer of the array , its tree built from it
Definition Fenwick.h:267
void on(const mod::Cleared &) noexcept
no value
Definition Fenwick.h:304
FenwickObserver(void)=default
an observer of no array, given one later by rebuild()
const Fenwick< G > & tree(void) const
the tree
Definition Fenwick.h:344
mod::Assigned< std::size_t, Value > Assign
a value written
Definition Fenwick.h:253
void flush(void) const
the tree built again if Rebuilt came
Definition Fenwick.h:313
void on(const Assign &e)
Definition Fenwick.h:284
void on(const mod::Rebuilt &) noexcept
the tree forgotten, built again at the next query
Definition Fenwick.h:310
the prefix aggregates of values of the monoid G
Definition Fenwick.h:97
void set(std::size_t i, const Value &v)
: by the difference in time in an abelian group, by the parts of the nodes in time otherwise
typename G::Value Value
the values
Definition Fenwick.h:101
void period(std::size_t p)
sets the period of the rebuilding to
Definition Fenwick.h:211
void clear(void) noexcept
all the values removed
Definition Fenwick.h:194
std::size_t period(void) const
the updates by difference after which a group of floating point values is rebuilt from its values
Definition Fenwick.h:207
const Value & get(std::size_t i) const
the value
Definition Fenwick.h:123
bool empty(void) const
whether there is no value
Definition Fenwick.h:119
Fenwick(I first, I last)
the values of , in time
Definition Fenwick.h:111
std::size_t size(void) const
the number of values
Definition Fenwick.h:116
Value prefix(std::size_t r) const
the prefix , the identity if , in time
void pop_back(void)
the last value removed, in time, since no other node contains it
Definition Fenwick.h:187
Fenwick(std::size_t n=0)
values, all the identity
Definition Fenwick.h:105
void assign(I first, I last)
the values of in place of all the others, the nodes built in time
G Algebra
their algebra
Definition Fenwick.h:102
void push_back(const Value &v)
appended as , its node built in time
void add(std::size_t i, const Value &d)
, in time
Value range(std::size_t l, std::size_t r) const
the range , i.e., , in time
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
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
Changed< Value, H, V > Assigned
the value of an item changed
Definition Modification.h:623
Definition ADT.h:96
V to
the value after
Definition Modification.h:608
H h
< the value
Definition Modification.h:606
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
V value
the value
Definition Modification.h:544
H h
< the value
Definition Modification.h:542
anything may have changed: forget and recompute
Definition Modification.h:708