File: Histogram.h

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (156 lines) | stat: -rw-r--r-- 4,514 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//===--- Histogram.h - Simple histogram for statistics ----------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include "llvm/ADT/ArrayRef.h"

#ifndef SWIFT_HISTOGRAM_H
#define SWIFT_HISTOGRAM_H

#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <vector>

namespace swift {

namespace rewriting {

/// A simple histogram that supports two operations: recording an element, and
/// printing a graphical representation.
///
/// Used by the rewrite system to record various statistics, which are dumped
/// when the -analyze-requirement-machine frontend flag is used.
class Histogram {
  unsigned Size;
  unsigned Start;
  std::vector<unsigned> Buckets;
  unsigned OverflowBucket;
  unsigned MaxValue = 0;

  const unsigned MaxWidth = 40;

  /// This is not really the base-10 logarithm, but rather the number of digits
  /// in \p x when printed in base 10.
  ///
  /// So log10(0) == 1, log10(1) == 1, log10(10) == 2, etc.
  static unsigned log10(unsigned x) {
    if (x == 0)
      return 1;
    unsigned result = 0;
    while (x != 0) {
      result += 1;
      x /= 10;
    }
    return result;
  }

public:
  /// Creates a histogram with \p size buckets, where the numbering begins at
  /// \p start.
  Histogram(unsigned size, unsigned start = 0)
      : Size(size),
        Start(start),
        Buckets(size, 0),
        OverflowBucket(0) {
  }

  /// Adds an entry to the histogram. Asserts if the \p value is smaller than
  /// Start. The value is allowed to be greater than or equal to Start + Size,
  /// in which case it's counted in the OverflowBucket.
  void add(unsigned value) {
    assert(value >= Start);
    value -= Start;
    if (value >= Size)
      ++OverflowBucket;
    else
      ++Buckets[value];

    if (value > MaxValue)
      MaxValue = value;
  }

  /// Print a nice-looking graphical representation of the histogram.
  void dump(llvm::raw_ostream &out, const StringRef labels[] = {}) {
    unsigned sumValues = 0;
    unsigned maxValue = 0;
    for (unsigned i = 0; i < Size; ++i) {
      sumValues += Buckets[i];
      maxValue = std::max(maxValue, Buckets[i]);
    }
    sumValues += OverflowBucket;
    maxValue = std::max(maxValue, OverflowBucket);

    size_t maxLabelWidth = 3 + log10(Size + Start);
    if (labels != nullptr) {
      for (unsigned i = 0; i < Size; ++i)
        maxLabelWidth = std::max(labels[i].size(), maxLabelWidth);
    }

    out << std::string(maxLabelWidth, ' ') << " |";
    out << std::string(std::min(MaxWidth, maxValue), ' ');
    out << maxValue << "\n";

    out << std::string(maxLabelWidth, ' ') << " |";
    out << std::string(std::min(MaxWidth, maxValue), ' ');
    out << "*\n";

    out << std::string(maxLabelWidth, '-') << "-+-";
    out << std::string(std::min(MaxWidth, maxValue), '-') << "\n";

    auto scaledValue = [&](unsigned value) {
      if (maxValue > MaxWidth) {
        if (value != 0) {
          // If the value is non-zero, print at least one '#'.
          return std::max(1U, (value * MaxWidth) / maxValue);
        }
      }
      return value;
    };

    for (unsigned i = 0; i < Size; ++i) {
      if (labels) {
        out << std::string(maxLabelWidth - labels[i].size(), ' ');
        out << labels[i];
      } else {
        unsigned key = i + Start;
        out << std::string(maxLabelWidth - log10(key), ' ');
        out << key;
      }
      out << " | ";

      unsigned value = scaledValue(Buckets[i]);
      out << std::string(value, '#') << "\n";
    }

    if (OverflowBucket > 0) {
      out << ">= " << (Size + Start) << " | ";

      unsigned value = scaledValue(OverflowBucket);
      out << std::string(value, '#') << "\n";
    }

    out << std::string(maxLabelWidth, '-') << "-+-";
    out << std::string(std::min(MaxWidth, maxValue), '-') << "\n";

    out << std::string(maxLabelWidth, ' ') << " | ";
    out << "Total: " << sumValues << "\n";
    out << std::string(maxLabelWidth, ' ') << " | ";
    out << "Max:   " << MaxValue << "\n";
  }
};

}  // end namespace rewriting

}  // end namespace swift

#endif