Merge pull request #34292 from cbodley/wip-bufferlist-comparison

bufferlist: use iterators for comparison operators

Reviewed-by: Kefu Chai <kchai@redhat.com>
This commit is contained in:
Kefu Chai 2020-04-27 11:38:16 +08:00 committed by GitHub
commit d9ca5e5d7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 30 deletions

View File

@ -1230,44 +1230,40 @@ inline namespace v15_2_0 {
}
};
inline bool operator>(const bufferlist& l, const bufferlist& r) {
for (unsigned p = 0; ; p++) {
if (l.length() > p && r.length() == p) return true;
if (l.length() == p) return false;
if (l[p] > r[p]) return true;
if (l[p] < r[p]) return false;
}
}
inline bool operator>=(const bufferlist& l, const bufferlist& r) {
for (unsigned p = 0; ; p++) {
if (l.length() > p && r.length() == p) return true;
if (r.length() == p && l.length() == p) return true;
if (l.length() == p && r.length() > p) return false;
if (l[p] > r[p]) return true;
if (l[p] < r[p]) return false;
}
inline bool operator==(const bufferlist &lhs, const bufferlist &rhs) {
if (lhs.length() != rhs.length())
return false;
return std::equal(lhs.begin(), lhs.end(), rhs.begin());
}
inline bool operator==(const bufferlist &l, const bufferlist &r) {
if (l.length() != r.length())
return false;
for (unsigned p = 0; p < l.length(); p++) {
if (l[p] != r[p])
return false;
inline bool operator<(const bufferlist& lhs, const bufferlist& rhs) {
auto l = lhs.begin(), r = rhs.begin();
for (; l != lhs.end() && r != rhs.end(); ++l, ++r) {
if (*l < *r) return true;
if (*l > *r) return false;
}
return true;
return (l == lhs.end()) && (r != rhs.end()); // lhs.length() < rhs.length()
}
inline bool operator<=(const bufferlist& lhs, const bufferlist& rhs) {
auto l = lhs.begin(), r = rhs.begin();
for (; l != lhs.end() && r != rhs.end(); ++l, ++r) {
if (*l < *r) return true;
if (*l > *r) return false;
}
return l == lhs.end(); // lhs.length() <= rhs.length()
}
inline bool operator!=(const bufferlist &l, const bufferlist &r) {
return !(l == r);
}
inline bool operator<(const bufferlist& l, const bufferlist& r) {
return r > l;
inline bool operator>(const bufferlist& lhs, const bufferlist& rhs) {
return rhs < lhs;
}
inline bool operator<=(const bufferlist& l, const bufferlist& r) {
return r >= l;
inline bool operator>=(const bufferlist& lhs, const bufferlist& rhs) {
return rhs <= lhs;
}
std::ostream& operator<<(std::ostream& out, const buffer::ptr& bp);
std::ostream& operator<<(std::ostream& out, const buffer::raw &r);

View File

@ -2540,8 +2540,9 @@ TEST(BufferList, crc32c_append_perf) {
TEST(BufferList, compare) {
bufferlist a;
a.append("A");
bufferlist ab;
ab.append("AB");
bufferlist ab; // AB in segments
ab.append(bufferptr("A", 1));
ab.append(bufferptr("B", 1));
bufferlist ac;
ac.append("AC");
//