Histogram function to write tab-separated values.
[folly.git] / folly / Histogram.h
index f25fcbb0b00d3b186c83b2efacd774e376315c00..fb72145047558f86f0e0b694d79b4ee3ee3208a6 100644 (file)
 #define FOLLY_HISTOGRAM_H_
 
 #include <cstddef>
-#include <cstdint>
 #include <limits>
+#include <ostream>
 #include <string>
 #include <vector>
 #include <stdexcept>
 
+#include "folly/detail/Stats.h"
+
 namespace folly {
 
 namespace detail {
@@ -223,36 +225,7 @@ template <typename T>
 class Histogram {
  public:
   typedef T ValueType;
-
-  struct Bucket {
-    Bucket()
-      : sum(0),
-        count(0) {}
-
-    void clear() {
-      sum = 0;
-      count = 0;
-    }
-
-    Bucket& merge(const Bucket &bucket) {
-      if (this != &bucket) {
-        sum += bucket.sum;
-        count += bucket.count;
-      }
-      return *this;
-    }
-
-    Bucket& operator=(const Bucket& bucket) {
-      if (this != &bucket) {
-        sum = bucket.sum;
-        count = bucket.count;
-      }
-      return *this;
-    }
-
-    ValueType sum;
-    uint64_t count;
-  };
+  typedef detail::Bucket<T> Bucket;
 
   Histogram(ValueType bucketSize, ValueType min, ValueType max)
     : buckets_(bucketSize, min, max, Bucket()) {}
@@ -298,7 +271,7 @@ class Histogram {
     }
 
     for (int i = 0; i < buckets_.getNumBuckets(); i++) {
-      buckets_.getByIndex(i).merge(hist.buckets_.getByIndex(i));
+      buckets_.getByIndex(i) += hist.buckets_.getByIndex(i);
     }
   }
 
@@ -395,6 +368,12 @@ class Histogram {
    */
   std::string debugString() const;
 
+  /*
+   * Write the histogram contents in tab-separated values (TSV) format.
+   * Format is "min max count sum".
+   */
+  void toTSV(std::ostream& out, bool skipEmptyBuckets = true) const;
+
  private:
   struct CountFromBucket {
     uint64_t operator()(const Bucket& bucket) const {