Histogram.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2013, Fraunhofer FKIE
00003  *
00004  * Authors: Bastian Gaspers
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *
00009  * * Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * * Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * * Neither the name of the Fraunhofer FKIE nor the names of its
00015  *   contributors may be used to endorse or promote products derived from
00016  *   this software without specific prior written permission.
00017  *
00018  * This file is part of the StructureColoring ROS package.
00019  *
00020  * The StructureColoring ROS package is free software:
00021  * you can redistribute it and/or modify it under the terms of the
00022  * GNU Lesser General Public License as published by the Free
00023  * Software Foundation, either version 3 of the License, or
00024  * (at your option) any later version.
00025  *
00026  * The StructureColoring ROS package is distributed in the hope that it will be useful,
00027  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00028  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00029  * GNU Lesser General Public License for more details.
00030  *
00031  * You should have received a copy of the GNU Lesser General Public License
00032  * along with The StructureColoring ROS package.
00033  * If not, see <http://www.gnu.org/licenses/>.
00034  */
00035 
00036 #ifndef HISTOGRAM_H_
00037 #define HISTOGRAM_H_
00038 
00039 #include <functional>
00040 #include <algorithm>
00041 #include <vector>
00042 #include <structureColoring/histograms/OutOfRangeException.h>
00043 
00049 template<class Key, class Value, class BinType = Value>
00050 class Histogram 
00051 {
00052 public:
00053         typedef OutOfRangeException OutOfRange;
00054 
00060         Histogram(const Key& min, const Key& max, const Key& binSize)
00061                 : mMin(min), mMax(max), mBinSize(binSize), mBins(static_cast<size_t>((mMax - mMin)/mBinSize))
00062         {}
00063         BinType& bin(const Key& k) { return mBins[getIdx(k)];}
00064         const BinType& bin(const Key& k) const { return mBins[getIdx(k)];}
00065         void addToBin(const Key& k, const Value& v) { mBins[getIdx(k)] += v;}
00066         void getMaxBin(BinType& bin, Key& k) const { getMaxBin(bin, k, std::less<BinType>());}
00067         template<class Comp>
00068         void getMaxBin(BinType& bin, Key& k, Comp comp) const;
00069 protected:
00070         typedef std::vector<BinType> Bins;
00071         size_t getIdx(const Key& k) const;
00072         Key getKey(const size_t& idx) const;
00073         Key mMin, mMax, mBinSize;
00074         Bins mBins;
00075 };
00076 
00077 template<class Key, class Value, class BinType>
00078 size_t Histogram<Key, Value, BinType>::getIdx(const Key& k) const
00079 {
00080         if ((k < mMin) || (k > mMax))
00081                 throw OutOfRange("key is out of range");
00082         if (k == mMax)
00083                 return mBins.size()-1; //construction of mBins is to short to hold mMax!
00084         return static_cast<size_t>((k - mMin)/mBinSize);
00085 }
00086 
00087 template<class Key, class Value, class BinType>
00088 Key Histogram<Key, Value, BinType>::getKey(const size_t& idx) const
00089 {
00090         return (Key(idx) * mBinSize) + mMin;
00091 }
00092 
00093 template<class Key, class Value, class BinType>
00094 template<class Comp>
00095 void Histogram<Key, Value, BinType>::getMaxBin(BinType& bin, Key& k, Comp comp) const
00096 {
00097         typename Bins::const_iterator it = std::max_element(mBins.begin(), mBins.end(), comp);
00098         size_t idx = it - mBins.begin();
00099         k = getKey(idx);
00100         bin = *it;
00101 //      std::cout << "Histogram.getMaxBin(bin, k, comp) called: number of bins: " << mBins.size() << "; mMin = " << mMin << "; mMax = " << mMax << std::endl;
00102 //      std::cout << "Indices of (min, max) = (" << getIdx(mMin) << ", " << getIdx(mMax) << ")" << std::endl;
00103 }
00104 
00105 #endif


structure_coloring_fkie
Author(s): Bastian Gaspers
autogenerated on Sun Jan 5 2014 11:38:09