swift
Loading...
Searching...
No Matches
collection.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#ifndef SWIFT_MISC_COLLECTION_H
7#define SWIFT_MISC_COLLECTION_H
8
9#include <algorithm>
10#include <initializer_list>
11#include <iterator>
12#include <type_traits>
13#include <typeindex>
14#include <utility>
15
16#include <QMap>
17
18#include "misc/containerbase.h"
21
23#define SWIFT_TEMPLATE_COLLECTION_MIXINS(NS, T, Set, Extern) \
24 namespace NS \
25 { \
26 class Set; \
27 } \
28 namespace swift::misc::private_ns \
29 { \
30 Extern template struct CValueObjectMetaInfo<NS::Set>; \
31 Extern template struct CValueObjectMetaInfo<CCollection<NS::T>>; \
32 Extern template struct MetaTypeHelper<NS::Set>; \
33 Extern template struct MetaTypeHelper<CCollection<NS::T>>; \
34 } \
35 namespace swift::misc::mixin \
36 { \
37 Extern template class MetaType<NS::Set>; \
38 Extern template class MetaType<CCollection<NS::T>>; \
39 Extern template class DBusOperators<CCollection<NS::T>>; \
40 Extern template class JsonOperators<CCollection<NS::T>>; \
41 Extern template class String<CCollection<NS::T>>; \
42 Extern template class DataStreamOperators<CCollection<NS::T>>; \
43 Extern template class Icon<CCollection<NS::T>>; \
44 }
46
52
57#if defined(Q_OS_WIN) && defined(Q_CC_GNU)
58# define SWIFT_DECLARE_COLLECTION_MIXINS(Namespace, T, Set)
59# define SWIFT_DEFINE_COLLECTION_MIXINS(Namespace, T, Set)
60#else
61# define SWIFT_DECLARE_COLLECTION_MIXINS(Namespace, T, Set) \
62 SWIFT_TEMPLATE_COLLECTION_MIXINS(Namespace, T, Set, extern)
63# define SWIFT_DEFINE_COLLECTION_MIXINS(Namespace, T, Set) SWIFT_TEMPLATE_COLLECTION_MIXINS(Namespace, T, Set, )
64#endif
65
66namespace swift::misc
67{
68
72 template <class T>
73 class QOrderedSet : public QMap<T, T>
74 {
75 public:
77 using value_type = T;
78
81
83 QOrderedSet() = default;
84
86 QOrderedSet(std::initializer_list<T> il)
87 {
88 for (const auto &v : il)
89 {
90 insert(v);
91 }
92 }
93
95 QOrderedSet(const QList<T> &list)
96 {
97 for (const auto &v : list)
98 {
99 insert(v);
100 }
101 }
102 };
103
108 template <class T>
110 public CContainerBase<CCollection<T>>,
111 public mixin::DataStreamOperators<CCollection<T>>,
112 public mixin::Icon<CCollection<T>>
113 {
114 public:
116 using key_type = T;
117
119 using value_type = T;
120
122 using reference = T &;
123
125 using const_reference = const T &;
126
128 using pointer = T *;
129
131 using const_pointer = const T *;
132
134 using const_iterator = typename QOrderedSet<T>::const_iterator;
135
137 using iterator = const_iterator; // can't modify elements in-place
138
140 using difference_type = ptrdiff_t;
141
143 using size_type = int;
144
146 CCollection() = default;
147
149 CCollection(std::initializer_list<T> il) : m_impl(il) {}
150
152 CCollection(const CCollection &other) = default;
153
155 CCollection(const QList<T> &list) : m_impl(list) {}
156
158 CCollection(CCollection &&other) = default;
159
161 CCollection &operator=(const CCollection &other) = default;
162
164 CCollection &operator=(CCollection &&other) = default;
165
167 ~CCollection() = default;
168
170 iterator begin() { return m_impl.begin(); }
171
173 const_iterator begin() const { return m_impl.begin(); }
174
176 const_iterator cbegin() const { return m_impl.cbegin(); }
177
179 iterator end() { return m_impl.end(); }
180
182 const_iterator end() const { return m_impl.end(); }
183
185 const_iterator cend() const { return m_impl.cend(); }
186
188 void swap(CCollection &other) noexcept { m_impl.swap(other.m_impl); }
189
191 size_type size() const { return m_impl.size(); }
192
194 bool empty() const { return m_impl.isEmpty(); }
195
197 bool isEmpty() const { return empty(); }
198
200 void clear() { m_impl.clear(); }
201
205 iterator insert(const_iterator hint, const T &value)
206 {
207 Q_UNUSED(hint);
208 return insert(value);
209 }
210
215 {
216 Q_UNUSED(hint);
217 return insert(std::move(value));
218 }
219
222 iterator insert(const T &value) { return m_impl.insert(value); }
223
226 iterator insert(T &&value) { return m_impl.insert(std::move(value)); }
227
229 void insert(const CCollection &other) { std::copy(other.begin(), other.end(), std::inserter(*this, begin())); }
230
233 void insert(CCollection &&other) { std::move(other.begin(), other.end(), std::inserter(*this, begin())); }
234
236 template <typename I>
237 void insert(const CRange<I> &range)
238 {
239 std::copy(range.begin(), range.end(), std::back_inserter(*this));
240 }
241
244 iterator push_back(const T &value) { return insert(value); }
245
248 iterator push_back(T &&value) { return insert(std::move(value)); }
249
251 void push_back(const CCollection &other) { insert(other); }
252
254 void push_back(CCollection &&other) { insert(std::move(other)); }
255
257 template <typename I>
258 void push_back(const CRange<I> &range)
259 {
260 std::copy(range.begin(), range.end(), std::back_inserter(*this));
261 }
262
264 template <class C>
265 CCollection makeUnion(const C &other) const
266 {
267 CCollection result;
268 std::set_union(begin(), end(), other.begin(), other.end(), std::inserter(result, result.begin()));
269 return result;
270 }
271
273 template <class C>
274 CCollection intersection(const C &other) const
275 {
276 CCollection result;
277 std::set_intersection(begin(), end(), other.begin(), other.end(), std::inserter(result, result.begin()));
278 return result;
279 }
280
283 template <class C>
284 CCollection difference(const C &other) const
285 {
286 CCollection result;
287 std::set_difference(begin(), end(), other.begin(), other.end(), std::inserter(result, result.begin()));
288 return result;
289 }
290
293 iterator erase(iterator pos) { return m_impl.erase(pos); }
294
298 {
299 while (it1 != it2)
300 {
301 it1 = erase(it1);
302 }
303 return it1;
304 }
305
308 iterator find(const T &value) { return m_impl.find(value); }
309
312 const_iterator find(const T &value) const { return m_impl.find(value); }
313
316 void remove(const T &object)
317 {
318 auto it = find(object);
319 if (it != end())
320 {
321 erase(it);
322 }
323 }
324
327 void remove(const CCollection &other) { *this = CCollection(*this).difference(other); }
328
332 template <class Predicate>
333 int removeIf(Predicate p)
334 {
335 int count = 0;
336 for (auto it = begin(); it != end();)
337 {
338 if (p(*it))
339 {
340 it = erase(it);
341 count++;
342 }
343 else
344 {
345 ++it;
346 }
347 }
348 return count;
349 }
350
352 template <class K0, class V0, class... KeysValues>
353 int removeIf(K0 k0, V0 v0, KeysValues... keysValues)
354 {
355 // using-declaration doesn't play nicely with injected template names
356 return CCollection::CContainerBase::removeIf(k0, v0, keysValues...);
357 }
358
360 QList<T> toQList() const { return this->to(QList<T>()); }
361
363 void detach() { m_impl.detach(); }
364
366 friend bool operator==(const CCollection &a, const CCollection &b) { return a.m_impl == b.m_impl; }
367
369 friend bool operator!=(const CCollection &a, const CCollection &b) { return a.m_impl != b.m_impl; }
370
372 void marshalToDataStream(QDataStream &stream) const { stream << m_impl; }
373
375 void unmarshalFromDataStream(QDataStream &stream) { stream >> m_impl; }
376
377 private:
378 QOrderedSet<T> m_impl;
379 };
380
381} // namespace swift::misc
382
383Q_DECLARE_METATYPE(swift::misc::CCollection<int>)
384Q_DECLARE_METATYPE(swift::misc::CCollection<uint>)
385Q_DECLARE_METATYPE(swift::misc::CCollection<qlonglong>)
386Q_DECLARE_METATYPE(swift::misc::CCollection<qulonglong>)
387// CCollection<double> not instantiated due to it being a dumb idea because of rounding issues
388
389#endif // SWIFT_MISC_COLLECTION_H
Generic ordered container with value semantics.
Definition collection.h:113
friend bool operator==(const CCollection &a, const CCollection &b)
Test for equality.
Definition collection.h:366
iterator erase(iterator it1, iterator it2)
Remove the range of elements between two iterators.
Definition collection.h:297
const T & const_reference
STL compatibility.
Definition collection.h:125
ptrdiff_t difference_type
STL compatibility.
Definition collection.h:140
void swap(CCollection &other) noexcept
Swap this collection with another.
Definition collection.h:188
void push_back(const CCollection &other)
Synonym for insert.
Definition collection.h:251
void insert(CCollection &&other)
Inserts all elements from another collection into this collection. This version moves elements instea...
Definition collection.h:233
void unmarshalFromDataStream(QDataStream &stream)
Unmarshal a value from a QDataStream.
Definition collection.h:375
iterator insert(const_iterator hint, T &&value)
For compatibility with std::inserter.
Definition collection.h:214
const_iterator find(const T &value) const
Efficient find method using the find of the implementation container. Typically O(log n).
Definition collection.h:312
size_type size() const
Returns number of elements in the collection.
Definition collection.h:191
CCollection difference(const C &other) const
Returns a collection which contains all the elements from this collection which are not in the other ...
Definition collection.h:284
void remove(const T &object)
Efficient remove using the find and erase of the implementation container. Typically O(log n).
Definition collection.h:316
const_iterator cbegin() const
Returns iterator at the beginning of the collection.
Definition collection.h:176
T * pointer
STL compatibility.
Definition collection.h:128
iterator insert(const_iterator hint, const T &value)
For compatibility with std::inserter.
Definition collection.h:205
CCollection()=default
Default constructor.
T & reference
STL compatibility.
Definition collection.h:122
iterator find(const T &value)
Efficient find method using the find of the implementation container. Typically O(log n).
Definition collection.h:308
void insert(const CCollection &other)
Inserts all elements from another collection into this collection.
Definition collection.h:229
T key_type
STL compatibility.
Definition collection.h:116
T value_type
STL compatibility.
Definition collection.h:119
CCollection intersection(const C &other) const
Returns a collection which is the intersection of this collection and another.
Definition collection.h:274
iterator insert(const T &value)
Inserts an element into the collection.
Definition collection.h:222
bool empty() const
Returns true if the collection is empty.
Definition collection.h:194
CCollection(const CCollection &other)=default
Copy constructor.
typename QOrderedSet< T >::const_iterator const_iterator
STL compatibility.
Definition collection.h:134
CCollection & operator=(const CCollection &other)=default
Copy assignment.
iterator begin()
Returns iterator at the beginning of the collection.
Definition collection.h:170
const_iterator end() const
Returns const iterator one past the end of the collection.
Definition collection.h:182
int size_type
STL compatibility.
Definition collection.h:143
const_iterator iterator
STL compatibility.
Definition collection.h:137
void push_back(const CRange< I > &range)
Synonym for insert.
Definition collection.h:258
bool isEmpty() const
Synonym for empty.
Definition collection.h:197
CCollection & operator=(CCollection &&other)=default
Move assignment.
int removeIf(Predicate p)
Remove elements for which a given predicate returns true.
Definition collection.h:333
friend bool operator!=(const CCollection &a, const CCollection &b)
Test for inequality.
Definition collection.h:369
CCollection(const QList< T > &list)
Constructor from QList.
Definition collection.h:155
iterator push_back(T &&value)
Synonym for insert.
Definition collection.h:248
CCollection makeUnion(const C &other) const
Returns a collection which is the union of this collection and another container.
Definition collection.h:265
void insert(const CRange< I > &range)
Appends all elements from a range at the end of this collection.
Definition collection.h:237
void push_back(CCollection &&other)
Synonym for insert.
Definition collection.h:254
const_iterator cend() const
Returns const iterator one past the end of the collection.
Definition collection.h:185
iterator push_back(const T &value)
Synonym for insert.
Definition collection.h:244
CCollection(std::initializer_list< T > il)
Initializer list constructor.
Definition collection.h:149
void clear()
Removes all elements in the collection.
Definition collection.h:200
iterator erase(iterator pos)
Remove the element pointed to by the given iterator.
Definition collection.h:293
int removeIf(K0 k0, V0 v0, KeysValues... keysValues)
Remove elements matching some particular key/value pair(s).
Definition collection.h:353
const T * const_pointer
STL compatibility.
Definition collection.h:131
QList< T > toQList() const
Convert to a QList.
Definition collection.h:360
void remove(const CCollection &other)
Removes from this collection all of the elements of another collection.
Definition collection.h:327
iterator insert(T &&value)
Moves an element into the collection.
Definition collection.h:226
void marshalToDataStream(QDataStream &stream) const
Marshal a value to a QDataStream.
Definition collection.h:372
~CCollection()=default
Destructor.
iterator end()
Returns iterator one past the end of the collection.
Definition collection.h:179
CCollection(CCollection &&other)=default
Move constructor.
const_iterator begin() const
Returns iterator at the beginning of the collection.
Definition collection.h:173
Base class for CCollection and CSequence adding mutating operations and CValueObject facility on top ...
A range is a conceptual container which does not contain any elements of its own, but is constructed ...
Definition range.h:199
const_iterator end() const
Begin and end iterators.
Definition range.h:222
const_iterator begin() const
Begin and end iterators.
Definition range.h:220
Needed for compatibility with C++ standard algorithms which expect ordered sets.
Definition collection.h:74
T value_type
Type of values stored in the set.
Definition collection.h:77
QOrderedSet(std::initializer_list< T > il)
Initializer list constructor.
Definition collection.h:86
QOrderedSet()=default
Default constructor.
QOrderedSet(const QList< T > &list)
Constructor from QList.
Definition collection.h:95
QMap< T, T >::iterator insert(const T &value)
Insert a new value into the set.
Definition collection.h:80
CRTP class template to generate non-member QDataStream streaming operators.
CRTP class template from which a derived class can inherit icon-related functions.
Definition mixinicon.h:28
Free functions in swift::misc.
QMap< Key, T >::iterator insert(QMap< Key, T >::const_iterator pos, const Key &key, const T &value)
T value(const Key &key, const T &defaultValue) const const