cluster.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* Copyright 2013 Google Inc. All Rights Reserved.
  2. Distributed under MIT license.
  3. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  4. */
  5. /* Functions for clustering similar histograms together. */
  6. #include "./cluster.h"
  7. #include "../common/platform.h"
  8. #include <brotli/types.h>
  9. #include "./bit_cost.h" /* BrotliPopulationCost */
  10. #include "./fast_log.h"
  11. #include "./histogram.h"
  12. #include "./memory.h"
  13. #if defined(__cplusplus) || defined(c_plusplus)
  14. extern "C" {
  15. #endif
  16. static BROTLI_INLINE BROTLI_BOOL HistogramPairIsLess(
  17. const HistogramPair* p1, const HistogramPair* p2) {
  18. if (p1->cost_diff != p2->cost_diff) {
  19. return TO_BROTLI_BOOL(p1->cost_diff > p2->cost_diff);
  20. }
  21. return TO_BROTLI_BOOL((p1->idx2 - p1->idx1) > (p2->idx2 - p2->idx1));
  22. }
  23. /* Returns entropy reduction of the context map when we combine two clusters. */
  24. static BROTLI_INLINE double ClusterCostDiff(size_t size_a, size_t size_b) {
  25. size_t size_c = size_a + size_b;
  26. return (double)size_a * FastLog2(size_a) +
  27. (double)size_b * FastLog2(size_b) -
  28. (double)size_c * FastLog2(size_c);
  29. }
  30. #define CODE(X) X
  31. #define FN(X) X ## Literal
  32. #include "./cluster_inc.h" /* NOLINT(build/include) */
  33. #undef FN
  34. #define FN(X) X ## Command
  35. #include "./cluster_inc.h" /* NOLINT(build/include) */
  36. #undef FN
  37. #define FN(X) X ## Distance
  38. #include "./cluster_inc.h" /* NOLINT(build/include) */
  39. #undef FN
  40. #undef CODE
  41. #if defined(__cplusplus) || defined(c_plusplus)
  42. } /* extern "C" */
  43. #endif