-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxvector.h
More file actions
185 lines (168 loc) · 6.29 KB
/
Copy pathxvector.h
File metadata and controls
185 lines (168 loc) · 6.29 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
/*
@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/>.
*/
#ifndef CIVITA_XVECTOR_H
#define CIVITA_XVECTOR_H
#include "dimension.h"
#include <boost/date_time.hpp>
#include <vector>
#include <regex>
#include <initializer_list>
#ifdef CLASSDESC
#include <classdesc_access.h>
#endif
namespace civita
{
// internal class for extracting quarter/year data
class Extractor
{
std::regex pattern;
bool swapVars=false;
std::string rePat;
#ifdef CLASSDESC
CLASSDESC_ACCESS(Extractor);
#endif
public:
/// initialise regex pattern
/// @param fmt dimenion unit string describing this time type
/// @param pq location of %Q in fmt string
void setPattern(const std::string& fmt, size_t pq);
/// Extract year and date values from \a data
/// If swapVars is true, then var1 is quarter, var2 is year, otherwise vice-versa
void operator()(const std::string& data, int& var1, int& var2) const;
};
/// convert string rep to an any rep
///two phase caching data independent computation for ptime conversion
class AnyVal
{
public:
enum TimeType {quarter, regular, time_input_facet};
AnyVal()=default;
AnyVal(const Dimension& dim) {setDimension(dim);}
void setDimension(const Dimension&);
const Dimension& dimension() const {return dim;}
any operator()(const std::string&) const;
private:
#ifdef CLASSDESC
CLASSDESC_ACCESS(AnyVal);
#endif
Dimension dim;
std::string format;
TimeType timeType=time_input_facet;
any constructAnyFromQuarter(const std::string&) const;
any constructAnyFromRegular(const std::string&) const;
Extractor extract;
};
/// convert string rep to an any rep
inline any anyVal(const Dimension& dim, const std::string& s)
{return AnyVal(dim)(s);}
/// return absolute difference between any elements
/// for strings, returns hamming distance
/// for time, returns seconds
/// @throw if any is an incompatible type with dimension
double diff(const any& x, const any& y);
struct AnyLess
{
bool operator()(const any& x, const any& y) const {return x<y;}
};
struct AnyVectorLess
{
bool operator()(const std::vector<any>& x, const std::vector<any>& y)
{return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end(),AnyLess());}
};
/// labels describing the points along dimensions. These can be strings (text type), time values (boost::posix_time type) or numerical values (double)
class XVector: public NamedDimension, public std::vector<any>
{
#ifdef CLASSDESC
CLASSDESC_ACCESS(XVector);
#endif
public:
typedef std::vector<any> V;
XVector() {}
XVector(const std::string& name, const Dimension& dimension={}, const V& v={}): NamedDimension(name,dimension), V(v) {}
XVector(const std::string& name, const Dimension& dimension, const std::initializer_list<const char*>& v): NamedDimension(name, dimension)
{for (auto i: v) push_back(i);}
bool operator==(const XVector& x) const {return static_cast<const V&>(*this)==x;}
void push_back(const std::string&);
void push_back(const char* x) {push_back(std::string(x));}
using V::push_back;
/// best time format given range of data for plot xticks and spreadsheet labels
std::string timeFormat() const;
/// rewrites the labels according to dimension
void imposeDimension();
/// @return true if all elements of this are of type T
template <class T>
bool checkType() const {
for (auto& i:*this)
if (i.type!=dimensionTypeOf<T>())
return false;
return true;
}
/// @return true if all elements have type described by @a dimension
bool checkThisType() const {
switch (dimension.type)
{
case Dimension::string:
return checkType<std::string>();
case Dimension::value:
return checkType<double>();
case Dimension::time:
return checkType<boost::posix_time::ptime>();
}
return false;
}
private:
/// cached AnyVal template for push operations
AnyVal pushTemplate;
};
}
#ifdef CLASSDESC
#define CLASSDESC_json_pack___civita__XVector
#define CLASSDESC_json_unpack___civita__XVector
#include <json_pack_base.h>
#include <xml_pack_base.h>
namespace classdesc
{
void json_pack(json_unpack_t& j, const std::string&, civita::XVector& x);
void json_unpack(json_unpack_t& j, const std::string&, civita::XVector& x);
}
// Extractor doesn't contain any state information, but Classdesc
// can't recognise this, so create null descriptors.
#define CLASSDESC_pack___civita__Extractor
#define CLASSDESC_unpack___civita__Extractor
#define CLASSDESC_json_pack___civita__Extractor
#define CLASSDESC_json_unpack___civita__Extractor
#define CLASSDESC_xml_pack___civita__Extractor
#define CLASSDESC_xml_unpack___civita__Extractor
#define CLASSDESC_random_init___civita__Extractor
namespace classdesc_access
{
template <> struct access_pack<civita::Extractor>:
public classdesc::NullDescriptor<classdesc::pack_t> {};
template <> struct access_unpack<civita::Extractor>:
public classdesc::NullDescriptor<classdesc::unpack_t> {};
template <> struct access_json_pack<civita::Extractor>:
public classdesc::NullDescriptor<classdesc::json_pack_t> {};
template <> struct access_json_unpack<civita::Extractor>:
public classdesc::NullDescriptor<classdesc::json_unpack_t> {};
template <> struct access_xml_pack<civita::Extractor>:
public classdesc::NullDescriptor<classdesc::xml_pack_t> {};
template <> struct access_xml_unpack<civita::Extractor>:
public classdesc::NullDescriptor<classdesc::xml_unpack_t> {};
template <> struct access_random_init<civita::Extractor>:
public classdesc::NullDescriptor<classdesc::random_init_t> {};
}
#endif
#endif