WValueSetHistogram.h 5.6 KB
Newer Older
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
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

#ifndef WVALUESETHISTOGRAM_H
#define WVALUESETHISTOGRAM_H

#include <map>
#include <list>
#include <utility>

#include <boost/shared_ptr.hpp>
#include <boost/scoped_array.hpp>
#include <boost/shared_array.hpp>

#include "../WValueSet.h"

/**
 * Used to find the occurrence frequencies of values in a value set. It implements a classical histogram but allows easy modification of bucket
 * sizes without unnecessary recalculation of the whole histogram.
 *
 * \note This histogram is different from from WValueSetHistogram which is a generic histogram class.
 */
class WValueSetHistogram
{
public:
    /**
     * Constructor. Creates the histogram for the specified value set.
     *
     * \param valueSet source of the data for the histogram
     */
    explicit WValueSetHistogram( boost::shared_ptr< WValueSetBase > valueSet );


    /**
     * Constructor. Creates the histogram for the specified value set.
     *
     * \param valueSet source of the data for the histogram
     */
60
    explicit WValueSetHistogram( const WValueSetBase& valueSet );
61 62

    /**
63
     * Copy constructor. If another interval size is given the histogram gets matched to it using the initial bucket data.
64 65
     *
     * \param histogram another WValueSetHistogram
66
     * \param buckets the new number of buckets.
67
     */
68
    explicit WValueSetHistogram( const WValueSetHistogram& histogram, size_t buckets = 0 );
69 70 71 72 73 74 75 76 77 78 79 80 81 82

    /**
     * Destructor.
     */
    ~WValueSetHistogram();

    /**
     * Get the size of the bucket.
     *
     * \param index which buckets size is to be returned, starts with 0 which is the bucket
     * containing the smallest values.
     *
     * \return elements in the bucket.
     */
83
    size_t operator[]( size_t index ) const;
84 85 86 87 88 89 90 91 92

    /**
     * Get the size of the bucket. Testing if the position is valid.
     *
     * \param index which buckets size is to be returned, starts with 0 which is the bar with
     * the smallest values
     *
     * \return elements in the bucket
     */
93
    size_t at( size_t index ) const;
94 95

    /**
96
     * Returns the number of buckets in the histogram with the actual mapping.
97 98 99
     *
     * \return number of buckets
     */
100
    size_t size() const;
101 102 103 104 105 106

    /**
     * Returns the minimum value in the value set.
     *
     * \return minimum
     */
107
    double getMinimum() const;
108 109 110 111 112 113

    /**
     * Returns the maximum value in the value set.
     *
     * \return maximum
     */
114 115 116 117 118 119 120 121
    double getMaximum() const;

    /**
     * Return the size of one bucket.
     *
     * \return the size of a bucket.
     */
    double getBucketSize() const;
122

123 124 125 126 127 128 129 130 131 132
    /**
     * Returns the actual interval associated with the given index. The interval is
     * getIntervalForIndex( i ).second == getIntervalForIndex( i + 1 ).first
     *
     * \param index the intex
     *
     * \return the open interval in [getMinimum(),getMaximum].
     */
    std::pair< double, double > getIntervalForIndex( size_t index ) const;

133 134 135 136 137 138
protected:
    /**
     * Return the initial buckets.
     *
     * \return m_initialBuckets
     */
139
    boost::shared_array< size_t > getInitialBuckets() const;
140 141 142 143 144 145

    /**
     * Return the number of initial buckets.
     *
     * \return m_nInitialBuckets
     */
146
    size_t getNInitialBuckets() const;
147 148 149 150 151 152

    /**
     * Return the size of one initial bucket.
     *
     * \return m_bucketSize
     */
153
    double getInitialBucketSize() const;
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

private:
    /**
     * The smallest value in the ValueSet
     */
    double m_minimum;

    /**
     * The biggest value in the ValueSet
     */
    double m_maximum;

    /**
     * Size of one bucket in the initial histogram.
     */
169
    double m_initialBucketSize;
170 171 172 173

    /**
     * Pointer to all initial buckets of the histogram.
     */
174
    boost::shared_array< size_t > m_initialBuckets;
175 176 177 178

    /**
     * Number of buckets in the initial histogram.
     */
179
    size_t m_nInitialBuckets;
180 181

    /**
182
     * Pointer to all initial buckets of the histogram.
183
     */
184
    boost::shared_array< size_t > m_mappedBuckets;
185 186 187 188

    /**
     * Tracks the number of a buckets in the mapped histogram.
     */
189 190 191 192 193 194
    size_t m_nMappedBuckets;

    /**
     * Size of one bucket in the mapped histogram.
     */
    double m_mappedBucketSize;
195 196 197 198 199 200 201

    /**
     * increment the value by one, contains the logic to find the element place in the array.
     * Should only be used in the constructor i.e. while iterating over WValueSet.
     *
     * \param value value to increment
     */
202
    void insert( double value );
203 204
};

205 206 207 208 209
/**
 * Write a histogram in string representation to the given output stream.
 */
std::ostream& operator<<( std::ostream& out, const WValueSetHistogram& h );

210 211
#endif  // WVALUESETHISTOGRAM_H