1 | //===----------------------------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include <algorithm> |
10 | #include <cassert> |
11 | #include <iterator> |
12 | |
13 | #include "test_macros.h" |
14 | |
15 | template <class T> |
16 | struct Iterator { |
17 | using value_type = T; |
18 | using pointer = value_type*; |
19 | using difference_type = std::ptrdiff_t; |
20 | using iterator_category = std::forward_iterator_tag; |
21 | struct reference { |
22 | T* ptr_; |
23 | |
24 | reference(T* ptr) : ptr_(ptr) {} |
25 | |
26 | friend bool operator<(reference a, reference b) { return *a.ptr_ < *b.ptr_; } |
27 | friend bool operator<(reference a, value_type const& b) { return *a.ptr_ < b; } |
28 | friend bool operator<(value_type const& a, reference b) { return a < *b.ptr_; } |
29 | |
30 | operator T&() const; |
31 | }; |
32 | |
33 | Iterator& operator++() { |
34 | ptr_++; |
35 | return *this; |
36 | } |
37 | |
38 | Iterator operator++(int) { |
39 | Iterator tmp = *this; |
40 | ptr_++; |
41 | return tmp; |
42 | } |
43 | |
44 | friend bool operator==(Iterator const& a, Iterator const& b) { return a.ptr_ == b.ptr_; } |
45 | friend bool operator!=(Iterator const& a, Iterator const& b) { return !(a == b); } |
46 | |
47 | reference operator*() const { return reference(ptr_); } |
48 | |
49 | explicit Iterator(T* ptr) : ptr_(ptr) {} |
50 | Iterator() = default; |
51 | Iterator(Iterator const&) = default; |
52 | Iterator(Iterator&&) = default; |
53 | |
54 | Iterator& operator=(Iterator const&) = default; |
55 | Iterator& operator=(Iterator&&) = default; |
56 | |
57 | private: |
58 | T* ptr_; |
59 | }; |
60 | |
61 | int main(int, char**) { |
62 | int array[5] = {1, 2, 3, 4, 5}; |
63 | Iterator<int> first(array); |
64 | Iterator<int> middle(array + 3); |
65 | Iterator<int> last(array + 5); |
66 | (void)std::binary_search(first: first, last: last, val: 3); |
67 | (void)std::equal_range(first: first, last: last, val: 3); |
68 | (void)std::includes(first1: first, last1: last, first2: first, last2: last); |
69 | (void)std::is_sorted_until(first: first, last: last); |
70 | (void)std::is_sorted(first: first, last: last); |
71 | (void)std::lexicographical_compare(first1: first, last1: last, first2: first, last2: last); |
72 | (void)std::lower_bound(first: first, last: last, val: 3); |
73 | (void)std::max_element(first: first, last: last); |
74 | (void)std::min_element(first: first, last: last); |
75 | (void)std::minmax_element(first: first, last: last); |
76 | (void)std::upper_bound(first: first, last: last, val: 3); |
77 | |
78 | return 0; |
79 | } |
80 | |