-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyalgorithm.hpp
181 lines (169 loc) · 5.59 KB
/
myalgorithm.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#pragma once
#ifndef __MY_ALGORITHM_HPP
#define __MY_ALGORITHM_HPP
#include <iostream>
#include <string.h>
#include "myIterator.hpp"
#include "myTrait.hpp"
namespace MySTL
{
template <typename RandomIterator, typename OutputIterator, typename Distance>
OutputIterator __copy_d(RandomIterator first, RandomIterator end,
OutputIterator result, Distance *) {
std::cout << "__copy_d (RandomIterator first, RandomIterator end,OutputIterator result, Distance *"
<< std::endl;
Distance n = end - first;
while (n > 0) {
*result = *first;
++result;
++first;
--n;
}
return result;
}
template<typename T>
T* __copy_t(const T* first, const T* end, T* result, __false_type) {
std::cout << "__copy_t(const T* first, const T* end, T* result, __false_type)"
<< std::endl;
return __copy_d(first, end, result, (ptrdiff_t*)0);
}
template<typename T>
T* __copy_t(const T* first, const T* end, T* result, __true_type) {
std::cout << "__copy_t(const T* first, const T* end, T* result, __false_type)"
<< std::endl;
memove(result, first, (end - first) * sizeof(T));
return result + end - first;
}
template<typename InputIterator, typename OutputIterator>
OutputIterator __copy(InputIterator first, InputIterator end, OutputIterator result,
input_iterator_tag) {
std::cout << "__copy(InputIterator first, InputIterator end, OutputIterator result,input_iterator_tag)"
<< std::endl;
while (first != end) {
*result = *first;
++first;
++result;
}
return result;
}
template<typename RandomIterator, typename OutputIterator>
OutputIterator __copy(RandomIterator first, RandomIterator end, OutputIterator result,
random_access_iterator_tag) {
return __copy_d(first, end, result, difference_type(first));
}
template<typename InputIterator, typename OutputIterator>
struct __copy_dispatch {
OutputIterator operator()(InputIterator first, InputIterator end,
OutputIterator result) {
return __copy(first, end, result, iterator_category(first));
}
};
template<typename T>
struct __copy_dispatch<T*, T*> {
T* operator()(T* first, T* end, T* result) {
typedef typename __true_traits<T>::has_trivial_assignment_operator has_trivial_assignment;
return __copy_t(first, end, result, has_trivial_assignment());
}
};
template<typename T>
struct __copy_dispatch<const T*, T*>
{
T* operator()(const T* first, const T* end, T* result) {
typedef typename __true_traits<T>::has_trivial_assignment_operator has_trivial_assignment;
return __copy_t(first, end, result, has_trivial_assignment());
}
};
template<typename InputIterator, typename OutputIterator>
OutputIterator copy(InputIterator first, InputIterator end, OutputIterator result) {
return __copy_dispatch<InputIterator, OutputIterator>()(first, end, result);
}
inline char* copy(const char *first, const char* end, char* result) {
memmove(result, first, (end - first) * sizeof(char));
return result + (end - first);
}
inline wchar_t* copy(const wchar_t *first, const wchar_t* end, wchar_t* result) {
memmove(result, first, (end - first) * sizeof(wchar_t));
return result + (end - first);
}
template<typename InputIterator, typename OutputIterator, typename Distance>
OutputIterator __copy_d_backward(InputIterator first, InputIterator end,
OutputIterator result, Distance *) {
Distance n = end - first;
while (n > 0) {
*(result - 1) = *(end - 1);
--result;
--end;
--n;
}
return result;
}
template<typename InputIterator, typename OutputIterator, typename Distance>
OutputIterator __copy_t_backward(InputIterator first, InputIterator end,
OutputIterator result, input_iterator_tag) {
while (end != first) {
*(result - 1) = *(end - 1);
--result;
--end;
}
return result;
}
template<typename InputIterator, typename OutputIterator, typename Distance>
OutputIterator __copy_t_backward(InputIterator first, InputIterator end,
OutputIterator result, random_access_iterator_tag) {
return __copy_d_backward(first, end, result, difference_type(first));
}
template<typename InputIterator, typename OutputIterator>
struct __copy_dispatch_backward {
OutputIterator operator()(InputIterator first, InputIterator end, OutputIterator result) {
return __copy_t_backward(first, end, result, iterator_category(first));
}
};
template<typename T>
struct __copy_dispatch_backward<T*, T*> {
T* operator()(T* first, T* end, T* result) {
return __copy_d_backward(first, end, result, (ptrdiff_t*)(0));
}
};
template<typename T>
struct __copy_dispatch_backward<const T*, T*> {
T* operator()(const T* first, const T* end, T* result) {
return __copy_d_backward(first, end, result, (ptrdiff_t*)(0));
}
};
template<typename OutputIterator, typename InputIterator>
OutputIterator copy_backward(InputIterator first, InputIterator end, OutputIterator result) {
return __copy_dispatch_backward<InputIterator, OutputIterator>()(first, end, result);
}
template<typename OutputIterator, typename T>
void fill(OutputIterator first, OutputIterator end, const T& value) {
while (first != end) {
*first = value;
++first;
}
}
template<typename OutputIterator, typename Size, typename T>
OutputIterator fill_n(OutputIterator first, Size n, const T& value) {
while (n > 0) {
*first = value;
--n;
++first;
}
return first;
}
template<typename T>
T max(const T& a, const T& b) {
return a > b ? a : b;
}
template<typename InputIterator, typename T>
InputIterator find(InputIterator first, InputIterator end, const T& value) {
while (first != end && *first != value) ++first;
return first;
}
template<typename T>
void swap(T& a, T& b) {
T temp = a;
a = b;
b = temp;
}
}
#endif