-
Notifications
You must be signed in to change notification settings - Fork 35
/
vec.h
102 lines (89 loc) · 3.37 KB
/
vec.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
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* Part of WinLamb - Win32 API Lambda Library
* https://github.com/rodrigocfd/winlamb
* Copyright 2017-present Rodrigo Cesar de Freitas Dias
* This library is released under the MIT License
*/
#pragma once
#include <algorithm>
#include <vector>
namespace wl {
// Utilities to std::vector.
namespace vec {
// Wrapper to std::for_each. Iterates over all elements, executing the lambda for each one.
template<typename T, typename predicateT>
inline void for_each(const std::vector<T>& v, predicateT&& func) {
// vector<wstring> ss = {L"a", L"b", L"c"};
// vec::for_each(ss, [](const wstring& c) -> void {
// size_t sz = c.length();
// });
std::for_each(v.cbegin(), v.cend(), std::forward<predicateT>(func));
}
// Wrapper to std::for_each. Iterates over all elements, executing the lambda for each one.
template<typename T, typename predicateT>
inline void for_each(std::vector<T>& v, predicateT&& func) {
// vector<wstring> ss = {L"a", L"b", L"c"};
// vec::for_each(ss, [](wstring& c) -> void {
// c.append(L"x");
// });
std::for_each(v.begin(), v.end(), std::forward<predicateT>(func));
}
// Wrapper to std::find. Returns index of first element which is equal to value, otherwise -1.
template<typename T>
inline size_t find(const std::vector<T>& v, const T& value) {
typename std::vector<T>::const_iterator it = std::find(v.cbegin(), v.cend(), value);
return it == v.cend() ? -1 : it - v.cbegin();
}
// Wrapper to std::find_if. Returns index of first element which is equal to value, otherwise -1.
template<typename T, typename predicateT>
inline size_t find_if(const std::vector<T>& v, predicateT&& func) {
// Usage example:
// vector<wstring> ss = {L"a", L"b", L"c"};
// vec::find_if(ss, [](const wstring& c) -> bool {
// return c == L"a";
// });
typename std::vector<T>::const_iterator it = std::find_if(v.cbegin(), v.cend(),
std::forward<predicateT>(func));
return it == v.cend() ? -1 : it - v.cbegin();
}
// Wrapper to std::find. Returns true if element exists.
template<typename T>
inline bool exists(const std::vector<T>& v, const T& value) {
return find(v, value) != -1;
}
// Wrapper to std::find_if. Returns true if predicate matches an element.
template<typename T, typename predicateT>
inline bool exists_if(const std::vector<T>& v, predicateT&& func) {
// Usage example:
// vector<wstring> ss = {L"a", L"b", L"c"};
// vec::exists_if(ss, [](const wstring& c) -> bool {
// return c == L"a";
// });
return find_if(v, std::forward<predicateT>(func)) != -1;
}
// Appends a vector onto another.
template<typename T>
inline void append(std::vector<T>& v, const std::vector<T>& other) {
v.insert(v.end(), other.cbegin(), other.cend());
}
// Removes an element by index.
template<typename T>
inline void remove(std::vector<T>& v, size_t index) {
v.erase(v.begin() + index);
}
// Wrapper to std::remove_if. Removes all elements which match the predicate.
template<typename T, typename predicateT>
inline void remove_if(std::vector<T>& v, predicateT&& func) {
// https://stackoverflow.com/a/9053941/6923555
// Usage example:
// vector<wstring> ss = {L"a", L"b", L"c"};
// vec::remove_if(ss, [](const wstring& c) -> bool {
// return c == L"a";
// });
v.erase(
std::remove_if(v.begin(), v.end(), std::forward<predicateT>(func)),
v.end()
);
}
}//namespace vec
}//namespace wl