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 start_index = other.start_index;
300 }
301
302#if ETL_USING_CPP11
303 //*********************************
305 //*********************************
306 histogram(histogram&& other)
307 {
308 this->accumulator = etl::move(other.accumulator);
309 start_index = other.start_index;
310 }
311#endif
312
313 //*********************************
315 //*********************************
317 {
318 this->accumulator = rhs.accumulator;
319 start_index = rhs.start_index;
320
321 return *this;
322 }
323
324#if ETL_USING_CPP11
325 //*********************************
327 //*********************************
329 {
330 this->accumulator = etl::move(rhs.accumulator);
331 start_index = rhs.start_index;
332
333 return *this;
334 }
335#endif
336
337 //*********************************
339 //*********************************
340 void add(key_type key)
341 {
342 ++this->accumulator[static_cast<size_t>(key - start_index)];
343 }
344
345 //*********************************
347 //*********************************
348 template <typename TIterator>
349 void add(TIterator first, TIterator last)
350 {
351 while (first != last)
352 {
353 add(*first);
354 ++first;
355 }
356 }
357
358 //*********************************
360 //*********************************
361 void operator()(key_type key)
362 {
363 add(key);
364 }
365
366 //*********************************
368 //*********************************
369 template <typename TIterator>
370 void operator()(TIterator first, TIterator last)
371 {
372 add(first, last);
373 }
374
375 //*********************************
377 //*********************************
378 value_type operator[](key_type key) const
379 {
380 return this->accumulator[static_cast<size_t>(key - start_index)];
381 }
382
383 private:
384
385 key_type start_index;
386 };
387
388 //***************************************************************************
390 //***************************************************************************
391 template <typename TKey, typename TCount, size_t Max_Size_>
392 class sparse_histogram : public etl::unary_function<TKey, void>
393 {
394 private:
395
396 typedef etl::flat_map<TKey, TCount, Max_Size_> accumulator_type;
397
398 public:
399
400 ETL_STATIC_ASSERT(etl::is_integral<TCount>::value, "Only integral count allowed");
401
402 static ETL_CONSTANT size_t Max_Size = Max_Size_;
403
404 typedef TKey key_type;
405 typedef TCount count_type;
406 typedef typename accumulator_type::value_type value_type;
407 typedef typename accumulator_type::const_iterator const_iterator;
408
409 public:
410
411 //*********************************
413 //*********************************
415
416 //*********************************
418 //*********************************
419 template <typename TIterator>
420 sparse_histogram(TIterator first, TIterator last)
421 {
422 add(first, last);
423 }
424
425 //*********************************
427 //*********************************
429 {
430 this->accumulator = other.accumulator;
431 }
432
433#if ETL_USING_CPP11
434 //*********************************
436 //*********************************
438 {
439 accumulator = etl::move(other.accumulator);
440 }
441#endif
442
443 //*********************************
445 //*********************************
447 {
448 accumulator = rhs.accumulator;
449
450 return *this;
451 }
452
453#if ETL_USING_CPP11
454 //*********************************
456 //*********************************
458 {
459 accumulator = etl::move(rhs.accumulator);
460
461 return *this;
462 }
463#endif
464
465 //*********************************
467 //*********************************
468 const_iterator begin() const
469 {
470 return accumulator.begin();
471 }
472
473 //*********************************
475 //*********************************
476 const_iterator cbegin() const
477 {
478 return accumulator.cbegin();
479 }
480
481 //*********************************
483 //*********************************
484 const_iterator end() const
485 {
486 return accumulator.end();
487 }
488
489 //*********************************
491 //*********************************
492 const_iterator cend() const
493 {
494 return accumulator.cend();
495 }
496
497 //*********************************
499 //*********************************
500 void add(const key_type& key)
501 {
502 ++accumulator[key];
503 }
504
505 //*********************************
507 //*********************************
508 template <typename TIterator>
509 void add(TIterator first, TIterator last)
510 {
511 while (first != last)
512 {
513 add(*first);
514 ++first;
515 }
516 }
517
518 //*********************************
520 //*********************************
521 void operator()(const key_type& key)
522 {
523 add(key);
524 }
525
526 //*********************************
528 //*********************************
529 template <typename TIterator>
530 void operator()(TIterator first, TIterator last)
531 {
532 add(first, last);
533 }
534
535 //*********************************
537 //*********************************
538 const value_type& operator[](const key_type& key) const
539 {
540 static const value_type unused(key_type(), count_type(0));
541
542 typename accumulator_type::const_iterator itr = accumulator.find(key);
543
544 if (itr != accumulator.end())
545 {
546 return *itr;
547 }
548 else
549 {
550 return unused;
551 }
552 }
553
554 //*********************************
556 //*********************************
557 void clear()
558 {
559 accumulator.clear();
560 }
561
562 //*********************************
564 //*********************************
565 size_t size() const
566 {
567 return accumulator.size();
568 }
569
570 //*********************************
572 //*********************************
573 ETL_CONSTEXPR size_t max_size() const
574 {
575 return Max_Size;
576 }
577
578 //*********************************
580 //*********************************
581 size_t count() const
582 {
583 count_type sum = count_type(0);
584
585 const_iterator itr = accumulator.begin();
586
587 while (itr != accumulator.end())
588 {
589 sum += (*itr).second;
590 ++itr;
591 }
592
593 return static_cast<size_t>(sum);
594 }
595
596 private:
597
599 };
600
601 template <typename TKey, typename TCount, size_t Max_Size_>
602 ETL_CONSTANT size_t sparse_histogram<TKey, TCount, Max_Size_>::Max_Size;
603} // namespace etl
604
605#endif
histogram & operator=(const histogram &rhs)
Copy assignment.
Definition histogram.h:316
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:378
void add(TIterator first, TIterator last)
Add.
Definition histogram.h:349
void operator()(TIterator first, TIterator last)
operator ()
Definition histogram.h:370
histogram(const histogram &other)
Copy constructor.
Definition histogram.h:296
void operator()(key_type key)
operator ()
Definition histogram.h:361
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:393
ETL_CONSTEXPR size_t max_size() const
Max size of the histogram.
Definition histogram.h:573
const_iterator begin() const
Beginning of the histogram.
Definition histogram.h:468
sparse_histogram(const sparse_histogram &other)
Copy constructor.
Definition histogram.h:428
const_iterator end() const
End of the histogram.
Definition histogram.h:484
void clear()
Clear the histogram.
Definition histogram.h:557
void add(TIterator first, TIterator last)
Add.
Definition histogram.h:509
const value_type & operator[](const key_type &key) const
operator []
Definition histogram.h:538
const_iterator cbegin() const
Beginning of the histogram.
Definition histogram.h:476
void operator()(const key_type &key)
operator ()
Definition histogram.h:521
sparse_histogram & operator=(const sparse_histogram &rhs)
Copy assignment.
Definition histogram.h:446
size_t size() const
Size of the histogram.
Definition histogram.h:565
sparse_histogram(TIterator first, TIterator last)
Constructor.
Definition histogram.h:420
sparse_histogram()
Constructor.
Definition histogram.h:414
void add(const key_type &key)
Add.
Definition histogram.h:500
void operator()(TIterator first, TIterator last)
operator ()
Definition histogram.h:530
const_iterator cend() const
End of the histogram.
Definition histogram.h:492
size_t count() const
Count of items in the histogram.
Definition histogram.h:581
ETL_CONSTEXPR14 T accumulate(TIterator first, TIterator last, T sum)
Definition algorithm.h:2321
Definition array.h:85
Definition flat_map.h:1279
Definition integral_limits.h:517
Definition absolute.h:40
unary_function
Definition functional.h:182