jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
column_type.hpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2012,2013 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_COLUMN_TYPE_HPP_
18 #define JUBATUS_CORE_STORAGE_COLUMN_TYPE_HPP_
19 
20 #include <algorithm>
21 #include <string>
22 #include <vector>
23 #include <iosfwd>
24 
25 #include "storage_exception.hpp"
26 #include "bit_vector.hpp"
27 
28 namespace jubatus {
29 namespace core {
30 namespace storage {
31 
32 class column_type {
33  public:
34  enum type_name {
47  array_type, // not implemented yet
49  };
51  explicit column_type(type_name name) : type_(name), bit_vector_length_(0) {
52  if (name == bit_vector_type) {
54  "bit_vector_type expects bit_vector's length as second argument");
55  }
56  }
57  column_type(type_name name, int length)
58  : type_(name),
59  bit_vector_length_(length) {
60  if (name != bit_vector_type) {
62  "unneeded second parameter, it is used only for bit_vector_type");
63  }
64  }
65 
66  bool is(const type_name& type) const {
67  return type_ == type;
68  }
69 
70  std::string type_as_string() const {
71  if (type_ == int8_type) {
72  return "int8";
73  } else if (type_ == int16_type) {
74  return "int16";
75  } else if (type_ == int32_type) {
76  return "int32";
77  } else if (type_ == int64_type) {
78  return "int64";
79  } else if (type_ == uint8_type) {
80  return "uint8";
81  } else if (type_ == uint16_type) {
82  return "uint16";
83  } else if (type_ == uint32_type) {
84  return "uint32";
85  } else if (type_ == uint64_type) {
86  return "uint64";
87  } else if (type_ == float_type) {
88  return "float";
89  } else if (type_ == double_type) {
90  return "double";
91  } else if (type_ == bit_vector_type) {
92  return "bit_vector";
93  } else if (type_ == string_type) {
94  return "string";
95  }
96  throw type_unmatch_exception("in type_as_string(): unknown type");
97  }
98 
99  size_t size() const {
100  switch (type_) {
101  case int8_type:
102  case uint8_type:
103  return 1;
104  case int16_type:
105  case uint16_type:
106  return 2;
107  case int32_type:
108  case uint32_type:
109  case float_type:
110  return 4;
111  case int64_type:
112  case uint64_type:
113  case double_type:
114  case string_type:
115  return 8;
116  case bit_vector_type:
118  case array_type:
119  case invalid_type:
120  default:
121  return 0;
122  }
123  }
124 
125 
126  size_t bit_vector_length() const {
127  if (type_ != bit_vector_type) {
129  "bit_vector length expected but type was" + type_as_string());
130  }
131  return bit_vector_length_;
132  }
133 
135  type_ = orig.type_;
137  return *this;
138  }
139 
140  void swap(column_type& rhs) {
141  using std::swap;
142  swap(type_, rhs.type_);
144  }
145 
146  friend std::ostream& operator<<(std::ostream& os, const column_type& type) {
147  os << "{ " << type.type_as_string();
148  if (type.type_ == bit_vector_type) {
149  os << ": " << type.bit_vector_length_;
150  }
151  os << " }";
152  return os;
153  }
154 
155  friend bool operator==(const column_type& x, const column_type& y) {
156  return x.type_ == y.type_ && x.bit_vector_length_ == y.bit_vector_length_;
157  }
158  friend bool operator!=(const column_type& x, const column_type& y) {
159  return !(x == y);
160  }
161 
162  template<class Buffer>
164  packer.pack_array(2);
165  packer.pack(static_cast<uint8_t>(type_));
166  packer.pack(static_cast<uint64_t>(bit_vector_length_));
167  }
168  void msgpack_unpack(msgpack::object o) {
169  if (o.type != msgpack::type::ARRAY || o.via.array.size != 2) {
170  throw msgpack::type_error();
171  }
172  uint8_t type;
173  o.via.array.ptr[0].convert(&type);
174  uint64_t bit_vector_length;
175  o.via.array.ptr[1].convert(&bit_vector_length);
176  type_ = static_cast<type_name>(type);
178  }
179 
180  private:
183 };
184 
185 } // namespace storage
186 } // namespace core
187 } // namespace jubatus
188 
189 #endif // JUBATUS_CORE_STORAGE_COLUMN_TYPE_HPP_
void msgpack_pack(msgpack::packer< Buffer > &packer) const
void msgpack_unpack(msgpack::object o)
column_type & operator=(const column_type &orig)
static size_t memory_size(size_t bit_width)
Definition: bit_vector.hpp:319
column_type(type_name name, int length)
Definition: column_type.hpp:57
friend bool operator!=(const column_type &x, const column_type &y)
bool is(const type_name &type) const
Definition: column_type.hpp:66
friend bool operator==(const column_type &x, const column_type &y)
std::string type_as_string() const
Definition: column_type.hpp:70
void swap(weighted_point &p1, weighted_point &p2)
Definition: types.hpp:47
msgpack::packer< jubatus_packer > packer
Definition: bandit_base.hpp:31
friend std::ostream & operator<<(std::ostream &os, const column_type &type)