Embedded Template Library 1.0
Loading...
Searching...
No Matches
histogram.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2021 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_HISTOGRAM_INCLUDED
32#define ETL_HISTOGRAM_INCLUDED
33
34#include "platform.h"
35#include "algorithm.h"
36#include "array.h"
37#include "flat_map.h"
38#include "functional.h"
39#include "integral_limits.h"
40#include "static_assert.h"
41#include "type_traits.h"
42
43namespace etl
44{
45 namespace private_histogram
46 {
47 //***************************************************************************
49 //***************************************************************************
50 template <typename TCount, size_t Max_Size_>
52 {
53 public:
54
55 ETL_STATIC_ASSERT(etl::is_integral<TCount>::value, "Only integral count allowed");
56
57 static ETL_CONSTANT size_t Max_Size = Max_Size_;
58
59 typedef typename etl::array<TCount, Max_Size>::const_iterator const_iterator;
60
61 //*********************************
63 //*********************************
64 const_iterator begin() const
65 {
66 return accumulator.begin();
67 }
68
69 //*********************************
71 //*********************************
72 const_iterator cbegin() const
73 {
74 return accumulator.cbegin();
75 }
76
77 //*********************************
79 //*********************************
80 const_iterator end() const
81 {
82 return accumulator.end();
83 }
84
85 //*********************************
87 //*********************************
88 const_iterator cend() const
89 {
90 return accumulator.cend();
91 }
92
93 //*********************************
95 //*********************************
96 void clear()
97 {
98 accumulator.fill(TCount(0));
99 }
100
101 //*********************************
103 //*********************************
104 ETL_CONSTEXPR size_t size() const
105 {
106 return Max_Size;
107 }
108
109 //*********************************
111 //*********************************
112 ETL_CONSTEXPR size_t max_size() const
113 {
114 return Max_Size;
115 }
116
117 //*********************************
119 //*********************************
120 size_t count() const
121 {
122 return etl::accumulate(accumulator.begin(), accumulator.end(), size_t(0));
123 }
124
125 protected:
126
128 };
129
130 template <typename TCount, size_t Max_Size_>
131 ETL_CONSTANT size_t histogram_common<TCount, Max_Size_>::Max_Size;
132 } // namespace private_histogram
133
134 //***************************************************************************
136 //***************************************************************************
137 template <typename TKey, typename TCount, size_t Max_Size, int32_t Start_Index = etl::integral_limits<int32_t>::max>
139 : public etl::private_histogram::histogram_common<TCount, Max_Size>
140 , public etl::unary_function<TKey, void>
141 {
142 public:
143
144 ETL_STATIC_ASSERT(etl::is_integral<TKey>::value, "Only integral keys allowed");
145 ETL_STATIC_ASSERT(etl::is_integral<TCount>::value, "Only integral count allowed");
146
147 typedef TKey key_type;
148 typedef TCount count_type;
149 typedef TCount value_type;
150
151 //*********************************
153 //*********************************
155 {
156 this->accumulator.fill(count_type(0));
157 }
158
159 //*********************************
161 //*********************************
162 template <typename TIterator>
163 histogram(TIterator first, TIterator last)
164 {
165 this->accumulator.fill(count_type(0));
166 add(first, last);
167 }
168
169 //*********************************
171 //*********************************
172 histogram(const histogram& other)
173 {
174 this->accumulator = other.accumulator;
175 }
176
177#if ETL_USING_CPP11
178 //*********************************
180 //*********************************
181 histogram(histogram&& other)
182 {
183 this->accumulator = etl::move(other.accumulator);
184 }
185#endif
186
187 //*********************************
189 //*********************************
191 {
192 this->accumulator = rhs.accumulator;
193
194 return *this;
195 }
196
197#if ETL_USING_CPP11
198 //*********************************
200 //*********************************
202 {
203 this->accumulator = etl::move(rhs.accumulator);
204
205 return *this;
206 }
207#endif
208
209 //*********************************
211 //*********************************
212 void add(key_type key)
213 {
214 ++this->accumulator[static_cast<size_t>(key - Start_Index)];
215 }
216
217 //*********************************
219 //*********************************
220 template <typename TIterator>
221 void add(TIterator first, TIterator last)
222 {
223 while (first != last)
224 {
225 add(*first);
226 ++first;
227 }
228 }
229
230 //*********************************
232 //*********************************
233 void operator()(key_type key)
234 {
235 add(key);
236 }
237
238 //*********************************
240 //*********************************
241 template <typename TIterator>
242 void operator()(TIterator first, TIterator last)
243 {
244 add(first, last);
245 }
246
247 //*********************************
249 //*********************************
250 value_type operator[](key_type key) const
251 {
252 return this->accumulator[static_cast<size_t>(key - Start_Index)];
253 }
254 };
255
256 //***************************************************************************
258 //***************************************************************************
259 template <typename TKey, typename TCount, size_t Max_Size>
260 class histogram<TKey, TCount, Max_Size, etl::integral_limits<int32_t>::max>
261 : public etl::private_histogram::histogram_common<TCount, Max_Size>
262 , public etl::unary_function<TKey, void>
263 {
264 public:
265
266 ETL_STATIC_ASSERT(etl::is_integral<TKey>::value, "Only integral keys allowed");
267 ETL_STATIC_ASSERT(etl::is_integral<TCount>::value, "Only integral count allowed");
268
269 typedef TKey key_type;
270 typedef TCount count_type;
271 typedef TCount value_type;
272
273 //*********************************
275 //*********************************
276 explicit histogram(key_type start_index_)
277 : start_index(start_index_)
278 {
279 this->accumulator.fill(count_type(0));
280 }
281
282 //*********************************
284 //*********************************
285 template <typename TIterator>
286 histogram(key_type start_index_, TIterator first, TIterator last)
287 : start_index(start_index_)
288 {
289 this->accumulator.fill(count_type(0));
290 add(first, last);
291 }
292
293 //*********************************
295 //*********************************
296 histogram(const histogram& other)
297 {
298 this->accumulator = other.accumulator;
299 }
300
301#if ETL_USING_CPP11
302 //*********************************
304 //*********************************
305 histogram(histogram&& other)
306 {
307 this->accumulator = etl::move(other.accumulator);
308 }
309#endif
310
311 //*********************************
313 //*********************************
315 {
316 this->accumulator = rhs.accumulator;
317
318 return *this;
319 }
320
321#if ETL_USING_CPP11
322 //*********************************
324 //*********************************
326 {
327 this->accumulator = etl::move(rhs.accumulator);
328
329 return *this;
330 }
331#endif
332
333 //*********************************
335 //*********************************
336 void add(key_type key)
337 {
338 ++this->accumulator[static_cast<size_t>(key - start_index)];
339 }
340
341 //*********************************
343 //*********************************
344 template <typename TIterator>
345 void add(TIterator first, TIterator last)
346 {
347 while (first != last)
348 {
349 add(*first);
350 ++first;
351 }
352 }
353
354 //*********************************
356 //*********************************
357 void operator()(key_type key)
358 {
359 add(key);
360 }
361
362 //*********************************
364 //*********************************
365 template <typename TIterator>
366 void operator()(TIterator first, TIterator last)
367 {
368 add(first, last);
369 }
370
371 //*********************************
373 //*********************************
374 value_type operator[](key_type key) const
375 {
376 return this->accumulator[static_cast<size_t>(key - start_index)];
377 }
378
379 private:
380
381 key_type start_index;
382 };
383
384 //***************************************************************************
386 //***************************************************************************
387 template <typename TKey, typename TCount, size_t Max_Size_>
388 class sparse_histogram : public etl::unary_function<TKey, void>
389 {
390 private:
391
392 typedef etl::flat_map<TKey, TCount, Max_Size_> accumulator_type;
393
394 public:
395
396 ETL_STATIC_ASSERT(etl::is_integral<TCount>::value, "Only integral count allowed");
397
398 static ETL_CONSTANT size_t Max_Size = Max_Size_;
399
400 typedef TKey key_type;
401 typedef TCount count_type;
402 typedef typename accumulator_type::value_type value_type;
403 typedef typename accumulator_type::const_iterator const_iterator;
404
405 public:
406
407 //*********************************
409 //*********************************
411
412 //*********************************
414 //*********************************
415 template <typename TIterator>
416 sparse_histogram(TIterator first, TIterator last)
417 {
418 add(first, last);
419 }
420
421 //*********************************
423 //*********************************
425 {
426 this->accumulator = other.accumulator;
427 }
428
429#if ETL_USING_CPP11
430 //*********************************
432 //*********************************
434 {
435 accumulator = etl::move(other.accumulator);
436 }
437#endif
438
439 //*********************************
441 //*********************************
443 {
444 accumulator = rhs.accumulator;
445
446 return *this;
447 }
448
449#if ETL_USING_CPP11
450 //*********************************
452 //*********************************
454 {
455 accumulator = etl::move(rhs.accumulator);
456
457 return *this;
458 }
459#endif
460
461 //*********************************
463 //*********************************
464 const_iterator begin() const
465 {
466 return accumulator.begin();
467 }
468
469 //*********************************
471 //*********************************
472 const_iterator cbegin() const
473 {
474 return accumulator.cbegin();
475 }
476
477 //*********************************
479 //*********************************
480 const_iterator end() const
481 {
482 return accumulator.begin();
483 }
484
485 //*********************************
487 //*********************************
488 const_iterator cend() const
489 {
490 return accumulator.cbegin();
491 }
492
493 //*********************************
495 //*********************************
496 void add(const key_type& key)
497 {
498 ++accumulator[key];
499 }
500
501 //*********************************
503 //*********************************
504 template <typename TIterator>
505 void add(TIterator first, TIterator last)
506 {
507 while (first != last)
508 {
509 add(*first);
510 ++first;
511 }
512 }
513
514 //*********************************
516 //*********************************
517 void operator()(const key_type& key)
518 {
519 add(key);
520 }
521
522 //*********************************
524 //*********************************
525 template <typename TIterator>
526 void operator()(TIterator first, TIterator last)
527 {
528 add(first, last);
529 }
530
531 //*********************************
533 //*********************************
534 const value_type& operator[](const key_type& key) const
535 {
536 static const value_type unused(key_type(), count_type(0));
537
538 typename accumulator_type::const_iterator itr = accumulator.find(key);
539
540 if (itr != accumulator.end())
541 {
542 return *itr;
543 }
544 else
545 {
546 return unused;
547 }
548 }
549
550 //*********************************
552 //*********************************
553 void clear()
554 {
555 accumulator.clear();
556 }
557
558 //*********************************
560 //*********************************
561 size_t size() const
562 {
563 return accumulator.size();
564 }
565
566 //*********************************
568 //*********************************
569 ETL_CONSTEXPR size_t max_size() const
570 {
571 return Max_Size;
572 }
573
574 //*********************************
576 //*********************************
577 size_t count() const
578 {
579 count_type sum = count_type(0);
580
581 const_iterator itr = accumulator.begin();
582
583 while (itr != accumulator.end())
584 {
585 sum += (*itr).second;
586 ++itr;
587 }
588
589 return static_cast<size_t>(sum);
590 }
591
592 private:
593
595 };
596
597 template <typename TKey, typename TCount, size_t Max_Size_>
598 ETL_CONSTANT size_t sparse_histogram<TKey, TCount, Max_Size_>::Max_Size;
599} // namespace etl
600
601#endif
histogram & operator=(const histogram &rhs)
Copy assignment.
Definition histogram.h:314
histogram(key_type start_index_, TIterator first, TIterator last)
Constructor.
Definition histogram.h:286
histogram(key_type start_index_)
Constructor.
Definition histogram.h:276
value_type operator[](key_type key) const
operator []
Definition histogram.h:374
void add(TIterator first, TIterator last)
Add.
Definition histogram.h:345
void operator()(TIterator first, TIterator last)
operator ()
Definition histogram.h:366
histogram(const histogram &other)
Copy constructor.
Definition histogram.h:296
void operator()(key_type key)
operator ()
Definition histogram.h:357
Histogram with a compile time start index.
Definition histogram.h:141
void operator()(key_type key)
operator ()
Definition histogram.h:233
void add(key_type key)
Add.
Definition histogram.h:212
value_type operator[](key_type key) const
operator []
Definition histogram.h:250
void add(TIterator first, TIterator last)
Add.
Definition histogram.h:221
histogram(TIterator first, TIterator last)
Constructor.
Definition histogram.h:163
histogram()
Constructor.
Definition histogram.h:154
histogram & operator=(const histogram &rhs)
Copy assignment.
Definition histogram.h:190
void operator()(TIterator first, TIterator last)
operator ()
Definition histogram.h:242
histogram(const histogram &other)
Copy constructor.
Definition histogram.h:172
Definition reference_flat_map.h:216
Base for histograms.
Definition histogram.h:52
const_iterator end() const
End of the histogram.
Definition histogram.h:80
const_iterator cend() const
End of the histogram.
Definition histogram.h:88
const_iterator cbegin() const
Beginning of the histogram.
Definition histogram.h:72
size_t count() const
Count of items in the histogram.
Definition histogram.h:120
const_iterator begin() const
Beginning of the histogram.
Definition histogram.h:64
void clear()
Clear the histogram.
Definition histogram.h:96
ETL_CONSTEXPR size_t size() const
Size of the histogram.
Definition histogram.h:104
ETL_CONSTEXPR size_t max_size() const
Max size of the histogram.
Definition histogram.h:112
Histogram for sparse keys.
Definition histogram.h:389
ETL_CONSTEXPR size_t max_size() const
Max size of the histogram.
Definition histogram.h:569
const_iterator begin() const
Beginning of the histogram.
Definition histogram.h:464
sparse_histogram(const sparse_histogram &other)
Copy constructor.
Definition histogram.h:424
const_iterator end() const
End of the histogram.
Definition histogram.h:480
void clear()
Clear the histogram.
Definition histogram.h:553
void add(TIterator first, TIterator last)
Add.
Definition histogram.h:505
const value_type & operator[](const key_type &key) const
operator []
Definition histogram.h:534
const_iterator cbegin() const
Beginning of the histogram.
Definition histogram.h:472
void operator()(const key_type &key)
operator ()
Definition histogram.h:517
sparse_histogram & operator=(const sparse_histogram &rhs)
Copy assignment.
Definition histogram.h:442
size_t size() const
Size of the histogram.
Definition histogram.h:561
sparse_histogram(TIterator first, TIterator last)
Constructor.
Definition histogram.h:416
sparse_histogram()
Constructor.
Definition histogram.h:410
void add(const key_type &key)
Add.
Definition histogram.h:496
void operator()(TIterator first, TIterator last)
operator ()
Definition histogram.h:526
const_iterator cend() const
End of the histogram.
Definition histogram.h:488
size_t count() const
Count of items in the histogram.
Definition histogram.h:577
ETL_CONSTEXPR14 T accumulate(TIterator first, TIterator last, T sum)
Definition algorithm.h:2284
Definition array.h:88
Definition flat_map.h:1142
Definition integral_limits.h:518
bitset_ext
Definition absolute.h:40
unary_function
Definition functional.h:182