jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
storage_type.hpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2011 Preferred Networks and Nippon Telegraph and Telephone Corporation.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License version 2.1 as published by the Free Software Foundation.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 
17 #ifndef JUBATUS_CORE_STORAGE_STORAGE_TYPE_HPP_
18 #define JUBATUS_CORE_STORAGE_STORAGE_TYPE_HPP_
19 
20 #include <algorithm>
21 #include <functional>
22 #include <map>
23 #include <string>
24 #include <utility>
25 #include <vector>
26 #include <msgpack.hpp>
27 #include "jubatus/util/data/unordered_map.h"
28 #include "../common/assoc_vector.hpp"
29 #include "../common/version.hpp"
30 #include "bit_vector.hpp"
31 
32 namespace jubatus {
33 namespace core {
34 namespace storage {
35 
37 typedef jubatus::util::data::unordered_map<std::string, row_t> tbl_t;
38 
39 typedef jubatus::util::data::unordered_map<std::string, bit_vector> bit_table_t;
40 
41 typedef jubatus::util::data::unordered_map<std::string, float> map_float_t;
42 typedef jubatus::util::data::unordered_map<uint64_t, float> imap_float_t;
43 
44 typedef double val1_t;
45 
46 struct val2_t {
48  : v1(0.0),
49  v2(0.0) {
50  } // undefined
51  val2_t(double v1, double v2)
52  : v1(v1),
53  v2(v2) {
54  }
55  double v1;
56  double v2;
57 
58  bool operator ==(const val2_t& v) const {
59  return v1 == v.v1 && v2 == v.v2;
60  }
61 
62  bool operator <(const val2_t& v) const {
63  return (v1 != v.v1) ? v1 < v.v1 : v2 < v.v2;
64  }
65 
66  val2_t operator +(const val2_t& r) const {
67  val2_t ret(*this);
68  ret += r;
69  return ret;
70  }
71 
72  val2_t& operator +=(const val2_t& r) {
73  v1 += r.v1;
74  v2 += r.v2;
75  return *this;
76  }
77 
78  val2_t operator -(const val2_t& r) const {
79  val2_t ret(*this);
80  ret -= r;
81  return ret;
82  }
83 
84  val2_t& operator -=(const val2_t& r) {
85  v1 -= r.v1;
86  v2 -= r.v2;
87  return *this;
88  }
89 
90  val2_t operator *(const val2_t& r) const {
91  val2_t ret(*this);
92  ret *= r;
93  return ret;
94  }
95 
96  val2_t& operator *=(const val2_t& r) {
97  v1 *= r.v1;
98  v2 *= r.v2;
99  return *this;
100  }
101 
102  val2_t operator /(const val2_t& r) const {
103  val2_t ret(*this);
104  ret /= r;
105  return ret;
106  }
107 
108  val2_t& operator /=(const val2_t& r) {
109  v1 /= r.v1;
110  v2 /= r.v2;
111  return *this;
112  }
113 
114  MSGPACK_DEFINE(v1, v2);
115 };
116 
117 struct val3_t {
119  : v1(0.0),
120  v2(0.0),
121  v3(0.0) {
122  } // undefined
123  val3_t(double v1, double v2, double v3)
124  : v1(v1),
125  v2(v2),
126  v3(v3) {
127  }
128  double v1;
129  double v2;
130  double v3;
131 
132  bool operator ==(const val3_t& v) const {
133  return v1 == v.v1 && v2 == v.v2 && v3 == v.v3;
134  }
135 
136  bool operator <(const val3_t& v) const {
137  return (v1 != v.v1) ? v1 < v.v1 : (v2 != v.v2) ? v2 < v.v2 : v3 < v.v3;
138  }
139 
140  val3_t operator +(const val3_t& r) const {
141  val3_t ret(*this);
142  ret += r;
143  return ret;
144  }
145 
146  val3_t& operator +=(const val3_t& r) {
147  v1 += r.v1;
148  v2 += r.v2;
149  v3 += r.v3;
150  return *this;
151  }
152 
153  val3_t operator -(const val3_t& r) const {
154  val3_t ret(*this);
155  ret -= r;
156  return ret;
157  }
158 
159  val3_t& operator -=(const val3_t& r) {
160  v1 -= r.v1;
161  v2 -= r.v2;
162  v3 -= r.v3;
163  return *this;
164  }
165 
166  val3_t operator *(const val3_t& r) const {
167  val3_t ret(*this);
168  ret *= r;
169  return ret;
170  }
171 
172  // pin-point: add other operators
173  val3_t operator *(double d) const {
174  val3_t ret(*this);
175  ret.v1 *= d;
176  ret.v2 *= d;
177  ret.v3 *= d;
178  return ret;
179  }
180 
181  val3_t& operator *=(const val3_t& r) {
182  v1 *= r.v1;
183  v2 *= r.v2;
184  v3 *= r.v3;
185  return *this;
186  }
187 
188  val3_t operator /(const val3_t& r) const {
189  val3_t ret(*this);
190  ret /= r;
191  return ret;
192  }
193 
194  val3_t& operator /=(const val3_t& r) {
195  v1 /= r.v1;
196  v2 /= r.v2;
197  v3 /= r.v3;
198  return *this;
199  }
200 
201  MSGPACK_DEFINE(v1, v2, v3);
202 
203  friend std::ostream& operator<<(std::ostream& os, const val3_t& v) {
204  os << "{v1: " << v.v1 << ", v2: " << v.v2 << ", v3: " << v.v3 << "}";
205  return os;
206  }
207 };
208 
209 typedef std::vector<std::pair<std::string, val1_t> > feature_val1_t;
210 typedef std::vector<std::pair<std::string, val2_t> > feature_val2_t;
211 typedef std::vector<std::pair<std::string, val3_t> > feature_val3_t;
212 
213 typedef std::vector<std::pair<std::string, feature_val1_t> > features1_t;
214 typedef std::vector<std::pair<std::string, feature_val2_t> > features2_t;
215 typedef std::vector<std::pair<std::string, feature_val3_t> > features3_t;
216 
217 typedef jubatus::util::data::unordered_map<std::string, val1_t>
219 typedef jubatus::util::data::unordered_map<std::string, val3_t>
221 typedef jubatus::util::data::unordered_map<std::string, map_feature_val3_t>
223 
224 struct diff_t {
225  features3_t diff;
227  MSGPACK_DEFINE(diff, expect_version);
228 };
229 
230 namespace detail {
231 
232 template <class E, class F>
233 std::vector<std::pair<std::string, E> >& binop(
234  std::vector<std::pair<std::string, E> >& lhs,
235  std::vector<std::pair<std::string, E> > rhs,
236  F f,
237  E default_value = E()) {
238  std::sort(lhs.begin(), lhs.end());
239  std::sort(rhs.begin(), rhs.end());
240 
241  size_t li = 0;
242  size_t lsize = lhs.size();
243  for (size_t ri = 0; ri < rhs.size(); ++ri) {
244  while (li < lsize && lhs[li].first < rhs[ri].first) {
245  lhs[li].second = f(lhs[li].second, default_value);
246  ++li;
247  }
248 
249  if (li < lsize && lhs[li].first == rhs[ri].first) {
250  lhs[li].second = f(lhs[li].second, rhs[ri].second);
251  ++li;
252  } else {
253  lhs.push_back(make_pair(rhs[ri].first, f(default_value, rhs[ri].second)));
254  }
255  }
256  while (li < lsize) {
257  lhs[li].second = f(lhs[li].second, default_value);
258  ++li;
259  }
260 
261  return lhs;
262 }
263 
264 template <class E>
265 std::vector<std::pair<std::string, E> >& mult_scalar(
266  std::vector<std::pair<std::string, E> >& lhs,
267  double d) {
268  for (size_t i = 0; i < lhs.size(); ++i) {
269  lhs[i].second = lhs[i].second * d;
270  }
271  return lhs;
272 }
273 
274 } // namespace detail
275 
276 template <class E>
277 inline std::vector<std::pair<std::string, E> > operator +(
278  std::vector<std::pair<std::string, E> > lhs,
279  const std::vector<std::pair<std::string, E> >& rhs) {
280  return lhs += rhs;
281 }
282 
283 template <class E>
284 inline std::vector<std::pair<std::string, E> >&
285 operator +=(std::vector<std::pair<std::string, E> >& lhs,
286  const std::vector<std::pair<std::string, E> >& rhs) {
287  return detail::binop(lhs, rhs, std::plus<E>());
288 }
289 
290 template <class E>
291 inline std::vector<std::pair<std::string, E> > operator -(
292  std::vector<std::pair<std::string, E> > lhs,
293  const std::vector<std::pair<std::string, E> >& rhs) {
294  return lhs -= rhs;
295 }
296 
297 template <class E>
298 inline std::vector<std::pair<std::string, E> >&
299 operator -=(std::vector<std::pair<std::string, E> >& lhs,
300  const std::vector<std::pair<std::string, E> >& rhs) {
301  return detail::binop(lhs, rhs, std::minus<E>());
302 }
303 
304 template <class E>
305 inline std::vector<std::pair<std::string, E> > operator *(
306  std::vector<std::pair<std::string, E> > lhs,
307  const std::vector<std::pair<std::string, E> >& rhs) {
308  return lhs *= rhs;
309 }
310 
311 template <class E>
312 inline std::vector<std::pair<std::string, E> >&
313 operator *=(std::vector<std::pair<std::string, E> >& lhs,
314  const std::vector<std::pair<std::string, E> >& rhs) {
315  return detail::binop(lhs, rhs, std::multiplies<E>());
316 }
317 
318 template <class E>
319 inline std::vector<std::pair<std::string, E> > operator /(
320  std::vector<std::pair<std::string, E> > lhs,
321  const std::vector<std::pair<std::string, E> >& rhs) {
322  return lhs /= rhs;
323 }
324 
325 template <class E>
326 inline std::vector<std::pair<std::string, E> >& operator /=(
327  std::vector<std::pair<std::string, E> >& lhs,
328  const std::vector<std::pair<std::string, E> >& rhs) {
329  return detail::binop(lhs, rhs, std::divides<E>());
330 }
331 
332 template <class E>
333 inline std::vector<std::pair<std::string, E> > operator *(
334  std::vector<std::pair<std::string, E> > lhs,
335  double d) {
336  return lhs *= d;
337 }
338 
339 template <class E>
340 inline std::vector<std::pair<std::string, E> >& operator *=(
341  std::vector<std::pair<std::string, E> >& lhs,
342  double d) {
343  return detail::mult_scalar(lhs, d);
344 }
345 
346 template <class E>
347 inline std::vector<std::pair<std::string, E> > operator /(
348  std::vector<std::pair<std::string, E> > lhs,
349  double d) {
350  return lhs /= d;
351 }
352 
353 template <class E>
354 inline std::vector<std::pair<std::string, E> >& operator /=(
355  std::vector<std::pair<std::string, E> >& lhs,
356  double d) {
357  return detail::mult_scalar(lhs, 1.0 / d);
358 }
359 
360 } // namespace storage
361 } // namespace core
362 } // namespace jubatus
363 
364 #endif // JUBATUS_CORE_STORAGE_STORAGE_TYPE_HPP_
val3_t(double v1, double v2, double v3)
val2_t(double v1, double v2)
val2_t & operator-=(const val2_t &r)
std::vector< std::pair< std::string, feature_val3_t > > features3_t
std::vector< std::pair< std::string, E > > & operator*=(std::vector< std::pair< std::string, E > > &lhs, const std::vector< std::pair< std::string, E > > &rhs)
val3_t operator*(const val3_t &r) const
val2_t operator/(const val2_t &r) const
std::vector< std::pair< std::string, E > > & operator/=(std::vector< std::pair< std::string, E > > &lhs, const std::vector< std::pair< std::string, E > > &rhs)
MSGPACK_DEFINE(diff, expect_version)
jubatus::util::data::unordered_map< std::string, val1_t > map_feature_val1_t
std::vector< std::pair< std::string, E > > & operator-=(std::vector< std::pair< std::string, E > > &lhs, const std::vector< std::pair< std::string, E > > &rhs)
val3_t & operator*=(const val3_t &r)
std::vector< std::pair< std::string, E > > & mult_scalar(std::vector< std::pair< std::string, E > > &lhs, double d)
bool operator==(const val3_t &v) const
bool operator<(const val3_t &v) const
std::vector< std::pair< std::string, feature_val1_t > > features1_t
std::vector< std::pair< std::string, E > > & operator+=(std::vector< std::pair< std::string, E > > &lhs, const std::vector< std::pair< std::string, E > > &rhs)
friend std::ostream & operator<<(std::ostream &os, const val3_t &v)
jubatus::util::data::unordered_map< std::string, val3_t > map_feature_val3_t
std::vector< std::pair< std::string, feature_val2_t > > features2_t
jubatus::util::data::unordered_map< std::string, map_feature_val3_t > map_features3_t
std::vector< std::pair< std::string, val1_t > > feature_val1_t
val3_t & operator/=(const val3_t &r)
std::vector< std::pair< std::string, E > > operator/(std::vector< std::pair< std::string, E > > lhs, const std::vector< std::pair< std::string, E > > &rhs)
val3_t operator/(const val3_t &r) const
jubatus::util::data::unordered_map< std::string, bit_vector > bit_table_t
jubatus::util::data::unordered_map< uint64_t, float > imap_float_t
jubatus::util::data::unordered_map< std::string, float > map_float_t
std::vector< T > v(size)
val2_t & operator*=(const val2_t &r)
val2_t & operator/=(const val2_t &r)
bool operator==(const val2_t &v) const
val3_t operator-(const val3_t &r) const
std::vector< std::pair< std::string, val3_t > > feature_val3_t
std::vector< std::pair< std::string, E > > operator-(std::vector< std::pair< std::string, E > > lhs, const std::vector< std::pair< std::string, E > > &rhs)
jubatus::util::data::unordered_map< std::string, row_t > tbl_t
bool operator<(const val2_t &v) const
std::vector< std::pair< std::string, val2_t > > feature_val2_t
std::vector< std::pair< std::string, E > > operator*(std::vector< std::pair< std::string, E > > lhs, const std::vector< std::pair< std::string, E > > &rhs)
val2_t operator*(const val2_t &r) const
val2_t & operator+=(const val2_t &r)
val2_t operator-(const val2_t &r) const
std::vector< std::pair< std::string, E > > & binop(std::vector< std::pair< std::string, E > > &lhs, std::vector< std::pair< std::string, E > > rhs, F f, E default_value=E())
jubatus::core::common::assoc_vector< uint64_t, float > row_t
std::vector< std::pair< std::string, E > > operator+(std::vector< std::pair< std::string, E > > lhs, const std::vector< std::pair< std::string, E > > &rhs)
val3_t & operator+=(const val3_t &r)
val2_t operator+(const val2_t &r) const
val3_t & operator-=(const val3_t &r)
val3_t operator+(const val3_t &r) const