#pragma once
+#include <algorithm>
#include <iostream>
+#include <boost/iterator/counting_iterator.hpp>
+
#include "include/byteorder.h"
#include "crimson/common/layout.h"
}
const_iterator lower_bound(K l) const {
- auto ret = begin();
- for (; ret != end(); ++ret) {
- if (ret->get_key() >= l)
- break;
- }
- return ret;
+ auto it = std::lower_bound(boost::make_counting_iterator<uint16_t>(0),
+ boost::make_counting_iterator<uint16_t>(get_size()),
+ l,
+ [this](uint16_t i, K key) {
+ const_iterator iter(this, i);
+ return iter->get_key() < key;
+ });
+ return const_iterator(this, *it);
}
+
iterator lower_bound(K l) {
const auto &tref = *this;
return iterator(this, tref.lower_bound(l).offset);
}
const_iterator upper_bound(K l) const {
- auto ret = begin();
- for (; ret != end(); ++ret) {
- if (ret->get_key() > l)
- break;
- }
- return ret;
+ auto it = std::upper_bound(boost::make_counting_iterator<uint16_t>(0),
+ boost::make_counting_iterator<uint16_t>(get_size()),
+ l,
+ [this](K key, uint16_t i) {
+ const_iterator iter(this, i);
+ return key < iter->get_key();
+ });
+ return const_iterator(this, *it);
}
+
iterator upper_bound(K l) {
const auto &tref = *this;
return iterator(this, tref.upper_bound(l).offset);
}
const_iterator string_lower_bound(std::string_view str) const {
- uint16_t start = 0, end = get_size();
- while (start != end) {
- unsigned mid = (start + end) / 2;
- const_iterator iter(this, mid);
- std::string s = iter->get_key();
- if (s < str) {
- start = ++mid;
- } else if ( s > str) {
- end = mid;
- } else {
- return iter;
- }
- }
- return const_iterator(this, start);
+ auto it = std::lower_bound(boost::make_counting_iterator<uint16_t>(0),
+ boost::make_counting_iterator<uint16_t>(get_size()),
+ str,
+ [this](uint16_t i, std::string_view str) {
+ const_iterator iter(this, i);
+ return iter->get_key() < str;
+ });
+ return const_iterator(this, *it);
}
iterator string_lower_bound(std::string_view str) {
}
const_iterator string_upper_bound(std::string_view str) const {
- auto ret = iter_begin();
- for (; ret != iter_end(); ++ret) {
- std::string s = ret->get_key();
- if (s > str)
- break;
- }
- return ret;
+ auto it = std::upper_bound(boost::make_counting_iterator<uint16_t>(0),
+ boost::make_counting_iterator<uint16_t>(get_size()),
+ str,
+ [this](std::string_view str, uint16_t i) {
+ const_iterator iter(this, i);
+ return str < iter->get_key();
+ });
+ return const_iterator(this, *it);
}
iterator string_upper_bound(std::string_view str) {