- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files
- Directories
- Examples

- File List
- File Members

# qtconcurrentmedian.h

Go to the documentation of this file.

    00001 /****************************************************************************
    00002 **
    00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
    00004 ** All rights reserved.
    00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
    00006 **
    00007 ** This file is part of the QtCore module of the Qt Toolkit.
    00008 **
    00009 ** $QT_BEGIN_LICENSE:LGPL$
    00010 ** Commercial Usage
    00011 ** Licensees holding valid Qt Commercial licenses may use this file in
    00012 ** accordance with the Qt Commercial License Agreement provided with the
    00013 ** Software or, alternatively, in accordance with the terms contained in
    00014 ** a written agreement between you and Nokia.
    00015 **
    00016 ** GNU Lesser General Public License Usage
    00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
    00018 ** General Public License version 2.1 as published by the Free Software
    00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
    00020 ** packaging of this file.  Please review the following information to
    00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
    00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    00023 **
    00024 ** In addition, as a special exception, Nokia gives you certain additional
    00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
    00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
    00027 **
    00028 ** GNU General Public License Usage
    00029 ** Alternatively, this file may be used under the terms of the GNU
    00030 ** General Public License version 3.0 as published by the Free Software
    00031 ** Foundation and appearing in the file LICENSE.GPL included in the
    00032 ** packaging of this file.  Please review the following information to
    00033 ** ensure the GNU General Public License version 3.0 requirements will be
    00034 ** met: http://www.gnu.org/copyleft/gpl.html.
    00035 **
    00036 ** If you have questions regarding the use of this file, please contact
    00037 ** Nokia at qt-info@nokia.com.
    00038 ** $QT_END_LICENSE$
    00039 **
    00040 ****************************************************************************/
    00041
    00042 #ifndef QTCONCURRENT_MEDIAN_H
    00043 #define QTCONCURRENT_MEDIAN_H
    00044
    00045 #include <QtCore/qglobal.h>
    00046
    00047 #ifndef QT_NO_CONCURRENT
    00048
    00049 #include <QtCore/qvector.h>
    00050 #include <QtCore/qalgorithms.h>
    00051
    00052 QT_BEGIN_HEADER
    00053 QT_BEGIN_NAMESPACE
    00054
    00055 QT_MODULE(Core)
    00056
    00057 #ifndef qdoc
    00058
    00059 namespace QtConcurrent {
    00060
    00061 template <typename T>
    00062 class Median
    00063 {
    00064 public:
    00065     Median(int _bufferSize)
    00066         : currentMedian(), bufferSize(_bufferSize), currentIndex(0), valid(false), dirty(true)
    00067     {
    00068         values.resize(bufferSize);
    00069     }
    00070
    00071     void reset()
    00072     {
    00073         values.fill(0);
    00074         currentIndex = 0;
    00075         valid = false;
    00076         dirty = true;
    00077     }
    00078
    00079     void addValue(T value)
    00080     {
    00081         currentIndex = ((currentIndex + 1) % bufferSize);
    00082         if (valid == false && currentIndex % bufferSize == 0)
    00083             valid = true;
    00084
    00085         // Only update the cached median value when we have to, that
    00086         // is when the new value is on then other side of the median
    00087         // compared to the current value at the index.
    00088         const T currentIndexValue = values[currentIndex];
    00089         if ((currentIndexValue > currentMedian && currentMedian > value)
    00090             || (currentMedian > currentIndexValue && value > currentMedian)) {
    00091             dirty = true;
    00092         }
    00093
    00094         values[currentIndex] = value;
    00095     }
    00096
    00097     bool isMedianValid() const
    00098     {
    00099         return valid;
    00100     }
    00101
    00102     T median()
    00103     {
    00104         if (dirty) {
    00105             dirty = false;
    00106             QVector<T> sorted = values;
    00107             qSort(sorted);
    00108             currentMedian = sorted.at(bufferSize / 2 + 1);
    00109         }
    00110         return currentMedian;
    00111     }
    00112 private:
    00113     QVector<T> values;
    00114     T currentMedian;
    00115     int bufferSize;
    00116     int currentIndex;
    00117     bool valid;
    00118     bool dirty;
    00119 };
    00120
    00121 } // namespace QtConcurrent
    00122
    00123 #endif //qdoc
    00124
    00125 QT_END_NAMESPACE
    00126 QT_END_HEADER
    00127
    00128 #endif // QT_NO_CONCURRENT
    00129
    00130 #endif