Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit cdfcbcc

Browse files
authored
Change BucketBoundaries' internal constructor to take a vector by value. (#38)
This better accords with conventions (https://github.com/census-instrumentation/opencensus-cpp/blob/master/opencensus/doc/conventions.md#classes-that-take-an-object-and-store-it) and avoids a copy for the Linear and Exponential constructors.
1 parent c169664 commit cdfcbcc

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

opencensus/stats/bucket_boundaries.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616
#define OPENCENSUS_STATS_BUCKET_BOUNDARIES_H_
1717

1818
#include <cstddef>
19-
#include <initializer_list>
2019
#include <string>
20+
#include <utility>
2121
#include <vector>
2222

23-
#include "absl/types/span.h"
24-
2523
namespace opencensus {
2624
namespace stats {
2725

@@ -53,7 +51,7 @@ class BucketBoundaries final {
5351
// [boundaries[i], boundaries[i+1]), as well as an underflow bucket covering
5452
// [-inf, boundaries[0]) and an overflow bucket covering
5553
// [boundaries[boundaries.size()-1], inf].
56-
static BucketBoundaries Explicit(std::initializer_list<double> boundaries);
54+
static BucketBoundaries Explicit(std::vector<double> boundaries);
5755

5856
// The number of buckets in a Distribution using this bucketer.
5957
int num_buckets() const { return lower_boundaries_.size() + 1; }
@@ -74,8 +72,8 @@ class BucketBoundaries final {
7472
}
7573

7674
private:
77-
BucketBoundaries(absl::Span<const double> lower_boundaries)
78-
: lower_boundaries_(lower_boundaries.begin(), lower_boundaries.end()) {}
75+
BucketBoundaries(std::vector<double> lower_boundaries)
76+
: lower_boundaries_(std::move(lower_boundaries)) {}
7977

8078
// The lower bound of each bucket, excluding the underflow bucket but
8179
// including the overflow bucket.

opencensus/stats/internal/bucket_boundaries.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <algorithm>
1818
#include <iostream>
19+
#include <vector>
1920

2021
#include "absl/base/macros.h"
2122
#include "absl/strings/str_cat.h"
@@ -39,7 +40,7 @@ BucketBoundaries BucketBoundaries::Linear(int num_finite_buckets, double offset,
3940
boundaries[i] = boundary;
4041
boundary += width;
4142
}
42-
return BucketBoundaries(boundaries);
43+
return BucketBoundaries(std::move(boundaries));
4344
}
4445

4546
// static
@@ -52,20 +53,18 @@ BucketBoundaries BucketBoundaries::Exponential(int num_finite_buckets,
5253
boundaries[i] = upper_bound;
5354
upper_bound *= growth_factor;
5455
}
55-
return BucketBoundaries(boundaries);
56+
return BucketBoundaries(std::move(boundaries));
5657
}
5758

5859
// static
59-
BucketBoundaries BucketBoundaries::Explicit(
60-
std::initializer_list<double> boundaries) {
60+
BucketBoundaries BucketBoundaries::Explicit(std::vector<double> boundaries) {
6161
if (!std::is_sorted(boundaries.begin(), boundaries.end())) {
6262
std::cerr << "BucketBoundaries::Explicit called with non-monotonic "
6363
"boundary list.\n";
6464
ABSL_ASSERT(0);
6565
return BucketBoundaries({});
6666
}
67-
return BucketBoundaries(
68-
absl::Span<const double>(boundaries.begin(), boundaries.size()));
67+
return BucketBoundaries(std::move(boundaries));
6968
}
7069

7170
int BucketBoundaries::BucketForValue(double value) const {

0 commit comments

Comments
 (0)