Browse Source

New improved Sample in TorrentSpeedMonitor

adaptive-webui-19844
Ivan Sorokin 10 years ago
parent
commit
4ff08ff691
  1. 68
      src/qtlibtorrent/torrentspeedmonitor.cpp

68
src/qtlibtorrent/torrentspeedmonitor.cpp

@ -36,12 +36,69 @@ @@ -36,12 +36,69 @@
using namespace libtorrent;
namespace {
template<class T> struct Sample {
Sample(const T down = 0, const T up = 0) : download(down), upload(up) {}
Sample()
: download()
, upload()
{}
Sample(T download, T upload)
: download(download)
, upload(upload)
{}
template <typename U>
explicit Sample(Sample<U> other)
: download(static_cast<U>(other.download))
, upload(static_cast<U>(other.upload))
{}
T download;
T upload;
};
template <typename T>
Sample<T>& operator+=(Sample<T>& lhs, Sample<T> const& rhs) {
lhs.download += rhs.download;
lhs.upload += rhs.upload;
return lhs;
}
template <typename T>
Sample<T>& operator-=(Sample<T>& lhs, Sample<T> const& rhs) {
lhs.download -= rhs.download;
lhs.upload -= rhs.upload;
return lhs;
}
template <typename T>
Sample<T> operator+(Sample<T> const& lhs, Sample<T> const& rhs) {
return Sample<T>(lhs.download + rhs.download, lhs.upload + rhs.upload);
}
template <typename T>
Sample<T> operator-(Sample<T> const& lhs, Sample<T> const& rhs) {
return Sample<T>(lhs.download - rhs.download, lhs.upload - rhs.upload);
}
template <typename T>
Sample<T> operator*(Sample<T> const& lhs, T rhs) {
return Sample<T>(lhs.download * rhs, lhs.upload * rhs);
}
template <typename T>
Sample<T> operator*(T lhs,Sample<T> const& rhs) {
return Sample<T>(lhs * rhs.download, lhs * rhs.upload);
}
template <typename T>
Sample<T> operator/(Sample<T> const& lhs, T rhs) {
return Sample<T>(lhs.download / rhs, lhs.upload / rhs);
}
}
class SpeedSample {
public:
@ -79,16 +136,13 @@ Sample<qreal> SpeedSample::average() const @@ -79,16 +136,13 @@ Sample<qreal> SpeedSample::average() const
if (m_speedSamples.empty())
return Sample<qreal>();
qlonglong sumDL = 0;
qlonglong sumUL = 0;
Sample<qlonglong> sum;
foreach (const Sample<int>& s, m_speedSamples) {
sumDL += s.download;
sumUL += s.upload;
sum += Sample<qlonglong>(s);
}
const qreal numSamples = m_speedSamples.size();
return Sample<qreal>(sumDL/numSamples, sumUL/numSamples);
return Sample<qreal>(sum) * (1. / m_speedSamples.size());
}
void TorrentSpeedMonitor::removeSamples(const QString &hash)

Loading…
Cancel
Save