-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.h
More file actions
145 lines (125 loc) · 5.53 KB
/
Copy pathalgorithms.h
File metadata and controls
145 lines (125 loc) · 5.53 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
140
141
142
143
144
/**
* @brief wrappers over standard algorithms, to minimize code size, and make more readable
* @brief cross-platform one-file C++ header
*
* @author Sergey Masuryan
* Contact Telegram: @SergeyJames
*
*/
#pragma once
#include <algorithm>
namespace wrp {
/**
* @brief : сheck if container contains specific element
* @required :
* @complexity : at most last - first applications of the predicate
* @return value :
* false if the range [first, last) is empty, or no such element is found in range [first, last).
* true if the range [first, last) contains const Ty& Val.
**/
template<class Init, class Ty>
inline bool contains(Init _begin, Init _end, const Ty& Val) noexcept {
return std::find(_begin, _end, Val) != _end;
}
/**
* @brief : сheck if container contains specific element
* @required :
* @complexity : at most last - first applications of the predicate
* @return value :
* false if the range [first, last) is empty, or no such element is found in range [first, last).
* true if the range [first, last) contains const Ty& Val.
**/
template<class C, class Ty>
inline bool contains(const C & c, const Ty& Val) noexcept {
return contains(std::begin(c), std::end(c), Val);
}
/**
* @brief : сheck if container contains specific element (use move semantic )
* @required :
* @complexity : At most last - first applications of the predicate
* @return value :
* false if the range [first, last) is empty, or no such element is found in range [first, last).
* true if the range [first, last) contains const Ty& Val.
**/
template<class C, class Ty>
inline bool contains(const C & c, Ty&& Val) noexcept {
return contains(std::begin(c), std::end(c), std::forward<Ty>(Val));
}
/**
* @brief : сheck if container contains specific element
* @required :
* @complexity : At most last - first applications of the predicate
* @return value :
* false if the range [first, last) is empty, or no such element predicate pred returns true in range [first, last).
* true if the range [first, last) an element for which predicate pred returns true
**/
template<class Init, class Pr>
inline bool contains_if(Init _begin, Init _end, Pr pred) noexcept {
return std::find_if(_begin, _end, pred) != _end;
}
/**
* @brief : сheck if container contains specific element
* @required :
* @complexity : At most last - first applications of the predicate
* @return value :
* false if the range [first, last) is empty, or no such element predicate pred returns true in range [first, last).
* true if the range [first, last) an element for which predicate pred returns true
**/
template<class C, class Pr>
inline bool contains_if(const C & c, Pr pred) noexcept {
return std::find_if(std::begin(c), std::end(c), pred) != std::end(c);
}
/**
* @brief :
* @required : unsorted dynamic contiguous sequence containers supporting 'Random Access Iterator' like std::vector or std::deque
* @complexity : Amortized constant.
* @return value : (void)
**/
template<class C>
inline void quik_remove_at(C& c, std::size_t idx) noexcept {
if (idx < c.size()) {
c[idx] = std::move(c.back());
c.pop_back();
}
}
/**
* @required : sorted container (class C)
* @complexity :
* 1-2) Logarithmic in the size of the container, O(log(size())).
* 3-4) Amortized constant if the insertion happens in the position just after the hint, logarithmic in the size of the container otherwise.(until C++11)
* 3-4) Amortized constant if the insertion happens in the position just before the hint, logarithmic in the size of the container otherwise. (since C++11)
* 5-6) O(N*log(size() + N)), where N is the number of elements to insert.
* 7) Logarithmic in the size of the container, O(log(size())).
* 8) Amortized constant if the insertion happens in the position just before the hint, logarithmic in the size of the container otherwise.
* @return value : (void)
**/
template<class C, class T>
inline void insert_sorted(C & c, const T& item) {
c.insert(std::lower_bound(c.begin(), c.end(), item), item);
}
/**
* @required : sorted container (class C)
* @complexity :
* 1-2) Logarithmic in the size of the container, O(log(size())).
* 3-4) Amortized constant if the insertion happens in the position just after the hint, logarithmic in the size of the container otherwise.(until C++11)
* 3-4) Amortized constant if the insertion happens in the position just before the hint, logarithmic in the size of the container otherwise. (since C++11)
* 5-6) O(N*log(size() + N)), where N is the number of elements to insert.
* 7) Logarithmic in the size of the container, O(log(size())).
* 8) Amortized constant if the insertion happens in the position just before the hint, logarithmic in the size of the container otherwise.
* @return value : (void)
**/
template<class C, class T>
inline void insert_sorted(C & c, T&& item) {
c.insert(std::lower_bound(c.begin(), c.end(), item), std::forward<T>(item));
}
/**
* @since : C++14 (and higer).
* @complexity : For nonempty ranges, exactly std::distance(first,last) -1 applications of the corresponding predicate.
* @return value : Forward iterator to the new end of the range
* @usage : Erase-remove idiom.
**/
template<class FwdIt>
inline FwdIt remove_multi_wshitespaces(FwdIt begin, FwdIt end) noexcept {
return std::unique(begin, end, [](const auto& a, const auto& b) { return isspace(a) && isspace(b); });
}
} // !namespace wrp