-
Notifications
You must be signed in to change notification settings - Fork 0
/
miscellaneous.hpp
165 lines (132 loc) · 4.8 KB
/
miscellaneous.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
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
#pragma once
//miscellaneous.hpp
# include <memory>
# include <cstddef>
# include <iostream>
# include <exception>
# include <functional>
# include <stddef.h>
#include <iterator>
namespace ft
{
//========================== Enable If ===========================//
//========================== ++++++++++++++++++++ ===========================//
template <class T>
struct is_integral { static const bool value = false;};
template <> struct
is_integral<bool> { static const bool value = true;};
template <>
struct is_integral<char> { static const bool value = true;};
template <>
struct is_integral<char16_t> { static const bool value = true;};
template <>
struct is_integral<char32_t> { static const bool value = true;};
template <>
struct is_integral<wchar_t> { static const bool value = true;};
template <>
struct is_integral<signed char> { static const bool value = true;};
template <>
struct is_integral<short int> { static const bool value = true;};
template <>
struct is_integral<int> { static const bool value = true;};
template <>
struct is_integral<long int> { static const bool value = true;};
template <>
struct is_integral<long long int> { static const bool value = true;};
template <>
struct is_integral<unsigned char> { static const bool value = true;};
template <>
struct is_integral<unsigned short int> { static const bool value = true;};
template <>
struct is_integral<unsigned int> { static const bool value = true;};
template <>
struct is_integral<unsigned long int> { static const bool value = true;};
template <>
struct is_integral<unsigned long long int> { static const bool value = true;};
template<bool B, class T = void>
struct enable_if
{ };
template<class T>
struct enable_if<true, T> { typedef T type; };
//========================== Relational Operators ===========================//
//========================== ++++++++++++++++++++ ===========================//
template <class InputIterator1, class InputIterator2>
bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2)
{
for(; first1 != last1 ; ++first1, ++first2)
{
if (first2 == last2 || *first2 < *first1)
return false;
else if (*first1 < *first2)
return true;
}
return (first2!=last2);
}
template <class InputIterator1, class InputIterator2, class Compare>
bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp)
{
for ( ; (first1 != last1) && (first2 != last2); ++first1, (void) ++first2) {
if (comp(*first1, *first2)) return true;
if (comp(*first2, *first1)) return false;
}
return ((first1 == last1) && (first2 != last2));
}
template <class InputIterator1, class InputIterator2>
bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
{
for (; first1!=last1; ++first1, ++first2)
if (!(*first1 == *first2))
return false;
return true;
}
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
bool equal (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred)
{
for (; first1!=last1; ++first1, ++first2)
if (!pred(*first1,*first2))
return false;
return true;
}
//========================== Iterator Traits ===========================//
//========================== ++++++++++++++++++++ ===========================//
template <class Category, class T, class Distance = ptrdiff_t, class Pointer = T*, class Reference = T&>
class iterator
{
public :
typedef T value_type;
typedef Distance difference_type;
typedef Pointer pointer;
typedef Reference reference;
typedef Category iterator_category;
};
template <class Iterator>
class iterator_traits
{
public :
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>
class iterator_traits<T*>
{
public :
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef std::random_access_iterator_tag iterator_category;
};
template <class T>
class iterator_traits<const T*>
{
public :
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef const T* pointer;
typedef const T& reference;
typedef std::random_access_iterator_tag iterator_category;
};
}