-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.h
More file actions
178 lines (147 loc) · 4.12 KB
/
utils.h
File metadata and controls
178 lines (147 loc) · 4.12 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#pragma once
#include <iterator>
namespace ft {
template<class InputIterator1, class InputIterator2>
bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2) {
while (first1 != last1) {
if (!(*first1 == *first2)) // or: if (!pred(*first1,*first2)), for version 2
return false;
++first1;
++first2;
}
return true;
}
template<class InputIterator1, class InputIterator2>
bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2) {
while (first1 != last1) {
if (first2 == last2 || *first2 < *first1) return false;
else if (*first1 < *first2) return true;
++first1;
++first2;
}
return (first2 != last2);
}
template<class iterator>
iterator prev(iterator it) {
return --it;
}
template<class iterator>
iterator next(iterator it) {
return ++it;
}
template<class T>
struct remove_const {
typedef T type;
};
template<class T>
struct remove_const<const T> {
typedef T type;
};
template<bool B, class T = void>
struct enable_if {};
template<class T>
struct enable_if<true, T> { typedef T type; };
template<typename Iter>
struct iterator_traits {
typedef typename Iter::iterator_category iterator_category;
typedef typename Iter::value_type value_type;
typedef typename Iter::difference_type difference_type;
typedef typename Iter::pointer pointer;
typedef typename Iter::reference reference;
};
template<typename T>
struct iterator_traits<T *> {
typedef ptrdiff_t difference_type;
typedef typename remove_const<T>::type value_type;
typedef T *pointer;
typedef T &reference;
typedef std::random_access_iterator_tag iterator_category;
};
template<class T>
struct is_integral_helper {
const static bool value = false;
};
template<>
struct is_integral_helper<bool> {
const static bool value = true;
};
template<>
struct is_integral_helper<int> {
const static bool value = true;
};
template<>
struct is_integral_helper<long> {
const static bool value = true;
};
template<>
struct is_integral_helper<long long> {
const static bool value = true;
};
template<>
struct is_integral_helper<char> {
const static bool value = true;
};
template<>
struct is_integral_helper<short> {
const static bool value = true;
};
template<>
struct is_integral_helper<unsigned int> {
const static bool value = true;
};
template<>
struct is_integral_helper<unsigned long> {
const static bool value = true;
};
template<>
struct is_integral_helper<unsigned long long> {
const static bool value = true;
};
template<>
struct is_integral_helper<unsigned char> {
const static bool value = true;
};
template<>
struct is_integral_helper<unsigned short> {
const static bool value = true;
};
template<class T>
struct is_integral {
const static bool value = is_integral_helper<typename remove_const<T>::type>::value;
};
template<typename T, typename U>
struct is_same {
static const bool value = false;
};
template<typename T>
struct is_same<T, T> {
static const bool value = true;
};
template<typename Iterator>
typename iterator_traits<Iterator>::difference_type
distance_helper(const Iterator &first, const Iterator &last, std::random_access_iterator_tag) {
return last - first;
}
template<typename Iterator>
typename iterator_traits<Iterator>::difference_type
distance_helper(const Iterator &first, const Iterator &last, std::input_iterator_tag) {
Iterator it = first;
typename iterator_traits<Iterator>::difference_type size = 0;
while (it++ != last)
size++;
return size;
}
template<typename Iterator>
ptrdiff_t distance(const Iterator &first, const Iterator &last) {
return distance_helper(first, last, typename iterator_traits<Iterator>::iterator_category());
}
template<class T>
struct is_const {
static const bool value = false;
};
template<class T>
struct is_const<const T> {
static const bool value = true;
};
}