-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator_traits.hpp
124 lines (103 loc) · 3.79 KB
/
iterator_traits.hpp
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* iterator_traits.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariahi <ariahi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/23 11:53:26 by ariahi #+# #+# */
/* Updated: 2023/01/29 16:21:03 by ariahi ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ITERATOR_TRAIT_HPP
#define ITERATOR_TRAIT_HPP
#include <cstddef>
#include <iterator>
namespace ft
{
// It's a way to get the type of the iterator.
struct input_iterator_tag {};
struct output_iterator_tag {};
struct forward_iterator_tag : public input_iterator_tag {};
struct bidirectional_iterator_tag : public forward_iterator_tag {};
struct random_access_iterator_tag : public bidirectional_iterator_tag {};
// It's a template struct that is used to get the type of the iterator.
template<class Iterator>
struct iterator_traits
{
typedef typename Iterator::difference_type difference_type;
typedef typename Iterator::value_type value_type;
typedef typename Iterator::pointer pointer;
typedef typename Iterator::reference reference;
typedef typename Iterator::iterator_category iterator_category;
};
template<class T>
struct iterator_traits<T*>
{
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef ft::random_access_iterator_tag iterator_category;
};
template<class T>
struct iterator_traits<const T*>
{
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef const T* pointer;
typedef const T& reference;
typedef random_access_iterator_tag iterator_category;
};
template <class T, class Category, class Distance = ptrdiff_t, class Pointer = T*, class Reference = T&>
struct iterator
{
typedef T value_type;
typedef Distance difference_type;
typedef Pointer pointer;
typedef Reference reference;
typedef Category iterator_category;
};
template<class IT>
typename ft::iterator_traits<IT>::difference_type do_distance(IT first, IT last, ft::input_iterator_tag)
{
// It's a way to get the type of the iterator.
typename ft::iterator_traits<IT>::difference_type res = 0;
// It's a way to get the distance between the first and the last iterator.
while (first != last)
{
first++;
res += 1;
}
return (res);
}
template<class IT>
typename ft::iterator_traits<IT>::difference_type do_distance(IT first, IT last, ft::random_access_iterator_tag)
{
return (last - first);
}
// template<class IT>
// typename ft::iterator_traits<IT>::ifference_type do_distance(IT first, IT last, std::input_iterator_tag)
// {
// // It's a way to get the type of the iterator.
// typename std::iterator_traits<IT>::difference_type res = 0;
// // It's a way to get the distance between the first and the last iterator.
// while (first != last)
// {
// first++;
// res += 1;
// }
// return (res);
// }
// template<class IT>
// typename ft::iterator_traits<IT>::difference_type do_distance(IT first, IT last, ft::random_acces_iterator_tag)
// {
// return (last - first);
// }
template<class IT>
typename ft::iterator_traits<IT>::difference_type distance(IT first, IT last)
{
return (ft::do_distance(first, last, typename ft::iterator_traits<IT>::iterator_category()));
}
} // namespace ft
#endif