-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpp.snippets
645 lines (548 loc) · 14.3 KB
/
cpp.snippets
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
priority -50
extends c
# We want to overwrite everything in parent ft.
priority -49
###########################################################################
# Global functions #
###########################################################################
global !p
def write_docstring_args(arglist, snip):
args = str(arglist).split(',')
if len(args) > 1:
c = 0
for arg in args:
if c == 0:
snip.rv += arg
c = 1
else:
snip += '* : %s' % arg.strip()
else:
snip.rv = args[0]
endglobal
###########################################################################
# TextMate Snippets #
###########################################################################
snippet beginend "$1.begin(), $1.end() (beginend)"
${1:v}${1/^.*?(-)?(>)?$/(?2::(?1:>:.))/}begin(), $1${1/^.*?(-)?(>)?$/(?2::(?1:>:.))/}end()
endsnippet
snippet cl "class .. (class)"
class ${1:`!p snip.rv = snip.basename or "name"`}
{
public:
${1/(\w+).*/$1/} (${2:arguments});
virtual ~${1/(\w+).*/$1/} ();
private:
${0:/* data */}
};
endsnippet
snippet ns "namespace .. (namespace)"
namespace${1/.+/ /m}${1:`!p snip.rv = snip.basename or "name"`}
{
${VISUAL}$0
}${1/.+/ \/* /m}$1${1/.+/ *\/ /m}
endsnippet
snippet readfile "read file (readF)"
std::vector<char> v;
if (FILE *fp = fopen(${1:"filename"}, "r"))
{
char buf[1024];
while(size_t len = fread(buf, 1, sizeof(buf), fp))
v.insert(v.end(), buf, buf + len);
fclose(fp);
}
endsnippet
snippet map "std::map (map)"
std::map<${1:key}, ${2:value}> map$0;
endsnippet
snippet vector "std::vector (v)"
std::vector<${1:char}> v$0;
endsnippet
snippet tp "template <typename ..> (template)"
template <typename ${1:_InputIter}>
endsnippet
snippet cla "An entire .h generator" b
#ifndef ${2:`!v substitute(vim_snippets#Filename('$1_H','ClassName'),'.*','\U&\E','')`}
#define $2
class ${1:`!v substitute(substitute(vim_snippets#Filename('$1','ClassName'),'^.','\u&',''), '_\(\w\)', '\u\1', 'g')`}
{
private:
$3
public:
$1();
virtual ~$1();
};
#endif /* $2 */
endsnippet
############################################### CUSTOM SNIPPETS ####################################################
snippet debug "Error function for debugging" b
template <typename A, typename B> string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string &s)
{
return '"' + s + '"';
}
string to_string(const char *s)
{
return to_string((string)s);
}
string to_string(bool b)
{
return (b ? "true" : "false");
}
string to_string(vector<bool> v)
{
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N> string to_string(bitset<N> v)
{
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A> string to_string(A v)
{
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B> string to_string(pair<A, B> p)
{
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p)
{
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p)
{
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out()
{
cerr << endl;
}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T)
{
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
endsnippet
snippet SEGMENT "SegmentTree(vector<int> v);" b
class SegmentTree {
int n;
vector<int> a, t;
void build(int v, int tl, int tr) {
if (tl == tr) { t[v] = a[tl]; }
else {
int tm = (tl + tr) >> 1;
build(2 * v + 1, tl, tm);
build(2 * v + 2, tm + 1, tr);
t[v] = t[2 * v + 1] + t[2 * v + 2];
}
}
long long query(int v, int tl, int tr, int l, int r) {
if (l > r) return 0;
if (l == tl && r == tr) { return t[v]; }
int tm = (tl + tr) >> 1;
return query(2 * v + 1, tl, tm, l, min(r, tm)) +
query(2 * v + 2, tm + 1, tr, max(l, tm + 1), r);
}
void update(int v, int tl, int tr, int pos, int val) {
if (tl == tr) { t[v] = val; }
else {
int tm = (tl + tr) >> 1;
if (pos <= tm) { update(2 * v + 1, tl, tm, pos, val); }
else { update(2 * v + 2, tm + 1, tr, pos, val); }
t[v] = t[2 * v + 1] + t[2 * v + 2];
}
}
void init() {
this->n = (int)a.size();
t.reserve(4 * n);
t.assign(4 * n, 0);
build(0, 0, n - 1);
}
public:
SegmentTree(int *st, int *en) { this->a = vector<int>(st, en); init(); }
SegmentTree(vector<int> &a) { this->a = a; init(); }
SegmentTree(int *a, int n) { this->a = vector<int>(a, a + n); init(); }
long long query(int l, int r) { return query(0, 0, n - 1, l, r);}
void update(int pos, int val) { update(0, 0, n - 1, pos, val);}
};
endsnippet
snippet DSU "DSU(int n);" b
class DSU {
vector<int> parent;
vector<int> sze;
int components = 0;
public:
DSU(int n = -1)
{
if (n >= 0)
init(n);
}
void init(int n)
{
parent.resize(n + 1);
sze.assign(n + 1, 1);
components = n;
for (int i = 0; i < n + 1; i++)
parent[i] = i;
}
int find_set(int x)
{
return x == parent[x] ? x : parent[x] = find_set(parent[x]);
}
bool union_set(int a, int b)
{
a = find_set(a);
b = find_set(b);
bool union_required = (a != b);
if (union_required) {
if (sze[a] < sze[b]) {
swap(a, b);
}
parent[b] = a;
sze[a] += sze[b];
components--;
}
return union_required;
}
int parts()
{
return components;
}
};
endsnippet
snippet solve "solve() function base file" b
#ifdef LOCAL
#include "header.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define dbg(...)
#define dbga(a, b)
#endif
#define rep(a, b, i) for (int i = (a); i < (b); ++i)
#define repi(a, b, i) for (int i = (a); i > (b); --i)
#define sz(x) (int)x.size()
#define pb push_back
#define mkp make_pair
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define vt vector
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef __int128 xll;
typedef pair<int, int> pii;
typedef vt<int> vi;
typedef vt<ll> vll;
typedef vt<pii> vpii;
__attribute__((unused)) const double pi = 3.141592653589793;
__attribute__((unused)) const ll inf = 0xFFFFFFFFFFFFFFFL;
__attribute__((unused)) const int MOD = 1e9 + 7;
__attribute__((unused)) const int mxn = 2e5 + 5;
//__attribute__((unused)) const int mxn = 1e5 + 5;
int solve() {
${1}
return 0;
}
int main() {
#ifndef LOCAL
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#endif
int t;
cin >> t;
//int x = 0;
while (t--) {
//cout << "Case #" << ++x << ": ";
solve();
}
return 0;
}
endsnippet
snippet single "No loop base file" b
#ifdef LOCAL
#include "header.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define dbg(...)
#define dbga(a, b)
#endif
#define rep(a, b, i) for (int i = (a); i < (b); ++i)
#define repi(a, b, i) for (int i = (a); i > (b); --i)
#define sz(x) (int)x.size()
#define pb push_back
#define mkp make_pair
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define vt vector
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef __int128 xll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
__attribute__((unused)) const double pi = 3.141592653589793;
__attribute__((unused)) const ll inf = 0xFFFFFFFFFFFFFFFL;
__attribute__((unused)) const int MOD = 1e9 + 7;
__attribute__((unused)) const int mxn = 2e5 + 5;
//__attribute__((unused)) const int mxn = 1e5 + 5;
int main() {
#ifndef LOCAL
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#endif
${1}
return 0;
}
endsnippet
snippet mod_int "Neal's modulo function" b
// by https://codeforces.com/profile/neal
template <const int &MOD>
struct _m_int {
int val;
_m_int(int64_t v = 0) {
if (v < 0) v = v % MOD + MOD;
if (v >= MOD) v %= MOD;
val = int(v);
}
_m_int(uint64_t v) {
if (v >= MOD) v %= MOD;
val = int(v);
}
_m_int(int v) : _m_int(int64_t(v)) {}
_m_int(unsigned v) : _m_int(uint64_t(v)) {}
explicit operator int() const { return val; }
explicit operator unsigned() const { return val; }
explicit operator int64_t() const { return val; }
explicit operator uint64_t() const { return val; }
explicit operator double() const { return val; }
explicit operator long double() const { return val; }
_m_int &operator+=(const _m_int &other) {
val -= MOD - other.val;
if (val < 0) val += MOD;
return *this;
}
_m_int &operator-=(const _m_int &other) {
val -= other.val;
if (val < 0) val += MOD;
return *this;
}
static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
#if !defined(_WIN32) || defined(_WIN64)
return unsigned(x % m);
#endif
// Optimized mod for Codeforces 32-bit machines.
// x must be less than 2^32 * m for this to work, so that x / m fits in an
// unsigned 32-bit int.
unsigned x_high = unsigned(x >> 32), x_low = unsigned(x);
unsigned quot, rem;
asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m));
return rem;
}
_m_int &operator*=(const _m_int &other) {
val = fast_mod(uint64_t(val) * other.val);
return *this;
}
_m_int &operator/=(const _m_int &other) { return *this *= other.inv(); }
friend _m_int operator+(const _m_int &a, const _m_int &b) { return _m_int(a) += b; }
friend _m_int operator-(const _m_int &a, const _m_int &b) { return _m_int(a) -= b; }
friend _m_int operator*(const _m_int &a, const _m_int &b) { return _m_int(a) *= b; }
friend _m_int operator/(const _m_int &a, const _m_int &b) { return _m_int(a) /= b; }
_m_int &operator++() {
val = val == MOD - 1 ? 0 : val + 1;
return *this;
}
_m_int &operator--() {
val = val == 0 ? MOD - 1 : val - 1;
return *this;
}
_m_int operator++(int) {
_m_int before = *this;
++*this;
return before;
}
_m_int operator--(int) {
_m_int before = *this;
--*this;
return before;
}
_m_int operator-() const { return val == 0 ? 0 : MOD - val; }
friend bool operator==(const _m_int &a, const _m_int &b) { return a.val == b.val; }
friend bool operator!=(const _m_int &a, const _m_int &b) { return a.val != b.val; }
friend bool operator<(const _m_int &a, const _m_int &b) { return a.val < b.val; }
friend bool operator>(const _m_int &a, const _m_int &b) { return a.val > b.val; }
friend bool operator<=(const _m_int &a, const _m_int &b) { return a.val <= b.val; }
friend bool operator>=(const _m_int &a, const _m_int &b) { return a.val >= b.val; }
static const int SAVE_INV = int(1e6) + 5;
static _m_int save_inv[SAVE_INV];
static void prepare_save() {
// Make sure MOD is prime, which is necessary for the inverse algorithm below
for (int p = 2; p * p <= MOD; p += p % 2 + 1) assert(MOD % p != 0);
save_inv[0] = 0, save_inv[1] = 1;
for (int i = 2; i < SAVE_INV; i++)
save_inv[i] = save_inv[MOD % i] * (MOD - MOD / i);
}
_m_int inv() const {
if (save_inv[1] == 0) prepare_save();
if (val < SAVE_INV) return save_inv[val];
_m_int product = 1;
int v = val;
while (v >= SAVE_INV) {
product *= MOD - MOD / v;
v = MOD % v;
}
return product * save_inv[v];
}
_m_int pow(int64_t p) const {
if (p < 0) return inv().pow(-p);
_m_int a = *this, result = 1;
while (p > 0) {
if (p & 1) result *= a;
p >>= 1;
if (p > 0) a *= a;
}
return result;
}
friend ostream &operator<<(ostream &os, const _m_int &m) { return os << m.val; }
};
template <const int &MOD>
_m_int<MOD> _m_int<MOD>::save_inv[_m_int<MOD>::SAVE_INV];
endsnippet
snippet simple "Simple snippet" b
#ifdef LOCAL
#include "header.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define dbg(...)
#define dbga(a, b)
#endif
_attribute__((unused)) const int MOD = 1e9 + 7;
int main() {
#ifndef LOCAL
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#endif
${1}
return 0;
}
endsnippet
snippet simples "Simple snippet with solve()" b
#ifdef LOCAL
#include "header.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define dbg(...)
#define dbga(a, b)
#endif
#define rep(a, b, i) for (int i = (a); i < (b); ++i)
#define repi(a, b, i) for (int i = (a); i > (b); --i)
#define sz(x) (int)x.size()
#define pb push_back
#define mkp make_pair
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define vt vector
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef __int128 xll;
typedef pair<int, int> pii;
typedef vt<int> vi;
typedef vt<ll> vll;
typedef vt<pii> vpii;
__attribute__((unused)) const double pi = 3.141592653589793;
__attribute__((unused)) const ll inf = 0xFFFFFFFFFFFFFFFL;
__attribute__((unused)) const int MOD = 1e9 + 7;
__attribute__((unused)) const int mxn = 2e5 + 5;
//__attribute__((unused)) const int mxn = 1e5 + 5;
int solve() {
return 0;
}
int main() {
#ifndef LOCAL
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#endif
int t;
cin >> t;
// int x = 0;
while (t--) {
// cout << "Case #" << ++x << ": ";
solve();
}
return 0;
}
endsnippet
snippet phs "Hashing pair" b
struct phs {
public:
template <typename T, typename U>
std::size_t operator()(const std::pair<T, U> &x) const {
return std::hash<T>()(x.first) ^ std::hash<U>(x.second);
}
};
endsnippet
snippet fnc "Basic c++ doxygen function template" b
/**
* @brief: ${4:brief}
*
* @param: `!p write_docstring_args(t[3],snip)`
*
* @return: `!p snip.rv = t[1]`
*/
${1:ReturnType} ${2:FunctionName}(${3:param})
{
${0:FunctionBody}
}
endsnippet
# vim:ft=snippets: