| 199 | } |
| 200 | |
| 201 | void Histogram::EncodeToProto(HistogramProto* proto, |
| 202 | bool preserve_zero_buckets) const { |
| 203 | proto->Clear(); |
| 204 | proto->set_min(min_); |
| 205 | proto->set_max(max_); |
| 206 | proto->set_num(num_); |
| 207 | proto->set_sum(sum_); |
| 208 | proto->set_sum_squares(sum_squares_); |
| 209 | for (size_t i = 0; i < buckets_.size();) { |
| 210 | double end = bucket_limits_[i]; |
| 211 | double count = buckets_[i]; |
| 212 | i++; |
| 213 | if (!preserve_zero_buckets && count <= 0.0) { |
| 214 | // Find run of empty buckets and collapse them into one |
| 215 | while (i < buckets_.size() && buckets_[i] <= 0.0) { |
| 216 | end = bucket_limits_[i]; |
| 217 | count = buckets_[i]; |
| 218 | i++; |
| 219 | } |
| 220 | } |
| 221 | proto->add_bucket_limit(end); |
| 222 | proto->add_bucket(count); |
| 223 | } |
| 224 | if (proto->bucket_size() == 0.0) { |
| 225 | // It's easier when we restore if we always have at least one bucket entry |
| 226 | proto->add_bucket_limit(DBL_MAX); |
| 227 | proto->add_bucket(0.0); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // ThreadSafeHistogram implementation. |
| 232 | bool ThreadSafeHistogram::DecodeFromProto(const HistogramProto& proto) { |