Skip to content

Commit b6074af

Browse files
committed
refactor(tdigest): refactor the GetCbeginIter and GetCendIter to detail inline functions
1 parent 4bdb79b commit b6074af

1 file changed

Lines changed: 32 additions & 29 deletions

File tree

src/types/redis_tdigest.h

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,26 @@
3434

3535
namespace redis {
3636

37+
namespace detail {
38+
template <bool Reverse, typename Container>
39+
inline decltype(auto) GetCbeginIter(const Container& centroids) {
40+
if constexpr (Reverse) {
41+
return centroids.crbegin();
42+
} else {
43+
return centroids.cbegin();
44+
}
45+
}
46+
47+
template <bool Reverse, typename Container>
48+
inline decltype(auto) GetCendIter(const Container& centroids) {
49+
if constexpr (Reverse) {
50+
return centroids.crend();
51+
} else {
52+
return centroids.cend();
53+
}
54+
}
55+
} // namespace detail
56+
3757
// TODO: It should be replaced by a iteration of the rocksdb iterator
3858
template <bool Reverse>
3959
class DummyCentroids {
@@ -46,66 +66,49 @@ class DummyCentroids {
4666
std::vector<Centroid>::const_iterator>;
4767
Iterator(IterType iter, const std::vector<Centroid>& centroids) : iter_(iter), centroids_(centroids) {}
4868
std::unique_ptr<Iterator> Clone() const {
49-
if (iter_ != GetCendIter(centroids_)) {
50-
return std::make_unique<Iterator>(
51-
std::next(GetCbeginIter(centroids_), std::distance(GetCbeginIter(centroids_), iter_)), centroids_);
69+
if (iter_ != detail::GetCendIter<Reverse>(centroids_)) {
70+
return std::make_unique<Iterator>(std::next(detail::GetCbeginIter<Reverse>(centroids_),
71+
std::distance(detail::GetCbeginIter<Reverse>(centroids_), iter_)),
72+
centroids_);
5273
}
53-
return std::make_unique<Iterator>(GetCendIter(centroids_), centroids_);
74+
return std::make_unique<Iterator>(detail::GetCendIter<Reverse>(centroids_), centroids_);
5475
}
5576
bool Next() {
5677
if (Valid()) {
5778
std::advance(iter_, 1);
5879
}
59-
return iter_ != GetCendIter(centroids_);
80+
return iter_ != detail::GetCendIter<Reverse>(centroids_);
6081
}
6182

6283
// The Prev function can only be called for item is not cend,
6384
// because we must guarantee the iterator to be inside the valid range before iteration.
6485
bool Prev() {
65-
if (Valid() && iter_ != GetCendIter(centroids_)) {
86+
if (Valid() && iter_ != detail::GetCendIter<Reverse>(centroids_)) {
6687
std::advance(iter_, -1);
6788
}
6889
return Valid();
6990
}
70-
bool Valid() const { return iter_ != GetCendIter(centroids_); }
91+
bool Valid() const { return iter_ != detail::GetCendIter<Reverse>(centroids_); }
7192
StatusOr<Centroid> GetCentroid() const {
72-
if (iter_ == GetCendIter(centroids_)) {
93+
if (iter_ == detail::GetCendIter<Reverse>(centroids_)) {
7394
return {::Status::NotOK, "invalid iterator during decoding tdigest centroid"};
7495
}
7596
return *iter_;
7697
}
7798

78-
template <typename Container>
79-
static decltype(auto) GetCbeginIter(const Container& centroids) {
80-
if constexpr (Reverse) {
81-
return centroids.crbegin();
82-
} else {
83-
return centroids.cbegin();
84-
}
85-
}
86-
87-
template <typename Container>
88-
static decltype(auto) GetCendIter(const Container& centroids) {
89-
if constexpr (Reverse) {
90-
return centroids.crend();
91-
} else {
92-
return centroids.cend();
93-
}
94-
}
95-
9699
private:
97100
IterType iter_;
98101
const std::vector<Centroid>& centroids_;
99102
};
100103

101104
std::unique_ptr<Iterator> Begin() const {
102-
return std::make_unique<Iterator>(Iterator::GetCbeginIter(centroids_), centroids_);
105+
return std::make_unique<Iterator>(detail::GetCbeginIter<Reverse>(centroids_), centroids_);
103106
}
104107
std::unique_ptr<Iterator> End() const {
105108
if (centroids_.empty()) {
106-
return std::make_unique<Iterator>(Iterator::GetCendIter(centroids_), centroids_);
109+
return std::make_unique<Iterator>(detail::GetCendIter<Reverse>(centroids_), centroids_);
107110
}
108-
return std::make_unique<Iterator>(std::prev(Iterator::GetCendIter(centroids_)), centroids_);
111+
return std::make_unique<Iterator>(std::prev(detail::GetCendIter<Reverse>(centroids_)), centroids_);
109112
}
110113
double TotalWeight() const { return static_cast<double>(meta_data_.total_weight); }
111114
double Min() const { return meta_data_.minimum; }

0 commit comments

Comments
 (0)