diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2019-05-22 22:01:20 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2019-05-24 12:27:36 +0000 |
commit | 54590c4f8acc8e1be24a1d4d4bb0d716018c9ba0 (patch) | |
tree | 6c5775bf091965b91f0a08da814a3e20cb79305b /examples/dbus | |
parent | c85e6dc99865c591167dd5a9dc2900247064cee6 (diff) |
Torrent example: rewrite some very convoluted code
The old code iterated over the peer list, inserting active peers into
a QSet, curiously not stopping to search for clients if it had already
determined the peer to be active.
It then iterated over the peers again, storing the indexes of the
peers it had determined to be active in the first loop, in a QList.
It _then_ iterated over the index list, in reverse, calling removeAt()
on the peers list.
<sean parent>That's a remove_if!</sean parent>
The twist is, that only some maximum number of inactive peers should
be removed, just enough to bring the number of peers below a
predefined number.
To solve, use a lambda that keeps track of the number of times it has
returned true, returning false once the count drops to zero. We can't
use a mutable lambda here, since the STL algorithms are allowed to
copy the predicate as many times as they wish, and, indeed, remove_if
is commonly implemented by calling find_if. But the standard
guarantees exactly one application of the predicate per element, so we
can assume that we're not called again on the same element, and
therefore keep a reference to an external count.
With this, what was a horrible mess becomes a single call to remove_if.
Also change a while(--n) c.removeFirst() loop to a single call of
range-erase.
Change-Id: I6c6a54a1805e5b376800e1116e7aec643e95e4e1
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'examples/dbus')
0 files changed, 0 insertions, 0 deletions