Qx v0.5.8
Qt Extensions Library
|
The Index template class provides a wrapper for integers with the notion of 'first', 'last' and 'null'. More...
#include <qx/core/qx-index.h>
Public Member Functions | |
constexpr | Index () |
constexpr | Index (Extent e) |
constexpr | Index (T value) |
bool | isLast () const |
bool | isNull () const |
T & | operator* () |
Index | operator* (const Index &other) |
Index & | operator*= (const Index &other) |
Index | operator+ (const Index &other) |
Index & | operator++ () |
Index | operator++ (int) |
Index & | operator+= (const Index &other) |
Index | operator- (const Index &other) |
Index & | operator-- () |
Index | operator-- (int) |
Index & | operator-= (const Index &other) |
Index | operator/ (const Index &other) |
Index & | operator/= (const Index &other) |
std::strong_ordering | operator<=> (const Index &other) const noexcept |
bool | operator== (const Index &other) const noexcept |
When working with contiguous data that must be referenced by index, it is often helpful to be able to refer to the last value of a dataset without necessarily knowing its size. Since integers only intrinsically support purely numerical values, this is often achieved by using the value -1, but this can be prone to error and require awkward handling of arithmetic, especially since implementations that use this approach also tend to simultaneously use -1 to mean 'none' in the context of searches.
The Index type is designed to remedy this issue by providing a more distinct representation of a set's final index via the constructor Index(Extent), which can also be used to more explicitly reference the first index. The type also can be null, providing further clarity for the 'none' case.
To minimize unintentional interactions, Index<T> only has comparison and arithmetic operators defined for when both operands are instances of the class and the underlying raw value must otherwise be accessed explicitly with Index<T>::operator*().
|
inlineconstexpr |
Creates an null index.
Creates an index at the given extent e.
|
inlineconstexpr |
Creates an index with the underlying value.
A value <
0 will result in a null index.
|
inline |
Returns true
if the index represents the last index of an arbitrary dataset; otherwise returns false
.
This is equivalent to *this == Index<T>(Qx::Last)
.
|
inline |
Returns true
if the index is null; otherwise returns false
.
|
inline |
Returns the underlying value of the index.
std::numeric_limits<T>::max()
and for 'last' indices and 0 for null indices respectively, and therefore can cause unexpected results if this operator is used blindly. Using this operator is essentially an acknowledgment that the null and 'last' states have already been checked/accounted for, or that this conversion behavior is desirable in a given context. Returns an index that is the product of this index and other.
Multiplies other with this index and returns a reference to this index.
Returns an index that is the sum between this index and other.
Increases the index by 1, unless its 'last' or null, and returns a reference to the index.
Increases the index by 1, unless its 'last' or null, and returns a pre-increment copy of the index.
Adds other from this index and returns a reference to this index.
Returns an index that is the difference between this index and other.
Decreases the index by 1, unless its 'last' or null, and returns a reference to the index.
Decreases the index by 1, unless its 'last' or null, and returns a pre-decrement copy of the index.
Subtracts other from this index and returns a reference to this index.
Returns an index that is the quotient of this index and other.
Divides other from this index and returns a reference to this index.
|
inlinenoexcept |
Performs a three-way comparison between this index and other.
Returns:
<0
if this index is less than other 0
if this index is equal to other >0
if this index is greater than other
|
inlinenoexcept |
Returns true
if this index and other are both null, 'last', or have the same underlying value; otherwise returns false
.