tomoto 0.1.4 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/ext/tomoto/ct.cpp +8 -4
- data/ext/tomoto/dmr.cpp +10 -4
- data/ext/tomoto/dt.cpp +13 -4
- data/ext/tomoto/extconf.rb +1 -1
- data/ext/tomoto/gdmr.cpp +14 -6
- data/ext/tomoto/hdp.cpp +9 -4
- data/ext/tomoto/hlda.cpp +9 -4
- data/ext/tomoto/hpa.cpp +9 -4
- data/ext/tomoto/lda.cpp +8 -4
- data/ext/tomoto/llda.cpp +8 -4
- data/ext/tomoto/mglda.cpp +11 -1
- data/ext/tomoto/pa.cpp +9 -4
- data/ext/tomoto/plda.cpp +8 -4
- data/ext/tomoto/slda.cpp +13 -5
- data/lib/tomoto/gdmr.rb +2 -2
- data/lib/tomoto/version.rb +1 -1
- data/vendor/EigenRand/EigenRand/Core.h +6 -1107
- data/vendor/EigenRand/EigenRand/Dists/Basic.h +490 -43
- data/vendor/EigenRand/EigenRand/Dists/Discrete.h +916 -285
- data/vendor/EigenRand/EigenRand/Dists/GammaPoisson.h +85 -36
- data/vendor/EigenRand/EigenRand/Dists/NormalExp.h +1038 -290
- data/vendor/EigenRand/EigenRand/EigenRand +2 -2
- data/vendor/EigenRand/EigenRand/Macro.h +4 -4
- data/vendor/EigenRand/EigenRand/MorePacketMath.h +54 -22
- data/vendor/EigenRand/EigenRand/MvDists/Multinomial.h +222 -0
- data/vendor/EigenRand/EigenRand/MvDists/MvNormal.h +492 -0
- data/vendor/EigenRand/EigenRand/PacketFilter.h +2 -2
- data/vendor/EigenRand/EigenRand/PacketRandomEngine.h +2 -2
- data/vendor/EigenRand/EigenRand/RandUtils.h +65 -11
- data/vendor/EigenRand/EigenRand/doc.h +142 -25
- data/vendor/EigenRand/LICENSE +1 -1
- data/vendor/EigenRand/README.md +109 -24
- data/vendor/tomotopy/README.kr.rst +27 -6
- data/vendor/tomotopy/README.rst +29 -8
- data/vendor/tomotopy/src/Labeling/FoRelevance.cpp +60 -12
- data/vendor/tomotopy/src/Labeling/FoRelevance.h +2 -2
- data/vendor/tomotopy/src/Labeling/Phraser.hpp +33 -21
- data/vendor/tomotopy/src/TopicModel/CT.h +8 -5
- data/vendor/tomotopy/src/TopicModel/CTModel.cpp +2 -6
- data/vendor/tomotopy/src/TopicModel/CTModel.hpp +29 -23
- data/vendor/tomotopy/src/TopicModel/DMR.h +33 -4
- data/vendor/tomotopy/src/TopicModel/DMRModel.cpp +2 -6
- data/vendor/tomotopy/src/TopicModel/DMRModel.hpp +231 -57
- data/vendor/tomotopy/src/TopicModel/DT.h +24 -5
- data/vendor/tomotopy/src/TopicModel/DTModel.cpp +2 -8
- data/vendor/tomotopy/src/TopicModel/DTModel.hpp +41 -28
- data/vendor/tomotopy/src/TopicModel/GDMR.h +31 -5
- data/vendor/tomotopy/src/TopicModel/GDMRModel.cpp +2 -7
- data/vendor/tomotopy/src/TopicModel/GDMRModel.hpp +211 -104
- data/vendor/tomotopy/src/TopicModel/HDP.h +11 -2
- data/vendor/tomotopy/src/TopicModel/HDPModel.cpp +2 -6
- data/vendor/tomotopy/src/TopicModel/HDPModel.hpp +52 -45
- data/vendor/tomotopy/src/TopicModel/HLDA.h +11 -2
- data/vendor/tomotopy/src/TopicModel/HLDAModel.cpp +2 -6
- data/vendor/tomotopy/src/TopicModel/HLDAModel.hpp +13 -16
- data/vendor/tomotopy/src/TopicModel/HPA.h +5 -2
- data/vendor/tomotopy/src/TopicModel/HPAModel.cpp +2 -6
- data/vendor/tomotopy/src/TopicModel/HPAModel.hpp +51 -21
- data/vendor/tomotopy/src/TopicModel/LDA.h +9 -2
- data/vendor/tomotopy/src/TopicModel/LDACVB0Model.hpp +8 -8
- data/vendor/tomotopy/src/TopicModel/LDAModel.cpp +2 -6
- data/vendor/tomotopy/src/TopicModel/LDAModel.hpp +70 -28
- data/vendor/tomotopy/src/TopicModel/LLDA.h +1 -2
- data/vendor/tomotopy/src/TopicModel/LLDAModel.cpp +2 -6
- data/vendor/tomotopy/src/TopicModel/LLDAModel.hpp +22 -12
- data/vendor/tomotopy/src/TopicModel/MGLDA.h +12 -3
- data/vendor/tomotopy/src/TopicModel/MGLDAModel.cpp +2 -10
- data/vendor/tomotopy/src/TopicModel/MGLDAModel.hpp +42 -19
- data/vendor/tomotopy/src/TopicModel/PA.h +9 -4
- data/vendor/tomotopy/src/TopicModel/PAModel.cpp +2 -6
- data/vendor/tomotopy/src/TopicModel/PAModel.hpp +48 -25
- data/vendor/tomotopy/src/TopicModel/PLDA.h +13 -2
- data/vendor/tomotopy/src/TopicModel/PLDAModel.cpp +2 -6
- data/vendor/tomotopy/src/TopicModel/PLDAModel.hpp +27 -19
- data/vendor/tomotopy/src/TopicModel/PT.h +12 -5
- data/vendor/tomotopy/src/TopicModel/PTModel.cpp +2 -3
- data/vendor/tomotopy/src/TopicModel/PTModel.hpp +29 -14
- data/vendor/tomotopy/src/TopicModel/SLDA.h +18 -6
- data/vendor/tomotopy/src/TopicModel/SLDAModel.cpp +2 -10
- data/vendor/tomotopy/src/TopicModel/SLDAModel.hpp +93 -43
- data/vendor/tomotopy/src/TopicModel/TopicModel.hpp +58 -23
- data/vendor/tomotopy/src/Utils/AliasMethod.hpp +6 -6
- data/vendor/tomotopy/src/Utils/Dictionary.h +11 -0
- data/vendor/tomotopy/src/Utils/SharedString.hpp +26 -1
- data/vendor/tomotopy/src/Utils/Trie.hpp +46 -21
- data/vendor/tomotopy/src/Utils/Utils.hpp +99 -14
- data/vendor/tomotopy/src/Utils/exception.h +1 -1
- data/vendor/tomotopy/src/Utils/math.h +5 -7
- data/vendor/tomotopy/src/Utils/serializer.hpp +329 -201
- data/vendor/tomotopy/src/Utils/text.hpp +8 -0
- data/vendor/tomotopy/src/Utils/tvector.hpp +49 -7
- metadata +9 -7
|
@@ -10,25 +10,110 @@
|
|
|
10
10
|
|
|
11
11
|
namespace tomoto
|
|
12
12
|
{
|
|
13
|
-
template<typename T, typename... Args,
|
|
14
|
-
typename std::enable_if<!std::is_array<T>::value, int>::type = 0>
|
|
15
|
-
std::unique_ptr<T> make_unique(Args&&... args)
|
|
16
|
-
{
|
|
17
|
-
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
template<typename T,
|
|
21
|
-
typename std::enable_if<std::is_array<T>::value, int>::type = 0>
|
|
22
|
-
std::unique_ptr<T> make_unique(size_t size)
|
|
23
|
-
{
|
|
24
|
-
return std::unique_ptr<T>(new typename std::remove_extent<T>::type [size]);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
13
|
template<typename T>
|
|
28
14
|
constexpr T * as_mutable(const T * value) noexcept {
|
|
29
15
|
return const_cast<T *>(value);
|
|
30
16
|
}
|
|
31
17
|
|
|
18
|
+
template <typename T>
|
|
19
|
+
class PreventCopy : public T
|
|
20
|
+
{
|
|
21
|
+
public:
|
|
22
|
+
template <typename... Args>
|
|
23
|
+
PreventCopy(Args&&... args) :
|
|
24
|
+
T(std::forward<Args>(args)...)
|
|
25
|
+
{
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
PreventCopy(const PreventCopy& from)
|
|
29
|
+
{
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
PreventCopy(PreventCopy&& from) :
|
|
33
|
+
T(static_cast<T&&>(from))
|
|
34
|
+
{
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
PreventCopy& operator=(const T& from)
|
|
38
|
+
{
|
|
39
|
+
T::operator=(from);
|
|
40
|
+
return *this;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
PreventCopy& operator=(T&& from)
|
|
44
|
+
{
|
|
45
|
+
T::operator=(std::move(from));
|
|
46
|
+
return *this;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
PreventCopy& operator=(const PreventCopy& from)
|
|
50
|
+
{
|
|
51
|
+
return *this;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
PreventCopy& operator=(PreventCopy&& from)
|
|
55
|
+
{
|
|
56
|
+
T::operator=(static_cast<T&&>(from));
|
|
57
|
+
return *this;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
template <typename T, typename Delegator>
|
|
62
|
+
class DelegateCopy : public T
|
|
63
|
+
{
|
|
64
|
+
public:
|
|
65
|
+
template <typename... Args>
|
|
66
|
+
DelegateCopy(Args&&... args) :
|
|
67
|
+
T(std::forward<Args>(args)...)
|
|
68
|
+
{
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
DelegateCopy(const T& from) :
|
|
72
|
+
T(Delegator{}(from))
|
|
73
|
+
{
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
DelegateCopy(const DelegateCopy& from) :
|
|
77
|
+
T(Delegator{}(from))
|
|
78
|
+
{
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
DelegateCopy(T&& from) :
|
|
82
|
+
T(static_cast<T&&>(from))
|
|
83
|
+
{
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
DelegateCopy(DelegateCopy&& from) :
|
|
87
|
+
T(static_cast<T&&>(from))
|
|
88
|
+
{
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
DelegateCopy& operator=(const T& from)
|
|
92
|
+
{
|
|
93
|
+
T::operator=(from);
|
|
94
|
+
return *this;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
DelegateCopy& operator=(T&& from)
|
|
98
|
+
{
|
|
99
|
+
T::operator=(std::move(from));
|
|
100
|
+
return *this;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
DelegateCopy& operator=(const DelegateCopy& from)
|
|
104
|
+
{
|
|
105
|
+
T::operator=(Delegator{}(from));
|
|
106
|
+
return *this;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
DelegateCopy& operator=(DelegateCopy&& from)
|
|
110
|
+
{
|
|
111
|
+
T::operator=(static_cast<T&&>(from));
|
|
112
|
+
return *this;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
|
|
32
117
|
template<bool _lock>
|
|
33
118
|
class OptionalLock : public std::lock_guard<std::mutex>
|
|
34
119
|
{
|
|
@@ -155,30 +155,28 @@ namespace tomoto
|
|
|
155
155
|
|
|
156
156
|
// approximation : lgamma(z) ~= (z+2.5)ln(z+3) - z - 3 + 0.5 ln (2pi) + 1/12/(z + 3) - ln (z(z+1)(z+2))
|
|
157
157
|
template<class _T>
|
|
158
|
-
inline auto lgammaApprox(_T z)
|
|
158
|
+
inline auto lgammaApprox(_T z)
|
|
159
159
|
{
|
|
160
160
|
return (z + 2.5) * log(z + 3) - (z + 3) + 0.91893853 + 1. / 12. / (z + 3) - log(z * (z + 1) * (z + 2));
|
|
161
161
|
}
|
|
162
162
|
|
|
163
163
|
// calc lgamma(z + a) - lgamma(z)
|
|
164
164
|
template<class _T, class _U>
|
|
165
|
-
inline auto lgammaSubt(_T z, _U a)
|
|
165
|
+
inline auto lgammaSubt(_T z, _U a)
|
|
166
166
|
{
|
|
167
|
-
return (z + a + 1.5) * log(z + a + 2) - (z + 1.5) * log(z + 2) - a + (1. / (z + a + 2) - 1. / (z + 2)) / 12. - log((
|
|
167
|
+
return (z + a + 1.5) * log(z + a + 2) - (z + 1.5) * log(z + 2) - a + (1. / (z + a + 2) - 1. / (z + 2)) / 12. - log((z + a) / z * (z + a + 1) / (z + 1));
|
|
168
168
|
}
|
|
169
169
|
|
|
170
170
|
// approximation : digamma(z) ~= ln(z+4) - 1/2/(z+4) - 1/12/(z+4)^2 - 1/z - 1/(z+1) - 1/(z+2) - 1/(z+3)
|
|
171
171
|
template<class _T>
|
|
172
|
-
inline auto digammaApprox(_T z)
|
|
172
|
+
inline auto digammaApprox(_T z)
|
|
173
173
|
{
|
|
174
174
|
return log(z + 4) - 1. / 2. / (z + 4) - 1. / 12. / ((z + 4) * (z + 4)) - 1. / z - 1. / (z + 1) - 1. / (z + 2) - 1. / (z + 3);
|
|
175
175
|
}
|
|
176
176
|
|
|
177
177
|
// calc digamma(z + a) - digamma(z)
|
|
178
178
|
template<class _T, class _U>
|
|
179
|
-
inline auto digammaSubt(_T z, _U a)
|
|
180
|
-
- 1. / (z + a) - 1. / (z + a + 1)
|
|
181
|
-
- 1. / z - 1. / (z + 1))
|
|
179
|
+
inline auto digammaSubt(_T z, _U a)
|
|
182
180
|
{
|
|
183
181
|
return log((z + a + 2) / (z + 2)) - (1 / (z + a + 2) - 1 / (z + 2)) / 2 - (1 / (z + a + 2) / (z + a + 2) - 1 / (z + 2) / (z + 2)) / 12
|
|
184
182
|
- 1. / (z + a) - 1. / (z + a + 1)
|
|
@@ -4,11 +4,11 @@
|
|
|
4
4
|
#include <string>
|
|
5
5
|
#include <array>
|
|
6
6
|
#include <type_traits>
|
|
7
|
-
#include <Eigen/Dense>
|
|
8
7
|
#include <vector>
|
|
9
|
-
#include
|
|
8
|
+
#include <map>
|
|
9
|
+
#include <unordered_map>
|
|
10
|
+
#include <Eigen/Dense>
|
|
10
11
|
#include "text.hpp"
|
|
11
|
-
#include "SharedString.hpp"
|
|
12
12
|
|
|
13
13
|
/*
|
|
14
14
|
|
|
@@ -30,6 +30,40 @@ namespace tomoto
|
|
|
30
30
|
{
|
|
31
31
|
namespace serializer
|
|
32
32
|
{
|
|
33
|
+
struct membuf : std::streambuf
|
|
34
|
+
{
|
|
35
|
+
membuf(char* base, std::ptrdiff_t n)
|
|
36
|
+
{
|
|
37
|
+
this->setg(base, base, base + n);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
pos_type seekpos(pos_type sp, std::ios_base::openmode which) override {
|
|
41
|
+
return seekoff(sp - pos_type(off_type(0)), std::ios_base::beg, which);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
pos_type seekoff(off_type off,
|
|
45
|
+
std::ios_base::seekdir dir,
|
|
46
|
+
std::ios_base::openmode which = std::ios_base::in) override {
|
|
47
|
+
if (dir == std::ios_base::cur)
|
|
48
|
+
gbump(off);
|
|
49
|
+
else if (dir == std::ios_base::end)
|
|
50
|
+
setg(eback(), egptr() + off, egptr());
|
|
51
|
+
else if (dir == std::ios_base::beg)
|
|
52
|
+
setg(eback(), eback() + off, egptr());
|
|
53
|
+
return gptr() - eback();
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
class imstream : public std::istream
|
|
58
|
+
{
|
|
59
|
+
membuf buf;
|
|
60
|
+
public:
|
|
61
|
+
imstream(const char* base, std::ptrdiff_t n)
|
|
62
|
+
: std::istream(&buf), buf((char*)base, n)
|
|
63
|
+
{
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
33
67
|
namespace detail
|
|
34
68
|
{
|
|
35
69
|
template<class _T> using Invoke = typename _T::type;
|
|
@@ -133,15 +167,49 @@ namespace tomoto
|
|
|
133
167
|
return Key<_n>{detail::to_arrayz(a)};
|
|
134
168
|
}
|
|
135
169
|
|
|
170
|
+
template<typename _Ty, typename = void>
|
|
171
|
+
struct Serializer;
|
|
172
|
+
|
|
173
|
+
template<typename _Ty, size_t _version = 0, typename = void>
|
|
174
|
+
struct SerializerV;
|
|
175
|
+
|
|
176
|
+
template<typename _Ty>
|
|
177
|
+
inline void writeToStream(std::ostream& ostr, const _Ty& v)
|
|
178
|
+
{
|
|
179
|
+
Serializer<
|
|
180
|
+
typename std::remove_const<typename std::remove_reference<_Ty>::type>::type
|
|
181
|
+
>{}.write(ostr, v);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
template<typename _Ty>
|
|
185
|
+
inline void readFromStream(std::istream& istr, _Ty& v)
|
|
186
|
+
{
|
|
187
|
+
Serializer<
|
|
188
|
+
typename std::remove_const<typename std::remove_reference<_Ty>::type>::type
|
|
189
|
+
>{}.read(istr, v);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
template<typename _Ty>
|
|
193
|
+
inline _Ty readFromStream(std::istream& istr)
|
|
194
|
+
{
|
|
195
|
+
_Ty v;
|
|
196
|
+
Serializer<
|
|
197
|
+
typename std::remove_const<typename std::remove_reference<_Ty>::type>::type
|
|
198
|
+
>{}.read(istr, v);
|
|
199
|
+
return v;
|
|
200
|
+
}
|
|
201
|
+
|
|
136
202
|
inline void writeMany(std::ostream& ostr)
|
|
137
203
|
{
|
|
138
204
|
// do nothing
|
|
139
205
|
}
|
|
140
206
|
|
|
141
207
|
template<typename _FirstTy, typename ... _RestTy>
|
|
142
|
-
inline typename std::enable_if
|
|
208
|
+
inline typename std::enable_if<
|
|
209
|
+
!is_key<typename std::remove_reference<_FirstTy>::type>::value
|
|
210
|
+
>::type writeMany(std::ostream& ostr, _FirstTy&& first, _RestTy&&... rest)
|
|
143
211
|
{
|
|
144
|
-
writeToStream(ostr, first);
|
|
212
|
+
writeToStream(ostr, std::forward<_FirstTy>(first));
|
|
145
213
|
writeMany(ostr, std::forward<_RestTy>(rest)...);
|
|
146
214
|
}
|
|
147
215
|
|
|
@@ -158,9 +226,11 @@ namespace tomoto
|
|
|
158
226
|
}
|
|
159
227
|
|
|
160
228
|
template<typename _FirstTy, typename ... _RestTy>
|
|
161
|
-
inline typename std::enable_if
|
|
229
|
+
inline typename std::enable_if<
|
|
230
|
+
!is_key<typename std::remove_reference<_FirstTy>::type>::value
|
|
231
|
+
>::type readMany(std::istream& istr, _FirstTy&& first, _RestTy&&... rest)
|
|
162
232
|
{
|
|
163
|
-
readFromStream(istr, first);
|
|
233
|
+
readFromStream(istr, std::forward<_FirstTy>(first));
|
|
164
234
|
readMany(istr, std::forward<_RestTy>(rest)...);
|
|
165
235
|
}
|
|
166
236
|
|
|
@@ -234,255 +304,313 @@ namespace tomoto
|
|
|
234
304
|
template<typename _Ty, size_t _version>
|
|
235
305
|
struct hasLoadV : decltype(detail::testLoadV<_Ty, _version>(0)){};
|
|
236
306
|
|
|
237
|
-
template<
|
|
238
|
-
|
|
307
|
+
template<typename _Ty>
|
|
308
|
+
struct Serializer<_Ty, typename std::enable_if<std::is_fundamental<_Ty>::value>::type>
|
|
239
309
|
{
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
310
|
+
void write(std::ostream& ostr, const _Ty& v)
|
|
311
|
+
{
|
|
312
|
+
if (!ostr.write((const char*)&v, sizeof(_Ty)))
|
|
313
|
+
throw std::ios_base::failure(std::string("writing type '") + typeid(_Ty).name() + std::string("' is failed"));
|
|
314
|
+
}
|
|
243
315
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
}
|
|
316
|
+
void read(std::istream& istr, _Ty& v)
|
|
317
|
+
{
|
|
318
|
+
if (!istr.read((char*)&v, sizeof(_Ty)))
|
|
319
|
+
throw std::ios_base::failure(std::string("reading type '") + typeid(_Ty).name() + std::string("' is failed"));
|
|
320
|
+
}
|
|
321
|
+
};
|
|
250
322
|
|
|
251
|
-
template<
|
|
252
|
-
|
|
323
|
+
template<typename _Ty>
|
|
324
|
+
struct Serializer<_Ty, typename std::enable_if<hasSave<_Ty>::value>::type>
|
|
253
325
|
{
|
|
254
|
-
|
|
255
|
-
|
|
326
|
+
void write(std::ostream& ostr, const _Ty& v)
|
|
327
|
+
{
|
|
328
|
+
v.serializerWrite(ostr);
|
|
329
|
+
}
|
|
256
330
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
v.serializerWrite(version_holder<_version>{}, ostr);
|
|
263
|
-
}
|
|
331
|
+
void read(std::istream& istr, _Ty& v)
|
|
332
|
+
{
|
|
333
|
+
v.serializerRead(istr);
|
|
334
|
+
}
|
|
335
|
+
};
|
|
264
336
|
|
|
265
|
-
template<
|
|
266
|
-
|
|
267
|
-
hasSaveV<_Ty, _version>::value && hasSaveV<_Ty, _version + 1>::value
|
|
268
|
-
>::type writeToBinStreamImpl(std::ostream& ostr, const _Ty& v)
|
|
337
|
+
template<typename _Ty>
|
|
338
|
+
struct Serializer<_Ty, typename std::enable_if<hasSaveV<_Ty, 0>::value>::type>
|
|
269
339
|
{
|
|
270
|
-
|
|
271
|
-
|
|
340
|
+
void write(std::ostream& ostr, const _Ty& v)
|
|
341
|
+
{
|
|
342
|
+
SerializerV<_Ty>{}.write(ostr, v);
|
|
343
|
+
}
|
|
272
344
|
|
|
273
|
-
|
|
274
|
-
|
|
345
|
+
void read(std::istream& istr, _Ty& v)
|
|
346
|
+
{
|
|
347
|
+
SerializerV<_Ty>{}.read(istr, v);
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
template<typename _Ty, size_t _version>
|
|
352
|
+
struct SerializerV<_Ty, _version, typename std::enable_if<
|
|
353
|
+
hasSaveV<_Ty, _version>::value && !hasSaveV<_Ty, _version + 1>::value
|
|
354
|
+
>::type>
|
|
275
355
|
{
|
|
276
|
-
v
|
|
277
|
-
|
|
356
|
+
void write(std::ostream& ostr, const _Ty& v)
|
|
357
|
+
{
|
|
358
|
+
v.serializerWrite(version_holder<_version>{}, ostr);
|
|
359
|
+
}
|
|
278
360
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
v.serializerRead(version_holder<_version>{}, istr);
|
|
285
|
-
}
|
|
361
|
+
void read(std::istream& istr, _Ty& v)
|
|
362
|
+
{
|
|
363
|
+
v.serializerRead(version_holder<_version>{}, istr);
|
|
364
|
+
}
|
|
365
|
+
};
|
|
286
366
|
|
|
287
|
-
template<
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
>::type
|
|
367
|
+
template<typename _Ty, size_t _version>
|
|
368
|
+
struct SerializerV<_Ty, _version, typename std::enable_if<
|
|
369
|
+
hasSaveV<_Ty, _version>::value && hasSaveV<_Ty, _version + 1>::value
|
|
370
|
+
>::type>
|
|
291
371
|
{
|
|
292
|
-
|
|
293
|
-
try
|
|
372
|
+
void write(std::ostream& ostr, const _Ty& v)
|
|
294
373
|
{
|
|
295
|
-
|
|
296
|
-
return readFromBinStreamImpl<_Ty, _version + 1>(istr, v);
|
|
374
|
+
SerializerV<_Ty, _version + 1>{}.write(ostr, v);
|
|
297
375
|
}
|
|
298
|
-
|
|
376
|
+
|
|
377
|
+
void read(std::istream& istr, _Ty& v)
|
|
299
378
|
{
|
|
300
|
-
istr.
|
|
301
|
-
|
|
302
|
-
|
|
379
|
+
auto pos = istr.tellg();
|
|
380
|
+
try
|
|
381
|
+
{
|
|
382
|
+
// try higher version first
|
|
383
|
+
return SerializerV<_Ty, _version + 1>{}.read(istr, v);
|
|
384
|
+
}
|
|
385
|
+
catch (const std::ios_base::failure&)
|
|
386
|
+
{
|
|
387
|
+
istr.seekg(pos);
|
|
388
|
+
// try current version if fails
|
|
389
|
+
v.serializerRead(version_holder<_version>{}, istr);
|
|
390
|
+
}
|
|
303
391
|
}
|
|
304
|
-
}
|
|
392
|
+
};
|
|
305
393
|
|
|
306
|
-
template<
|
|
307
|
-
|
|
394
|
+
template<typename _Ty>
|
|
395
|
+
struct Serializer<Eigen::Matrix<_Ty, -1, -1>>
|
|
308
396
|
{
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
397
|
+
using VTy = Eigen::Matrix<_Ty, -1, -1>;
|
|
398
|
+
void write(std::ostream& ostr, const VTy& v)
|
|
399
|
+
{
|
|
400
|
+
writeMany(ostr, (uint32_t)v.rows(), (uint32_t)v.cols());
|
|
401
|
+
if (!ostr.write((const char*)v.data(), sizeof(_Ty) * v.size()))
|
|
402
|
+
throw std::ios_base::failure(std::string("writing type '") + typeid(_Ty).name() + std::string("' is failed"));
|
|
403
|
+
}
|
|
314
404
|
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
}
|
|
405
|
+
void read(std::istream& istr, VTy& v)
|
|
406
|
+
{
|
|
407
|
+
uint32_t rows, cols;
|
|
408
|
+
readMany(istr, rows, cols);
|
|
409
|
+
v = Eigen::Matrix<_Ty, -1, -1>::Zero(rows, cols);
|
|
410
|
+
if (!istr.read((char*)v.data(), sizeof(_Ty) * rows * cols))
|
|
411
|
+
throw std::ios_base::failure(std::string("reading type '") + typeid(_Ty).name() + std::string("' is failed"));
|
|
412
|
+
}
|
|
413
|
+
};
|
|
324
414
|
|
|
325
|
-
template<
|
|
326
|
-
|
|
415
|
+
template<typename _Ty>
|
|
416
|
+
struct Serializer<Eigen::Matrix<_Ty, -1, 1>>
|
|
327
417
|
{
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
418
|
+
using VTy = Eigen::Matrix<_Ty, -1, 1>;
|
|
419
|
+
void write(std::ostream& ostr, const VTy& v)
|
|
420
|
+
{
|
|
421
|
+
writeMany(ostr, (uint32_t)v.rows(), (uint32_t)v.cols());
|
|
422
|
+
if (!ostr.write((const char*)v.data(), sizeof(_Ty) * v.size()))
|
|
423
|
+
throw std::ios_base::failure(std::string("writing type '") + typeid(_Ty).name() + std::string("' is failed"));
|
|
424
|
+
}
|
|
333
425
|
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
}
|
|
426
|
+
void read(std::istream& istr, VTy& v)
|
|
427
|
+
{
|
|
428
|
+
uint32_t rows, cols;
|
|
429
|
+
readMany(istr, rows, cols);
|
|
430
|
+
if (cols != 1) throw std::ios_base::failure("matrix cols != 1");
|
|
431
|
+
v = Eigen::Matrix<_Ty, -1, -1>::Zero(rows, cols);
|
|
432
|
+
if (!istr.read((char*)v.data(), sizeof(_Ty) * rows * cols))
|
|
433
|
+
throw std::ios_base::failure(std::string("reading type '") + typeid(_Ty).name() + std::string("' is failed"));
|
|
434
|
+
}
|
|
435
|
+
};
|
|
344
436
|
|
|
345
|
-
template<
|
|
346
|
-
|
|
437
|
+
template<typename _Ty>
|
|
438
|
+
struct Serializer<PreventCopy<_Ty>> : public Serializer<_Ty>
|
|
347
439
|
{
|
|
348
|
-
|
|
349
|
-
for (auto& e : v) writeToStream(ostr, e);
|
|
350
|
-
}
|
|
440
|
+
};
|
|
351
441
|
|
|
352
|
-
template<
|
|
353
|
-
|
|
442
|
+
template<typename _Ty, typename _Ty2>
|
|
443
|
+
struct Serializer<DelegateCopy<_Ty, _Ty2>> : public Serializer<_Ty>
|
|
354
444
|
{
|
|
355
|
-
|
|
356
|
-
v.resize(size);
|
|
357
|
-
for (auto& e : v) readFromStream(istr, e);
|
|
358
|
-
}
|
|
445
|
+
};
|
|
359
446
|
|
|
360
|
-
template<
|
|
361
|
-
|
|
447
|
+
template<typename _Ty>
|
|
448
|
+
struct Serializer<std::vector<_Ty>, typename std::enable_if<std::is_fundamental<_Ty>::value>::type>
|
|
362
449
|
{
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
450
|
+
using VTy = std::vector<_Ty>;
|
|
451
|
+
void write(std::ostream& ostr, const VTy& v)
|
|
452
|
+
{
|
|
453
|
+
writeToStream(ostr, (uint32_t)v.size());
|
|
454
|
+
if (!ostr.write((const char*)v.data(), sizeof(_Ty) * v.size()))
|
|
455
|
+
throw std::ios_base::failure(std::string("writing type '") + typeid(_Ty).name() + std::string("' is failed"));
|
|
456
|
+
}
|
|
366
457
|
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
458
|
+
void read(std::istream& istr, VTy& v)
|
|
459
|
+
{
|
|
460
|
+
auto size = readFromStream<uint32_t>(istr);
|
|
461
|
+
v.resize(size);
|
|
462
|
+
if (!istr.read((char*)v.data(), sizeof(_Ty) * size))
|
|
463
|
+
throw std::ios_base::failure(std::string("reading type '") + typeid(_Ty).name() + std::string("' is failed"));
|
|
464
|
+
}
|
|
465
|
+
};
|
|
373
466
|
|
|
374
|
-
template<
|
|
375
|
-
|
|
467
|
+
template<typename _Ty>
|
|
468
|
+
struct Serializer<std::vector<_Ty>, typename std::enable_if<!std::is_fundamental<_Ty>::value>::type>
|
|
376
469
|
{
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
470
|
+
using VTy = std::vector<_Ty>;
|
|
471
|
+
void write(std::ostream& ostr, const VTy& v)
|
|
472
|
+
{
|
|
473
|
+
writeToStream(ostr, (uint32_t)v.size());
|
|
474
|
+
for (auto& e : v) Serializer<_Ty>{}.write(ostr, e);
|
|
475
|
+
}
|
|
380
476
|
|
|
381
|
-
|
|
382
|
-
inline void readFromBinStreamImpl(std::istream& istr, std::unordered_map<_KeyTy, _ValTy>& v)
|
|
383
|
-
{
|
|
384
|
-
uint32_t size = readFromStream<uint32_t>(istr);
|
|
385
|
-
v.clear();
|
|
386
|
-
for (size_t i = 0; i < size; ++i)
|
|
477
|
+
void read(std::istream& istr, VTy& v)
|
|
387
478
|
{
|
|
388
|
-
|
|
479
|
+
auto size = readFromStream<uint32_t>(istr);
|
|
480
|
+
v.resize(size);
|
|
481
|
+
for (auto& e : v) Serializer<_Ty>{}.read(istr, e);
|
|
389
482
|
}
|
|
390
|
-
}
|
|
483
|
+
};
|
|
391
484
|
|
|
392
|
-
template<
|
|
393
|
-
|
|
485
|
+
template<typename _Ty, size_t n>
|
|
486
|
+
struct Serializer<std::array<_Ty, n>, typename std::enable_if<std::is_fundamental<_Ty>::value>::type>
|
|
394
487
|
{
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
488
|
+
using VTy = std::array<_Ty, n>;
|
|
489
|
+
void write(std::ostream& ostr, const VTy& v)
|
|
490
|
+
{
|
|
491
|
+
writeToStream(ostr, (uint32_t)v.size());
|
|
492
|
+
if (!ostr.write((const char*)v.data(), sizeof(_Ty) * v.size()))
|
|
493
|
+
throw std::ios_base::failure(std::string("writing type '") + typeid(_Ty).name() + std::string("' is failed"));
|
|
494
|
+
}
|
|
398
495
|
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
496
|
+
void read(std::istream& istr, VTy& v)
|
|
497
|
+
{
|
|
498
|
+
auto size = readFromStream<uint32_t>(istr);
|
|
499
|
+
if (n != size) throw std::ios_base::failure(text::format("the size of array must be %zd, not %zd", n, size));
|
|
500
|
+
if (!istr.read((char*)v.data(), sizeof(_Ty) * size))
|
|
501
|
+
throw std::ios_base::failure(std::string("reading type '") + typeid(_Ty).name() + std::string("' is failed"));
|
|
502
|
+
}
|
|
503
|
+
};
|
|
406
504
|
|
|
407
|
-
template<
|
|
408
|
-
|
|
505
|
+
template<typename _Ty, size_t n>
|
|
506
|
+
struct Serializer<std::array<_Ty, n>, typename std::enable_if<!std::is_fundamental<_Ty>::value>::type>
|
|
409
507
|
{
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
508
|
+
using VTy = std::array<_Ty, n>;
|
|
509
|
+
void write(std::ostream& ostr, const VTy& v)
|
|
510
|
+
{
|
|
511
|
+
writeToStream(ostr, (uint32_t)v.size());
|
|
512
|
+
for (auto& e : v) Serializer<_Ty>{}.write(ostr, e);
|
|
513
|
+
}
|
|
413
514
|
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
}
|
|
515
|
+
void read(std::istream& istr, VTy& v)
|
|
516
|
+
{
|
|
517
|
+
auto size = readFromStream<uint32_t>(istr);
|
|
518
|
+
if (n != size) throw std::ios_base::failure(text::format("the size of array must be %zd, not %zd", n, size));
|
|
519
|
+
for (auto& e : v) Serializer<_Ty>{}.read(istr, e);
|
|
520
|
+
}
|
|
521
|
+
};
|
|
421
522
|
|
|
422
|
-
template<
|
|
423
|
-
|
|
523
|
+
template<typename _Ty>
|
|
524
|
+
struct Serializer<std::basic_string<_Ty>>
|
|
424
525
|
{
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
526
|
+
using VTy = std::basic_string<_Ty>;
|
|
527
|
+
void write(std::ostream& ostr, const VTy& v)
|
|
528
|
+
{
|
|
529
|
+
writeToStream(ostr, (uint32_t)v.size());
|
|
530
|
+
if (!ostr.write((const char*)v.data(), sizeof(_Ty) * v.size()))
|
|
531
|
+
throw std::ios_base::failure(std::string("writing type '") + typeid(_Ty).name() + std::string("' is failed"));
|
|
532
|
+
}
|
|
429
533
|
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
}
|
|
534
|
+
void read(std::istream& istr, VTy& v)
|
|
535
|
+
{
|
|
536
|
+
auto size = readFromStream<uint32_t>(istr);
|
|
537
|
+
v.resize(size);
|
|
538
|
+
if (!istr.read((char*)v.data(), sizeof(_Ty) * size))
|
|
539
|
+
throw std::ios_base::failure(std::string("reading type '") + typeid(_Ty).name() + std::string("' is failed"));
|
|
540
|
+
}
|
|
541
|
+
};
|
|
438
542
|
|
|
439
|
-
|
|
543
|
+
template<typename _Ty1, typename _Ty2>
|
|
544
|
+
struct Serializer<std::pair<_Ty1, _Ty2>>
|
|
440
545
|
{
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
546
|
+
using VTy = std::pair<_Ty1, _Ty2>;
|
|
547
|
+
void write(std::ostream& ostr, const VTy& v)
|
|
548
|
+
{
|
|
549
|
+
writeMany(ostr, v.first, v.second);
|
|
550
|
+
}
|
|
445
551
|
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
throw std::ios_base::failure(std::string("reading type 'SharedString' is failed"));
|
|
452
|
-
v = SharedString{ t.data(), t.data() + t.size() };
|
|
453
|
-
}
|
|
552
|
+
void read(std::istream& istr, VTy& v)
|
|
553
|
+
{
|
|
554
|
+
readMany(istr, v.first, v.second);
|
|
555
|
+
}
|
|
556
|
+
};
|
|
454
557
|
|
|
455
|
-
template<
|
|
456
|
-
|
|
558
|
+
template<typename _Ty1, typename _Ty2>
|
|
559
|
+
struct Serializer<std::unordered_map<_Ty1, _Ty2>>
|
|
457
560
|
{
|
|
458
|
-
|
|
459
|
-
|
|
561
|
+
using VTy = std::unordered_map<_Ty1, _Ty2>;
|
|
562
|
+
void write(std::ostream& ostr, const VTy& v)
|
|
563
|
+
{
|
|
564
|
+
writeToStream(ostr, (uint32_t)v.size());
|
|
565
|
+
for (auto& e : v) writeToStream(ostr, e);
|
|
566
|
+
}
|
|
460
567
|
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
568
|
+
void read(std::istream& istr, VTy& v)
|
|
569
|
+
{
|
|
570
|
+
auto size = readFromStream<uint32_t>(istr);
|
|
571
|
+
v.clear();
|
|
572
|
+
for (size_t i = 0; i < size; ++i)
|
|
573
|
+
{
|
|
574
|
+
v.emplace(readFromStream<std::pair<_Ty1, _Ty2>>(istr));
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
};
|
|
466
578
|
|
|
467
|
-
template<typename
|
|
468
|
-
|
|
579
|
+
template<typename _Ty1, typename _Ty2>
|
|
580
|
+
struct Serializer<std::map<_Ty1, _Ty2>>
|
|
469
581
|
{
|
|
470
|
-
|
|
471
|
-
|
|
582
|
+
using VTy = std::map<_Ty1, _Ty2>;
|
|
583
|
+
void write(std::ostream& ostr, const VTy& v)
|
|
584
|
+
{
|
|
585
|
+
writeToStream(ostr, (uint32_t)v.size());
|
|
586
|
+
for (auto& e : v) writeToStream(ostr, e);
|
|
587
|
+
}
|
|
472
588
|
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
589
|
+
void read(std::istream& istr, VTy& v)
|
|
590
|
+
{
|
|
591
|
+
auto size = readFromStream<uint32_t>(istr);
|
|
592
|
+
v.clear();
|
|
593
|
+
for (size_t i = 0; i < size; ++i)
|
|
594
|
+
{
|
|
595
|
+
v.emplace(readFromStream<std::pair<_Ty1, _Ty2>>(istr));
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
};
|
|
478
599
|
|
|
479
600
|
template<typename _Ty>
|
|
480
|
-
|
|
601
|
+
struct Serializer<std::unique_ptr<_Ty>, typename std::enable_if<std::is_abstract<_Ty>::value>::type>
|
|
481
602
|
{
|
|
482
|
-
_Ty
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
603
|
+
using VTy = std::unique_ptr<_Ty>;
|
|
604
|
+
void write(std::ostream& ostr, const VTy& v)
|
|
605
|
+
{
|
|
606
|
+
_Ty::serializerWrite(v, ostr);
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
void read(std::istream& istr, VTy& v)
|
|
610
|
+
{
|
|
611
|
+
_Ty::serializerRead(v, istr);
|
|
612
|
+
}
|
|
613
|
+
};
|
|
486
614
|
|
|
487
615
|
static auto taggedDataKey = to_key("TPTK");
|
|
488
616
|
|