-
Notifications
You must be signed in to change notification settings - Fork 1
/
algorithm.h
89 lines (75 loc) · 2.17 KB
/
algorithm.h
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
#pragma once
#include "cstdlib/cstdint.h"
namespace firefly::std {
template <class ForwardIt, class T>
void fill(ForwardIt first, const ForwardIt last, const T& value) {
for (; first != last; ++first) {
*first = value;
}
}
template <class InputIt, class OutputIt>
OutputIt copy(InputIt first, const InputIt last, OutputIt d_first) {
while (first != last) {
*d_first++ = *first++;
}
return d_first;
}
template <typename InputIt, typename OutputIt, typename UnaryPredicate>
OutputIt copy_if(InputIt begin, const InputIt end, OutputIt write,
UnaryPredicate func) {
while (begin < end) {
if (! func(*begin)) {
begin++;
continue;
}
*(write++) = *(begin++);
}
return write;
}
template <typename InputIt>
InputIt max_element(InputIt first, InputIt last) {
if (first == last)
return last;
InputIt largest = first;
++first;
for (; first != last; ++first) {
if (*largest < *first) {
largest = first;
}
}
return largest;
}
template <typename InputIt, typename UnaryPredicate>
size_t count_if(InputIt first, const InputIt last,
UnaryPredicate func) {
size_t count = 0;
for (; first != last; first++) {
if (func(*first))
count++;
}
return count;
}
template <typename InputIt, typename T> size_t count(InputIt first, const InputIt last,
const T& lval) {
size_t count = 0;
for (; first != last; first++) {
if (*first == lval)
count++;
}
return count;
}
/*
template <class RandomIt>
void sort(RandomIt first, RandomIt last) {
if (first < last) {
int partitionIndex = partition(first, last);
(void) partitionIndex;
}
}
*/
/*
template <class RandomIt, class Compare>
void sort(RandomIt first, RandomIt last, Compare comp) {
}
*/
} // namespace firefly::std