OpenWalnut  1.5.0dev
WHistogramND.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WHISTOGRAMND_H
26 #define WHISTOGRAMND_H
27 
28 #include <utility>
29 
30 #include <boost/array.hpp>
31 
32 /**
33  * This template should handly arbitrary N-dimensional histograms.
34  *
35  * \tparam N specifies the dimensionality
36  * \tparam T specifies the type of data. Normally this should be double or float.
37  */
38 template< std::size_t N, typename T = double >
39 /**
40  * Interface for an N-dimensional histogram.
41  */
42 class WHistogramND // NOLINT
43 {
44 public:
45  /**
46  * Shorthand for N-dimensional indices, counter, etc.
47  */
48  typedef boost::array< size_t, N > SizeArray;
49 
50  /**
51  * Shorthand for N-dimensional values of type T.
52  */
53  typedef boost::array< T, N > TArray; // e.g. DoubleArray for T == double
54 
55  /**
56  * Default constructor. Creates an empty N-dimensional histogram covering the specified min and max values with the specified number of buckets.
57  *
58  * \param min the smallest value(s) in each dimension
59  * \param max the largest value(s) in each dimension
60  * \param buckets the number of buckets in each direction (may be non uniform).
61  */
62  WHistogramND( TArray min, TArray max, SizeArray buckets );
63 
64  /**
65  * Copy constructor. Creates a deep copy of the specified ND histogram.
66  *
67  * \param hist the HistogramND to copy.
68  */
69  WHistogramND( const WHistogramND& hist );
70 
71  /**
72  * Default destructor.
73  */
74  virtual ~WHistogramND();
75 
76  /**
77  * Get the count of the specified bucket.
78  *
79  * \param index in each dimension
80  *
81  * \return elements in the bucket.
82  */
83  virtual size_t operator()( SizeArray index ) const = 0;
84 
85  /**
86  * Returns the number of buckets in the HistogramND with the actual mapping.
87  *
88  * \return number of buckets
89  */
90  virtual size_t size() const;
91 
92  /**
93  * Returns the minimum value(s).
94  *
95  * \return minimum
96  */
97  virtual TArray getMinima() const;
98 
99  /**
100  * Returns the maximum value(s).
101  *
102  * \return maximum
103  */
104  virtual TArray getMaxima() const;
105 
106  /**
107  * Return the measure of one specific bucket. For one dimensional Histograms this is the width of the bucket, for two
108  * dimensions this is the area, for three dims this is the volume, etc.
109  *
110  * \param index the measure for this bucket is queried.
111  *
112  * \return the size of a bucket.
113  */
114  virtual T getBucketSize( SizeArray index ) const = 0;
115 
116  /**
117  * Returns the actual (right-open) interval in each dimension associated with the given index.
118  *
119  * \param index for this bucket the intervals will be returned
120  *
121  * \return the right-open interval in each dimension.
122  */
123  virtual boost::array< std::pair< T, T >, N > getIntervalForIndex( SizeArray index ) const = 0;
124 
125 protected:
126  /**
127  * Default constructor is protected to allow subclassing constructors not to use initialization lists which would
128  * imply C++11 or GNU++11 style initializers for boost::array members. To workaround, reset() member function is
129  * provided.
130  */
132 
133  /**
134  * Initializes all members. This exists because to circumvent boost::array initializers in c'tor init lists and
135  * reduces code duplication, as it is used in this abstract class as well as in its base classes.
136  *
137  * \param min Minimal values in each dimension.
138  * \param max Maximal values in each dimension.
139  * \param buckets \#buckets in each dimension.
140  */
141  void reset( TArray min, TArray max, SizeArray buckets );
142 
143  /**
144  * The smallest value in each dimension.
145  */
147 
148  /**
149  * The biggest value in each dimension.
150  */
152 
153  /**
154  * The number of buckets.
155  */
157 
158  /**
159  * Total number of buckets.
160  */
161  size_t m_nbBuckets;
162 
163 private:
164 };
165 
166 template< std::size_t N, typename T >
168 {
169 }
170 
171 template< std::size_t N, typename T >
173 {
174  reset( min, max, buckets );
175 }
176 
177 template< std::size_t N, typename T >
179 {
180  m_min = min;
181  m_max = max;
182 
183  WAssert( min.size() == max.size(), "Error, WHistogram initialized with wrong dimensionality" );
184  for( size_t i = 0; i < min.size(); ++i )
185  {
186  WAssert( min[i] <= max[i], "Error, WHistogram has at least one dimension where max is smaller than min" );
187  }
188 
189  m_buckets = buckets;
190  m_nbBuckets = 1;
191  for( typename SizeArray::const_iterator cit = buckets.begin(); cit != buckets.end(); ++cit )
192  {
193  m_nbBuckets *= *cit;
194  }
195 }
196 
197 template< std::size_t N, typename T >
199 {
200 }
201 
202 template< std::size_t N, typename T >
204 {
205  m_min = other.m_min;
206  m_max = other.m_max;
207  m_buckets = other.m_buckets;
208  m_nbBuckets = other.m_nbBuckets;
209 }
210 
211 template< std::size_t N, typename T >
213 {
214  return m_nbBuckets;
215 }
216 
217 template< std::size_t N, typename T >
219 {
220  return m_min;
221 }
222 
223 template< std::size_t N, typename T >
225 {
226  return m_max;
227 }
228 
229 #endif // WHISTOGRAMND_H
This template should handly arbitrary N-dimensional histograms.
Definition: WHistogramND.h:43
size_t m_nbBuckets
Total number of buckets.
Definition: WHistogramND.h:161
virtual size_t operator()(SizeArray index) const =0
Get the count of the specified bucket.
void reset(TArray min, TArray max, SizeArray buckets)
Initializes all members.
Definition: WHistogramND.h:178
virtual T getBucketSize(SizeArray index) const =0
Return the measure of one specific bucket.
SizeArray m_buckets
The number of buckets.
Definition: WHistogramND.h:156
boost::array< T, N > TArray
Shorthand for N-dimensional values of type T.
Definition: WHistogramND.h:53
WHistogramND()
Default constructor is protected to allow subclassing constructors not to use initialization lists wh...
Definition: WHistogramND.h:167
WHistogramND(const WHistogramND &hist)
Copy constructor.
Definition: WHistogramND.h:203
TArray m_min
The smallest value in each dimension.
Definition: WHistogramND.h:146
TArray m_max
The biggest value in each dimension.
Definition: WHistogramND.h:151
virtual ~WHistogramND()
Default destructor.
Definition: WHistogramND.h:198
boost::array< size_t, N > SizeArray
Shorthand for N-dimensional indices, counter, etc.
Definition: WHistogramND.h:48
virtual boost::array< std::pair< T, T >, N > getIntervalForIndex(SizeArray index) const =0
Returns the actual (right-open) interval in each dimension associated with the given index.
virtual TArray getMaxima() const
Returns the maximum value(s).
Definition: WHistogramND.h:224
WHistogramND(TArray min, TArray max, SizeArray buckets)
Default constructor.
Definition: WHistogramND.h:172
virtual size_t size() const
Returns the number of buckets in the HistogramND with the actual mapping.
Definition: WHistogramND.h:212
virtual TArray getMinima() const
Returns the minimum value(s).
Definition: WHistogramND.h:218