-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.hpp
More file actions
96 lines (88 loc) · 1.87 KB
/
algorithm.hpp
File metadata and controls
96 lines (88 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#ifndef ALGORITHM_HPP
#define ALGORITHM_HPP
#include "iterator.hpp"
namespace ft
{
template <typename T>
void swap(T& a, T& b)
{
T tmp = a;
a = b;
b = tmp;
}
template <typename InputIterator1, typename InputIterator2>
bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)
{
while (first1 != last1)
{
if (*first1 != *first2)
{
return false;
}
++first1;
++first2;
}
return true;
}
template <typename InputIterator1, typename InputIterator2, typename BinaryPredicate>
bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate p)
{
while (first1 != last1)
{
if (!p(*first1 == *first2))
{
return false;
}
++first1;
++first2;
}
return true;
}
template <typename InputIterator1, typename InputIterator2>
bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2)
{
while (first1 != last1)
{
if (first2 == last2 || *first2 < *first1)
{
return false;
}
if (*first1 < *first2)
{
return true;
}
++first1;
++first2;
}
return (first2 != last2);
}
template <typename InputIterator1, typename InputIterator2, typename Compare>
bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2, Compare cmp)
{
while (first1 != last1)
{
if (first2 == last2 || cmp(*first2, *first1))
{
return false;
}
if (cmp(*first1, *first2))
{
return true;
}
++first1;
++first2;
}
return (first2 != last2);
}
template <typename InputIterator>
typename ft::iterator_traits<InputIterator>::difference_type difference(InputIterator first, InputIterator last)
{
typename ft::iterator_traits<InputIterator>::difference_type n;
n = 0;
for (; first != last; first++)
n++;
return n;
}
}
#endif