LindaSummer commented on code in PR #3130:
URL: https://github.com/apache/kvrocks/pull/3130#discussion_r2475934194
##########
src/commands/cmd_tdigest.cc:
##########
@@ -176,6 +176,45 @@ class CommandTDigestAdd : public Commander {
std::vector<double> values_;
};
+class CommandTDigestRevRank : public Commander {
+ public:
+ Status Parse(const std::vector<std::string> &args) override {
+ key_name_ = args[1];
+ inputs_.reserve(args.size() - 2);
+ for (size_t i = 2; i < args.size(); i++) {
Review Comment:
Would it be better to unique the input string here and then parse to double?
After unique, we don't need to use the below `map` to group them and it will
keep the result stable basing on the command's inoput.
##########
src/types/tdigest.h:
##########
@@ -150,3 +152,93 @@ inline StatusOr<double> TDigestQuantile(TD&& td, double q)
{
diff /= (lc.weight / 2 + rc.weight / 2);
return Lerp(lc.mean, rc.mean, diff);
}
+
+inline void AssignRankForEqualInputs(const std::vector<size_t>& indices,
double cumulative_weight,
+ std::vector<int>& result) {
+ for (auto index : indices) {
+ result[index] = static_cast<int>(cumulative_weight);
+ }
+}
+
+inline int DoubleCompare(double a, double b, double rel_eps = 1e-12, double
abs_eps = 1e-9) {
+ double diff = a - b;
+ double adiff = std::abs(diff);
+ if (adiff <= abs_eps) return 0;
+ double maxab = std::max(std::abs(a), std::abs(b));
+ if (adiff <= maxab * rel_eps) return 0;
+ return (diff < 0) ? -1 : 1;
+}
+
+inline bool DoubleEqual(double a, double b, double rel_eps = 1e-12, double
abs_eps = 1e-9) {
+ return DoubleCompare(a, b, rel_eps, abs_eps) == 0;
+}
+
+struct DoubleComparator {
+ bool operator()(const double& a, const double& b) const { return
DoubleCompare(a, b) == -1; }
+};
+
+template <typename TD>
+inline Status TDigestRevRank(TD&& td, const std::vector<double>& inputs,
std::vector<int>& result) {
+ std::map<double, std::vector<size_t>, DoubleComparator> value_to_indices;
+ for (size_t i = 0; i < inputs.size(); ++i) {
+ value_to_indices[inputs[i]].push_back(i);
+ }
+
+ double cumulative_weight = 0;
+ result.resize(inputs.size());
Review Comment:
```suggestion
result.clear();
result.resize(inputs.size(), -2);
```
Maybe it would be better to us `-2` to init the result and add a guard to
validate the result.
##########
src/types/tdigest.h:
##########
@@ -150,3 +152,93 @@ inline StatusOr<double> TDigestQuantile(TD&& td, double q)
{
diff /= (lc.weight / 2 + rc.weight / 2);
return Lerp(lc.mean, rc.mean, diff);
}
+
+inline void AssignRankForEqualInputs(const std::vector<size_t>& indices,
double cumulative_weight,
Review Comment:
Would it be better that we only allow the `Status TDigestRevRank(TD&& td,
const std::vector<double>& inputs, std::vector<int>& result)` in this file only
allows unique inputs?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]