Qx v0.5.8
Qt Extensions Library
|
The FreeIndexTracker class tracks which slots from a given range of indices are in use and which are available. More...
#include <qx/core/qx-freeindextracker.h>
Public Member Functions | |
FreeIndexTracker (quint64 min=0, quint64 max=1, QSet< quint64 > reserved=QSet< quint64 >()) | |
std::optional< quint64 > | firstFree () const |
std::optional< quint64 > | firstReserved () const |
quint64 | free () const |
bool | isBooked () const |
bool | isReserved (quint64 index) const |
std::optional< quint64 > | lastFree () const |
std::optional< quint64 > | lastReserved () const |
quint64 | maximum () const |
quint64 | minimum () const |
std::optional< quint64 > | nearestFree (quint64 index) const |
std::optional< quint64 > | nextFree (quint64 index) const |
std::optional< quint64 > | previousFree (quint64 index) const |
quint64 | range () const |
bool | release (quint64 index) |
bool | releaseAll () |
bool | reserve (quint64 index) |
bool | reserveAll () |
quint64 | reserved () const |
std::optional< quint64 > | reserveFirstFree () |
std::optional< quint64 > | reserveLastFree () |
std::optional< quint64 > | reserveNearestFree (quint64 index) |
std::optional< quint64 > | reserveNextFree (quint64 index) |
std::optional< quint64 > | reservePreviousFree (quint64 index) |
The range of an index tracker can be expanded or contracted, and indices within that range can be reserved or released. An example use-case for this class might be tracking which spaces in a parking lot are free.
Qx::FreeIndexTracker::FreeIndexTracker | ( | quint64 | min = 0, |
quint64 | max = 1, | ||
QSet< quint64 > | reserved = QSet<quint64>() ) |
Creates an index tracker for the range min to max, with the indices from reserved already reserved.
If any of the values within reserved fall outside the range of min:max, the range of the tracker will automatically be resized to cover them.
std::optional< quint64 > Qx::FreeIndexTracker::firstFree | ( | ) | const |
Returns the lowest available index, or std::nullopt
if all are reserved.
std::optional< quint64 > Qx::FreeIndexTracker::firstReserved | ( | ) | const |
Returns the lowest index that is currently reserved, or std::nullopt
if all are free.
quint64 Qx::FreeIndexTracker::free | ( | ) | const |
Returns the number of unoccupied indices.
bool Qx::FreeIndexTracker::isBooked | ( | ) | const |
Returns true
if all indices are reserved; otherwise, returns false
.
bool Qx::FreeIndexTracker::isReserved | ( | quint64 | index | ) | const |
Returns true
if index is occupied; otherwise returns false
.
std::optional< quint64 > Qx::FreeIndexTracker::lastFree | ( | ) | const |
Returns the highest available index, or std::nullopt
if all are reserved.
std::optional< quint64 > Qx::FreeIndexTracker::lastReserved | ( | ) | const |
Returns the highest index that is currently reserved, or std::nullopt
if all are free.
quint64 Qx::FreeIndexTracker::maximum | ( | ) | const |
Returns the upper bound of the index tracker.
quint64 Qx::FreeIndexTracker::minimum | ( | ) | const |
Returns the lower bound of the index tracker.
std::optional< quint64 > Qx::FreeIndexTracker::nearestFree | ( | quint64 | index | ) | const |
Returns the nearest free index to index, or std::nullopt
if all are reserved.
std::optional< quint64 > Qx::FreeIndexTracker::nextFree | ( | quint64 | index | ) | const |
Returns the nearest free index that is at or after index, or std::nullopt
if all are reserved.
std::optional< quint64 > Qx::FreeIndexTracker::previousFree | ( | quint64 | index | ) | const |
Returns the nearest free index that is at or before index, or std::nullopt
if all are reserved.
quint64 Qx::FreeIndexTracker::range | ( | ) | const |
bool Qx::FreeIndexTracker::release | ( | quint64 | index | ) |
Attempts to mark index as unoccupied and returns true
if successful, or false
if the index was already free.
bool Qx::FreeIndexTracker::releaseAll | ( | ) |
Attemps to mark all indicies as unoccupied and returns true
if successful, or false
if all were already free.
bool Qx::FreeIndexTracker::reserve | ( | quint64 | index | ) |
Attempts to mark index as occupied and returns true
if successful, or false
if the index was already reserved.
bool Qx::FreeIndexTracker::reserveAll | ( | ) |
Attemps to mark all indicies as occupied and returns true
if successful, or false
if all were already reserved.
quint64 Qx::FreeIndexTracker::reserved | ( | ) | const |
Returns the number of occupied indices.
std::optional< quint64 > Qx::FreeIndexTracker::reserveFirstFree | ( | ) |
Attempts to mark the lowest available index as occupied and return it if successful, or std::nullopt
if there are no free indices.
std::optional< quint64 > Qx::FreeIndexTracker::reserveLastFree | ( | ) |
Attempts to mark the highest available index as occupied and return it if successful, or std::nullopt
if there are no free indices.
std::optional< quint64 > Qx::FreeIndexTracker::reserveNearestFree | ( | quint64 | index | ) |
Attempts to mark the nearest free index to index as occupied and returns it if successful, or std::nullopt
if there are no free indices.
std::optional< quint64 > Qx::FreeIndexTracker::reserveNextFree | ( | quint64 | index | ) |
Attempts to mark the nearest free index that is at or after index as occupied and returns it if successful, or std::nullopt
if there are no free indices.
std::optional< quint64 > Qx::FreeIndexTracker::reservePreviousFree | ( | quint64 | index | ) |
Attempts to mark the nearest free index that is at or before index as occupied and returns it if successful, or std::nullopt
if there are no free indices.