Repository: dogecoin/dogecoin
File: src/qt/trafficgraphwidget.cpp (C++)

Code before:
        nTimespanLastBytesIn = model->getTotalBytesRecv();
        nTimespanLastBytesOut = model->getTotalBytesSent();
        nBaseLastBytesIn = nTimespanLastBytesIn;
        nBaseLastBytesOut = nTimespanLastBytesOut;

        baseTimer->start(BASE_SAMPLE_PERIOD_MS);
    }
}

int TrafficGraphWidget::getGraphRangeMins() const
{
    return nMins;
}

float TrafficGraphWidget::resampleSamples(const QVector<float> &source, QVector<float> &dest) const
{
    int numSourceSamples = nMins * 60;
    float stride = (float)numSourceSamples / dest.size();
    float currentStride = stride;
    float maxSample = 0.0f;
    float lastAverageRate = 0.0f;

    int sourceTailIndex = nBaseSamplesHeadIndex - numSourceSamples;
    if (sourceTailIndex < 0) {
        sourceTailIndex += BASE_SAMPLES_BUF_SIZE;
    }

    int currentSampleIndex = sourceTailIndex;
    for (int destIndex = 0; destIndex < dest.size(); destIndex++) {
        int numSamplesToRead = (int)currentStride;

        float aggregate = 0.0f;
        for (int i = 0; i < numSamplesToRead; ++i) {
            float currentSample = source[currentSampleIndex];

            aggregate += currentSample;

            currentSampleIndex++;
            if (currentSampleIndex >= source.size()) {
                currentSampleIndex = 0;
            }
        }

        float averageRate = 0.0f;
        if (numSamplesToRead == 0) {
            // We seem to be trying to upsample. just use the previous sample.
            averageRate = lastAverageRate;
        }
        else {
            averageRate = aggregate / numSamplesToRead;
        }

Review comment (suggestion):
I'm trying to figure out what this represents without re-reading the previous discussion; is this an index into a ring buffer, so that this addition if negative achieves wraparound behavior?

If so, a comment to that effect could make this clearer.

Code after:
        nTimespanLastBytesIn = model->getTotalBytesRecv();
        nTimespanLastBytesOut = model->getTotalBytesSent();
        nBaseLastBytesIn = nTimespanLastBytesIn;
        nBaseLastBytesOut = nTimespanLastBytesOut;

        baseTimer->start(BASE_SAMPLE_PERIOD_MS);
    }
}

int TrafficGraphWidget::getGraphRangeMins() const
{
    return nMins;
}

float TrafficGraphWidget::resampleSamples(const QVector<float> &source, QVector<float> &dest) const
{
    int numSourceSamples = nMins * 60;
    float stride = (float)numSourceSamples / dest.size();
    float currentStride = stride;
    float maxSample = 0.0f;
    float lastAverageRate = 0.0f;

    int sourceTailIndex = nBaseSamplesHeadIndex - numSourceSamples;
    if (sourceTailIndex < 0) {
        sourceTailIndex += BASE_SAMPLES_BUF_SIZE;
    }

    int currentSampleIndex = sourceTailIndex;
    for (int destIndex = 0; destIndex < dest.size(); destIndex++) {
        int numSamplesToRead = (int)currentStride;

        float aggregate = 0.0f;
        for (int i = 0; i < numSamplesToRead; ++i) {
            float currentSample = source[currentSampleIndex];

            aggregate += currentSample;

            currentSampleIndex++;
            if (currentSampleIndex >= source.size()) {
                currentSampleIndex = 0;
            }
        }

        float averageRate = 0.0f;
        if (numSamplesToRead == 0) {
            // We seem to be trying to upsample. just use the previous sample.
            averageRate = lastAverageRate;
        }
        else {
            averageRate = aggregate / numSamplesToRead;
        }