MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
全部  文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
lower_bound.h
1/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
2miniob is licensed under Mulan PSL v2.
3You can use this software according to the terms and conditions of the Mulan PSL v2.
4You may obtain a copy of Mulan PSL v2 at:
5 http://license.coscl.org.cn/MulanPSL2
6THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
7EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
8MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9See the Mulan PSL v2 for more details. */
10
11//
12//
13// Created by Wangyunlai
14//
15
16#pragma once
17
18#include "common/lang/iterator.h"
19
20namespace common {
31template <typename ForwardIterator, typename T, typename Compare>
32ForwardIterator lower_bound(
33 ForwardIterator first, ForwardIterator last, const T &val, Compare comp, bool *_found = nullptr)
34{
35 bool found = false;
36 ForwardIterator iter;
37 const auto count = distance(first, last);
38 auto last_count = count;
39 while (last_count > 0) {
40 iter = first;
41 auto step = last_count / 2;
42 advance(iter, step);
43 int result = comp(*iter, val);
44 if (0 == result) {
45 first = iter;
46 found = true;
47 break;
48 }
49 if (result < 0) {
50 first = ++iter;
51 last_count -= step + 1;
52 } else {
53 last_count = step;
54 }
55 }
56
57 if (_found) {
58 *_found = found;
59 }
60 return first;
61}
62
63template <typename T>
65{
66public:
67 int operator()(const T &v1, const T &v2) const
68 {
69 if (v1 < v2) {
70 return -1;
71 }
72 if (v1 > v2) {
73 return 1;
74 }
75 return 0;
76 }
77};
78
79template <typename ForwardIterator, typename T>
80ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last, const T &val, bool *_found = nullptr)
81{
82 return lower_bound<ForwardIterator, T, Comparator<T>>(first, last, val, Comparator<T>(), _found);
83}
84
85// std::iterator is deprecated
86// refer to
87// https://www.fluentcpp.com/2018/05/08/std-iterator-deprecated/#:~:text=std%3A%3Aiterator%20is%20deprecated%2C%20so%20we%20should%20stop%20using,the%205%20aliases%20inside%20of%20your%20custom%20iterators.
88// a sample code:
89// https://github.com/google/googletest/commit/25208a60a27c2e634f46327595b281cb67355700
90template <typename T, typename Distance = ptrdiff_t>
92{
93public:
94 using iterator_category = random_access_iterator_tag;
95 using value_type = T;
96 using difference_type = Distance;
97 using pointer = value_type *;
98 using reference = value_type &;
99
100public:
101 BinaryIterator() = default;
102 BinaryIterator(size_t item_num, T *data) : item_num_(item_num), data_(data) {}
103
104 BinaryIterator &operator+=(int n)
105 {
106 data_ += (item_num_ * n);
107 return *this;
108 }
109 BinaryIterator &operator-=(int n) { return this->operator+(-n); }
110 BinaryIterator &operator++() { return this->operator+=(1); }
111 BinaryIterator operator++(int)
112 {
113 BinaryIterator tmp(*this);
114 this-> operator++();
115 return tmp;
116 }
117 BinaryIterator &operator--() { return this->operator+=(-1); }
118 BinaryIterator operator--(int)
119 {
120 BinaryIterator tmp(*this);
121 *this += -1;
122 return tmp;
123 }
124
125 bool operator==(const BinaryIterator &other) const { return data_ == other.data_; }
126 bool operator!=(const BinaryIterator &other) const { return !(this->operator==(other)); }
127
128 T *operator*() { return data_; }
129 T *operator->() { return data_; }
130
131 friend Distance operator-(const BinaryIterator &left, const BinaryIterator &right)
132 {
133 return (left.data_ - right.data_) / left.item_num_;
134 }
135
136private:
137 size_t item_num_ = 0;
138 T *data_ = nullptr;
139};
140} // namespace common
Definition: lower_bound.h:92
Definition: lower_bound.h:65