summaryrefslogtreecommitdiffstats
path: root/src/bm/resulthistoryinfo.cpp
blob: 94c69ff1a034b40640378f433cd0ec4bcb35d768 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the BM project on Qt Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 or 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include "resulthistoryinfo.h"
#include "bmmisc.h"

// ### 2 B DOCUMENTED!
bool ResultHistoryInfo::findTargetPos(
    int timestamp, int medianWinSize, int *pos, qreal *distSum) const
{
    int pos_ = -1;

    // Find target position ...
    QList<int>::const_iterator it = qLowerBound(timestamps_.begin(), timestamps_.end(), timestamp);
    if (it == timestamps_.begin()) {
        Q_ASSERT(timestamp <= timestamps_.first());
        if (timestamp == timestamps_.first())
            pos_ = 0;
    } else if (it == timestamps_.end()) {
        Q_ASSERT(timestamp > timestamps_.last());
        pos_ = timestamps_.size() - 1;
    } else {
        Q_ASSERT(timestamp > *(it - 1));
        pos_ = (it - 1) - timestamps_.begin();
    }
    Q_ASSERT(pos_ >= -1);
    Q_ASSERT(pos_ < timestamps_.size());

    // Return true iff the target position is high enough to accommodate a right-aligned window
    // (i.e. a window in which the target value is the rightmost value) ...
    if ((pos_ + 1) >= medianWinSize) {
        if (pos)
            *pos = pos_;

        if (distSum) {
            // Compute sum of distances to the contributing values ...
            *distSum = 0.0;
            for (int i = (pos_ - medianWinSize) + 1; i <= pos_; ++i) {
                Q_ASSERT(timestamps_.at(i) <= timestamp);
                (*distSum) += static_cast<qreal>((timestamp - timestamps_.at(i)));
            }
        }

        return true;
    }

    return false;
}

// Returns true iff at least \a medianWinSize values exist at or before
// \a timestamp. Passes the zero-based position of the smoothed value (if found) in \a smoothPos.
// If \a cache is true, the previously cached position will be replaced (or initially recorded).
// (Note that the cache capacity is 1, which explains why the \a cache argument needs to be
// passed; only the client knows which timestamp is likely to be requested multiple times!)
// If \a distSum is non-null, it will be set to the sum of the distances between \a timestamp
// and the timestamp of each contributing value.
bool ResultHistoryInfo::getSmoothPos(
    int timestamp, int medianWinSize, int *smoothPos, bool cache, qreal *distSum) const
{
    Q_ASSERT(timestamp >= 0);
    Q_ASSERT(medianWinSize > 0);

    if (medianWinSize > timestamps_.size())
        return false;

    // Return cached position if possible ...
    if (timestamp == cachedTimestamp) {
        *smoothPos = cachedSmoothPos;
        return true;
    }

    // Find a valid target position if possible ...
    int pos = -1;
    if (!findTargetPos(timestamp, medianWinSize, &pos, distSum)) {
        if (cache)
            cachedTimestamp = -1; // Invalidate cache
        return false;
    }

    // Compute value ...
    const int lo = (pos - medianWinSize) + 1;
    *smoothPos = lo + BMMisc::medianPos(values.mid(lo, medianWinSize));

    if (cache) {
        // Load cache ...
        cachedSmoothPos = *smoothPos;
        cachedTimestamp = timestamp;
    }

    return true;
}

// ### 2 B DOCUMENTED!
bool ResultHistoryInfo::getSmoothValues(int medianWinSize, QList<qreal> *smoothValues) const
{
    Q_ASSERT(medianWinSize > 0);
    Q_ASSERT(timestamps_.size() == values.size());
    Q_ASSERT(smoothValues);

    if (timestamps_.isEmpty())
        return false;

    smoothValues->clear();

    for (int pos = 0; pos < timestamps_.size(); ++pos) {
        int lo = pos;
        int hi = pos;
        while (true) {
            Q_ASSERT((hi - lo) + 1 <= medianWinSize);
            if ((hi - lo) + 1 == medianWinSize) break;
            lo = qMax(lo - 1, 0);
            if ((hi - lo) + 1 == medianWinSize) break;
            hi = qMin(hi + 1, values.size() - 1);
            if ((hi - lo) + 1 == medianWinSize) break;
        }

        const qreal smoothValue = BMMisc::median(values.mid(lo, (hi - lo) + 1));
        smoothValues->append(smoothValue);
    }

    return true;
}