Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
99 | pmbaty | 1 | /* |
2 | Texel - A UCI chess engine. |
||
3 | Copyright (C) 2013 Peter Ă–sterlund, peterosterlund2@gmail.com |
||
4 | |||
5 | This program is free software: you can redistribute it and/or modify |
||
6 | it under the terms of the GNU General Public License as published by |
||
7 | the Free Software Foundation, either version 3 of the License, or |
||
8 | (at your option) any later version. |
||
9 | |||
10 | This program is distributed in the hope that it will be useful, |
||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
13 | GNU General Public License for more details. |
||
14 | |||
15 | You should have received a copy of the GNU General Public License |
||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
17 | */ |
||
18 | |||
19 | /* |
||
20 | * histogram.hpp |
||
21 | * |
||
22 | * Created on: Oct 20, 2013 |
||
23 | * Author: petero |
||
24 | */ |
||
25 | |||
26 | #ifndef HISTOGRAM_HPP_ |
||
27 | #define HISTOGRAM_HPP_ |
||
28 | |||
29 | #include "util.hpp" |
||
30 | |||
31 | /** A histogram over integer range [minV,maxV[. Out of range values are ignored. */ |
||
32 | template <int minV, int maxV> |
||
33 | class Histogram { |
||
34 | public: |
||
35 | /** Create an empty histogram. */ |
||
36 | Histogram(); |
||
37 | |||
38 | /** Set all counts to 0. */ |
||
39 | void clear(); |
||
40 | |||
41 | /** Add a data point. */ |
||
42 | void add(int value, int count = 1); |
||
43 | |||
44 | /** Get count corresponding to value. */ |
||
45 | int get(int value) const; |
||
46 | |||
47 | constexpr static int minValue() { return minV; } |
||
48 | constexpr static int maxValue() { return maxV; } |
||
49 | |||
50 | private: |
||
51 | int counts[maxV - minV]; |
||
52 | }; |
||
53 | |||
54 | |||
55 | /** RAII class that adds a value to a histogram in the destructor. |
||
56 | * The value added is equal to the number of times inc() was called. */ |
||
57 | template <typename Histogram> |
||
58 | class HistogramAdder { |
||
59 | public: |
||
60 | /** Constructor. */ |
||
61 | HistogramAdder(Histogram& h); |
||
62 | |||
63 | /** Destructor. Adds current value to histogram. */ |
||
64 | ~HistogramAdder(); |
||
65 | |||
66 | /** Increment value to be added to histogram. */ |
||
67 | void inc(); |
||
68 | |||
69 | private: |
||
70 | int value; |
||
71 | Histogram& hist; |
||
72 | }; |
||
73 | |||
74 | |||
75 | template <int minV, int maxV> |
||
76 | inline |
||
77 | Histogram<minV,maxV>::Histogram() { |
||
78 | static_assert(maxV >= minV, "Negative size not allowed"); |
||
79 | clear(); |
||
80 | } |
||
81 | |||
82 | template <int minV, int maxV> |
||
83 | inline void |
||
84 | Histogram<minV,maxV>::clear() { |
||
85 | for (int i = 0; i < (int)COUNT_OF(counts); i++) |
||
86 | counts[i] = 0; |
||
87 | } |
||
88 | |||
89 | template <int minV, int maxV> |
||
90 | inline void |
||
91 | Histogram<minV,maxV>::add(int value, int count) { |
||
92 | if ((value >= minV) && (value < maxV)) |
||
93 | counts[value - minV] += count; |
||
94 | } |
||
95 | |||
96 | template <int minV, int maxV> |
||
97 | inline int |
||
98 | Histogram<minV,maxV>::get(int value) const { |
||
99 | if ((value >= minV) && (value < maxV)) |
||
100 | return counts[value - minV]; |
||
101 | else |
||
102 | return 0; |
||
103 | } |
||
104 | |||
105 | |||
106 | template <typename Histogram> |
||
107 | inline |
||
108 | HistogramAdder<Histogram>::HistogramAdder(Histogram& h) |
||
109 | : value(0), hist(h) { |
||
110 | } |
||
111 | |||
112 | template <typename Histogram> |
||
113 | inline |
||
114 | HistogramAdder<Histogram>::~HistogramAdder() { |
||
115 | hist.add(value); |
||
116 | } |
||
117 | |||
118 | template <typename Histogram> |
||
119 | inline void |
||
120 | HistogramAdder<Histogram>::inc() { |
||
121 | value++; |
||
122 | } |
||
123 | |||
124 | #endif /* HISTOGRAM_HPP_ */ |