-
Notifications
You must be signed in to change notification settings - Fork 8
/
util.h
260 lines (229 loc) · 6.15 KB
/
util.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
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
//
// Utility functions
//
// Copyright(C) 2010 Mizuki Fujisawa <[email protected]>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
#ifndef BAYON_UTIL_H_
#define BAYON_UTIL_H_
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
/* include hash_map headers. */
#ifdef HAVE_GOOGLE_DENSE_HASH_MAP
#include <google/dense_hash_map>
#elif HAVE_EXT_HASH_MAP
#include <ext/hash_map>
#else
#include <map>
#endif
/* Print debug messages. */
#ifdef DEBUG
#define show_log(msg) \
do { fprintf(stderr, "[log] %s\n", msg); } while (false);
#else
#define show_log(msg) \
do { } while (false);
#endif
/* isnan */
#ifndef isnan
#ifdef _WIN32
#include <cfloat>
#define isnan(x) _isnan(x)
#else
#define isnan(x) (x != x)
#endif
#endif
/* hash function of string key for __gnu_cxx::hash_map */
#if (defined(_WIN32) || !defined(HAVE_GOOGLE_DENSE_HASH_MAP)) && defined(HAVE_EXT_HASH_MAP)
namespace __gnu_cxx {
template<> struct hash<std::string> {
size_t operator() (const std::string &x) const {
return hash<const char *>()(x.c_str());
}
};
template<> struct hash<long long> {
size_t operator()(long long __x) const {
return __x;
}
};
template<> struct hash<unsigned long long> {
size_t operator()(unsigned long long __x) const {
return __x;
}
};
}
#endif
namespace bayon {
/**
* typedef template of hash map class
* 'google::dense_hash_map' or '__gnu__cxx::hash_map' or 'std::map'
*/
template<typename KeyType, typename ValueType>
struct HashMap {
#ifdef HAVE_GOOGLE_DENSE_HASH_MAP
typedef google::dense_hash_map<KeyType, ValueType> type;
#elif HAVE_EXT_HASH_MAP
typedef __gnu_cxx::hash_map<KeyType, ValueType> type;
#else
typedef std::map<KeyType, ValueType> type;
#endif
};
const unsigned int DEFAULT_SEED = 12345; ///< default seed value
const std::string DELIMITER("\t"); ///< delimiter string
const double MAX_LOAD_FACTOR = 0.9; ///< max load factor of hash_map
/**
* Initialize a hash_map object (for google::dense_hash_map)
* @param empty_key key of empty entry
* @param deleted_key key of deleted entry
*/
template<typename KeyType, typename HashType>
void init_hash_map(const KeyType &empty_key, HashType &hmap) {
#ifdef HAVE_GOOGLE_DENSE_HASH_MAP
hmap.max_load_factor(MAX_LOAD_FACTOR);
hmap.set_empty_key(empty_key);
#endif
}
/**
* Initialize a hash_map object (for google::dense_hash_map)
* @param empty_key key of a empty entry
* @param deleted_key key of a deleted entry
* @param bucket_count bucket count
*/
template<typename KeyType, typename HashType>
void init_hash_map(const KeyType &empty_key, HashType &hmap,
size_t bucket_count) {
#ifdef HAVE_GOOGLE_DENSE_HASH_MAP
hmap.rehash(bucket_count);
hmap.max_load_factor(MAX_LOAD_FACTOR);
hmap.set_empty_key(empty_key);
#endif
}
/**
* Compare pair items.
* @param left item
* @param right item
* @return return true if left_value > right_value
*/
template<typename KeyType, typename ValueType>
bool greater_pair(const std::pair<KeyType, ValueType> &left,
const std::pair<KeyType, ValueType> &right) {
if (left.second > right.second) {
return true;
} else if (left.second == right.second) {
return left.first > right.first;
} else {
return false;
}
}
/**
* Compare pair items by absolute values.
* @param left item
* @param right item
* @return return true if abs(left_value) > abs(right_value)
*/
template<typename KeyType, typename ValueType>
bool greater_pair_abs(const std::pair<KeyType, ValueType> &left,
const std::pair<KeyType, ValueType> &right) {
ValueType absleft = std::abs(left.second);
ValueType absright = std::abs(right.second);
if (absleft > absright) {
return true;
} else if (absleft == absright) {
return left.first > right.first;
} else {
return false;
}
}
/**
* Set a seed for a random number generator.
* @param seed seed
*/
void mysrand(unsigned int seed);
/**
* Get a random number.
* @param seed the pointer of a seed
* @return a random number
*/
int myrand(unsigned int *seed);
/**
* Get current time.
* @return current time
*/
double get_time();
/**
* Get a random ASCII string.
* @param max max size of output string
* @param result output string
*/
void random_string(size_t max, std::string &result);
/**
* Get a file extension.
* @param filename file name
* @return extension
*/
std::string get_extension(const std::string filename);
/**
* Split a string by delimiter string
* @param s input string to be splited
* @param delimiter delimiter string
* @param splited splited strings
*/
void split_string(const std::string &s, const std::string &delimiter,
std::vector<std::string> &splited);
/**
* Random number generator class.
*/
class Random {
private:
unsigned int seed_; ///< a seed number
public:
/**
* Constructor.
*/
Random() : seed_(DEFAULT_SEED) { }
/**
* Constructor.
* @param seed a seed number
*/
explicit Random(unsigned int seed) : seed_(seed) { }
/**
* Destructor
*/
~Random() { }
/**
* Set a seed number.
* @param seed a seed number
*/
void set_seed(unsigned int seed) { seed_ = seed; }
/**
* Operator()
* @param max maximum number
* return a random number
*/
unsigned int operator()(unsigned int max) {
double ratio = static_cast<double>(myrand(&seed_))
/ static_cast<double>(RAND_MAX);
return static_cast<unsigned int>(ratio * max);
}
};
} /* namespace bayon */
#endif // BAYON_UTIL_H_