-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxvector.cc
More file actions
354 lines (327 loc) · 10.6 KB
/
xvector.cc
File metadata and controls
354 lines (327 loc) · 10.6 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
@copyright Russell Standish 2019
@author Russell Standish
This file is part of Civita.
Civita 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 3 of the License, or
(at your option) any later version.
Civita 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 Civita. If not, see <http://www.gnu.org/licenses/>.
*/
#include "xvector.h"
#include <regex>
using namespace std;
#include <boost/date_time.hpp>
using namespace boost::posix_time;
using namespace boost::gregorian;
#include <time.h>
namespace civita
{
namespace
{
struct InvalidDate: public std::runtime_error
{
InvalidDate(const std::string& dateString, const std::string& format):
runtime_error("invalid date/time: "+dateString+" for format "+format) {}
};
}
void Extractor::setPattern(const string& fmt, size_t pq)
{
auto pos1=fmt.find("%Y");
if (pos1==string::npos)
throw runtime_error("year not specified in format string");
auto pos2=pq;
auto re1="(\\d{4})", re2="(\\d)";
if (pos2<pos1)
{
swap(pos2,pos1);
swap(re1,re2);
swapVars=true;
}
rePat="\\s*"+fmt.substr(0,pos1)+re1+
fmt.substr(pos1+2,pos2-pos1-2)+re2+
fmt.substr(pos2+2)+"\\s*";
pattern=regex(rePat);
}
void Extractor::operator()(const string& data, int& var1, int& var2) const
{
smatch match;
if (regex_match(data,match,pattern))
{
var1=stoi(match[1]);
var2=stoi(match[2]);
if (swapVars) swap(var1,var2);
}
else
throw runtime_error("data "+data+" fails to match pattern "+rePat);
}
void XVector::push_back(const std::string& s)
{
if (pushTemplate.dimension()!=dimension)
pushTemplate=AnyVal(dimension);
V::push_back(pushTemplate(s));
}
void AnyVal::setDimension(const Dimension& dim)
{
this->dim=dim;
switch (dim.type)
{
case Dimension::string:
case Dimension::value:
return;
case Dimension::time:
if (auto pq=dim.units.find("%Q"); pq!=string::npos)
{
timeType=quarter;
extract.setPattern(dim.units,pq);
return;
}
// handle date formats with any combination of %Y, %m, %d, %H, %M, %S
// handle dates with 1 or 2 digits see Ravel ticket #35.
// Delegate to std::time_facet if time fields abut or more complicated formatting is requested
static regex nonStandardDateTimes{"%[^mdyYHMS]|%[mdyYHMS]%[mdyYHMS]"};
smatch match;
if (!regex_search(dim.units, match,nonStandardDateTimes))
{
timeType=regular;
static regex thisTimeFormat{"%([mdyYHMS])"};
for (string formatStr=dim.units.empty()? "%Y %m %d %H %M %S": dim.units;
regex_search(formatStr, match, thisTimeFormat);
formatStr=match.suffix())
{format.push_back(match.str(1)[0]);}
return;
}
timeType=time_input_facet;
return;
}
}
any AnyVal::constructAnyFromQuarter(const std::string& s) const
{
// year quarter format expected. Takes the first %Y (or
// %y) and first %Q for year and quarter
// respectively. Everything else is passed to regex, which
// can be used to match complicated patterns.
string pattern;
static greg_month quarterMonth[]={Jan,Apr,Jul,Oct};
int year, quarter;
extract(s,year,quarter);
if (quarter<1 || quarter>4)
throw runtime_error("invalid quarter "+to_string(quarter));
return ptime(date(year, quarterMonth[quarter-1], 1));
}
// handle date formats with any combination of %Y, %m, %d, %H, %M, %S
any AnyVal::constructAnyFromRegular(const std::string& s) const
{
smatch val;
static regex valParser{"(\\d+)"};
int day=1, month=1, year=0, hours=0, minutes=0, seconds=0;
size_t i=0;
for (auto ss=s.c_str(); i<format.size(); ++i)
{
for (; *ss && !isdigit(*ss); ++ss); // skip to next integer field
if (!*ss) break;
int v=strtol(ss, const_cast<char**>(&ss),10);
switch (format[i])
{
case 'd': day=v; break;
case 'm': month=v; break;
case 'y':
if (v>99) throw runtime_error(to_string(v)+" is out of range for %y");
year=v>68? v+1900: v+2000;
break;
case 'Y': year=v; break;
case 'H': hours=v; break;
case 'M': minutes=v; break;
case 'S': seconds=v; break;
}
}
if (!dim.units.empty() && i<format.size()) throw InvalidDate(s, dim.units);
return ptime(date(year,month,day),time_duration(hours,minutes,seconds));
}
any AnyVal::operator()(const std::string& s) const
{
switch (dim.type)
{
case Dimension::string:
if (s.empty()) return " "; // empty strings have a special meaning, so on construction, replace with a blank string
return s;
case Dimension::value:
if (s.empty()) return nan("");
return stod(s);
case Dimension::time:
if (s.empty()) return not_a_date_time;
switch (timeType)
{
case quarter: return constructAnyFromQuarter(s);
case regular: return constructAnyFromRegular(s);
case time_input_facet:
{
// default case - delegate to std::time_input_facet
istringstream is(s);
is.imbue(locale(is.getloc(), new boost::posix_time::time_input_facet(dim.units)));
ptime pt(not_a_date_time);
is>>pt;
if (pt.is_special())
throw InvalidDate(s, dim.units);
return pt;
}
// note: boost time_input_facet too restrictive, so this was a strptime attempt. See Ravel ticket #35
// strptime is not available on Windows alas
}
}
assert(false);
return any(); // shut up compiler warning
}
double diff(const any& x, const any& y)
{
if (x.type!=y.type)
#ifdef CLASSDESC_H
throw runtime_error("incompatible types "+classdesc::to_string(x.type)+" and "+classdesc::to_string(y.type)+" in diff");
#else
throw runtime_error("incompatible types in diff");
#endif
switch (x.type)
{
case Dimension::string:
{
// Hamming distance
double r=abs(double(x.string.length())-double(y.string.length()));
for (size_t i=0; i<x.string.length() && i<y.string.length(); ++i)
r += x.string[i]!=y.string[i];
return x.string<y.string? -r: r;
}
case Dimension::value:
return x.value-y.value;
case Dimension::time:
{
auto d=x.time-y.time;
auto cutoff=hours(1000000);
if (d<cutoff && d>-cutoff) // arbitrary cutoff, but well below overflow - million hours = a bit over a century
return 1e-9*d.total_nanoseconds();
return 1e-6*d.total_microseconds();
}
}
assert(false);
return 0; // should not be here
}
// format a string with two integer arguments
string formatString(const std::string& format, int i, int j)
{
char r[512];
snprintf(r,sizeof(r),format.c_str(),i,j);
return r;
}
string str(const any& v, const string& format)
{
switch (v.type)
{
case Dimension::string: return v.string;
case Dimension::value: return to_string(v.value);
case Dimension::time:
{
string::size_type pq;
if (format.empty())
return to_iso_extended_string(v.time);
if ((pq=format.find("%Q"))!=string::npos)
{
auto pY=format.find("%Y");
if (pY==string::npos)
throw runtime_error("year not specified in format string");
// replace %Q and %Y with %d
string sformat=format;
for (size_t i=1; i<sformat.size(); ++i)
if (sformat[i-1]=='%' && (sformat[i]=='Q' || sformat[i]=='Y'))
sformat[i]='d';
char result[100];
auto tm=to_tm(v.time.date());
if (pq<pY)
return formatString(sformat,tm.tm_mon/3+1, tm.tm_year+1900);
return formatString(sformat, tm.tm_year+1900, tm.tm_mon/3+1);
}
else
{
unique_ptr<time_facet> facet(new time_facet(format.c_str()));
ostringstream os;
os.imbue(locale(os.getloc(), facet.release()));
os<<v.time;
return os.str();
}
}
}
assert(false); // shouldn't be here
return {};
}
string XVector::timeFormat() const
{
if (dimension.type!=Dimension::time || empty()) return "";
static const auto day=hours(24);
static const auto month=day*30;
static const auto year=day*365;
auto f=front().time, b=back().time;
if (f>b) std::swap(f,b);
auto dt=b-f;
if (dt > year*5)
return "%Y";
if (dt > year)
return "%b %Y";
if (dt > month*6)
return "%b";
if (dt > month)
return "%d %b";
if (dt > day)
return "%d %H:%M";
if (dt > hours(1))
return "%H:%M";
if (dt > minutes(1))
return "%M:%S";
return "%s";
}
void XVector::imposeDimension()
{
// check if anything to be done
switch (dimension.type)
{
case Dimension::string:
if (checkType<string>()) return;
break;
case Dimension::value:
if (checkType<double>()) return;
break;
case Dimension::time:
if (checkType<ptime>()) return;
break;
}
for (auto& i: *this)
i=anyVal(dimension, str(i));
assert(checkThisType());
}
}
#ifdef CLASSDESC
namespace classdesc
{
void json_pack(json_unpack_t& j, const std::string&, civita::XVector& x)
{
json_pack(j,"",static_cast<civita::NamedDimension&>(x));
vector<string> sliceLabels;
for (auto& i: x)
sliceLabels.push_back(str(i, x.dimension.units));
json_pack(j,".slices",sliceLabels);
}
void json_unpack(json_unpack_t& j, const std::string&, civita::XVector& x)
{
x.clear();
json_unpack(j,"",static_cast<civita::NamedDimension&>(x));
vector<string> slices;
json_unpack(j,".slices",slices);
for (auto& i: slices) x.push_back(i);
}
}
#include "dimension.jcd"
#include "xvector.jcd"
#include <classdesc_epilogue.h>
#endif