-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxvector.h
More file actions
141 lines (127 loc) · 4.71 KB
/
xvector.h
File metadata and controls
141 lines (127 loc) · 4.71 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
/*
@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>
namespace civita
{
// internal class for extracting quarter/year data
class Extractor
{
std::regex pattern;
bool swapVars=false;
std::string rePat;
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
{
Dimension dim;
std::string format;
enum TimeType {quarter, regular, time_input_facet} timeType=time_input_facet;
any constructAnyFromQuarter(const std::string&) const;
any constructAnyFromRegular(const std::string&) const;
Extractor extract;
public:
AnyVal()=default;
AnyVal(const Dimension& dim) {setDimension(dim);}
void setDimension(const Dimension&);
const Dimension& dimension() const {return dim;}
any operator()(const std::string&) const;
};
/// 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>
{
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
#pragma omit json_pack civita::xvector
#pragma omit json_unpack civita::xvector
#include <json_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);
}
#endif
#endif