From 3f2463ce8ca3c0d13f313499787d095d31728d27 Mon Sep 17 00:00:00 2001 From: Brian Yi Date: Wed, 7 Feb 2024 19:42:29 +0000 Subject: [PATCH] Rename c++ ::wfa::any_sketch::Distribution to ::wfa::any_sketch::BaseDistribution --- src/main/cc/any_sketch/any_sketch.cc | 5 +++-- src/main/cc/any_sketch/any_sketch.h | 4 ++-- src/main/cc/any_sketch/distributions.cc | 22 ++++++++++---------- src/main/cc/any_sketch/distributions.h | 22 ++++++++++---------- src/main/cc/any_sketch/value_function.h | 2 +- src/test/cc/any_sketch/any_sketch_test.cc | 9 ++++---- src/test/cc/any_sketch/distributions_test.cc | 8 +++---- src/test/cc/estimation/estimators_test.cc | 4 ++-- 8 files changed, 39 insertions(+), 37 deletions(-) diff --git a/src/main/cc/any_sketch/any_sketch.cc b/src/main/cc/any_sketch/any_sketch.cc index de90657..6b21888 100644 --- a/src/main/cc/any_sketch/any_sketch.cc +++ b/src/main/cc/any_sketch/any_sketch.cc @@ -35,7 +35,7 @@ namespace wfa::any_sketch { -AnySketch::AnySketch(std::vector> indexes, +AnySketch::AnySketch(std::vector> indexes, std::vector values) : indexes_(indexes.size()), values_(values.size()) { std::move(indexes.begin(), indexes.end(), indexes_.begin()); @@ -75,7 +75,8 @@ absl::StatusOr AnySketch::GetIndex( absl::string_view item, const ItemMetadata& item_metadata) const { uint64_t product = 1; uint64_t linearized_index = 0; - for (const std::unique_ptr& distribution : indexes_) { + for (const std::unique_ptr& distribution : + indexes_) { ASSIGN_OR_RETURN(int64_t distribution_value, distribution->Apply(item, item_metadata)); int64_t index_part = distribution_value - distribution->min_value(); diff --git a/src/main/cc/any_sketch/any_sketch.h b/src/main/cc/any_sketch/any_sketch.h index a9b7ba2..ee754b1 100644 --- a/src/main/cc/any_sketch/any_sketch.h +++ b/src/main/cc/any_sketch/any_sketch.h @@ -75,7 +75,7 @@ class AnySketch { // Creates a new, empty AnySketch. // // The inputs will be moved from. - AnySketch(std::vector> indexes, + AnySketch(std::vector> indexes, std::vector values); AnySketch(const AnySketch &) = delete; @@ -117,7 +117,7 @@ class AnySketch { private: absl::flat_hash_map> registers_; - absl::FixedArray> indexes_; + absl::FixedArray> indexes_; absl::FixedArray values_; size_t register_size() const; diff --git a/src/main/cc/any_sketch/distributions.cc b/src/main/cc/any_sketch/distributions.cc index 0c997fa..64685e0 100644 --- a/src/main/cc/any_sketch/distributions.cc +++ b/src/main/cc/any_sketch/distributions.cc @@ -32,9 +32,9 @@ namespace wfa::any_sketch { namespace { -class BaseDistribution : public Distribution { +class BaseDistributionImpl : public BaseDistribution { public: - BaseDistribution(int64_t min_value, int64_t max_value) + BaseDistributionImpl(int64_t min_value, int64_t max_value) : min_value_(min_value), max_value_(max_value) {} absl::StatusOr Apply( @@ -53,7 +53,7 @@ class BaseDistribution : public Distribution { absl::string_view item, const ItemMetadata& item_metadata) const = 0; }; -absl::StatusOr BaseDistribution::Apply( +absl::StatusOr BaseDistributionImpl::Apply( absl::string_view item, const ItemMetadata& item_metadata) const { ASSIGN_OR_RETURN(int64_t value, ApplyInternal(item, item_metadata)); @@ -69,11 +69,11 @@ absl::StatusOr BaseDistribution::Apply( return value; } -class OracleDistribution : public BaseDistribution { +class OracleDistribution : public BaseDistributionImpl { public: OracleDistribution(int64_t min_value, int64_t max_value, absl::string_view feature_name) - : BaseDistribution(min_value, max_value), feature_name_(feature_name) {} + : BaseDistributionImpl(min_value, max_value), feature_name_(feature_name) {} private: absl::StatusOr ApplyInternal( @@ -90,11 +90,11 @@ class OracleDistribution : public BaseDistribution { std::string feature_name_; }; -class FingerprintingDistribution : public BaseDistribution { +class FingerprintingDistribution : public BaseDistributionImpl { public: FingerprintingDistribution(int64_t min_value, int64_t max_value, const Fingerprinter* fingerprinter) - : BaseDistribution(min_value, max_value), fingerprinter_(fingerprinter) {} + : BaseDistributionImpl(min_value, max_value), fingerprinter_(fingerprinter) {} private: absl::StatusOr ApplyInternal( @@ -173,23 +173,23 @@ class GeometricDistribution : public FingerprintingDistribution { }; } // namespace -std::unique_ptr GetOracleDistribution( +std::unique_ptr GetOracleDistribution( absl::string_view feature_name, int64_t min_value, int64_t max_value) { return absl::make_unique(min_value, max_value, feature_name); } -std::unique_ptr GetUniformDistribution( +std::unique_ptr GetUniformDistribution( const Fingerprinter* fingerprinter, int64_t min_value, int64_t max_value) { return absl::make_unique(min_value, max_value, fingerprinter); } -std::unique_ptr GetExponentialDistribution( +std::unique_ptr GetExponentialDistribution( const Fingerprinter* fingerprinter, double rate, int64_t size) { ABSL_ASSERT(rate > 0.0); ABSL_ASSERT(size > 0); return absl::make_unique(rate, size, fingerprinter); } -std::unique_ptr GetGeometricDistribution( +std::unique_ptr GetGeometricDistribution( const Fingerprinter* fingerprinter, int64_t min_value, int64_t max_value) { return absl::make_unique(min_value, max_value, fingerprinter); diff --git a/src/main/cc/any_sketch/distributions.h b/src/main/cc/any_sketch/distributions.h index 23c2d3b..2d1ee15 100644 --- a/src/main/cc/any_sketch/distributions.h +++ b/src/main/cc/any_sketch/distributions.h @@ -28,14 +28,14 @@ namespace wfa::any_sketch { using ItemMetadata = absl::flat_hash_map; -// Base for representing distributions -- a way of deterministically mapping an -// item and associated metadata to a number. -class Distribution { +// Abstract Base for representing distributions -- a way of deterministically mapping an item +// and associated metadata to a number. +class BaseDistribution { public: - Distribution(const Distribution&) = delete; - Distribution& operator=(const Distribution&) = delete; + BaseDistribution(const BaseDistribution&) = delete; + BaseDistribution& operator=(const BaseDistribution&) = delete; - virtual ~Distribution() = default; + virtual ~BaseDistribution() = default; // The smallest value (inclusive) that the Distribution can return. virtual int64_t min_value() const = 0; @@ -52,16 +52,16 @@ class Distribution { absl::string_view item, const ItemMetadata& item_metadata) const = 0; protected: - Distribution() = default; + BaseDistribution() = default; }; -std::unique_ptr GetOracleDistribution( +std::unique_ptr GetOracleDistribution( absl::string_view feature_name, int64_t min_value, int64_t max_value); -std::unique_ptr GetUniformDistribution( +std::unique_ptr GetUniformDistribution( const Fingerprinter* fingerprinter, int64_t min_value, int64_t max_value); -std::unique_ptr GetExponentialDistribution( +std::unique_ptr GetExponentialDistribution( const Fingerprinter* fingerprinter, double rate, int64_t size); -std::unique_ptr GetGeometricDistribution( +std::unique_ptr GetGeometricDistribution( const Fingerprinter* fingerprinter, int64_t min_value, int64_t max_value); } // namespace wfa::any_sketch diff --git a/src/main/cc/any_sketch/value_function.h b/src/main/cc/any_sketch/value_function.h index 49b83d8..d1ea0b5 100644 --- a/src/main/cc/any_sketch/value_function.h +++ b/src/main/cc/any_sketch/value_function.h @@ -27,7 +27,7 @@ namespace wfa::any_sketch { struct ValueFunction { std::string name; AggregatorType aggregator_type; - std::unique_ptr distribution; + std::unique_ptr distribution; }; } // namespace wfa::any_sketch diff --git a/src/test/cc/any_sketch/any_sketch_test.cc b/src/test/cc/any_sketch/any_sketch_test.cc index e90ed44..f04a645 100644 --- a/src/test/cc/any_sketch/any_sketch_test.cc +++ b/src/test/cc/any_sketch/any_sketch_test.cc @@ -69,7 +69,7 @@ Matcher RegisterIs(uint64_t index, new RegisterIsMatcher(index, std::move(values))); } -class FakeDistribution : public Distribution { +class FakeDistribution : public BaseDistribution { public: absl::StatusOr Apply( absl::string_view item, @@ -82,7 +82,7 @@ class FakeDistribution : public Distribution { int64_t max_value() const override { return 10; } }; -std::unique_ptr MakeFakeDistribution() { +std::unique_ptr MakeFakeDistribution() { return absl::make_unique(); } @@ -93,12 +93,13 @@ std::vector MakeSingleItemVector(T&& t) { return v; } -std::vector> MakeFakeDistributionIndex() { +std::vector> MakeFakeDistributionIndex() { return MakeSingleItemVector(MakeFakeDistribution()); } ValueFunction MakeValueFunction(AggregatorType aggregator, - std::unique_ptr distribution) { + std::unique_ptr + distribution) { return {.name = "SomeValueFunction", .aggregator_type = aggregator, .distribution = std::move(distribution)}; diff --git a/src/test/cc/any_sketch/distributions_test.cc b/src/test/cc/any_sketch/distributions_test.cc index d510c74..3f54186 100644 --- a/src/test/cc/any_sketch/distributions_test.cc +++ b/src/test/cc/any_sketch/distributions_test.cc @@ -34,7 +34,7 @@ class FakeFingerprinter : public Fingerprinter { }; TEST(DistributionsTest, OracleDistribution) { - std::unique_ptr distribution = + std::unique_ptr distribution = GetOracleDistribution("foo", 3, 10); ASSERT_FALSE(distribution == nullptr); @@ -53,7 +53,7 @@ TEST(DistributionsTest, OracleDistribution) { TEST(DistributionsTest, UniformDistribution) { FakeFingerprinter fingerprinter; - std::unique_ptr distribution = + std::unique_ptr distribution = GetUniformDistribution(&fingerprinter, 3, 10); ASSERT_FALSE(distribution == nullptr); @@ -76,7 +76,7 @@ TEST(DistributionsTest, UniformDistribution) { TEST(DistributionsTest, ExponentialDistribution) { FakeFingerprinter fingerprinter; - std::unique_ptr distribution = + std::unique_ptr distribution = GetExponentialDistribution(&fingerprinter, 2, 10); ASSERT_FALSE(distribution == nullptr); @@ -94,7 +94,7 @@ TEST(DistributionsTest, ExponentialDistribution) { TEST(DistributionsTest, GeometricDistribution) { FakeFingerprinter fingerprinter; - std::unique_ptr distribution = + std::unique_ptr distribution = GetGeometricDistribution(&fingerprinter, 10, 74); ASSERT_FALSE(distribution == nullptr); diff --git a/src/test/cc/estimation/estimators_test.cc b/src/test/cc/estimation/estimators_test.cc index 13c876c..2203b70 100644 --- a/src/test/cc/estimation/estimators_test.cc +++ b/src/test/cc/estimation/estimators_test.cc @@ -30,7 +30,7 @@ namespace wfa::estimation { namespace { -using ::wfa::any_sketch::Distribution; +using ::wfa::any_sketch::BaseDistribution; MATCHER_P2(EqWithError, value, error, "") { // Since value and arg might be of different, possibly unsigned types, @@ -60,7 +60,7 @@ uint64_t GenerateRandomSketchAndGetSize(double decay_rate, absl::flat_hash_set indexes; const Fingerprinter& fingerprinter = GetSha256Fingerprinter(); - std::unique_ptr exponential_distribution = + std::unique_ptr exponential_distribution = wfa::any_sketch::GetExponentialDistribution(&fingerprinter, decay_rate, num_of_total_registers);