QCPDataContainer< DataType > Class Template Reference
Author: Emanuel Eichhammer

· · · ·
Public Types | Public Functions | Protected Functions | Related Non-Members
QCPDataContainer< DataType > Class Template Reference

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

## Public Types
typedef QVector< DataType >::const_iterator | const_iterator
typedef QVector< DataType >::iterator | iterator

## Public 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 Functions
void | preallocateGrow (int minimumPreallocSize)
void | performAutoSqueeze ()

## Related Non-Members
(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<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).

## 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

## ◆ size()

template<class DataType>

int QCPDataContainer< DataType >::size | ( |  | ) | const
| inline

Returns the number of data points in the container.

## ◆ isEmpty()

template<class DataType>

bool QCPDataContainer< DataType >::isEmpty | ( |  | ) | const
| inline

Returns whether this container holds no data points.

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

## ◆ set() [1/2]

template<class DataType>

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

This is an overloaded function.
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 function.
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

## ◆ add() [1/3]

template<class DataType>

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

This is an overloaded function.
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 function.
Adds the provided single data point to the current data.

See also
  remove

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

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

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

## ◆ clear()

template<class DataType>

void QCPDataContainer< DataType >::clear | ( |  | ) |

Removes all data points.

See also
  remove, removeAfter, removeBefore

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

## ◆ constBegin()

template<class DataType>

const_iterator QCPDataContainer< DataType >::constBegin | ( |  | ) | const
| inline

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

## ◆ constEnd()

template<class DataType>

const_iterator QCPDataContainer< DataType >::constEnd | ( |  | ) | const
| inline

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

## ◆ begin()

template<class DataType>

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.

## ◆ end()

template<class DataType>

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

## ◆ at()

template<class DataType>

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.

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

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

Inf and -Inf data values are ignored.

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

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

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

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

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

## 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

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

- src/datacontainer.h