1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the documentation of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:FDL$
9 ** GNU Free Documentation License
10 ** Alternatively, this file may be used under the terms of the GNU Free
11 ** Documentation License version 1.3 as published by the Free Software
12 ** Foundation and appearing in the file included in the packaging of
16 ** Alternatively, this file may be used in accordance with the terms
17 ** and conditions contained in a signed written agreement between you
26 ****************************************************************************/
30 \brief The QSet class is a template class that provides a hash-table-based set.
37 QSet<T> is one of Qt's generic \l{container classes}. It stores
38 values in an unspecified order and provides very fast lookup of
39 the values. Internally, QSet<T> is implemented as a QHash.
41 Here's an example QSet with QString values:
43 \snippet doc/src/snippets/code/doc_src_qset.cpp 0
45 To insert a value into the set, use insert():
47 \snippet doc/src/snippets/code/doc_src_qset.cpp 1
49 Another way to insert items into the set is to use operator<<():
51 \snippet doc/src/snippets/code/doc_src_qset.cpp 2
53 To test whether an item belongs to the set or not, use contains():
55 \snippet doc/src/snippets/code/doc_src_qset.cpp 3
57 If you want to navigate through all the values stored in a QSet,
58 you can use an iterator. QSet supports both \l{Java-style
59 iterators} (QSetIterator and QMutableSetIterator) and \l{STL-style
60 iterators} (QSet::iterator and QSet::const_iterator). Here's how
61 to iterate over a QSet<QWidget *> using a Java-style iterator:
63 \snippet doc/src/snippets/code/doc_src_qset.cpp 4
65 Here's the same code, but using an STL-style iterator:
67 \snippet doc/src/snippets/code/doc_src_qset.cpp 5
69 QSet is unordered, so an iterator's sequence cannot be assumed to
70 be predictable. If ordering by key is required, use a QMap.
72 To navigate through a QSet, you can also use \l{foreach}:
74 \snippet doc/src/snippets/code/doc_src_qset.cpp 6
76 Items can be removed from the set using remove(). There is also a
77 clear() function that removes all items.
79 QSet's value data type must be an \l{assignable data type}. You
80 cannot, for example, store a QWidget as a value; instead, store a
81 QWidget *. In addition, the type must provide \c operator==(), and
82 there must also be a global qHash() function that returns a hash
83 value for an argument of the key's type. See the QHash
84 documentation for a list of types supported by qHash().
86 Internally, QSet uses a hash table to perform lookups. The hash
87 table automatically grows and shrinks to provide fast lookups
88 without wasting memory. You can still control the size of the hash
89 table by calling reserve(), if you already know approximately how
90 many elements the QSet will contain, but this isn't necessary to
91 obtain good performance. You can also call capacity() to retrieve
92 the hash table's size.
94 \sa QSetIterator, QMutableSetIterator, QHash, QMap
100 Constructs an empty set.
106 \fn QSet::QSet(const QSet<T> &other)
108 Constructs a copy of \a other.
110 This operation occurs in \l{constant time}, because QSet is
111 \l{implicitly shared}. This makes returning a QSet from a
112 function very fast. If a shared instance is modified, it will be
113 copied (copy-on-write), and this takes \l{linear time}.
119 \fn QSet<T> &QSet::operator=(const QSet<T> &other)
121 Assigns the \a other set to this set and returns a reference to
126 \fn void QSet::swap(QSet<T> &other)
128 Swaps set \a other with this set. This operation is very fast and
133 \fn bool QSet::operator==(const QSet<T> &other) const
135 Returns true if the \a other set is equal to this set; otherwise
138 Two sets are considered equal if they contain the same elements.
140 This function requires the value type to implement \c operator==().
146 \fn bool QSet::operator!=(const QSet<T> &other) const
148 Returns true if the \a other set is not equal to this set; otherwise
151 Two sets are considered equal if they contain the same elements.
153 This function requires the value type to implement \c operator==().
159 \fn int QSet::size() const
161 Returns the number of items in the set.
163 \sa isEmpty(), count()
167 \fn bool QSet::isEmpty() const
169 Returns true if the set contains no elements; otherwise returns
176 \fn int QSet::capacity() const
178 Returns the number of buckets in the set's internal hash
181 The sole purpose of this function is to provide a means of fine
182 tuning QSet's memory usage. In general, you will rarely ever need
183 to call this function. If you want to know how many items are in
184 the set, call size().
186 \sa reserve(), squeeze()
189 /*! \fn void QSet::reserve(int size)
191 Ensures that the set's internal hash table consists of at
192 least \a size buckets.
194 This function is useful for code that needs to build a huge set
195 and wants to avoid repeated reallocation. For example:
197 \snippet doc/src/snippets/code/doc_src_qset.cpp 7
199 Ideally, \a size should be slightly more than the maximum number
200 of elements expected in the set. \a size doesn't have to be prime,
201 because QSet will use a prime number internally anyway. If \a size
202 is an underestimate, the worst that will happen is that the QSet
203 will be a bit slower.
205 In general, you will rarely ever need to call this function.
206 QSet's internal hash table automatically shrinks or grows to
207 provide good performance without wasting too much memory.
209 \sa squeeze(), capacity()
213 \fn void QSet::squeeze()
215 Reduces the size of the set's internal hash table to save
218 The sole purpose of this function is to provide a means of fine
219 tuning QSet's memory usage. In general, you will rarely ever
220 need to call this function.
222 \sa reserve(), capacity()
226 \fn void QSet::detach()
230 Detaches this set from any other sets with which it may share
236 /*! \fn bool QSet::isDetached() const
240 Returns true if the set's internal data isn't shared with any
241 other set object; otherwise returns false.
247 \fn void QSet::setSharable(bool sharable)
252 \fn void QSet::clear()
254 Removes all elements from the set.
260 \fn bool QSet::remove(const T &value)
262 Removes any occurrence of item \a value from the set. Returns
263 true if an item was actually removed; otherwise returns false.
265 \sa contains(), insert()
269 \fn QSet::iterator QSet::erase(iterator pos)
272 Removes the item at the iterator position \a pos from the set, and
273 returns an iterator positioned at the next item in the set.
275 Unlike remove(), this function never causes QSet to rehash its
276 internal data structure. This means that it can safely be called
277 while iterating, and won't affect the order of items in the set.
282 /*! \fn QSet::const_iterator QSet::find(const T &value) const
285 Returns a const iterator positioned at the item \a value in the
286 set. If the set contains no item \a value, the function returns
289 \sa constFind(), contains()
292 /*! \fn QSet::iterator QSet::find(const T &value)
296 Returns a non-const iterator positioned at the item \a value in
297 the set. If the set contains no item \a value, the function
301 /*! \fn QSet::const_iterator QSet::constFind(const T &value) const
304 Returns a const iterator positioned at the item \a value in the
305 set. If the set contains no item \a value, the function returns
308 \sa find(), contains()
312 \fn bool QSet::contains(const T &value) const
314 Returns true if the set contains item \a value; otherwise returns
317 \sa insert(), remove(), find()
321 \fn bool QSet::contains(const QSet<T> &other) const
324 Returns true if the set contains all items from the \a other set;
325 otherwise returns false.
327 \sa insert(), remove(), find()
330 /*! \fn QSet::const_iterator QSet::begin() const
332 Returns a const \l{STL-style iterator} positioned at the first
335 \sa constBegin(), end()
338 /*! \fn QSet::iterator QSet::begin()
342 Returns a non-const \l{STL-style iterator} positioned at the first
346 /*! \fn QSet::const_iterator QSet::cbegin() const
349 Returns a const \l{STL-style iterator} positioned at the first
355 /*! \fn QSet::const_iterator QSet::constBegin() const
357 Returns a const \l{STL-style iterator} positioned at the first
360 \sa begin(), constEnd()
363 /*! \fn QSet::const_iterator QSet::end() const
365 Returns a const \l{STL-style iterator} positioned at the imaginary
366 item after the last item in the set.
368 \sa constEnd(), begin()
371 /*! \fn QSet::iterator QSet::end()
375 Returns a non-const \l{STL-style iterator} pointing to the
376 imaginary item after the last item in the set.
379 /*! \fn QSet::const_iterator QSet::cend() const
382 Returns a const \l{STL-style iterator} pointing to the imaginary
383 item after the last item in the set.
388 /*! \fn QSet::const_iterator QSet::constEnd() const
390 Returns a const \l{STL-style iterator} pointing to the imaginary
391 item after the last item in the set.
393 \sa constBegin(), end()
397 \typedef QSet::Iterator
400 Qt-style synonym for QSet::iterator.
404 \typedef QSet::ConstIterator
406 Qt-style synonym for QSet::const_iterator.
410 \typedef QSet::const_pointer
412 Typedef for const T *. Provided for STL compatibility.
416 \typedef QSet::const_reference
418 Typedef for const T &. Provided for STL compatibility.
422 \typedef QSet::difference_type
424 Typedef for const ptrdiff_t. Provided for STL compatibility.
428 \typedef QSet::key_type
430 Typedef for T. Provided for STL compatibility.
434 \typedef QSet::pointer
436 Typedef for T *. Provided for STL compatibility.
440 \typedef QSet::reference
442 Typedef for T &. Provided for STL compatibility.
446 \typedef QSet::size_type
448 Typedef for int. Provided for STL compatibility.
452 \typedef QSet::value_type
454 Typedef for T. Provided for STL compatibility.
458 \fn QSet::const_iterator QSet::insert(const T &value)
460 Inserts item \a value into the set, if \a value isn't already
461 in the set, and returns an iterator pointing at the inserted
464 \sa operator<<(), remove(), contains()
468 \fn QSet<T> &QSet::unite(const QSet<T> &other)
470 Each item in the \a other set that isn't already in this set is
471 inserted into this set. A reference to this set is returned.
473 \sa operator|=(), intersect(), subtract()
477 \fn QSet<T> &QSet::intersect(const QSet<T> &other)
479 Removes all items from this set that are not contained in the
480 \a other set. A reference to this set is returned.
482 \sa operator&=(), unite(), subtract()
486 \fn QSet<T> &QSet::subtract(const QSet<T> &other)
488 Removes all items from this set that are contained in the
489 \a other set. Returns a reference to this set.
491 \sa operator-=(), unite(), intersect()
495 \fn bool QSet::empty() const
497 Returns true if the set is empty. This function is provided
498 for STL compatibility. It is equivalent to isEmpty().
502 \fn bool QSet::count() const
508 \fn QSet<T> &QSet::operator<<(const T &value)
509 \fn QSet<T> &QSet::operator+=(const T &value)
510 \fn QSet<T> &QSet::operator|=(const T &value)
512 Inserts a new item \a value and returns a reference to the set.
513 If \a value already exists in the set, the set is left unchanged.
519 \fn QSet<T> &QSet::operator-=(const T &value)
521 Removes the occurrence of item \a value from the set, if
522 it is found, and returns a reference to the set. If the
523 \a value is not contained the set, nothing is removed.
529 \fn QSet<T> &QSet::operator|=(const QSet<T> &other)
530 \fn QSet<T> &QSet::operator+=(const QSet<T> &other)
532 Same as unite(\a other).
534 \sa operator|(), operator&=(), operator-=()
538 \fn QSet<T> &QSet::operator&=(const QSet<T> &other)
540 Same as intersect(\a other).
542 \sa operator&(), operator|=(), operator-=()
546 \fn QSet<T> &QSet::operator&=(const T &value)
550 Same as intersect(\e{other}), if we consider \e{other} to be a set
551 that contains the singleton \a value.
556 \fn QSet<T> &QSet::operator-=(const QSet<T> &other)
558 Same as subtract(\a{other}).
560 \sa operator-(), operator|=(), operator&=()
564 \fn QSet<T> QSet::operator|(const QSet<T> &other) const
565 \fn QSet<T> QSet::operator+(const QSet<T> &other) const
567 Returns a new QSet that is the union of this set and the
570 \sa unite(), operator|=(), operator&(), operator-()
574 \fn QSet<T> QSet::operator&(const QSet<T> &other) const
576 Returns a new QSet that is the intersection of this set and the
579 \sa intersect(), operator&=(), operator|(), operator-()
583 \fn QSet<T> QSet::operator-(const QSet<T> &other) const
585 Returns a new QSet that is the set difference of this set and
586 the \a other set, i.e., this set - \a other set.
588 \sa subtract(), operator-=(), operator|(), operator&()
592 \class QSet::iterator
594 \brief The QSet::iterator class provides an STL-style non-const iterator for QSet.
596 QSet features both \l{STL-style iterators} and
597 \l{Java-style iterators}. The STL-style iterators are more
598 low-level and more cumbersome to use; on the other hand, they are
599 slightly faster and, for developers who already know STL, have
600 the advantage of familiarity.
602 QSet<T>::iterator allows you to iterate over a QSet and to remove
603 items (using QSet::erase()) while you iterate. (QSet doesn't let
604 you \e modify a value through an iterator, because that
605 would potentially require moving the value in the internal hash
606 table used by QSet.) If you want to iterate over a const QSet,
607 you should use QSet::const_iterator. It is generally good
608 practice to use QSet::const_iterator on a non-const QSet as well,
609 unless you need to change the QSet through the iterator. Const
610 iterators are slightly faster, and can improve code readability.
612 QSet\<T\>::iterator allows you to iterate over a QSet\<T\> and
613 modify it as you go (using QSet::erase()). However,
615 The default QSet::iterator constructor creates an uninitialized
616 iterator. You must initialize it using a function like
617 QSet::begin(), QSet::end(), or QSet::insert() before you can
618 start iterating. Here's a typical loop that prints all the items
621 \snippet doc/src/snippets/code/doc_src_qset.cpp 8
623 Here's a loop that removes certain items (all those that start
624 with 'J') from a set while iterating:
626 \snippet doc/src/snippets/code/doc_src_qset.cpp 9
628 STL-style iterators can be used as arguments to \l{generic
629 algorithms}. For example, here's how to find an item in the set
630 using the qFind() algorithm:
632 \snippet doc/src/snippets/code/doc_src_qset.cpp 10
634 Multiple iterators can be used on the same set. However, you may
635 not attempt to modify the container while iterating on it.
637 \sa QSet::const_iterator, QMutableSetIterator
641 \class QSet::const_iterator
642 \brief The QSet::const_iterator class provides an STL-style const iterator for QSet.
645 QSet features both \l{STL-style iterators} and
646 \l{Java-style iterators}. The STL-style iterators are more
647 low-level and more cumbersome to use; on the other hand, they are
648 slightly faster and, for developers who already know STL, have
649 the advantage of familiarity.
651 QSet\<Key, T\>::const_iterator allows you to iterate over a QSet.
652 If you want to modify the QSet as you iterate over it, you must
653 use QSet::iterator instead. It is generally good practice to use
654 QSet::const_iterator on a non-const QSet as well, unless you need
655 to change the QSet through the iterator. Const iterators are
656 slightly faster, and can improve code readability.
658 The default QSet::const_iterator constructor creates an
659 uninitialized iterator. You must initialize it using a function
660 like QSet::begin(), QSet::end(), or QSet::insert() before you can
661 start iterating. Here's a typical loop that prints all the items
664 \snippet doc/src/snippets/code/doc_src_qset.cpp 11
666 STL-style iterators can be used as arguments to \l{generic
667 algorithms}. For example, here's how to find an item in the set
668 using the qFind() algorithm:
670 \snippet doc/src/snippets/code/doc_src_qset.cpp 12
672 Multiple iterators can be used on the same set. However, you may
673 not attempt to modify the container while iterating on it.
675 \sa QSet::iterator, QSetIterator
679 \fn QSet::iterator::iterator()
680 \fn QSet::const_iterator::const_iterator()
682 Constructs an uninitialized iterator.
684 Functions like operator*() and operator++() should not be called
685 on an uninitialized iterator. Use operator=() to assign a value
686 to it before using it.
688 \sa QSet::begin(), QSet::end()
692 \fn QSet::iterator::iterator(typename Hash::iterator i)
693 \fn QSet::const_iterator::const_iterator(typename Hash::const_iterator i)
699 \typedef QSet::iterator::iterator_category
700 \typedef QSet::const_iterator::iterator_category
702 Synonyms for \e {std::bidirectional_iterator_tag} indicating
703 these iterators are bidirectional iterators.
707 \typedef QSet::iterator::difference_type
708 \typedef QSet::const_iterator::difference_type
714 \typedef QSet::iterator::value_type
715 \typedef QSet::const_iterator::value_type
721 \typedef QSet::iterator::pointer
722 \typedef QSet::const_iterator::pointer
728 \typedef QSet::iterator::reference
729 \typedef QSet::const_iterator::reference
735 \fn QSet::iterator::iterator(const iterator &other)
736 \fn QSet::const_iterator::const_iterator(const const_iterator &other)
738 Constructs a copy of \a other.
742 \fn QSet::const_iterator::const_iterator(const iterator &other)
746 Constructs a copy of \a other.
750 \fn QSet::iterator &QSet::iterator::operator=(const iterator &other)
751 \fn QSet::const_iterator &QSet::const_iterator::operator=(const const_iterator &other)
753 Assigns \a other to this iterator.
757 \fn const T &QSet::iterator::operator*() const
758 \fn const T &QSet::const_iterator::operator*() const
760 Returns a reference to the current item.
766 \fn const T *QSet::iterator::operator->() const
767 \fn const T *QSet::const_iterator::operator->() const
769 Returns a pointer to the current item.
775 \fn bool QSet::iterator::operator==(const iterator &other) const
776 \fn bool QSet::const_iterator::operator==(const const_iterator &other) const
778 Returns true if \a other points to the same item as this
779 iterator; otherwise returns false.
785 \fn bool QSet::iterator::operator==(const const_iterator &other) const
786 \fn bool QSet::iterator::operator!=(const const_iterator &other) const
792 \fn bool QSet::iterator::operator!=(const iterator &other) const
793 \fn bool QSet::const_iterator::operator!=(const const_iterator &other) const
795 Returns true if \a other points to a different item than this
796 iterator; otherwise returns false.
802 \fn QSet::iterator &QSet::iterator::operator++()
803 \fn QSet::const_iterator &QSet::const_iterator::operator++()
805 The prefix ++ operator (\c{++it}) advances the iterator to the
806 next item in the set and returns an iterator to the new current
809 Calling this function on QSet::constEnd() leads to
816 \fn QSet::iterator QSet::iterator::operator++(int)
817 \fn QSet::const_iterator QSet::const_iterator::operator++(int)
821 The postfix ++ operator (\c{it++}) advances the iterator to the
822 next item in the set and returns an iterator to the previously
827 \fn QSet::iterator &QSet::iterator::operator--()
828 \fn QSet::const_iterator &QSet::const_iterator::operator--()
830 The prefix -- operator (\c{--it}) makes the preceding item
831 current and returns an iterator to the new current item.
833 Calling this function on QSet::begin() leads to undefined
840 \fn QSet::iterator QSet::iterator::operator--(int)
841 \fn QSet::const_iterator QSet::const_iterator::operator--(int)
845 The postfix -- operator (\c{it--}) makes the preceding item
846 current and returns an iterator to the previously current item.
850 \fn QSet::iterator QSet::iterator::operator+(int j) const
851 \fn QSet::const_iterator QSet::const_iterator::operator+(int j) const
853 Returns an iterator to the item at \a j positions forward from
854 this iterator. (If \a j is negative, the iterator goes backward.)
856 This operation can be slow for large \a j values.
862 \fn QSet::iterator QSet::iterator::operator-(int j) const
863 \fn QSet::const_iterator QSet::const_iterator::operator-(int j) const
865 Returns an iterator to the item at \a j positions backward from
866 this iterator. (If \a j is negative, the iterator goes forward.)
868 This operation can be slow for large \a j values.
874 \fn QSet::iterator &QSet::iterator::operator+=(int j)
875 \fn QSet::const_iterator &QSet::const_iterator::operator+=(int j)
877 Advances the iterator by \a j items. (If \a j is negative, the
878 iterator goes backward.)
880 This operation can be slow for large \a j values.
882 \sa operator-=(), operator+()
886 \fn QSet::iterator &QSet::iterator::operator-=(int j)
887 \fn QSet::const_iterator &QSet::const_iterator::operator-=(int j)
889 Makes the iterator go back by \a j items. (If \a j is negative,
890 the iterator goes forward.)
892 This operation can be slow for large \a j values.
894 \sa operator+=(), operator-()
897 /*! \fn QList<T> QSet<T>::toList() const
899 Returns a new QList containing the elements in the set. The
900 order of the elements in the QList is undefined.
904 \snippet doc/src/snippets/code/doc_src_qset.cpp 13
906 \sa fromList(), QList::fromSet(), qSort()
909 /*! \fn QList<T> QSet<T>::values() const
911 Returns a new QList containing the elements in the set. The
912 order of the elements in the QList is undefined.
914 This is the same as toList().
916 \sa fromList(), QList::fromSet(), qSort()
920 /*! \fn QSet<T> QSet<T>::fromList(const QList<T> &list)
922 Returns a new QSet object containing the data contained in \a
923 list. Since QSet doesn't allow duplicates, the resulting QSet
924 might be smaller than the \a list, because QList can contain
929 \snippet doc/src/snippets/code/doc_src_qset.cpp 14
931 \sa toList(), QList::toSet()
935 \fn QDataStream &operator<<(QDataStream &out, const QSet<T> &set)
938 Writes the \a set to stream \a out.
940 This function requires the value type to implement \c operator<<().
942 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
946 \fn QDataStream &operator>>(QDataStream &in, QSet<T> &set)
949 Reads a set from stream \a in into \a set.
951 This function requires the value type to implement \c operator>>().
953 \sa \link datastreamformat.html Format of the QDataStream operators \endlink