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.
Files changed (94) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/ext/tomoto/ct.cpp +8 -4
  4. data/ext/tomoto/dmr.cpp +10 -4
  5. data/ext/tomoto/dt.cpp +13 -4
  6. data/ext/tomoto/extconf.rb +1 -1
  7. data/ext/tomoto/gdmr.cpp +14 -6
  8. data/ext/tomoto/hdp.cpp +9 -4
  9. data/ext/tomoto/hlda.cpp +9 -4
  10. data/ext/tomoto/hpa.cpp +9 -4
  11. data/ext/tomoto/lda.cpp +8 -4
  12. data/ext/tomoto/llda.cpp +8 -4
  13. data/ext/tomoto/mglda.cpp +11 -1
  14. data/ext/tomoto/pa.cpp +9 -4
  15. data/ext/tomoto/plda.cpp +8 -4
  16. data/ext/tomoto/slda.cpp +13 -5
  17. data/lib/tomoto/gdmr.rb +2 -2
  18. data/lib/tomoto/version.rb +1 -1
  19. data/vendor/EigenRand/EigenRand/Core.h +6 -1107
  20. data/vendor/EigenRand/EigenRand/Dists/Basic.h +490 -43
  21. data/vendor/EigenRand/EigenRand/Dists/Discrete.h +916 -285
  22. data/vendor/EigenRand/EigenRand/Dists/GammaPoisson.h +85 -36
  23. data/vendor/EigenRand/EigenRand/Dists/NormalExp.h +1038 -290
  24. data/vendor/EigenRand/EigenRand/EigenRand +2 -2
  25. data/vendor/EigenRand/EigenRand/Macro.h +4 -4
  26. data/vendor/EigenRand/EigenRand/MorePacketMath.h +54 -22
  27. data/vendor/EigenRand/EigenRand/MvDists/Multinomial.h +222 -0
  28. data/vendor/EigenRand/EigenRand/MvDists/MvNormal.h +492 -0
  29. data/vendor/EigenRand/EigenRand/PacketFilter.h +2 -2
  30. data/vendor/EigenRand/EigenRand/PacketRandomEngine.h +2 -2
  31. data/vendor/EigenRand/EigenRand/RandUtils.h +65 -11
  32. data/vendor/EigenRand/EigenRand/doc.h +142 -25
  33. data/vendor/EigenRand/LICENSE +1 -1
  34. data/vendor/EigenRand/README.md +109 -24
  35. data/vendor/tomotopy/README.kr.rst +27 -6
  36. data/vendor/tomotopy/README.rst +29 -8
  37. data/vendor/tomotopy/src/Labeling/FoRelevance.cpp +60 -12
  38. data/vendor/tomotopy/src/Labeling/FoRelevance.h +2 -2
  39. data/vendor/tomotopy/src/Labeling/Phraser.hpp +33 -21
  40. data/vendor/tomotopy/src/TopicModel/CT.h +8 -5
  41. data/vendor/tomotopy/src/TopicModel/CTModel.cpp +2 -6
  42. data/vendor/tomotopy/src/TopicModel/CTModel.hpp +29 -23
  43. data/vendor/tomotopy/src/TopicModel/DMR.h +33 -4
  44. data/vendor/tomotopy/src/TopicModel/DMRModel.cpp +2 -6
  45. data/vendor/tomotopy/src/TopicModel/DMRModel.hpp +231 -57
  46. data/vendor/tomotopy/src/TopicModel/DT.h +24 -5
  47. data/vendor/tomotopy/src/TopicModel/DTModel.cpp +2 -8
  48. data/vendor/tomotopy/src/TopicModel/DTModel.hpp +41 -28
  49. data/vendor/tomotopy/src/TopicModel/GDMR.h +31 -5
  50. data/vendor/tomotopy/src/TopicModel/GDMRModel.cpp +2 -7
  51. data/vendor/tomotopy/src/TopicModel/GDMRModel.hpp +211 -104
  52. data/vendor/tomotopy/src/TopicModel/HDP.h +11 -2
  53. data/vendor/tomotopy/src/TopicModel/HDPModel.cpp +2 -6
  54. data/vendor/tomotopy/src/TopicModel/HDPModel.hpp +52 -45
  55. data/vendor/tomotopy/src/TopicModel/HLDA.h +11 -2
  56. data/vendor/tomotopy/src/TopicModel/HLDAModel.cpp +2 -6
  57. data/vendor/tomotopy/src/TopicModel/HLDAModel.hpp +13 -16
  58. data/vendor/tomotopy/src/TopicModel/HPA.h +5 -2
  59. data/vendor/tomotopy/src/TopicModel/HPAModel.cpp +2 -6
  60. data/vendor/tomotopy/src/TopicModel/HPAModel.hpp +51 -21
  61. data/vendor/tomotopy/src/TopicModel/LDA.h +9 -2
  62. data/vendor/tomotopy/src/TopicModel/LDACVB0Model.hpp +8 -8
  63. data/vendor/tomotopy/src/TopicModel/LDAModel.cpp +2 -6
  64. data/vendor/tomotopy/src/TopicModel/LDAModel.hpp +70 -28
  65. data/vendor/tomotopy/src/TopicModel/LLDA.h +1 -2
  66. data/vendor/tomotopy/src/TopicModel/LLDAModel.cpp +2 -6
  67. data/vendor/tomotopy/src/TopicModel/LLDAModel.hpp +22 -12
  68. data/vendor/tomotopy/src/TopicModel/MGLDA.h +12 -3
  69. data/vendor/tomotopy/src/TopicModel/MGLDAModel.cpp +2 -10
  70. data/vendor/tomotopy/src/TopicModel/MGLDAModel.hpp +42 -19
  71. data/vendor/tomotopy/src/TopicModel/PA.h +9 -4
  72. data/vendor/tomotopy/src/TopicModel/PAModel.cpp +2 -6
  73. data/vendor/tomotopy/src/TopicModel/PAModel.hpp +48 -25
  74. data/vendor/tomotopy/src/TopicModel/PLDA.h +13 -2
  75. data/vendor/tomotopy/src/TopicModel/PLDAModel.cpp +2 -6
  76. data/vendor/tomotopy/src/TopicModel/PLDAModel.hpp +27 -19
  77. data/vendor/tomotopy/src/TopicModel/PT.h +12 -5
  78. data/vendor/tomotopy/src/TopicModel/PTModel.cpp +2 -3
  79. data/vendor/tomotopy/src/TopicModel/PTModel.hpp +29 -14
  80. data/vendor/tomotopy/src/TopicModel/SLDA.h +18 -6
  81. data/vendor/tomotopy/src/TopicModel/SLDAModel.cpp +2 -10
  82. data/vendor/tomotopy/src/TopicModel/SLDAModel.hpp +93 -43
  83. data/vendor/tomotopy/src/TopicModel/TopicModel.hpp +58 -23
  84. data/vendor/tomotopy/src/Utils/AliasMethod.hpp +6 -6
  85. data/vendor/tomotopy/src/Utils/Dictionary.h +11 -0
  86. data/vendor/tomotopy/src/Utils/SharedString.hpp +26 -1
  87. data/vendor/tomotopy/src/Utils/Trie.hpp +46 -21
  88. data/vendor/tomotopy/src/Utils/Utils.hpp +99 -14
  89. data/vendor/tomotopy/src/Utils/exception.h +1 -1
  90. data/vendor/tomotopy/src/Utils/math.h +5 -7
  91. data/vendor/tomotopy/src/Utils/serializer.hpp +329 -201
  92. data/vendor/tomotopy/src/Utils/text.hpp +8 -0
  93. data/vendor/tomotopy/src/Utils/tvector.hpp +49 -7
  94. 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
  {
@@ -3,7 +3,7 @@
3
3
  #include "text.hpp"
4
4
  namespace tomoto
5
5
  {
6
- namespace exception
6
+ namespace exc
7
7
  {
8
8
  class TrainingError : public std::runtime_error
9
9
  {
@@ -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) -> decltype((z + 2.5)* log(z + 3) - (z + 3) + 0.91893853 + 1. / 12. / (z + 3) - log(z * (z + 1) * (z + 2)))
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) -> decltype((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 + a + 1)) / (z * (z + 1))))
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(((z + a) * (z + a + 1)) / (z * (z + 1)));
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) -> decltype(log(z + 4) - 1. / 2. / (z + 4) - 1. / 12. / ((z + 4) * (z + 4)) - 1. / z - 1. / (z + 1) - 1. / (z + 2) - 1. / (z + 3))
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) -> decltype(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
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 "tvector.hpp"
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<!is_key<_FirstTy>::value>::type writeMany(std::ostream& ostr, const _FirstTy& first, _RestTy&&... rest)
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<!is_key<_FirstTy>::value>::type readMany(std::istream& istr, _FirstTy& first, _RestTy&&... rest)
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<class _Ty>
238
- inline typename std::enable_if<std::is_fundamental<_Ty>::value>::type writeToBinStreamImpl(std::ostream& ostr, const _Ty& v)
307
+ template<typename _Ty>
308
+ struct Serializer<_Ty, typename std::enable_if<std::is_fundamental<_Ty>::value>::type>
239
309
  {
240
- if (!ostr.write((const char*)&v, sizeof(_Ty)))
241
- throw std::ios_base::failure(std::string("writing type '") + typeid(_Ty).name() + std::string("' is failed") );
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
- template<class _Ty>
245
- inline typename std::enable_if<std::is_fundamental<_Ty>::value>::type readFromBinStreamImpl(std::istream& istr, _Ty& v)
246
- {
247
- if (!istr.read((char*)&v, sizeof(_Ty)))
248
- throw std::ios_base::failure(std::string("reading type '") + typeid(_Ty).name() + std::string("' is failed") );
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<class _Ty>
252
- inline typename std::enable_if<hasSave<_Ty>::value>::type writeToBinStreamImpl(std::ostream& ostr, const _Ty& v)
323
+ template<typename _Ty>
324
+ struct Serializer<_Ty, typename std::enable_if<hasSave<_Ty>::value>::type>
253
325
  {
254
- v.serializerWrite(ostr);
255
- }
326
+ void write(std::ostream& ostr, const _Ty& v)
327
+ {
328
+ v.serializerWrite(ostr);
329
+ }
256
330
 
257
- template<class _Ty, size_t _version = 0>
258
- inline typename std::enable_if<
259
- hasSaveV<_Ty, _version>::value && !hasSaveV<_Ty, _version + 1>::value
260
- >::type writeToBinStreamImpl(std::ostream& ostr, const _Ty& v)
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<class _Ty, size_t _version = 0>
266
- inline typename std::enable_if<
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
- return writeToBinStreamImpl<_Ty, _version + 1>(ostr, v);
271
- }
340
+ void write(std::ostream& ostr, const _Ty& v)
341
+ {
342
+ SerializerV<_Ty>{}.write(ostr, v);
343
+ }
272
344
 
273
- template<class _Ty>
274
- inline typename std::enable_if<hasLoad<_Ty>::value>::type readFromBinStreamImpl(std::istream& istr, _Ty& v)
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.serializerRead(istr);
277
- }
356
+ void write(std::ostream& ostr, const _Ty& v)
357
+ {
358
+ v.serializerWrite(version_holder<_version>{}, ostr);
359
+ }
278
360
 
279
- template<class _Ty, size_t _version = 0>
280
- inline typename std::enable_if<
281
- hasLoadV<_Ty, _version>::value && !hasLoadV<_Ty, _version + 1>::value
282
- >::type readFromBinStreamImpl(std::istream& istr, _Ty& v)
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<class _Ty, size_t _version = 0>
288
- inline typename std::enable_if<
289
- hasLoadV<_Ty, _version>::value && hasLoadV<_Ty, _version + 1>::value
290
- >::type readFromBinStreamImpl(std::istream& istr, _Ty& v)
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
- auto pos = istr.tellg();
293
- try
372
+ void write(std::ostream& ostr, const _Ty& v)
294
373
  {
295
- // try higher version first
296
- return readFromBinStreamImpl<_Ty, _version + 1>(istr, v);
374
+ SerializerV<_Ty, _version + 1>{}.write(ostr, v);
297
375
  }
298
- catch (const std::ios_base::failure&)
376
+
377
+ void read(std::istream& istr, _Ty& v)
299
378
  {
300
- istr.seekg(pos);
301
- // try current version if fails
302
- v.serializerRead(version_holder<_version>{}, istr);
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<class _Ty>
307
- inline void writeToBinStreamImpl(std::ostream& ostr, const Eigen::Matrix<_Ty, -1, -1>& v)
394
+ template<typename _Ty>
395
+ struct Serializer<Eigen::Matrix<_Ty, -1, -1>>
308
396
  {
309
- writeToStream<uint32_t>(ostr, (uint32_t)v.rows());
310
- writeToStream<uint32_t>(ostr, (uint32_t)v.cols());
311
- if (!ostr.write((const char*)v.data(), sizeof(_Ty) * v.size()))
312
- throw std::ios_base::failure( std::string("writing type '") + typeid(_Ty).name() + std::string("' is failed") );
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
- template<class _Ty>
316
- inline void readFromBinStreamImpl(std::istream& istr, Eigen::Matrix<_Ty, -1, -1>& v)
317
- {
318
- uint32_t rows = readFromStream<uint32_t>(istr);
319
- uint32_t cols = readFromStream<uint32_t>(istr);
320
- v = Eigen::Matrix<_Ty, -1, -1>::Zero(rows, cols);
321
- if (!istr.read((char*)v.data(), sizeof(_Ty) * rows * cols))
322
- throw std::ios_base::failure( std::string("reading type '") + typeid(_Ty).name() + std::string("' is failed") );
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<class _Ty>
326
- inline void writeToBinStreamImpl(std::ostream& ostr, const Eigen::Matrix<_Ty, -1, 1>& v)
415
+ template<typename _Ty>
416
+ struct Serializer<Eigen::Matrix<_Ty, -1, 1>>
327
417
  {
328
- writeToStream<uint32_t>(ostr, (uint32_t)v.rows());
329
- writeToStream<uint32_t>(ostr, (uint32_t)v.cols());
330
- if (!ostr.write((const char*)v.data(), sizeof(_Ty) * v.size()))
331
- throw std::ios_base::failure( std::string("writing type '") + typeid(_Ty).name() + std::string("' is failed") );
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
- template<class _Ty>
335
- inline void readFromBinStreamImpl(std::istream& istr, Eigen::Matrix<_Ty, -1, 1>& v)
336
- {
337
- uint32_t rows = readFromStream<uint32_t>(istr);
338
- uint32_t cols = readFromStream<uint32_t>(istr);
339
- if (cols != 1) throw std::ios_base::failure( "matrix cols != 1'" );
340
- v = Eigen::Matrix<_Ty, -1, 1>::Zero(rows);
341
- if (!istr.read((char*)v.data(), sizeof(_Ty) * rows * cols))
342
- throw std::ios_base::failure( std::string("reading type '") + typeid(_Ty).name() + std::string("' is failed") );
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<class _Ty>
346
- inline void writeToBinStreamImpl(std::ostream& ostr, const std::vector<_Ty>& v)
437
+ template<typename _Ty>
438
+ struct Serializer<PreventCopy<_Ty>> : public Serializer<_Ty>
347
439
  {
348
- writeToStream<uint32_t>(ostr, (uint32_t)v.size());
349
- for (auto& e : v) writeToStream(ostr, e);
350
- }
440
+ };
351
441
 
352
- template<class _Ty>
353
- inline void readFromBinStreamImpl(std::istream& istr, std::vector<_Ty>& v)
442
+ template<typename _Ty, typename _Ty2>
443
+ struct Serializer<DelegateCopy<_Ty, _Ty2>> : public Serializer<_Ty>
354
444
  {
355
- uint32_t size = readFromStream<uint32_t>(istr);
356
- v.resize(size);
357
- for (auto& e : v) readFromStream(istr, e);
358
- }
445
+ };
359
446
 
360
- template<class _Ty1, class _Ty2>
361
- inline void writeToBinStreamImpl(std::ostream& ostr, const std::pair<_Ty1, _Ty2>& v)
447
+ template<typename _Ty>
448
+ struct Serializer<std::vector<_Ty>, typename std::enable_if<std::is_fundamental<_Ty>::value>::type>
362
449
  {
363
- writeToStream(ostr, v.first);
364
- writeToStream(ostr, v.second);
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
- template<class _Ty1, class _Ty2>
368
- inline void readFromBinStreamImpl(std::istream& istr, std::pair<_Ty1, _Ty2>& v)
369
- {
370
- readFromStream(istr, v.first);
371
- readFromStream(istr, v.second);
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<class _KeyTy, class _ValTy>
375
- inline void writeToBinStreamImpl(std::ostream& ostr, const std::unordered_map<_KeyTy, _ValTy>& v)
467
+ template<typename _Ty>
468
+ struct Serializer<std::vector<_Ty>, typename std::enable_if<!std::is_fundamental<_Ty>::value>::type>
376
469
  {
377
- writeToStream<uint32_t>(ostr, (uint32_t)v.size());
378
- for (auto& e : v) writeToStream(ostr, e);
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
- template<class _KeyTy, class _ValTy>
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
- v.emplace(readFromStream<std::pair<_KeyTy, _ValTy>>(istr));
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<class _Ty, size_t _N>
393
- inline void writeToBinStreamImpl(std::ostream& ostr, const std::array<_Ty, _N>& v)
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
- writeToStream<uint32_t>(ostr, (uint32_t)v.size());
396
- for (auto& e : v) writeToStream(ostr, e);
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
- template<class _Ty, size_t _N>
400
- inline void readFromBinStreamImpl(std::istream& istr, std::array<_Ty, _N>& v)
401
- {
402
- uint32_t size = readFromStream<uint32_t>(istr);
403
- if (_N != size) throw std::ios_base::failure( text::format("the size of array must be %zd, not %zd", _N, size) );
404
- for (auto& e : v) readFromStream(istr, e);
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<class _Ty>
408
- inline void writeToBinStreamImpl(std::ostream& ostr, const tvector<_Ty>& v)
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
- writeToStream<uint32_t>(ostr, (uint32_t)v.size());
411
- for (auto& e : v) writeToStream(ostr, e);
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
- template<class _Ty>
415
- inline void readFromBinStreamImpl(std::istream& istr, tvector<_Ty>& v)
416
- {
417
- uint32_t size = readFromStream<uint32_t>(istr);
418
- v.resize(size);
419
- for (auto& e : v) readFromStream(istr, e);
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<class _Ty>
423
- inline void writeToBinStreamImpl(std::ostream& ostr, const std::basic_string<_Ty>& v)
523
+ template<typename _Ty>
524
+ struct Serializer<std::basic_string<_Ty>>
424
525
  {
425
- writeToStream<uint32_t>(ostr, (uint32_t)v.size());
426
- if (!ostr.write((const char*)v.data(), sizeof(_Ty) * v.size()))
427
- throw std::ios_base::failure( std::string("writing type '") + typeid(_Ty).name() + std::string("' is failed") );
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
- template<class _Ty>
431
- inline void readFromBinStreamImpl(std::istream& istr, std::basic_string<_Ty>& v)
432
- {
433
- uint32_t size = readFromStream<uint32_t>(istr);
434
- v.resize(size);
435
- if (!istr.read((char*)v.data(), sizeof(_Ty) * v.size()))
436
- throw std::ios_base::failure( std::string("reading type '") + typeid(_Ty).name() + std::string("' is failed") );
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
- inline void writeToBinStreamImpl(std::ostream& ostr, const SharedString& v)
543
+ template<typename _Ty1, typename _Ty2>
544
+ struct Serializer<std::pair<_Ty1, _Ty2>>
440
545
  {
441
- writeToStream<uint32_t>(ostr, (uint32_t)v.size());
442
- if (!ostr.write((const char*)v.data(), v.size()))
443
- throw std::ios_base::failure(std::string("writing type 'SharedString' is failed"));
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
- inline void readFromBinStreamImpl(std::istream& istr, SharedString& v)
447
- {
448
- uint32_t size = readFromStream<uint32_t>(istr);
449
- std::vector<char> t(size);
450
- if (!istr.read((char*)t.data(), t.size()))
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<class _Ty>
456
- inline typename std::enable_if<std::is_abstract<_Ty>::value>::type writeToBinStreamImpl(std::ostream& ostr, const std::unique_ptr<_Ty>& v)
558
+ template<typename _Ty1, typename _Ty2>
559
+ struct Serializer<std::unordered_map<_Ty1, _Ty2>>
457
560
  {
458
- _Ty::serializerWrite(v, ostr);
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
- template<class _Ty>
462
- inline typename std::enable_if<std::is_abstract<_Ty>::value>::type readFromBinStreamImpl(std::istream& istr, std::unique_ptr<_Ty>& v)
463
- {
464
- _Ty::serializerRead(v, istr);
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 _Ty>
468
- inline void writeToStream(std::ostream& ostr, const _Ty& v)
579
+ template<typename _Ty1, typename _Ty2>
580
+ struct Serializer<std::map<_Ty1, _Ty2>>
469
581
  {
470
- return writeToBinStreamImpl(ostr, v);
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
- template<typename _Ty>
474
- inline void readFromStream(std::istream& istr, _Ty& v)
475
- {
476
- return readFromBinStreamImpl(istr, v);
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
- inline _Ty readFromStream(std::istream& istr)
601
+ struct Serializer<std::unique_ptr<_Ty>, typename std::enable_if<std::is_abstract<_Ty>::value>::type>
481
602
  {
482
- _Ty v;
483
- readFromBinStreamImpl(istr, v);
484
- return v;
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