QCPDataContainer

Search for usage in LXR

#include <qcustomplot.h>

Public Member Functions

 QCPDataContainer ()
 
void add (const DataType &data)
 
void add (const QCPDataContainer< DataType > &data)
 
void add (const QVector< DataType > &data, bool alreadySorted=false)
 
const_iterator at (int index) const
 
bool autoSqueeze () const
 
iterator begin ()
 
void clear ()
 
const_iterator constBegin () const
 
const_iterator constEnd () const
 
QCPDataRange dataRange () const
 
iterator end ()
 
const_iterator findBegin (double sortKey, bool expandedRange=true) const
 
const_iterator findEnd (double sortKey, bool expandedRange=true) const
 
bool isEmpty () const
 
QCPRange keyRange (bool &foundRange, QCP::SignDomain signDomain=QCP::sdBoth)
 
void limitIteratorsToDataRange (const_iterator &begin, const_iterator &end, const QCPDataRange &dataRange) const
 
void remove (double sortKey)
 
void remove (double sortKeyFrom, double sortKeyTo)
 
void removeAfter (double sortKey)
 
void removeBefore (double sortKey)
 
void set (const QCPDataContainer< DataType > &data)
 
void set (const QVector< DataType > &data, bool alreadySorted=false)
 
void setAutoSqueeze (bool enabled)
 
int size () const
 
void sort ()
 
void squeeze (bool preAllocation=true, bool postAllocation=true)
 
QCPRange valueRange (bool &foundRange, QCP::SignDomain signDomain=QCP::sdBoth, const QCPRange &inKeyRange=QCPRange())
 

Protected Member Functions

void performAutoSqueeze ()
 
void preallocateGrow (int minimumPreallocSize)
 

Protected Attributes

bool mAutoSqueeze
 
QVector< DataType > mData
 
int mPreallocIteration
 
int mPreallocSize
 

Related Symbols

(Note that these are not member symbols.)

template<class DataType >
bool qcpLessThanSortKey (const DataType &a, const DataType &b)
 

Detailed Description

template<class DataType>
class QCPDataContainer< DataType >

The generic data container for one-dimensional plottables.

This class template provides a fast container for data storage of one-dimensional data. The data type is specified as template parameter (called DataType in the following) and must provide some methods as described in the next section.

The data is stored in a sorted fashion, which allows very quick lookups by the sorted key as well as retrieval of ranges (see findBegin, findEnd, keyRange) using binary search. The container uses a preallocation and a postallocation scheme, such that appending and prepending data (with respect to the sort key) is very fast and minimizes reallocations. If data is added which needs to be inserted between existing keys, the merge usually can be done quickly too, using the fact that existing data is always sorted. The user can further improve performance by specifying that added data is already itself sorted by key, if he can guarantee that this is the case (see for example add(const QVector<DataType> &data, bool alreadySorted)).

The data can be accessed with the provided const iterators (constBegin, constEnd). If it is necessary to alter existing data in-place, the non-const iterators can be used (begin, end). Changing data members that are not the sort key (for most data types called key) is safe from the container's perspective.

Great care must be taken however if the sort key is modified through the non-const iterators. For performance reasons, the iterators don't automatically cause a re-sorting upon their manipulation. It is thus the responsibility of the user to leave the container in a sorted state when finished with the data manipulation, before calling any other methods on the container. A complete re-sort (e.g. after finishing all sort key manipulation) can be done by calling sort. Failing to do so can not be detected by the container efficiently and will cause both rendering artifacts and potential data loss.

Implementing one-dimensional plottables that make use of a QCPDataContainer<T> is usually done by subclassing from QCPAbstractPlottable1D, which introduces an according mDataContainer member and some convenience methods.

Requirements for the DataType template parameter

The template parameter DataType is the type of the stored data points. It must be trivially copyable and have the following public methods, preferably inline:

  • double sortKey() const
    Returns the member variable of this data point that is the sort key, defining the ordering in the container. Often this variable is simply called key.
  • static DataType fromSortKey(double sortKey)
    Returns a new instance of the data type initialized with its sort key set to sortKey.
  • static bool sortKeyIsMainKey()
    Returns true if the sort key is equal to the main key (see method mainKey below). For most plottables this is the case. It is not the case for example for QCPCurve, which uses t as sort key and key as main key. This is the reason why QCPCurve unlike QCPGraph can display parametric curves with loops.
  • double mainKey() const
    Returns the variable of this data point considered the main key. This is commonly the variable that is used as the coordinate of this data point on the key axis of the plottable. This method is used for example when determining the automatic axis rescaling of key axes (QCPAxis::rescale).
  • double mainValue() const
    Returns the variable of this data point considered the main value. This is commonly the variable that is used as the coordinate of this data point on the value axis of the plottable.
  • QCPRange valueRange() const
    Returns the range this data point spans in the value axis coordinate. If the data is single-valued (e.g. QCPGraphData), this is simply a range with both lower and upper set to the main data point value. However if the data points can represent multiple values at once (e.g QCPFinancialData with its high, low, open and close values at each key) this method should return the range those values span. This method is used for example when determining the automatic axis rescaling of value axes (QCPAxis::rescale).

Definition at line 2556 of file qcustomplot.h.

Constructor & Destructor Documentation

◆ QCPDataContainer()

template<class DataType >
QCPDataContainer< DataType >::QCPDataContainer ( )

Constructs a QCPDataContainer used for plottable classes that represent a series of key-sorted data

Definition at line 2749 of file qcustomplot.h.

Member Function Documentation

◆ add() [1/3]

template<class DataType >
void QCPDataContainer< DataType >::add ( const DataType & data)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Adds the provided single data point to the current data.

See also
remove

Definition at line 2883 of file qcustomplot.h.

◆ add() [2/3]

template<class DataType >
void QCPDataContainer< DataType >::add ( const QCPDataContainer< DataType > & data)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Adds the provided data to the current data in this container.

See also
set, remove

Definition at line 2814 of file qcustomplot.h.

◆ add() [3/3]

template<class DataType >
void QCPDataContainer< DataType >::add ( const QVector< DataType > & data,
bool alreadySorted = false )

Adds the provided data points in data to the current data.

If you can guarantee that the data points in data have ascending order with respect to the DataType's sort key, set alreadySorted to true to avoid an unnecessary sorting run.

See also
set, remove

Definition at line 2846 of file qcustomplot.h.

◆ at()

template<class DataType >
QCPDataContainer::const_iterator QCPDataContainer< DataType >::at ( int index) const
inline

Returns a const iterator to the element with the specified index. If index points beyond the available elements in this container, returns constEnd, i.e. an iterator past the last valid element.

You can use this method to easily obtain iterators from a QCPDataRange, see the data selection page for an example.

Definition at line 2592 of file qcustomplot.h.

◆ autoSqueeze()

template<class DataType >
bool QCPDataContainer< DataType >::autoSqueeze ( ) const
inline

Definition at line 2567 of file qcustomplot.h.

◆ begin()

template<class DataType >
QCPDataContainer::iterator QCPDataContainer< DataType >::begin ( )
inline

Returns a non-const iterator to the first data point in this container.

You can manipulate the data points in-place through the non-const iterators, but great care must be taken when manipulating the sort key of a data point, see sort, or the detailed description of this class.

Definition at line 2588 of file qcustomplot.h.

◆ clear()

template<class DataType >
void QCPDataContainer< DataType >::clear ( )

Removes all data points.

See also
remove, removeAfter, removeBefore

Definition at line 2981 of file qcustomplot.h.

◆ constBegin()

template<class DataType >
QCPDataContainer::const_iterator QCPDataContainer< DataType >::constBegin ( ) const
inline

Returns a const iterator to the first data point in this container.

Definition at line 2586 of file qcustomplot.h.

◆ constEnd()

template<class DataType >
QCPDataContainer::const_iterator QCPDataContainer< DataType >::constEnd ( ) const
inline

Returns a const iterator to the element past the last data point in this container.

Definition at line 2587 of file qcustomplot.h.

◆ dataRange()

template<class DataType >
QCPDataRange QCPDataContainer< DataType >::dataRange ( ) const
inline

Returns a QCPDataRange encompassing the entire data set of this container. This means the begin index of the returned range is 0, and the end index is size.

Definition at line 2595 of file qcustomplot.h.

◆ end()

template<class DataType >
QCPDataContainer::iterator QCPDataContainer< DataType >::end ( )
inline

Returns a non-const iterator to the element past the last data point in this container.

You can manipulate the data points in-place through the non-const iterators, but great care must be taken when manipulating the sort key of a data point, see sort, or the detailed description of this class.

Definition at line 2589 of file qcustomplot.h.

◆ findBegin()

template<class DataType >
QCPDataContainer< DataType >::const_iterator QCPDataContainer< DataType >::findBegin ( double sortKey,
bool expandedRange = true ) const

Returns an iterator to the data point with a (sort-)key that is equal to, just below, or just above sortKey. If expandedRange is true, the data point just below sortKey will be considered, otherwise the one just above.

This can be used in conjunction with findEnd to iterate over data points within a given key range, including or excluding the bounding data points that are just beyond the specified range.

If expandedRange is true but there are no data points below sortKey, constBegin is returned.

If the container is empty, returns constEnd.

See also
findEnd, QCPPlottableInterface1D::findBegin

Definition at line 3048 of file qcustomplot.h.

◆ findEnd()

template<class DataType >
QCPDataContainer< DataType >::const_iterator QCPDataContainer< DataType >::findEnd ( double sortKey,
bool expandedRange = true ) const

Returns an iterator to the element after the data point with a (sort-)key that is equal to, just above or just below sortKey. If expandedRange is true, the data point just above sortKey will be considered, otherwise the one just below.

This can be used in conjunction with findBegin to iterate over data points within a given key range, including the bounding data points that are just below and above the specified range.

If expandedRange is true but there are no data points above sortKey, constEnd is returned.

If the container is empty, constEnd is returned.

See also
findBegin, QCPPlottableInterface1D::findEnd

Definition at line 3075 of file qcustomplot.h.

◆ isEmpty()

template<class DataType >
bool QCPDataContainer< DataType >::isEmpty ( ) const
inline

Returns whether this container holds no data points.

Definition at line 2566 of file qcustomplot.h.

◆ keyRange()

template<class DataType >
QCPRange QCPDataContainer< DataType >::keyRange ( bool & foundRange,
QCP::SignDomain signDomain = QCP::sdBoth )

Returns the range encompassed by the (main-)key coordinate of all data points. The output parameter foundRange indicates whether a sensible range was found. If this is false, you should not use the returned QCPRange (e.g. the data container is empty or all points have the same key).

Use signDomain to control which sign of the key coordinates should be considered. This is relevant e.g. for logarithmic plots which can mathematically only display one sign domain at a time.

If the DataType reports that its main key is equal to the sort key (sortKeyIsMainKey), as is the case for most plottables, this method uses this fact and finds the range very quickly.

See also
valueRange

Definition at line 3102 of file qcustomplot.h.

◆ limitIteratorsToDataRange()

template<class DataType >
void QCPDataContainer< DataType >::limitIteratorsToDataRange ( const_iterator & begin,
const_iterator & end,
const QCPDataRange & dataRange ) const

Makes sure begin and end mark a data range that is both within the bounds of this data container's data, as well as within the specified dataRange. The initial range described by the passed iterators begin and end is never expanded, only contracted if necessary.

This function doesn't require for dataRange to be within the bounds of this data container's valid range.

Definition at line 3313 of file qcustomplot.h.

◆ performAutoSqueeze()

template<class DataType >
void QCPDataContainer< DataType >::performAutoSqueeze ( )
protected

This method decides, depending on the total allocation size and the size of the unused pre- and postallocation pools, whether it is sensible to reduce the pools in order to free up unused memory. It then possibly calls squeeze to do the deallocation.

If setAutoSqueeze is enabled, this method is called automatically each time data points are removed from the container (e.g. remove).

Note
when changing the decision parameters, care must be taken not to cause a back-and-forth between squeezing and reallocation due to the growth strategy of the internal QVector and preallocateGrow. The hysteresis between allocation and deallocation should be made high enough (at the expense of possibly larger unused memory from time to time).

Definition at line 3361 of file qcustomplot.h.

◆ preallocateGrow()

template<class DataType >
void QCPDataContainer< DataType >::preallocateGrow ( int minimumPreallocSize)
protected

Increases the preallocation pool to have a size of at least minimumPreallocSize. Depending on the preallocation history, the container will grow by more than requested, to speed up future consecutive size increases.

if minimumPreallocSize is smaller than or equal to the current preallocation pool size, this method does nothing.

Definition at line 3331 of file qcustomplot.h.

◆ remove() [1/2]

template<class DataType >
void QCPDataContainer< DataType >::remove ( double sortKey)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Removes a single data point at sortKey. If the position is not known with absolute (binary) precision, consider using remove(double sortKeyFrom, double sortKeyTo) with a small fuzziness interval around the suspected position, depeding on the precision with which the (sort-)key is known.

See also
removeBefore, removeAfter, clear

Definition at line 2961 of file qcustomplot.h.

◆ remove() [2/2]

template<class DataType >
void QCPDataContainer< DataType >::remove ( double sortKeyFrom,
double sortKeyTo )

Removes all data points with (sort-)keys between sortKeyFrom and sortKeyTo. if sortKeyFrom is greater or equal to sortKeyTo, the function does nothing. To remove a single data point with known (sort-)key, use remove(double sortKey).

See also
removeBefore, removeAfter, clear

Definition at line 2939 of file qcustomplot.h.

◆ removeAfter()

template<class DataType >
void QCPDataContainer< DataType >::removeAfter ( double sortKey)

Removes all data points with (sort-)keys greater than or equal to sortKey.

See also
removeBefore, remove, clear

Definition at line 2922 of file qcustomplot.h.

◆ removeBefore()

template<class DataType >
void QCPDataContainer< DataType >::removeBefore ( double sortKey)

Removes all data points with (sort-)keys smaller than or equal to sortKey.

See also
removeAfter, remove, clear

Definition at line 2907 of file qcustomplot.h.

◆ set() [1/2]

template<class DataType >
void QCPDataContainer< DataType >::set ( const QCPDataContainer< DataType > & data)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Replaces the current data in this container with the provided data.

See also
add, remove

Definition at line 2782 of file qcustomplot.h.

◆ set() [2/2]

template<class DataType >
void QCPDataContainer< DataType >::set ( const QVector< DataType > & data,
bool alreadySorted = false )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Replaces the current data in this container with the provided data

If you can guarantee that the data points in data have ascending order with respect to the DataType's sort key, set alreadySorted to true to avoid an unnecessary sorting run.

See also
add, remove

Definition at line 2798 of file qcustomplot.h.

◆ setAutoSqueeze()

template<class DataType >
void QCPDataContainer< DataType >::setAutoSqueeze ( bool enabled)

Sets whether the container automatically decides when to release memory from its post- and preallocation pools when data points are removed. By default this is enabled and for typical applications shouldn't be changed.

If auto squeeze is disabled, you can manually decide when to release pre-/postallocation with squeeze.

Definition at line 2765 of file qcustomplot.h.

◆ size()

template<class DataType >
int QCPDataContainer< DataType >::size ( ) const
inline

Returns the number of data points in the container.

Definition at line 2565 of file qcustomplot.h.

◆ sort()

template<class DataType >
void QCPDataContainer< DataType >::sort ( )

Re-sorts all data points in the container by their sort key.

When setting, adding or removing points using the QCPDataContainer interface (set, add, remove, etc.), the container makes sure to always stay in a sorted state such that a full resort is never necessary. However, if you choose to directly manipulate the sort key on data points by accessing and modifying it through the non-const iterators (begin, end), it is your responsibility to bring the container back into a sorted state before any other methods are called on it. This can be achieved by calling this method immediately after finishing the sort key manipulation.

Definition at line 3000 of file qcustomplot.h.

◆ squeeze()

template<class DataType >
void QCPDataContainer< DataType >::squeeze ( bool preAllocation = true,
bool postAllocation = true )

Frees all unused memory that is currently in the preallocation and postallocation pools.

Note that QCPDataContainer automatically decides whether squeezing is necessary, if setAutoSqueeze is left enabled. It should thus not be necessary to use this method for typical applications.

The parameters preAllocation and postAllocation control whether pre- and/or post allocation should be freed, respectively.

Definition at line 3016 of file qcustomplot.h.

◆ valueRange()

template<class DataType >
QCPRange QCPDataContainer< DataType >::valueRange ( bool & foundRange,
QCP::SignDomain signDomain = QCP::sdBoth,
const QCPRange & inKeyRange = QCPRange() )

Returns the range encompassed by the value coordinates of the data points in the specified key range (inKeyRange), using the full DataType::valueRange reported by the data points. The output parameter foundRange indicates whether a sensible range was found. If this is false, you should not use the returned QCPRange (e.g. the data container is empty or all points have the same value).

If inKeyRange has both lower and upper bound set to zero (is equal to QCPRange()), all data points are considered, without any restriction on the keys.

Use signDomain to control which sign of the value coordinates should be considered. This is relevant e.g. for logarithmic plots which can mathematically only display one sign domain at a time.

See also
keyRange

Definition at line 3225 of file qcustomplot.h.

Friends And Related Symbol Documentation

◆ qcpLessThanSortKey()

template<class DataType >
bool qcpLessThanSortKey ( const DataType & a,
const DataType & b )
related

Returns whether the sort key of a is less than the sort key of b.

See also
QCPDataContainer::sort

Definition at line 2553 of file qcustomplot.h.

Member Data Documentation

◆ mAutoSqueeze

template<class DataType >
bool QCPDataContainer< DataType >::mAutoSqueeze
protected

Definition at line 2600 of file qcustomplot.h.

◆ mData

template<class DataType >
QVector<DataType> QCPDataContainer< DataType >::mData
protected

Definition at line 2603 of file qcustomplot.h.

◆ mPreallocIteration

template<class DataType >
int QCPDataContainer< DataType >::mPreallocIteration
protected

Definition at line 2605 of file qcustomplot.h.

◆ mPreallocSize

template<class DataType >
int QCPDataContainer< DataType >::mPreallocSize
protected

Definition at line 2604 of file qcustomplot.h.


The documentation for this class was generated from the following file:
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:19:05 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.