40 #ifndef QCONTIGUOUSCACHE_H
41 #define QCONTIGUOUSCACHE_H
43 #include <QtCore/qatomic.h>
49 #undef QT_QCONTIGUOUSCACHE_DEBUG
63 #ifdef QT_QCONTIGUOUSCACHE_DEBUG
76 static_assert(std::is_nothrow_destructible_v<T>,
"Types with throwing destructors are not supported in Qt containers.");
95 inline void detach() {
if (
d->ref.loadRelaxed() != 1) detach_helper(); }
96 inline bool isDetached()
const {
return d->ref.loadRelaxed() == 1; }
103 template <
typename U = T>
108 if (
other.d->start !=
d->start
109 ||
other.d->count !=
d->count
110 ||
other.d->offset !=
d->offset
111 ||
other.d->alloc !=
d->alloc)
118 template <
typename U = T>
120 {
return !(*
this ==
other); }
130 inline bool isEmpty()
const {
return d->count == 0; }
131 inline bool isFull()
const {
return d->count ==
d->alloc; }
165 {
return d->offset >= 0 &&
d->offset < (std::numeric_limits<qsizetype>::max)() -
d->count && (
d->offset %
d->alloc) ==
d->start; }
169 #ifdef QT_QCONTIGUOUSCACHE_DEBUG
170 void dump()
const {
d->dump(); }
173 void detach_helper();
176 void freeData(
Data *
x);
179 template <
typename T>
182 Data *
x = allocateData(
d->alloc);
183 x->ref.storeRelaxed(1);
186 x->offset =
d->offset;
189 T *dest =
x->array +
x->start;
190 T *
src =
d->array +
d->start;
195 if (dest ==
x->array +
x->alloc)
198 if (
src ==
d->array +
d->alloc)
207 template <
typename T>
211 if (asize ==
d->alloc)
214 Data *
x = allocateData(asize);
215 x->ref.storeRelaxed(1);
217 x->count = qMin(
d->count, asize);
218 x->offset =
d->offset +
d->count -
x->count;
220 x->start =
x->offset %
x->alloc;
227 T *dest =
x->array + (
x->start +
x->count-1) %
x->alloc;
228 T *
src =
d->array + (
d->start +
d->count-1) %
d->alloc;
231 if (dest ==
x->array)
232 dest =
x->array +
x->alloc;
235 src =
d->array +
d->alloc;
244 template <
typename T>
247 if (
d->ref.loadRelaxed() == 1) {
250 T *
i =
d->array +
d->start;
251 T *
e =
d->array +
d->alloc;
259 d->count =
d->start =
d->offset = 0;
261 Data *
x = allocateData(
d->alloc);
262 x->ref.storeRelaxed(1);
264 x->count =
x->start =
x->offset = 0;
271 template <
typename T>
277 template <
typename T>
281 d = allocateData(
cap);
282 d->ref.storeRelaxed(1);
284 d->count =
d->start =
d->offset = 0;
287 template <
typename T>
297 template <
typename T>
302 T *
i =
d->array +
d->start;
303 T *
e =
d->array +
d->alloc;
313 template <
typename T>
319 if (
d->count ==
d->alloc)
320 (
d->array + (
d->start+
d->count) %
d->alloc)->~T();
321 new (
d->array + (
d->start+
d->count) %
d->alloc)
T(std::move(
value));
323 if (
d->count ==
d->alloc) {
325 d->start %=
d->alloc;
332 template <
typename T>
338 if (
d->count ==
d->alloc)
339 (
d->array + (
d->start+
d->count) %
d->alloc)->~T();
340 new (
d->array + (
d->start+
d->count) %
d->alloc)
T(
value);
342 if (
d->count ==
d->alloc) {
344 d->start %=
d->alloc;
360 d->start =
d->alloc-1;
363 if (
d->count !=
d->alloc)
366 (
d->array +
d->start)->~T();
368 new (
d->array +
d->start)
T(std::move(
value));
380 d->start =
d->alloc-1;
383 if (
d->count !=
d->alloc)
386 (
d->array +
d->start)->~T();
394 Q_ASSERT_X(
pos >= 0,
"QContiguousCache<T>::insert",
"index out of range");
398 if (containsIndex(
pos)) {
400 }
else if (
pos ==
d->offset-1)
402 else if (
pos ==
d->offset+
d->count)
408 d->start =
pos %
d->alloc;
410 new (
d->array +
d->start)
T(std::move(
value));
419 template <
typename T>
421 {
Q_ASSERT_X(
pos >=
d->offset &&
pos -
d->offset <
d->count,
"QContiguousCache<T>::at",
"index out of range");
return d->array[
pos %
d->alloc]; }
422 template <
typename T>
426 template <
typename T>
430 if (!containsIndex(
pos))
432 return d->array[
pos %
d->alloc];
435 template <
typename T>
442 (
d->array +
d->start)->~
T();
443 d->start = (
d->start + 1) %
d->alloc;
447 template <
typename T>
454 (
d->array + (
d->start +
d->count) %
d->alloc)->~T();
457 template <
typename T>
459 {
T t = std::move(
first()); removeFirst();
return t; }
461 template <
typename T>
463 {
T t = std::move(last()); removeLast();
return t; }
small capitals from c petite p scientific i
[1]
The QContiguousCache class is a template class that provides a contiguous cache.\reentrant.
const value_type * const_pointer
qsizetype firstIndex() const
qsizetype available() const
void setCapacity(qsizetype size)
qsizetype lastIndex() const
const T & at(qsizetype pos) const
void insert(qsizetype pos, T &&value)
void append(const T &value)
QTypeTraits::compare_eq_result< U > operator!=(const QContiguousCache< T > &other) const
bool containsIndex(qsizetype pos) const
const T & operator[](qsizetype i) const
qsizetype capacity() const
void prepend(const T &value)
QContiguousCache(qsizetype capacity=0)
QContiguousCache< T > & operator=(const QContiguousCache< T > &other)
bool areIndexesValid() const
void insert(qsizetype pos, const T &value)
const value_type & const_reference
T & operator[](qsizetype i)
QContiguousCache(const QContiguousCache< T > &v)
void swap(QContiguousCache &other) noexcept
QTypeTraits::compare_eq_result< U > operator==(const QContiguousCache< T > &other) const
map insert("Paris", "France")
list append(new Employee("Blackpool", "Stephen"))
std::enable_if_t< std::conjunction_v< QTypeTraits::has_operator_equal< T >... >, bool > compare_eq_result
EGLOutputLayerEXT EGLint EGLAttrib value
#define QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(Class)
GLsizei const GLfloat * v
[13]
GLint GLint GLint GLint GLint x
[0]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
#define Q_ASSERT_X(cond, x, msg)
static QContiguousCacheData * allocateData(qsizetype size, qsizetype alignment)
void dump(QAbstractItemModel *model, QString const &indent=" - ", QModelIndex const &parent={})