Qx v0.5.8
Qt Extensions Library
Loading...
Searching...
No Matches
Qx::Table< T > Class Template Reference

The Table class is a template class that provides a dynamic two-dimensional array. More...

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

Collaboration diagram for Qx::Table< T >:
[legend]

Public Types

typedef QList< QList< T > >::const_iterator row_iterator
 

Public Member Functions

 Table ()
 
 Table (QSize size)
 
 Table (QSize size, const T &value)
 
 Table (std::initializer_list< std::initializer_list< T > > table)
 
void addColumns (qsizetype c)
 
void addRows (qsizetype r)
 
void appendColumn (const QList< T > &c)
 
void appendRow (const QList< T > &r)
 
T & at (qsizetype r, qsizetype c)
 
const T & at (qsizetype r, qsizetype c) const
 
QSize capacity () const
 
QList< T > columnAt (qsizetype i) const
 
qsizetype columnCount () const
 
void fill (const T &value, QSize size)
 
QList< T > firstColumn () const
 
const QList< T > & firstRow () const
 
qsizetype height () const
 
void insertColumn (qsizetype i, const QList< T > &c)
 
void insertRow (qsizetype i, const QList< T > &r)
 
bool isEmpty () const
 
QList< T > lastColumn () const
 
const QList< T > & lastRow () const
 
bool operator!= (const Table &other) const
 
bool operator== (const Table &other) const
 
void removeColumnAt (qsizetype i)
 
void removeColumns (qsizetype i, qsizetype n=1)
 
void removeFirstColumn ()
 
void removeFirstRow ()
 
void removeLastColumn ()
 
void removeLastRow ()
 
void removeRowAt (qsizetype i)
 
void removeRows (qsizetype i, qsizetype n=1)
 
void replaceColumn (qsizetype i, const QList< T > &c)
 
void replaceRow (qsizetype i, const QList< T > &r)
 
void reserve (QSize size)
 
void resize (QSize size)
 
void resizeColumns (qsizetype size)
 
void resizeRows (qsizetype size)
 
const QList< T > & rowAt (qsizetype i) const
 
row_iterator rowBegin () const
 
qsizetype rowCount () const
 
row_iterator rowEnd () const
 
Table section (qsizetype r, qsizetype c, qsizetype height, qsizetype width) const
 
QSize size () const
 
void squeeze ()
 
QList< T > takeColumnAt (qsizetype i)
 
QList< T > takeFirstColumn ()
 
QList< T > takeFirstRow ()
 
QList< T > takeLastColumn ()
 
QList< T > takeLastRow ()
 
QList< T > takeRowAt (qsizetype i)
 
value (qsizetype r, qsizetype c) const
 
value (qsizetype r, qsizetype c, const T &defaultValue) const
 
qsizetype width () const
 

Protected Attributes

QList< QList< T > > mTable
 

Detailed Description

template<typename T>
class Qx::Table< T >

Table<T> is a generic container class that features fast index-based access and a similar API to QList<T>.

Member Typedef Documentation

◆ row_iterator

template<typename T >
Qx::Table< T >::row_iterator

The Table::row_iterator typedef provides an STL_style const iterator for Table.

This iterator steps through each row of the table.

Constructor & Destructor Documentation

◆ Table() [1/4]

template<typename T >
Qx::Table< T >::Table ( )
inline

Constructs an empty Table.

◆ Table() [2/4]

template<typename T >
Qx::Table< T >::Table ( QSize size)
inline

Constructs a Table of size size, with every element initialized with a default-constructed value.

◆ Table() [3/4]

template<typename T >
Qx::Table< T >::Table ( QSize size,
const T & value )
inline

Constructs a Table of size size, with every element initialized to value.

◆ Table() [4/4]

template<typename T >
Qx::Table< T >::Table ( std::initializer_list< std::initializer_list< T > > table)
inline

Constructs a Table with a copy of each row in the initializer list list.

If all rows in the list are not the same size, this constructor will produce an empty table.

Member Function Documentation

◆ addColumns()

template<typename T >
void Qx::Table< T >::addColumns ( qsizetype c)
inline

Appends c columns, with their elements initialized to default-constructed values, to the 'right' end of the table.

Equivalent to:

qsizetype columnCount() const
Definition qx-table.h:98
void resizeColumns(qsizetype size)
Definition qx-table.h:352
See also
resizeColumns(), and appendColumn().

◆ addRows()

template<typename T >
void Qx::Table< T >::addRows ( qsizetype c)
inline

Appends c columns, with their elements initialized to default-constructed values, to the 'bottom' end of the table.

Equivalent to:

qsizetype rowCount() const
Definition qx-table.h:139
void resizeRows(qsizetype size)
Definition qx-table.h:361
See also
resizeRows(), and appendRow().

◆ appendColumn()

template<typename T >
void Qx::Table< T >::appendColumn ( const QList< T > & c)
inline

Appends c as a column to the 'right' end of the table.

If c is smaller than the current height (rowCount()) of the table, it will be expanded with default-constructed values after insertion to match the current height. If c is larger than the current height, the rest of the table's columns will be expanded with default-constructed values to match the height of c.

◆ appendRow()

template<typename T >
void Qx::Table< T >::appendRow ( const QList< T > & r)
inline

Appends r as a row to the 'bottom' end of the table.

If r is smaller than the current width (columnCount()) of the table, it will be expanded with default-constructed values after insertion to match the current width. If r is larger than the current width, the rest of the table's rows will be expanded with default-constructed values to match the width of c.

◆ at() [1/2]

template<typename T >
T & Qx::Table< T >::at ( qsizetype r,
qsizetype c )
inline

Returns the element at row r and column c as a modifiable reference.

r and c must point to a valid position within the table (i.e. 0 <= r < rowCount() && 0 <= c < columnCount()).

See also
value().

◆ at() [2/2]

template<typename T >
const T & Qx::Table< T >::at ( qsizetype r,
qsizetype c ) const
inline

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

Returns the element as a constant reference.

◆ capacity()

template<typename T >
QSize Qx::Table< T >::capacity ( ) const
inline

Returns the capacity of the table as a QSize object.

See also
reserve(), and squeeze().

◆ columnAt()

template<typename T >
QList< T > Qx::Table< T >::columnAt ( qsizetype i) const
inline

Returns the items in column i of the table as a list.

i must be a valid column index in the table (i.e. 0 <= i < columnCount())

See also
section().

◆ columnCount()

template<typename T >
qsizetype Qx::Table< T >::columnCount ( ) const
inline

Returns the number of columns in the table.

See also
isEmpty(), rowCount(), and resizeColumns().

◆ fill()

template<typename T >
void Qx::Table< T >::fill ( const T & value,
QSize size )
inline

Assigns value to all items in the table. If size is not null (the default), the table is resized to size beforehand.

See also
resize().

◆ firstColumn()

template<typename T >
QList< T > Qx::Table< T >::firstColumn ( ) const
inline

Returns the items in the first column of the table as a list. This function assumes that the table isn't empty.

See also
isEmpty(), lastColumn(), and firstRow().

◆ firstRow()

template<typename T >
QList< T > Qx::Table< T >::firstRow ( ) const
inline

Returns the items in the first row of the table as a list. This function assumes that the table isn't empty.

See also
isEmpty(), lastRow(), and firstColumn().

◆ height()

template<typename T >
qsizetype Qx::Table< T >::height ( ) const
inline

Same as rowCount().

◆ insertColumn()

template<typename T >
void Qx::Table< T >::insertColumn ( qsizetype i,
const QList< T > & c )
inline

Inserts c as a column to column index i in the table. If i is 0, the column is prepended to the table. If i is columnSize(), the column is appended to the table.

If c is smaller than the current height (rowCount()) of the table, it will be expanded with default-constructed values after insertion to match the current height. If c is larger than the current height, the rest of the table's columns will be expanded with default-constructed values to match the height of c.

◆ insertRow()

template<typename T >
void Qx::Table< T >::insertRow ( qsizetype i,
const QList< T > & r )
inline

Inserts r as a row to row index i in the table. If i is 0, the row is prepended to the table. If i is rowSize(), the row is appended to the table.

If r is smaller than the current width (columnCount()) of the table, it will be expanded with default-constructed values after insertion to match the current width. If r is larger than the current width, the rest of the table's rows will be expanded with default-constructed values to match the width of c.

◆ isEmpty()

template<typename T >
bool Qx::Table< T >::isEmpty ( ) const
inline

Returns true if the table has no elements; otherwise, returns false.

See also
size(), and resize().

◆ lastColumn()

template<typename T >
QList< T > Qx::Table< T >::lastColumn ( ) const
inline

Returns the items in the last column of the table as a list. This function assumes that the table isn't empty.

See also
isEmpty(), firstColumn(), and lastRow().

◆ lastRow()

template<typename T >
QList< T > Qx::Table< T >::lastRow ( ) const
inline

Returns the items in the last row of the table as a list. This function assumes that the table isn't empty.

See also
isEmpty(), firstRow(), and lastColumn().

◆ operator!=()

template<typename T >
bool Qx::Table< T >::operator!= ( const Table< T > & other) const
inline

Returns true if the table does not contain the same elements as other; otherwise, returns false.

◆ operator==()

template<typename T >
bool Qx::Table< T >::operator== ( const Table< T > & other) const
inline

Returns true if the table contains the same elements as other; otherwise, returns false.

◆ removeColumnAt()

template<typename T >
void Qx::Table< T >::removeColumnAt ( qsizetype i)
inline

Removes the column i from the table.

Element removal will preserve the table's capacity and not reduce the amount of allocated memory. To shed extra capacity and free as much memory as possible, call squeeze().

See also
removeColumns(), and removeRows().

◆ removeColumns()

template<typename T >
void Qx::Table< T >::removeColumns ( qsizetype i,
qsizetype n = 1 )
inline

Removes n columns from the table, starting at column i.

Element removal will preserve the table's capacity and not reduce the amount of allocated memory. To shed extra capacity and free as much memory as possible, call squeeze().

See also
insertColumn(), replaceColumn(), and fill().

◆ removeFirstColumn()

template<typename T >
void Qx::Table< T >::removeFirstColumn ( )
inline

Removes the first column from the table. Calling this function is equivalent to calling removeColumns(0). The table must have columns. To prevent failure, call columnCount() before calling this function.

Element removal will preserve the table's capacity and not reduce the amount of allocated memory. To shed extra capacity and free as much memory as possible, call squeeze().

See also
removeColumns(), takeFirstColumn(), and columnCount().

◆ removeFirstRow()

template<typename T >
void Qx::Table< T >::removeFirstRow ( )
inline

Removes the first row from the table. Calling this function is equivalent to calling removeRows(0). The table must have rows. To prevent failure, call rowCount() before calling this function.

Element removal will preserve the table's capacity and not reduce the amount of allocated memory. To shed extra capacity and free as much memory as possible, call squeeze().

See also
removeRows(), takeFirstRow(), and rowCount().

◆ removeLastColumn()

template<typename T >
void Qx::Table< T >::removeLastColumn ( )
inline

Removes the last column from the table. Calling this function is equivalent to calling removeColumns(columnCount() - 1). The table must have columns. To prevent failure, call columnCount() before calling this function.

Element removal will preserve the table's capacity and not reduce the amount of allocated memory. To shed extra capacity and free as much memory as possible, call squeeze().

See also
removeColumns(), takeLastColumn(), and columnCount().

◆ removeLastRow()

template<typename T >
void Qx::Table< T >::removeLastRow ( )
inline

Removes the last row from the table. Calling this function is equivalent to calling removeRows(rowCount() - 1). The table must have rows. To prevent failure, call rowCount() before calling this function.

Element removal will preserve the table's capacity and not reduce the amount of allocated memory. To shed extra capacity and free as much memory as possible, call squeeze().

See also
removeRows(), takeLastRow(), and rowCount().

◆ removeRowAt()

template<typename T >
void Qx::Table< T >::removeRowAt ( qsizetype i)
inline

Removes the row i from the table.

Element removal will preserve the table's capacity and not reduce the amount of allocated memory. To shed extra capacity and free as much memory as possible, call squeeze().

See also
removeRows(), and removeColumns().

◆ removeRows()

template<typename T >
void Qx::Table< T >::removeRows ( qsizetype i,
qsizetype n = 1 )
inline

Removes n rows from the table, starting at row i.

Element removal will preserve the table's capacity and not reduce the amount of allocated memory. To shed extra capacity and free as much memory as possible, call squeeze().

See also
insertRow(), replaceRow(), and fill().

◆ replaceColumn()

template<typename T >
void Qx::Table< T >::replaceColumn ( qsizetype i,
const QList< T > & c )
inline

Replaces column i with c in the table.

If c is smaller than the current height (rowCount()) of the table, it will be expanded with default-constructed values after insertion to match the current height. If c is larger than the current height, the rest of the table's columns will be expanded with default-constructed values to match the height of c.

◆ replaceRow()

template<typename T >
void Qx::Table< T >::replaceRow ( qsizetype i,
const QList< T > & r )
inline

Replaces row i with r in the table.

If r is smaller than the current width (columnCount()) of the table, it will be expanded with default-constructed values after insertion to match the current width. If r is larger than the current width, the rest of the table's rows will be expanded with default-constructed values to match the width of c.

◆ reserve()

template<typename T >
void Qx::Table< T >::reserve ( QSize size)
inline

Attempts to allocate memory for size elements.

If you know in advance how large the table will be, you should call this function to prevent reallocations and memory fragmentation. If you resize the table often, you are also likely to get better performance.

If in doubt about how much space shall be needed, it is usually better to use an upper bound as size, or a high estimate of the most likely size, if a strict upper bound would be much bigger than this. If size is an underestimate, the table will grow as needed once the reserved size is exceeded, which may lead to a larger allocation than your best overestimate would have and will slow the operation that triggers it.

Warning
reserve() reserves memory but does not change the size of the table. Accessing data beyond the current bounds of the table is undefined behavior. If you need to access memory beyond the current bounds of the table, use resize().
Warning
Due to technical limitations, the reserve space specified for the width of size will only apply to existing rows, and not any that are only reserved. This may be resolved in a future version.
See also
squeeze(), capacity(), and resize().

◆ resize()

template<typename T >
void Qx::Table< T >::resize ( QSize size)
inline

Sets the size of the table to size. If size is greater than the current size, elements are added to each end of the table; the new elements are initialized with default-constructed values. If size is less than the current size, elements are removed from each end.

See also
size(), resizeColumns(), and resizeRows().

◆ resizeColumns()

template<typename T >
void Qx::Table< T >::resizeColumns ( qsizetype size)
inline

Sets the number of columns in the table to size. If size is greater than the current column count, columns are added to the 'right' end of the table; the new elements are initialized with default-constructed values. If size is less than the current column count, columns are removed from the 'right' end.

See also
resize(), size(), and resizeRows().

◆ resizeRows()

template<typename T >
void Qx::Table< T >::resizeRows ( qsizetype size)
inline

Sets the number of rows in the table to size. If size is greater than the current row count, rows are added to the 'bottom' end of the table; the new elements are initialized with default-constructed values. If size is less than the current row count, rows are removed from the 'bottom' end.

See also
resize(), size(), and resizeColumns().

◆ rowAt()

template<typename T >
QList< T > Qx::Table< T >::rowAt ( qsizetype i) const
inline

Returns the items in row i of the table as a list.

i must be a valid column index in the table (i.e. 0 <= i < rowCount())

See also
section().

◆ rowBegin()

template<typename T >
row_iterator Qx::Table< T >::rowBegin ( ) const
inline

Returns a const STL-style iterator pointing to the first row in the table.

Warning
The returned iterator is invalidated when the Table is modified.
See also
rowEnd().

◆ rowCount()

template<typename T >
qsizetype Qx::Table< T >::rowCount ( ) const
inline

Returns the number of rows in the table.

See also
isEmpty(), columnCount(), and resizeRows().

◆ rowEnd()

template<typename T >
row_iterator Qx::Table< T >::rowEnd ( ) const
inline

Returns a const STL-style iterator pointing just after the last row in the table.

Warning
The returned iterator is invalidated when the Table is modified.
See also
rowBegin().

◆ section()

template<typename T >
Table< T > Qx::Table< T >::section ( qsizetype r,
qsizetype c,
qsizetype height,
qsizetype width ) const
inline

Returns a new Table that contains a portion of the table's elements.

r and specify the origin of the subsection, while height specifies the number of rows to include, and width specifies the number of columns to include.

Any portions of the specified section that fall outside the bounds of the table are simply ignored, which can result in the new table being empty if the section does not intersect with the table whatsoever.

{0, 1, 2},
{3, 4, 5},
{6, 7, 8}
});
Qx::DsvTable<int> sectionTable({
{1, 2},
{4, 5},
{7, 8}
});
qDebug() << sectionTable == table.section(0, 1, 3, 2);
// true
See also
columnAt(), and rowAt().

◆ size()

template<typename T >
QSize Qx::Table< T >::size ( ) const
inline

Returns the column count (width), and row count (height) of the table as a QSize object.

See also
columnCount(), and rowCount().

◆ squeeze()

template<typename T >
void Qx::Table< T >::squeeze ( )
inline

Releases any memory not required to store the elements.

The sole purpose of this function is to provide a means of fine tuning Table's memory usage. In general, you will rarely ever need to call this function.

See also
reserve(), and capacity().

◆ takeColumnAt()

template<typename T >
void Qx::Table< T >::takeColumnAt ( qsizetype i)
inline

Removes column i and returns it.

Equivalent to:

QList<T> col = columnAt(i);
return col;
void removeColumnAt(qsizetype i)
Definition qx-table.h:246
QList< T > columnAt(qsizetype i) const
Definition qx-table.h:85
See also
takeFirstColumn(), takeLastColumn(), and takeRowAt().

◆ takeFirstColumn()

template<typename T >
void Qx::Table< T >::takeFirstColumn ( )
inline

Removes the first column from the table and returns it. This function assumes the table has columns. To avoid failure, call columnCount() before calling this function.

See also
takeLastColumn(), takeFirstRow(), and takeColumnAt().

◆ takeFirstRow()

template<typename T >
void Qx::Table< T >::takeFirstRow ( )
inline

Removes the first row from the table and returns it. This function assumes the table has rows. To avoid failure, call rowCount() before calling this function.

See also
takeLastRow(), takeFirstColumn(), and takeRowAt().

◆ takeLastColumn()

template<typename T >
void Qx::Table< T >::takeLastColumn ( )
inline

Removes the last column from the table and returns it. This function assumes the table has columns. To avoid failure, call columnCount() before calling this function.

See also
takeFirstColumn(), takeLastRow(), and takeColumnAt().

◆ takeLastRow()

template<typename T >
void Qx::Table< T >::takeLastRow ( )
inline

Removes the last row from the table and returns it. This function assumes the table has rows. To avoid failure, call rowCount() before calling this function.

See also
takeFirstRow(), takeLastColumn(), and takeRowAt().

◆ takeRowAt()

template<typename T >
void Qx::Table< T >::takeRowAt ( qsizetype i)
inline

Removes row i and returns it.

Equivalent to:

QList<T> row = rowAt(i);
return row;
const QList< T > & rowAt(qsizetype i) const
Definition qx-table.h:130
void removeRowAt(qsizetype i)
Definition qx-table.h:260
See also
takeFirstRow(), takeLastRow(), and takeColumnAt().

◆ value() [1/2]

template<typename T >
T Qx::Table< T >::value ( qsizetype r,
qsizetype c ) const
inline

Returns the element at row r and column c.

If the position is out of bounds, the function returns an invalid QVariant. If you are certain that r and c are within bounds, you can use at() instead, which is slightly faster.

◆ value() [2/2]

template<typename T >
T Qx::Table< T >::value ( qsizetype r,
qsizetype c,
const T & defaultValue ) const
inline

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

If the position is out of bounds, the function returns defaultValue.

◆ width()

template<typename T >
qsizetype Qx::Table< T >::width ( ) const
inline

Same as columnCount().

Member Data Documentation

◆ mTable

template<typename T >
Qx::Table< T >::mTable
protected

This is the internal structure of the table, available for direct modification in derived classes.


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