-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.h
129 lines (106 loc) · 2.9 KB
/
common.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
//
// Created by Gyebro on 2017. 12. 01..
//
#ifndef AOC17_COMMON_H
#define AOC17_COMMON_H
#include <vector>
#include <utility>
#include <string>
#include <iostream>
#include <c++/fstream>
#include <c++/sstream>
#include <cmath>
#include <algorithm>
#include <chrono>
#include <iomanip>
#include "windows.h"
using namespace std;
typedef pair<size_t, size_t> size_p;
string file_to_string(string filename);
template<typename T>
void split(const std::string &s, char delim, T result) {
stringstream ss(s); string item;
while (getline(ss, item, delim)) {
*(result++) = item;
}
}
vector<string> split(const string &s, char delim);
void split_at_multiple(const string& str, const string& delims, vector<string>& splittedString, vector<char>& splitterDelims, const bool& toTrim);
/**
* Check whether string s1 and s2 are anagrams (e.g. abcd and cadb are anagrams)
* @param s1
* @param s2
* @return
*/
bool anagram_pair_test(string s1, string s2);
template<typename T>
size_t max_idx(const vector<T> v) {
auto result = std::max_element(v.begin(), v.end());
auto index = std::distance(v.begin(), result);
return index;
}
template<typename T>
size_t min_idx(const vector<T>& v) {
auto result = std::min_element(v.begin(), v.end());
auto index = std::distance(v.begin(), result);
return index;
}
template <class T>
bool has_elem(const vector<T>& v, const T e) {
return find(v.begin(), v.end(), e) != v.end();
}
template<class T>
long long int find(const vector<T>& v, const T e) {
return distance(v.begin(), find(v.begin(), v.end(), e));
}
/**
* Check if str has the substring find inside
* @param str String which is searched
* @param target Target word to be found
* @param loc Location of first match
* @param i_begin Optional beginning position of the search
* @return
*/
bool has_string(const string str, const string target, size_t& loc, size_t i_begin=0);
string trim_spaces(const string s);
template <class Clock>
void
display_precision()
{
typedef std::chrono::duration<double, std::nano> NS;
NS ns = typename Clock::duration(1);
std::cout << ns.count() << " ns\n";
}
class Clock {
private:
chrono::high_resolution_clock::time_point t_start, t_stop;
public:
Clock();
void start();
void stop();
double read_sec();
long long int read_millisec();
long long int read_microsec();
long long int read_nanosec();
void tell_sec();
void tell_millisec();
void tell_microsec();
void tell_nanosec();
};
class WinClock {
private:
double PCFreq;
LARGE_INTEGER li;
__int64 CounterStart, CounterStop;
public:
WinClock();
void start();
void stop();
double read_millisec();
double read_microsec();
};
string bytes_to_hex_string(vector<uint8_t> bytes);
string hex_string_to_binary(const string s);
string ReplaceAll(string str, const string& from, const string& to);
bool is_digits(const string &str);
#endif