WHistogram.h 6.17 KB
Newer Older
Robert Frohl's avatar
Robert Frohl committed
1 2 3 4
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
Robert Frohl's avatar
[STYLE]  
Robert Frohl committed
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
Robert Frohl's avatar
Robert Frohl committed
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
// 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 WHISTOGRAM_H
#define WHISTOGRAM_H

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

Robert Frohl's avatar
[STYLE]  
Robert Frohl committed
32 33 34
/**
 * Used to track (later: display) the occurrence frequencies of values in a value set.
 **/
Robert Frohl's avatar
Robert Frohl committed
35 36 37 38 39 40 41 42
class WHistogram
{
    /**
     * To compare the elements of the std::map used in WHistogram.
     **/
    class cmp
    {
        public:
Robert Frohl's avatar
[STYLE]  
Robert Frohl committed
43 44 45 46 47 48 49 50 51
            /**
             * Compares two double values.
             *
             * \param p1 first value
             * \param p2 second value
             *
             * \return the same bolean value as applying the less-than operator (a<b)
             **/
            bool operator()( double const& p1, double const& p2 ) const
Robert Frohl's avatar
Robert Frohl committed
52 53 54 55 56 57 58 59 60 61 62 63
            {
                return p1 < p2;
            }
    };

    /**
     * WBar is an element of WHistogram, where the range of monitored values equals 1.
     * Meaning WBar represents the amount of each specific value in the value set.
     **/
    class WBar
    {
        private:
Robert Frohl's avatar
[STYLE]  
Robert Frohl committed
64 65 66
            /**
             * Represents this bars height.
             **/
Robert Frohl's avatar
Robert Frohl committed
67 68 69 70 71 72
            unsigned int value;
            //unsigned int distance;
            //std::pair<int, int> range;

        public:
            /**
Robert Frohl's avatar
[STYLE]  
Robert Frohl committed
73
             * Default constructor.
Robert Frohl's avatar
Robert Frohl committed
74
             **/
Robert Frohl's avatar
[STYLE]  
Robert Frohl committed
75
            WBar() : value( 0 )
Robert Frohl's avatar
Robert Frohl committed
76 77 78 79
            {
            }

            /**
Robert Frohl's avatar
[STYLE]  
Robert Frohl committed
80 81 82
             * Clones this WBar from another WBar.
             *
             * \param bar WBar which serves as the origin of the height.
Robert Frohl's avatar
Robert Frohl committed
83
             **/
Robert Frohl's avatar
[STYLE]  
Robert Frohl committed
84
            explicit WBar( WBar const& bar ) : value( bar.value )
Robert Frohl's avatar
Robert Frohl committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98
            {
            }


            /**
             * Destroys this Bar instance.
             **/
            ~WBar()
            {
            }

            /**
             * Postfix inkrement (x++) the bar by one.
             **/
Robert Frohl's avatar
[STYLE]  
Robert Frohl committed
99
            WBar& operator++( int ) // x++
Robert Frohl's avatar
Robert Frohl committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
            {
                WBar* tmp = this;
                tmp->value++;
                return *tmp;
            }

            /**
             * Prefix inkrement (++x) the bar by one.
             **/
            WBar& operator++() // ++x
            {
                WBar* tmp = this;
                tmp->value++;
                return *tmp;
            }

            /**
Robert Frohl's avatar
[STYLE]  
Robert Frohl committed
117 118 119
             * To access the height of the specific bar.
             *
             * \return the value of the bar.
Robert Frohl's avatar
Robert Frohl committed
120 121 122 123 124 125 126 127
             **/
            unsigned int getValue() const
            {
                return value;
            }
    };

    private:
Robert Frohl's avatar
[STYLE]  
Robert Frohl committed
128 129 130
        /**
         * Describes the uniform size of a bucket in the mapped histogram.
         **/
Robert Frohl's avatar
Robert Frohl committed
131
        unsigned int uniformInterval;
Robert Frohl's avatar
[STYLE]  
Robert Frohl committed
132 133 134 135 136

        /**
         * Pointer used to speed up operator[]( unsigned int ) if the parameter increases by 1
         * each call (supposed standard behaviour).
         **/
Robert Frohl's avatar
Robert Frohl committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
        std::pair< std::list<std::pair< const WBar*, unsigned int > >::iterator, unsigned int > mappingPointer;

        /**
         * Orderd sequence of WBar, which only account for specific values(the
         * double value).
         **/
        std::map< double, WBar, cmp > elements;

        /**
         * The Mapping, to gain bar's with a range greater than 1, is stored as
         * std::pair<WBar, unsigned int>.
         * Where the unsigned int is the the height of the specific bar.
         * The *WBar is the initial point (related to the std::map<> elements)
         * of the next bar.
         **/
        std::list< std::pair< const WBar*, unsigned int > > mapping;

        /**
         * Creates the mapping from the given range.
         **/
        void calculateMapping(); // for uniformInterval only

    public:
        /**
Robert Frohl's avatar
[STYLE]  
Robert Frohl committed
161
         * Default constructor.
Robert Frohl's avatar
Robert Frohl committed
162 163 164 165
         **/
        WHistogram();

        /**
Robert Frohl's avatar
[STYLE]  
Robert Frohl committed
166 167 168
         * Constructs a histogram with a given uniform intervall.
         *
         * \param interval the interval size if uniform intervals are used
Robert Frohl's avatar
Robert Frohl committed
169
         **/
Robert Frohl's avatar
[STYLE]  
Robert Frohl committed
170
        explicit WHistogram( unsigned int interval );
Robert Frohl's avatar
Robert Frohl committed
171 172 173 174 175 176 177 178

        /**
         * Destroys this Histogram instance.
         **/
        ~WHistogram();

        /**
         * Add an element to one of the WBar.
Robert Frohl's avatar
[STYLE]  
Robert Frohl committed
179 180
         *
         * \param value the bar that represents value in the histogram is increased bye one
Robert Frohl's avatar
Robert Frohl committed
181
         **/
Robert Frohl's avatar
[STYLE]  
Robert Frohl committed
182
        void add( double value );
Robert Frohl's avatar
Robert Frohl committed
183 184

        /**
Robert Frohl's avatar
[STYLE]  
Robert Frohl committed
185 186 187 188
         * Set and change the uniform interval.
         * Each time a new mapping is created.
         *
         * \param interval the new bucket size
Robert Frohl's avatar
Robert Frohl committed
189
         **/
Robert Frohl's avatar
[STYLE]  
Robert Frohl committed
190
        void setUniformInterval( unsigned int interval );
Robert Frohl's avatar
Robert Frohl committed
191 192

        /**
Robert Frohl's avatar
[STYLE]  
Robert Frohl committed
193 194 195 196 197
         * Sets intervals that are not uniform.
         * A new mapping is created each time.
         *
         * \param rangeList list that represents the size of each bucket starting with the lowest
         * value
Robert Frohl's avatar
Robert Frohl committed
198
         **/
Robert Frohl's avatar
[STYLE]  
Robert Frohl committed
199
        void setInterval( const std::list<unsigned int>& rangeList );
Robert Frohl's avatar
Robert Frohl committed
200 201 202

        /**
         * Get the height of a bar.
Robert Frohl's avatar
[STYLE]  
Robert Frohl committed
203 204 205 206 207
         *
         * \param position which bars height is to be returned, starts with 0 which is the bar with
         * the smalest values
         *
         * \return height of the bar i.e. size of the bucket
Robert Frohl's avatar
Robert Frohl committed
208
         **/
Robert Frohl's avatar
[STYLE]  
Robert Frohl committed
209
        unsigned int operator[]( unsigned int position ); // const;
Robert Frohl's avatar
Robert Frohl committed
210

Robert Frohl's avatar
[STYLE]  
Robert Frohl committed
211 212 213
        /**
         * Simple output to std::cout to test functionality.
         **/
Robert Frohl's avatar
Robert Frohl committed
214 215 216
        void test();
};

Robert Frohl's avatar
[STYLE]  
Robert Frohl committed
217
#endif  // WHISTOGRAM_H
Robert Frohl's avatar
Robert Frohl committed
218