-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathc_algo.c
373 lines (326 loc) · 8.42 KB
/
c_algo.c
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
*
* Copyright (c) 1994
* Hewlett-Packard Company
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Hewlett-Packard Company makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
*
* Copyright (c) 1996
* Silicon Graphics Computer Systems, Inc.
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Silicon Graphics makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*/
/*
Copyright (c) 2007 Wenbo Lao(Rob Lao)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
Wenbo Lao(Rob Lao)
viewpl(at)gmail.com
*/
#include "c_algo.h"
void c_iter_swap(c_iterator x, c_iterator y)
{
value_type tmp = ITER_REF(x);
ITER_REF_ASSIGN(x, ITER_REF(y));
ITER_REF_ASSIGN(y, tmp);
}
c_iterator c_copy(c_iterator first, c_iterator last, c_iterator result)
{
for(; !ITER_EQUAL(first, last); ITER_INC(first), ITER_INC(result))
ITER_REF_ASSIGN(result, ITER_REF(first));
return result;
}
c_iterator c_copy_backward(c_iterator first, c_iterator last, c_iterator result)
{
while(!ITER_EQUAL(first, last))
{
ITER_DEC(result);
ITER_DEC(last);
ITER_REF_ASSIGN(result, ITER_REF(last));
}
return result;
}
c_bool c_lexicographical_compare(c_iterator first1,
c_iterator last1,
c_iterator first2,
c_iterator last2,
COMPARER cmp)
{
for(; !ITER_EQUAL(first1, last1) &&
!ITER_EQUAL(first2, last2);
ITER_INC(first1), ITER_INC(first2))
{
if((*cmp)(ITER_REF(first1), ITER_REF(first2)) < 0)
return true;
if((*cmp)(ITER_REF(first1), ITER_REF(first2)) > 0)
return false;
}
return (ITER_EQUAL(first1, last1) && !ITER_EQUAL(first2, last2));
}
c_iterator c_uninitialized_copy(c_iterator first, c_iterator last, c_iterator result)
{
return c_copy(first, last, result);
}
void c_fill(c_iterator first, c_iterator last, const value_type val)
{
for(; !ITER_EQUAL(first, last); ITER_INC(first))
ITER_REF_ASSIGN(first, val);
}
c_iterator c_fill_n(c_iterator first, size_type n, const value_type val)
{
for(; n > 0; -- n, ITER_INC(first))
ITER_REF_ASSIGN(first, val);
return first;
}
c_iterator c_uninitialized_fill_n(c_iterator first, size_type n, const value_type val)
{
return c_fill_n(first, n, val);
}
c_bool c_equal(c_iterator first1, c_iterator last1, c_iterator first2, BINARY_PREDICATE pf)
{
for(; !ITER_EQUAL(first1, last1); ITER_INC(first1), ITER_INC(first2))
if(!(pf)(ITER_REF(first1), ITER_REF(first2)))
return false;
return true;
}
c_bool c_equal2(c_iterator first1, c_iterator last1, c_iterator first2, c_binary_predicate binary_pred)
{
for(; !ITER_EQUAL(first1, last1); ITER_INC(first1), ITER_INC(first2))
if(!(binary_pred.O)(&binary_pred, ITER_REF(first1), ITER_REF(first2)))
return false;
return true;
}
UNARY_FUNCTION c_for_each(c_iterator first, c_iterator last, UNARY_FUNCTION pf)
{
for(; !ITER_EQUAL(first, last); ITER_INC(first))
pf(ITER_REF(first));
return pf;
}
c_iterator c_find(c_iterator first, c_iterator last, const value_type val)
{
if(CHECK_RANDOM_ACCESS_ITERATOR(first))
{
difference_type tpc = ITER_DIFF(last, first) >> 2;
for(; tpc > 0; -- tpc)
{
if(ITER_REF(first) == val)
return first;
ITER_INC(first);
if(ITER_REF(first) == val)
return first;
ITER_INC(first);
if(ITER_REF(first) == val)
return first;
ITER_INC(first);
if(ITER_REF(first) == val)
return first;
ITER_INC(first);
}
switch(ITER_DIFF(last, first))
{
case 3:
if(ITER_REF(first) == val)
return first;
ITER_INC(first);
case 2:
if(ITER_REF(first) == val)
return first;
ITER_INC(first);
case 1:
if(ITER_REF(first) == val)
return first;
ITER_INC(first);
case 0:
default:
return last;
}
}
else
{
while(!ITER_EQUAL(first, last) && !(ITER_REF(first) == val))
ITER_INC(first);
return first;
}
}
c_iterator c_find_if(c_iterator first, c_iterator last, UNARY_PREDICATE pf)
{
if(CHECK_RANDOM_ACCESS_ITERATOR(first))
{
difference_type tpc = ITER_DIFF(last, first) >> 2;
for(; tpc > 0; -- tpc)
{
if(pf(ITER_REF(first)))
return first;
ITER_INC(first);
if(pf(ITER_REF(first)))
return first;
ITER_INC(first);
if(pf(ITER_REF(first)))
return first;
ITER_INC(first);
if(pf(ITER_REF(first)))
return first;
ITER_INC(first);
}
switch(ITER_DIFF(last, first))
{
case 3:
if(pf(ITER_REF(first)))
return first;
ITER_INC(first);
case 2:
if(pf(ITER_REF(first)))
return first;
ITER_INC(first);
case 1:
if(pf(ITER_REF(first)))
return first;
ITER_INC(first);
case 0:
default:
return last;
}
}
else
{
while(!ITER_EQUAL(first, last) && !pf(ITER_REF(first)))
ITER_INC(first);
return first;
}
}
c_iterator c_adjacent_find(c_iterator first, c_iterator last, BINARY_PREDICATE pf)
{
c_iterator next = first;
if(ITER_EQUAL(first, last))
return last;
while(!c_iter_equal(ITER_INC(next), last))
{
if(pf(ITER_REF(first), ITER_REF(next)))
return first;
first = next;
}
return last;
}
size_type c_count(c_iterator first, c_iterator last, const value_type val)
{
size_type n = 0;
for(; !ITER_EQUAL(first, last); ITER_INC(first))
if(ITER_REF(first) == val)
++ n;
return n;
}
size_type c_count_if(c_iterator first, c_iterator last, UNARY_PREDICATE pf)
{
size_type n = 0;
for(; !ITER_EQUAL(first, last); ITER_INC(first))
if(pf(ITER_REF(first)))
++ n;
return n;
}
void c_reverse(c_iterator first, c_iterator last)
{
if(CHECK_RANDOM_ACCESS_ITERATOR(first))
{
while(ITER_LESS(first, last))
{
c_iter_swap(first, ITER_DEC(last));
ITER_INC(first);
}
}
else
{
while(true)
if(ITER_EQUAL(first, last) ||
c_iter_equal(first, ITER_DEC(last)))
return;
else
{
c_iter_swap(first, last);
ITER_INC(first);
}
}
}
c_iterator c_search(c_iterator first1,
c_iterator last1,
c_iterator first2,
c_iterator last2,
BINARY_PREDICATE pf)
{
if(ITER_EQUAL(first1, last1) ||
ITER_EQUAL(first2, last2))
return first1;
if(true)
{
c_iterator tmp = first2;
ITER_INC(tmp);
if(ITER_EQUAL(tmp, last2))
{
while(!ITER_EQUAL(first1, last1) &&
!pf(ITER_REF(first1), ITER_REF(first2)))
ITER_INC(first1);
return first1;
}
}
if(true)
{
c_iterator p1, p;
c_iterator current = first1;
p1 = first2;
ITER_INC(p1);
while(!ITER_EQUAL(first1, last1))
{
while(!ITER_EQUAL(first1, last1))
{
if(pf(ITER_REF(first1), ITER_REF(first2)))
break;
ITER_INC(first1);
}
while(!ITER_EQUAL(first1, last1) &&
!pf(ITER_REF(first1), ITER_REF(first2)))
ITER_INC(first1);
if(ITER_EQUAL(first1, last1))
return last1;
p = p1;
current = first1;
ITER_INC(current);
if(ITER_EQUAL(current, last1))
return last1;
while(pf(ITER_REF(current), ITER_REF(p)))
{
ITER_INC(p);
if(ITER_EQUAL(p, last2))
return first1;
ITER_INC(current);
if(ITER_EQUAL(current, last1))
return last1;
}
ITER_INC(first1);
}
return first1;
}
}