Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
Latest commit
126 lines (101 loc) · 3.34 KB
/
Copy pathutil.cpp
File metadata and controls
126 lines (101 loc) · 3.34 KB
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
/* This file is a part of runjam <https://github.com/idthic/runjam>.
Copyright (C) 2014-2020, Koichi Murase <myoga.murase at gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
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; either version 2 of the
License, or any later version.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA */
#include"util.hpp"
#include<cstddef>
#include<cstring>
#include<random>
#include<string>
#include<cstdarg>
namespaceidt::util {
boolends_with(constchar* s, std::size_t l, constchar* suffix, std::size_t len) {
return l >= len && std::memcmp(s + (l - len), suffix, len) == 0;
}
boolends_with(std::string const& s, std::string const& suffix) {
returnends_with(s.c_str(), s.size(), suffix.c_str(), suffix.size());
}
boolends_with(std::string const& s, constchar* suffix) {
returnends_with(s.c_str(), s.size(), suffix, std::strlen(suffix));
}
boolends_with(constchar* s, constchar* suffix) {
returnends_with(s, std::strlen(s), suffix, std::strlen(suffix));
}
//-----------------------------------------------------------------------------
// taken from fitmass/common.cpp
std::string vstrprintf(constchar* fmt, std::va_list va) {
std::va_list va1;
va_copy(va1, va);
std::size_tconst sz = vsnprintf(NULL, 0, fmt, va1);
va_end(va1);
char* buff = (char*) std::malloc(sz + 1);
va_copy(va1, va);
std::vsprintf(buff, fmt, va1);
va_end(va1);
std::string str = buff;
std::free(buff);
return str;
}
std::string strprintf(constchar* fmt, ...) {
std::va_list va;
va_start(va, fmt);
std::string const str = vstrprintf(fmt, va);
va_end(va);
return str;
}
std::string strprintf(std::string const& fmt, ...) {
std::va_list va;
va_start(va, fmt);
std::string const str = vstrprintf(fmt.c_str(), va);
va_end(va);
return str;
}
//-----------------------------------------------------------------------------
static std::mt19937 eng;
std::mt19937& random_engine() {
return eng;
}
voidset_random_seed(int seed) {
eng.seed(seed);
}
doubleurand() {
static std::uniform_real_distribution<double> dist(0.0, 1.0);
returndist(eng);
}
doublenrand() {
static std::normal_distribution<double> dist;
returndist(eng);
}
intirand(int n) {
std::uniform_int_distribution<int> dist(0, n - 1);
returndist(eng);
}
std::size_tirand(std::size_t n) {
std::uniform_int_distribution<std::size_t> dist(0, n - 1);
returndist(eng);
}
intirand_poisson(double lambda){
if (lambda == 0.0) return0;
std::poisson_distribution<int> dist(lambda);
returndist(eng);
}
intirand_binomial(int trial, double probability) {
std::binomial_distribution<int> dist(trial, probability);
returndist(eng);
}
std::size_tirand_binomial(std::size_t trial, double probability) {
std::binomial_distribution<std::size_t> dist(trial, probability);
returndist(eng);
}
}