-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutilities.h
128 lines (111 loc) · 4.16 KB
/
utilities.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
#ifndef utilities_H
#define utilities_H
#include <string>
#include <vector>
#include <set>
#include <algorithm>
//----------
//
// prototypes for functions in this module--
//
//----------
bool is_prefix_of (const std::string& s, const std::string& prefix);
bool is_suffix_of (const std::string& s, const std::string& suffix);
std::string strip_blank_ends (const std::string& s);
std::string strip_blank_prefix (const std::string& s);
std::string strip_blank_suffix (const std::string& s);
std::string strip_prefix (const std::string& s, const std::string& prefix);
std::string strip_suffix (const std::string& s, const std::string& suffix);
std::string strip_file_path (const std::string& filename);
int string_to_int (const std::string& s, const bool allowHex=false);
std::uint32_t string_to_u32 (const std::string& s, const bool allowHex=false);
std::uint64_t string_to_u64 (const std::string& s, const bool allowHex=false);
int string_to_unitized_int (const std::string& s, const int unitScale=1000);
std::uint32_t string_to_unitized_u32 (const std::string& s, const int unitScale=1000);
std::uint64_t string_to_unitized_u64 (const std::string& s, const int unitScale=1000);
std::uint32_t hex_string_to_u32 (const std::string& s);
std::uint64_t hex_string_to_u64 (const std::string& s);
double string_to_double (const std::string& s);
double string_to_probability (const std::string& s);
std::string to_lower (const std::string& s);
std::string reverse_complement (const std::string& s);
bool nt_is_acgt (const char nt);
std::uint32_t update_crc (std::uint32_t crc, std::uint8_t ch);
void fatal (const std::string& message="");
#define round_up_16(b) ((((std::uint64_t) (b))+15)&(~15))
// timer stuff
#include <chrono>
#define get_wall_time() std::chrono::system_clock::now()
#define elapsed_wall_time(start) ((std::chrono::duration_cast<std::chrono::nanoseconds>(get_wall_time()-start).count()) / 1000000000.0)
#define wall_time_ty std::chrono::time_point<std::chrono::system_clock>
// macro to declare a variable of type T with the gnu c compiler not caring
// that it's not used
#ifdef __GNUC__
#define notused(T) T unused##__LINE__ __attribute__ ((unused))
#else
#define notused(T) T unused##__LINE__
#endif // __GNUC__
// macro to convince gnu c compiler not to complain about unusued function
// arguments
#ifdef __GNUC__
#define arg_dont_complain(arg) arg __attribute__ ((unused))
#else
#define arg_dont_complain(arg) arg
#endif // __GNUC__
//----------
//
// contains--
// Determine if a set contains a particular element.
//
//----------
//
// Arguments (variant 1):
// const std::set<T>& container: The set.
// const T& element: The element to check for.
//
// Arguments (variant 2):
// const std::vector<T>& container: The set, as a vector.
// const T& element: The element to check for.
//
// Returns:
// true if the set (or vector) contains the element; false otherwise.
//
//----------
//
// Notes:
// [1] It's hard to understand why C++ doesn't provide this function.
// [2] This code was based on the discussions at
// http://stackoverflow.com/questions/1701067/how-to-check-that-an-element-is-in-a-stdset
// and
// http://stackoverflow.com/questions/6277646/in-c-check-if-stdvectorstring-contains-a-certain-value
//
//----------
template<class T>
bool contains
(const std::set<T>& container,
const T& element)
{
return (container.find(element) != container.end());
}
template<class T>
bool contains
(const std::set<T>& container,
const char* element)
{
return (container.find(std::string(element)) != container.end());
}
template<class T>
bool contains
(const std::vector<T>& container,
const T& element)
{
return (std::find(container.begin(),container.end(),element) != container.end());
}
template<class T>
bool contains
(const std::vector<T>& container,
const char* element)
{
return (std::find(container.begin(),container.end(),std::string(element)) != container.end());
}
#endif // utilities_H