Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qminimalflatset_p.h
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QTDECLARATIVE_QMINIMALFLATSET_P_H
5#define QTDECLARATIVE_QMINIMALFLATSET_P_H
6
7#if __has_include(<QtCore/private/qminimalflatset_p.h>)
8# include <QtCore/private/qminimalflatset_p.h>
9#else
10
11//
12// W A R N I N G
13// -------------
14//
15// This file is not part of the Qt API. It exists purely as an
16// implementation detail. This header file may change from version to
17// version without notice, or even be removed.
18//
19// We mean it.
20//
21
22#include <QtQuick/qtquickglobal.h>
23
24#include <QtCore/qcontainerfwd.h>
25#include <QtCore/private/qglobal_p.h>
26
27//#define QMINIMAL_FLAT_SET_DEBUG
28#ifdef QMINIMAL_FLAT_SET_DEBUG
29# include <QtCore/qscopeguard.h>
30# include <QtCore/qdebug.h>
31# define QMINIMAL_FLAT_SET_PRINT_AT_END \
32 const auto sg = qScopeGuard([&] { qDebug() << this << *this; });
33#else
34# define QMINIMAL_FLAT_SET_PRINT_AT_END
35#endif
36
37#include <algorithm> // for std::lower_bound
38
40
41/*
42 This is a minimal version of a QFlatSet, the std::set version of QFlatMap.
43 Like QFlatMap, it has linear insertion and removal, not logarithmic, like
44 real QMap and std::set, so it's only a good container if you either have
45 very few entries or lots, but with separate setup and lookup stages.
46 Because a full QFlatSet would be 10x the work on writing this minimal one,
47 we keep it here for now. When more users pop up and the class has matured a
48 bit, we can consider moving it alongside QFlatMap in QtCore.
49*/
50
51template <typename T, typename Container = QList<T>>
53{
54 Container c;
55public:
56 // compiler-generated default ctor is ok!
57 // compiler-generated copy/move ctor/assignment operators are ok!
58 // compiler-generated dtor is ok!
59
60 using const_iterator = typename Container::const_iterator;
62 using const_reverse_iterator = typename Container::const_reverse_iterator;
64 using value_type = T;
65
66 iterator begin() const { return c.cbegin(); }
67 iterator end() const { return c.cend(); }
68 iterator cbegin() const { return begin(); }
69 iterator cend() const { return cend(); }
70
71 reverse_iterator rbegin() const { return c.crbegin(); }
72 reverse_iterator rend() const { return c.crend(); }
73 reverse_iterator crbegin() const { return rbegin(); }
74 reverse_iterator crend() const { return rend(); }
75
76 void clear() {
78 c.clear();
79 }
80 auto size() const { return c.size(); }
81 auto count() const { return size(); }
82 bool isEmpty() const { return size() == 0; }
83
84 std::pair<iterator, bool> insert(value_type &&v)
85 {
87 const auto r = lookup(v);
88 if (r.exists)
89 return {r.it, false};
90 else
91 return {c.insert(r.it, std::move(v)), true};
92 }
93
94 std::pair<iterator, bool> insert(const value_type &v)
95 {
97 const auto r = lookup(v);
98 if (r.exists)
99 return {r.it, false};
100 else
101 return {c.insert(r.it, v), true};
102 }
103
104 void erase(const value_type &v)
105 {
107 const auto r = lookup(v);
108 if (r.exists)
109 c.erase(r.it);
110 }
111 void remove(const value_type &v) { erase(v); }
112
113 bool contains(const value_type &v) const
114 {
115 return lookup(v).exists;
116 }
117
118 const Container &values() const & { return c; }
119 Container values() && { return std::move(c); }
120
121private:
122 auto lookup(const value_type &v) const
123 {
124 struct R {
125 iterator it;
126 bool exists;
127 };
128
129 const auto it = std::lower_bound(c.cbegin(), c.cend(), v);
130 return R{it, it != c.cend() && !(v < *it)};
131 }
132
133#ifdef QMINIMAL_FLAT_SET_DEBUG
134 friend QDebug operator<<(QDebug dbg, const QMinimalFlatSet &set)
135 {
136 const QDebugStateSaver saver(dbg);
137 dbg.nospace() << "QMinimalFlatSet{";
138 for (auto &e : set)
139 dbg << e << ", ";
140 return dbg << "}";
141 }
142#endif
143};
144
146
147#endif // !__has_include(<QtCore/private/qminimalflatset_p.h>)
148
149#endif // QTDECLARATIVE_QMINIMALFLATSET_P_H
\inmodule QtCore
\inmodule QtCore
const_reverse_iterator reverse_iterator
iterator cend() const
reverse_iterator rend() const
bool isEmpty() const
std::pair< iterator, bool > insert(value_type &&v)
void erase(const value_type &v)
reverse_iterator rbegin() const
Container values() &&
typename Container::const_iterator const_iterator
const Container & values() const &
typename Container::const_reverse_iterator const_reverse_iterator
reverse_iterator crend() const
iterator cbegin() const
reverse_iterator crbegin() const
bool contains(const value_type &v) const
const_iterator iterator
void remove(const value_type &v)
iterator begin() const
iterator end() const
std::pair< iterator, bool > insert(const value_type &v)
const_iterator cend() const noexcept
Definition qset.h:142
QSet< QString >::iterator it
Combined button and popup list for selecting options.
GLsizei const GLfloat * v
[13]
GLboolean r
[2]
const GLubyte * c
#define QMINIMAL_FLAT_SET_PRINT_AT_END
QFuture< QSet< QChar > > set
[10]
QDataStream & operator<<(QDataStream &out, const MyClass &myObj)
[4]