QCPDataContainer< DataType > Class Template Reference

The generic data container for one-dimensional plottables. More...

#include </home/florent/devel/release/src/gepetto-viewer/plugins/pyqcustomplot/qcustomplot-2.0.1/qcustomplot.h>

Public Types

typedef QVector< DataType >::const_iterator const_iterator
 
typedef QVector< DataType >::iterator iterator
 

Public Member Functions

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

Protected Member Functions

void preallocateGrow (int minimumPreallocSize)
 
void performAutoSqueeze ()
 

Protected Attributes

bool mAutoSqueeze
 
QVector< DataType > mData
 
int mPreallocSize
 
int mPreallocIteration
 

Related Functions

(Note that these are not member functions.)

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<T>, 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).

Member Typedef Documentation

◆ const_iterator

template<class DataType>
typedef QVector<DataType>::const_iterator QCPDataContainer< DataType >::const_iterator

◆ iterator

template<class DataType>
typedef QVector<DataType>::iterator QCPDataContainer< DataType >::iterator

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

Member Function Documentation

◆ add() [1/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

◆ add() [2/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

◆ add() [3/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

◆ 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.

◆ autoSqueeze()

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

◆ 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.

◆ clear()

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

Removes all data points.

See also
remove, removeAfter, removeBefore

◆ constBegin()

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

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

◆ 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.

◆ 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.

◆ 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.

◆ 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

◆ 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

◆ isEmpty()

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

Returns whether this container holds no data points.

◆ 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

◆ 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.

◆ performAutoSqueeze()

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

◆ preallocateGrow()

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

◆ remove() [1/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

◆ remove() [2/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

◆ 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

◆ 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

◆ 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

◆ 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

◆ 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.

◆ size()

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

Returns the number of data points in the container.

◆ 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.

◆ 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.

◆ 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

Friends And Related Function 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

Member Data Documentation

◆ mAutoSqueeze

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

◆ mData

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

◆ mPreallocIteration

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

◆ mPreallocSize

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

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