Qx v0.5.8
Qt Extensions Library
Loading...
Searching...
No Matches
Qx::Cumulation< K, V > Class Template Reference

The Cumulation template class tracks the sum of multiple key-value components that can be changed independently. More...

#include <qx/core/qx-cumulation.h>

Public Member Functions

 Cumulation ()
 
void clear ()
 
QList< K > components () const
 
bool contains (K component) const
 
qsizetype count () const
 
decrement (K component)
 
void increase (K component, V amount)
 
increment (K component)
 
void insert (K component, V value, V scalar=1)
 
bool isEmpty () const
 
mean () const
 
bool operator!= (const Cumulation &other) const
 
bool operator== (const Cumulation &other) const
 
void reduce (K component, V amount)
 
void remove (K component)
 
void setScalar (K component, V scalar)
 
void setValue (K component, V value)
 
total () const
 
value (K component) const
 

Detailed Description

template<typename K, typename V>
requires arithmetic<V>
class Qx::Cumulation< K, V >

A cumulation is a collection of key-value pairs (where V is any arithmetic type) with which the sum of all contained values is always known, and any individual value can be added, removed, or updated through its corresponding key.

Additionally, a cumulation can have optional scalars applied to its components in order to differently weight their individual effect on the total.

This is generally useful for keeping a running total, but when a previously added value may need to be revised later, such as when tracking the overall progress of multiple downloads from a server using their individual progress as components.

Constructor & Destructor Documentation

◆ Cumulation()

template<typename K , typename V >
Qx::Cumulation< K, V >::Cumulation ( )
inline

Creates an empty cumulation with a total of zero.

Member Function Documentation

◆ clear()

template<typename K , typename V >
void Qx::Cumulation< K, V >::clear ( )
inline

Removes all keys/values from the cumulation, resulting in a total of zero.

◆ components()

template<typename K , typename V >
QList< K > Qx::Cumulation< K, V >::components ( ) const
inline

Returns a list containing all the components in the cumulation, in an arbitrary order.

This function creates a new list, in linear time.

◆ contains()

template<typename K , typename V >
bool Qx::Cumulation< K, V >::contains ( K component) const
inline

Returns true if the cumulation contains a value associated with the key component; otherwise returns false.

◆ count()

template<typename K , typename V >
qsizetype Qx::Cumulation< K, V >::count ( ) const
inline

Returns that number of components that compose the cumulation.

◆ decrement()

template<typename K , typename V >
V Qx::Cumulation< K, V >::decrement ( K component)
inline

Decrements the value of the given component and returns the new total.

If the cumulation does not contain the specified component it will be added with a value of -1 and a scalar of 1.

◆ increase()

template<typename K , typename V >
void Qx::Cumulation< K, V >::increase ( K component,
V amount )
inline

Adds amount to the value of component.

If the cumulation does not contain the specified component it will be added with a value of amount and a scalar of 1.

◆ increment()

template<typename K , typename V >
V Qx::Cumulation< K, V >::increment ( K component)
inline

Increments the value of the given component and returns the new total.

If the cumulation does not contain the specified component it will be added with a value of 1 and a scalar of 1.

◆ insert()

template<typename K , typename V >
void Qx::Cumulation< K, V >::insert ( K component,
V value,
V scalar = 1 )
inline

Inserts a new component with key component, value value, and scalar scalar.

If there is already a component with the same key, that component's value and scalar are replaced with value and scalar respectively.

◆ isEmpty()

template<typename K , typename V >
bool Qx::Cumulation< K, V >::isEmpty ( ) const
inline

Returns true if the cumulation has no components; otherwise, returns false.

◆ mean()

template<typename K , typename V >
V Qx::Cumulation< K, V >::mean ( ) const
inline

Returns the current mean of the cumulation, which is the sum of all its component values divided the number of components, or zero if the cumulation is empty.

If V is an integral type, the result is rounded to the nearest integer.

◆ operator!=()

template<typename K , typename V >
V Qx::Cumulation< K, V >::operator!= ( const Cumulation< K, V > & other) const
inline

Returns true if this Cumulation and other Cumulation are not the same; otherwise, returns false.

◆ operator==()

template<typename K , typename V >
V Qx::Cumulation< K, V >::operator== ( const Cumulation< K, V > & other) const
inline

Returns true if this Cumulation and other Cumulation are the same; otherwise, returns false.

◆ reduce()

template<typename K , typename V >
void Qx::Cumulation< K, V >::reduce ( K component,
V amount )
inline

Subtracts amount from the value of component.

If the cumulation does not contain the specified component it will be added with a value of -amount and a scalar of 1.

◆ remove()

template<typename K , typename V >
void Qx::Cumulation< K, V >::remove ( K component)
inline

Removes the value associated with the key component from the cumulation, if it exists.

◆ setScalar()

template<typename K , typename V >
void Qx::Cumulation< K, V >::setScalar ( K component,
V scalar )
inline

Sets the scalar of component to scalar.

The amount that a component contributes to a cumulation's total is its value multiplied by its scalar.

If the cumulation does not contain the specified component it will be added with a value of 0 and a scalar of scalar.

◆ setValue()

template<typename K , typename V >
void Qx::Cumulation< K, V >::setValue ( K component,
V value )
inline

Sets the value of component to value.

If the cumulation does not contain the specified component it will be added with a value of value and a scalar of 1.

◆ total()

template<typename K , typename V >
V Qx::Cumulation< K, V >::total ( ) const
inline

Returns the current total of the cumulation, which is the sum of all its component values.

◆ value()

template<typename K , typename V >
V Qx::Cumulation< K, V >::value ( K component) const
inline

Returns the value of component, or a default-constructed value if not present.


The documentation for this class was generated from the following files: