-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.h
More file actions
139 lines (114 loc) · 3.36 KB
/
Copy pathutil.h
File metadata and controls
139 lines (114 loc) · 3.36 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
#ifndef UTIL_H
#define UTIL_H
#include <cmath>
#include <deque>
#include <iostream>
#include <vector>
// template <typename T>
// T find_optimum(const std::vector<T> &array,
// bool (*comp)(const T &a, const T &b)) { // first class function
// T opt { array[0] };
// for (size_t i { 1 }; i < array.size(); i++)
// if (comp(array[i], opt))
// opt = array[i];
// return opt;
// }
// template<typename T>
// bool smaller(const T &a, const T &b) { // callback
// return a < b;
// }
// // bool smaller(const long &a, const long &b) { // callback
// // return a < b;
// // }
// bool larger(const long &a, const long &b) {
// return a > b;
// }
template <class T> using Comparison = bool (*)(const T &a, const T &b);
// template <typename T>
// T find_optimum(const std::vector<T> &array, Comparison<T> comp) { // first class function
// T opt { array[0] };
// for (size_t i { 1 }; i < array.size(); i++)
// if (comp(array[i], opt))
// opt = array[i];
// return opt;
// }
// template <typename T>
// const T *find_optimum(const std::vector<T> &array, Comparison<T> comp) { // first class function
// const T *opt { &array[0] };
// for (size_t i { 1 }; i < array.size(); i++)
// if (comp(array[i], *opt))
// opt = &array[i];
// return opt;
// }
// struct Less {
// bool operator()(const long &a, const long &b) {
// return a < b;
// }
// };
template <typename T> struct Less {
bool operator()(const T &a, const T &b) {
return a < b;
}
};
// struct Nearer {
// Nearer(int _X) : X { _X }{}
// bool operator()(const long &a, const long &b) {
// return std::abs(a-X) < std::abs(b-X);
// }
// int X;
// };
template <typename T> struct Nearer {
Nearer(double _X) : X { _X } {
}
bool operator()(const T &a, const T &b) {
return std::abs(a - X) < std::abs(b - X);
}
double X;
};
// template <typename T, typename Comp>
// const T *find_optimum(const std::vector<T> &array, Comp comp) { // first class function
// const T *opt { &array[0] };
// for (size_t i { 1 }; i < array.size(); i++)
// if (comp(array[i], *opt))
// opt = &array[i];
// return opt;
// }
template <typename Iter, typename Comp>
Iter find_optimum(const Iter start, const Iter end, Comp comp) { // first class function
if (start == end)
return start;
Iter opt { start };
for (Iter it { start + 1 }; it != end; it++)
if (comp(*it, *opt))
opt = it;
return opt;
}
template <class T> bool smaller(const T &a, const T &b) { // callback
return a < b;
}
// bool smaller(const long &a, const long &b) { // callback
// return a < b;
// }
bool larger(const long &a, const long &b) {
return a > b;
}
template <typename Iter> void disp(Iter begin, Iter end) {
for (Iter iter { begin }; iter != end; iter++)
std::cout << *iter << " ";
std::cout << std::endl;
}
// void disp(std::deque<int>::iterator begin, std::deque<int>::iterator end) {
// for (auto iter { begin }; iter != end; iter++)
// std::cout << *iter << " ";
// std::cout << std::endl;
// }
struct Point {
double X, Y;
double mag() const {
return std::sqrt(X * X + Y * Y);
}
// bool operator<(const Point &p) const {
// return mag() < p.mag();
// }
};
#endif