-
Notifications
You must be signed in to change notification settings - Fork 2
/
capi_algorithm.h
216 lines (190 loc) · 5.49 KB
/
capi_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
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
#ifndef PKGDEPDB_CAPI_ALGORITHM_H__
#define PKGDEPDB_CAPI_ALGORITHM_H__
#include <set>
#include <string>
#include <vector>
#include <algorithm>
template<class T, class STRLIST>
size_t pkgdepdb_strlist_count(const T& obj, STRLIST T::*member) {
return (obj.*member).size();
}
template<class STRLIST>
size_t pkgdepdb_strlist_get(const STRLIST& lst, const char **out, size_t off,
size_t count)
{
auto size = lst.size();
if (off >= size)
return 0;
auto i = lst.begin();
auto end = lst.end();
// in case of std::set:
while (off--)
++i;
size_t got = 0;
for (; i != end; ++i) {
if (!count--)
return got;
out[got++] = i->c_str();
}
return got;
}
template<class T, class STRLIST>
size_t pkgdepdb_strlist_get(const T& obj, STRLIST T::*member,
const char **out, size_t off, size_t count)
{
return pkgdepdb_strlist_get(obj.*member, out, off, count);
}
template<class T>
size_t pkgdepdb_strlist_add_unique(T& obj, std::set<std::string> T::*member,
const char *value)
{
return std::get<1>((obj.*member).insert(value)) ? 1 : 0;
}
template<class T>
size_t pkgdepdb_strlist_insert_unique(T& obj,
std::set<std::string> T::*member,
size_t count, const char **values)
{
auto& v = obj.*member;
v.insert(values, values + count);
return 1;
}
template<class T>
size_t pkgdepdb_strlist_add_always(T& obj, std::vector<std::string> T::*member,
const char *value)
{
(obj.*member).emplace_back(value);
return 1;
}
template<class T>
size_t pkgdepdb_strlist_add_unique(T& obj, std::vector<std::string> T::*member,
const char *value)
{
auto& v = obj.*member;
auto beg = v.begin();
auto end = v.end();
if (std::find(beg, end, value) == end)
return 0;
v.emplace_back(value);
return 1;
}
template<class T>
size_t pkgdepdb_strlist_insert_always(T& obj,
std::vector<std::string> T::*member,
size_t index, size_t count,
const char **values)
{
auto& v = obj.*member;
v.insert(v.begin() + index, values, values + count);
return 1;
}
template<class T>
size_t pkgdepdb_strlist_insert_unique(T& obj,
std::vector<std::string> T::*member,
size_t index, size_t count,
const char **values)
{
auto& mem = obj.*member;
std::vector<std::string> vec(values, values + count);
for (const auto& v : mem) {
auto found = std::find(vec.begin(), vec.end(), v);
if (found != vec.end())
vec.erase(found);
}
mem.insert(mem.begin() + index,
std::make_move_iterator(vec.begin()),
std::make_move_iterator(vec.end()));
return vec.size();
}
static inline
size_t pkgdepdb_strlist_del_s_one(std::vector<std::string>& lst, const char *v)
{
auto beg = lst.begin();
auto end = lst.end();
auto it = std::find(beg, end, v);
if (it == end)
return 0;
lst.erase(it);
return 1;
}
static inline
size_t pkgdepdb_strlist_del_s_one(std::set<std::string>& lst, const char *v) {
return lst.erase(v) ? 1 : 0;
}
template<class T>
size_t pkgdepdb_strlist_del_s_one(T& obj, std::set<std::string> T::*member,
const char *value)
{
return (obj.*member).erase(value) ? 1 : 0;
}
template<class T>
size_t pkgdepdb_strlist_del_s_one(T& obj, std::vector<std::string> T::*member,
const char *value)
{
return pkgdepdb_strlist_del_s_one(obj.*member, value);
}
static inline
size_t pkgdepdb_strlist_del_s_all(std::set<std::string>& lst, const char *v) {
return lst.erase(v) ? 1 : 0;
}
template<class T>
size_t pkgdepdb_strlist_del_s_all(T& obj, std::set<std::string> T::*member,
const char *value)
{
return (obj.*member).erase(value) ? 1 : 0;
}
static inline
size_t pkgdepdb_strlist_del_s_all(std::vector<std::string>& lst, const char *v)
{
auto beg = lst.begin();
auto end = lst.end();
auto it = std::remove(beg, end, v);
end = lst.end(); // std::remove is a write operation after all
size_t count = end - it;
lst.erase(it, end);
return count;
}
template<class T>
size_t pkgdepdb_strlist_del_s_all(T& obj, std::vector<std::string> T::*member,
const char *value)
{
return pkgdepdb_strlist_del_s_all(obj.*member, value);
}
template<class T, class STRLIST>
size_t pkgdepdb_strlist_del_i(T& obj, STRLIST T::*member, size_t index) {
if (index >= (obj.*member).size())
return 0;
auto i = (obj.*member).begin();
while (index--)
++i;
(obj.*member).erase(i);
return 1;
}
template<class STRLIST>
size_t pkgdepdb_strlist_del_r(STRLIST& lst, size_t index, size_t count) {
size_t max = lst.size();
if (index >= max)
return 0;
if (count > max - index)
count = max - index;
auto i = lst.begin();
while (index--) ++i;
auto e = i;
for (index = count; index--;) ++e;
lst.erase(i, e);
return count;
}
static inline
bool pkgdepdb_strlist_set_i(std::vector<std::string>& lst, size_t index,
const char *value)
{
if (index >= lst.size())
return false;
lst[index] = value;
return true;
}
template<class STRLIST>
bool pkgdepdb_strlist_contains(const STRLIST& lst, const char *value) {
return std::find(lst.begin(), lst.end(), value) != lst.end();
}
#endif