histogram_inc.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* NOLINT(build/header_guard) */
  2. /* Copyright 2013 Google Inc. All Rights Reserved.
  3. Distributed under MIT license.
  4. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  5. */
  6. /* template parameters: Histogram, DATA_SIZE, DataType */
  7. /* A simple container for histograms of data in blocks. */
  8. typedef struct FN(Histogram) {
  9. uint32_t data_[DATA_SIZE];
  10. size_t total_count_;
  11. double bit_cost_;
  12. } FN(Histogram);
  13. static BROTLI_INLINE void FN(HistogramClear)(FN(Histogram)* self) {
  14. memset(self->data_, 0, sizeof(self->data_));
  15. self->total_count_ = 0;
  16. self->bit_cost_ = HUGE_VAL;
  17. }
  18. static BROTLI_INLINE void FN(ClearHistograms)(
  19. FN(Histogram)* array, size_t length) {
  20. size_t i;
  21. for (i = 0; i < length; ++i) FN(HistogramClear)(array + i);
  22. }
  23. static BROTLI_INLINE void FN(HistogramAdd)(FN(Histogram)* self, size_t val) {
  24. ++self->data_[val];
  25. ++self->total_count_;
  26. }
  27. static BROTLI_INLINE void FN(HistogramAddVector)(FN(Histogram)* self,
  28. const DataType* p, size_t n) {
  29. self->total_count_ += n;
  30. n += 1;
  31. while (--n) ++self->data_[*p++];
  32. }
  33. static BROTLI_INLINE void FN(HistogramAddHistogram)(FN(Histogram)* self,
  34. const FN(Histogram)* v) {
  35. size_t i;
  36. self->total_count_ += v->total_count_;
  37. for (i = 0; i < DATA_SIZE; ++i) {
  38. self->data_[i] += v->data_[i];
  39. }
  40. }
  41. static BROTLI_INLINE size_t FN(HistogramDataSize)(void) { return DATA_SIZE; }