-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstd_lib.h
174 lines (145 loc) · 4.02 KB
/
std_lib.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
#pragma once
#include <bits/stdc++.h>
#include "../terminal_colors/terminal_colors.h"
#include "boost/lexical_cast.hpp"
using namespace std;
using namespace terminal_colors;
void print_line_break (size_t n = 80)
{
for (size_t i { 0 } ; i < n ; ++i)
{
printf("-");
}
printf("\n");
}
void header (const string &header, bool first = false)
{
if (!first)
{
putchar('\n');
}
printf("### %s ###", header.c_str());
// for (auto i { header.size() + 5 } ; i <= 80 ; ++i)
// {
// putchar('#');
// }
printf("\n\n");
}
vector<string> split (const string &text, char delimiter = ' ')
{
vector<string> words { };
istringstream iss { text };
string s;
while (getline(iss, s, delimiter))
{
words.push_back(s);
}
return words;
}
// random number generators. See 24.7.
inline std::default_random_engine &get_rand ()
{
static std::default_random_engine ran; // note: not thread_local
return ran;
}
inline void seed_randint (int s)
{
get_rand().seed(s);
}
inline int randint (int min, int max)
{
return std::uniform_int_distribution<> { min, max }(get_rand());
}
inline int randint (int max)
{
return randint(0, max);
}
template < typename Container >
void print (Container container)
{
cout << "{ ";
for (auto iter { container.begin() } ; iter != container.end() ; ++iter)
{
auto last = container.end();
last--;
if (iter != last)
{
cout << *iter << ", ";
}
else
{
cout << *iter;
}
}
cout << " }" << endl;
}
template < typename T >
struct Debug_vector : std::vector<T>
{
using size_type = typename std::vector<T>::size_type;
using std::vector<T>::vector; // inheriting constructor
T &operator[] (unsigned int i) // rather than return at(i);
{
if (i < 0 || this->size() <= i)
{
throw out_of_range(to_string(i));
}
return std::vector<T>::operator[](i);
}
const T &operator[] (unsigned int i) const
{
if (i < 0 || this->size() <= i)
{ throw out_of_range(to_string(i)); }
return std::vector<T>::operator[](i);
}
friend ostream &operator<< (ostream &os, const Debug_vector &v)
{
cout << "{ ";
for (auto iter { v.begin() } ; iter != v.end() ; ++iter)
{
iter != v.end() - 1 ? os << *iter << ", " : os << *iter;
}
cout << " }";
return os;
}
friend istream &operator>> (istream &is, vector<T> &v)
{
string str;
while (is >> str)
{
v.push_back(boost::lexical_cast<T>(str));
}
return is;
}
};
template < typename Container1, typename Container2 >
// requires Container<C>()
void copy (Container1 &from_container, Container2 &to_container)
{
std::copy(from_container.begin(), from_container.end(), to_container.begin());
}
template < typename Container >
// requires Container<C>()
void sort (Container &c)
{
std::sort(c.begin(), c.end());
}
template < typename Container, typename Predicate >
// requires Container<C>() && Binary_Predicate<Value_type<C>>()
void sort (Container &c, Predicate predicate)
{
std::sort(c.begin(), c.end(), predicate);
}
template < typename Container, typename Value >
// requires Container<C>() && Equality_comparable<C,Val>()
auto find (Container &c, Value value)
{
return std::find(c.begin(), c.end(), value);
}
template < typename Container, typename Predicate >
// requires Container<C>() && Predicate<Predicate,Value_type<C>>()
auto find_if (Container &c, Predicate predicate)
{
return std::find_if(c.begin(), c.end(), predicate);
}
//#define vector Debug_vector