Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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// Qt-Security score:significant reason:default
4
5#ifndef QTCORE_QMINIMALFLATSET_P_H
6#define QTCORE_QMINIMALFLATSET_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtCore/qcontainerfwd.h>
20#include <QtCore/qfunctionaltools_impl.h> // CompactStorage
21#include <QtCore/private/qglobal_p.h>
22
23//#define QMINIMAL_FLAT_SET_DEBUG
24#ifdef QMINIMAL_FLAT_SET_DEBUG
25# include <QtCore/qscopeguard.h>
26# include <QtCore/qdebug.h>
27# define QMINIMAL_FLAT_SET_PRINT_AT_END
28 const auto sg = qScopeGuard([&] { qDebug() << this << *this; });
29#else
30# define QMINIMAL_FLAT_SET_PRINT_AT_END
31#endif
32
33#include <algorithm> // for std::lower_bound, std::sort
34#include <functional> // for std::less, std::ref
35#include <iterator> // for std::iterator_traits
36
37QT_BEGIN_NAMESPACE
38
39/*
40 This is a minimal version of a QFlatSet, the std::set version of QFlatMap.
41 Like QFlatMap, it has linear insertion and removal, not logarithmic, like
42 real QMap and std::set, so it's only a good container if you either have
43 very few entries or lots, but with separate setup and lookup stages.
44 Because a full QFlatSet would be 10x the work on writing this minimal one,
45 we keep it here for now. When more users pop up and the class has matured a
46 bit, we can consider moving it as QFlatSet alongside QFlatMap.
47*/
48
49template <typename T, typename Container = QList<T>, typename Compare = std::less<T>>
50class QMinimalFlatSet : QtPrivate::CompactStorage<Compare>
51{
52 Container c;
53 using CompareStorage = QtPrivate::CompactStorage<Compare>;
54public:
55 QMinimalFlatSet() = default;
56 explicit QMinimalFlatSet(const Compare &cmp) : CompareStorage{cmp} {}
57 explicit QMinimalFlatSet(std::initializer_list<T> init, const Compare &cmp = Compare())
58 : QMinimalFlatSet(init.begin(), init.end(), cmp)
59 {
60 }
61 explicit QMinimalFlatSet(Container init, const Compare &cmp = Compare())
62 : CompareStorage{cmp}, c(std::move(init))
63 {
64 std::sort(c.begin(), c.end(), comparisonObject());
65 c.erase(std::unique(c.begin(), c.end()), c.end());
66 }
67 template <typename InputIt>
68 QMinimalFlatSet(InputIt first, InputIt last, const Compare &cmp = Compare())
69 : CompareStorage{cmp}, c(first, last)
70 {
71 std::sort(c.begin(), c.end(), comparisonObject());
72 c.erase(std::unique(c.begin(), c.end()), c.end());
73 }
74
75 // Rule Of Zero applies
76
77 using const_iterator = typename Container::const_iterator;
78 using iterator = const_iterator;
79 using const_reverse_iterator = typename Container::const_reverse_iterator;
80 using reverse_iterator = const_reverse_iterator;
81 using value_type = T;
82 using key_compare = Compare;
83 using value_compare = Compare;
84
85 key_compare key_comp() const { return this->object(); }
86 value_compare value_comp() const { return key_comp(); }
87
88 iterator begin() const { return c.cbegin(); }
89 iterator end() const { return c.cend(); }
90 iterator cbegin() const { return begin(); }
91 iterator cend() const { return end(); }
92
93 reverse_iterator rbegin() const { return c.crbegin(); }
94 reverse_iterator rend() const { return c.crend(); }
95 reverse_iterator crbegin() const { return rbegin(); }
96 reverse_iterator crend() const { return rend(); }
97
98 void clear() {
100 c.clear();
101 }
102 auto size() const { return c.size(); }
103 auto count() const { return size(); }
104 bool isEmpty() const { return size() == 0; }
105
106 std::pair<iterator, bool> insert(value_type &&v)
107 {
109 const auto r = lookup(v);
110 if (r.exists)
111 return {r.it, false};
112 else
113 return {c.insert(r.it, std::move(v)), true};
114 }
115
116 std::pair<iterator, bool> insert(const value_type &v)
117 {
119 const auto r = lookup(v);
120 if (r.exists)
121 return {r.it, false};
122 else
123 return {c.insert(r.it, v), true};
124 }
125
126 void erase(const value_type &v)
127 {
129 const auto r = lookup(v);
130 if (r.exists)
131 c.erase(r.it);
132 }
133 void remove(const value_type &v) { erase(v); }
134
135 bool contains(const value_type &v) const
136 {
137 return lookup(v).exists;
138 }
139
140 const Container &values() const & { return c; }
141 Container values() && { return std::move(c); }
142
143private:
144 auto comparisonObject() const { return std::cref(this->object()); }
145
146 auto lookup(const value_type &v) const
147 {
148 struct R {
149 iterator it;
150 bool exists;
151 };
152
153 const auto it = std::lower_bound(c.cbegin(), c.cend(), v, comparisonObject());
154 return R{it, it != c.cend() && !comparisonObject()(v, *it)};
155 }
156
157#ifdef QMINIMAL_FLAT_SET_DEBUG
158 friend QDebug operator<<(QDebug dbg, const QMinimalFlatSet &set)
159 {
160 const QDebugStateSaver saver(dbg);
161 dbg.nospace() << "QMinimalFlatSet{";
162 for (auto &e : set)
163 dbg << e << ", ";
164 return dbg << "}";
165 }
166#endif
167};
168
169template <typename InputIt,
170 typename Compare = std::less<typename std::iterator_traits<InputIt>::value_type>>
171QMinimalFlatSet(InputIt, InputIt, Compare = Compare())
174
175#undef QMINIMAL_FLAT_SET_PRINT_AT_END
176
177template <typename T, qsizetype N = QVarLengthArrayDefaultPrealloc, typename Compare = std::less<T>>
178using QMinimalVarLengthFlatSet = QMinimalFlatSet<T, QVarLengthArray<T, N>, Compare>;
179
180QT_END_NAMESPACE
181
182#endif // QTCORE_QMINIMALFLATSET_P_H
QMinimalFlatSet(InputIt, InputIt, Compare=Compare()) -> QMinimalFlatSet< typename std::iterator_traits< InputIt >::value_type, QList< typename std::iterator_traits< InputIt >::value_type >, Compare >
#define QMINIMAL_FLAT_SET_PRINT_AT_END