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