jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | Friends | List of all members
jubatus::core::storage::bit_vector_base< bit_base > Struct Template Reference

#include <bit_vector_ranking.hpp>

Inheritance diagram for jubatus::core::storage::bit_vector_base< bit_base >:
Inheritance graph
Collaboration diagram for jubatus::core::storage::bit_vector_base< bit_base >:
Collaboration graph

Public Member Functions

void alloc_memory ()
 
size_t bit_count () const
 
size_t bit_num () const
 
 bit_vector_base ()
 
 bit_vector_base (const bit_base *bits, size_t bit_num)
 
 bit_vector_base (int bit_num)
 
 bit_vector_base (const bit_vector_base &orig)
 
uint64_t calc_hamming_distance (const bit_vector_base &bv) const
 
uint64_t calc_hamming_similarity (const bit_vector_base &bv) const
 
void clear ()
 
void clear_bit (size_t pos)
 
void debug_print (std::ostream &os) const
 
bool get_bit (size_t pos) const
 
bool is_empty () const
 
template<typename Buffer >
void msgpack_pack (msgpack::packer< Buffer > &packer) const
 
void msgpack_unpack (msgpack::object o)
 
bool operator!= (const bit_vector_base &rhs) const
 
bit_vector_baseoperator= (const bit_vector_base &orig)
 
bool operator== (const bit_vector_base &rhs) const
 
template<typename packable >
void pack (packable &buffer) const
 
bit_base * raw_data_unsafe ()
 
const bit_base * raw_data_unsafe () const
 
void resize_and_clear (uint64_t bit_num)
 
void reverse_bit (size_t pos)
 
void set_bit (size_t pos)
 
void status (std::ostream &os) const
 
void swap (bit_vector_base &x)
 
size_t used_bytes () const
 
 ~bit_vector_base ()
 

Static Public Member Functions

static size_t memory_size (size_t bit_width)
 

Static Public Attributes

static const size_t BASE_BITS = sizeof(bit_base) * 8
 
static const size_t BLOCKSIZE = sizeof(bit_base)
 

Private Member Functions

void duplicate (const bit_vector_base &orig)
 
bool release ()
 

Private Attributes

size_t bit_num_
 
bit_base * bits_
 
bool own_
 

Friends

std::ostream & operator<< (std::ostream &os, const bit_vector_base &bv)
 
void swap (bit_vector_base &l, bit_vector_base &r)
 

Detailed Description

template<typename bit_base>
struct jubatus::core::storage::bit_vector_base< bit_base >

Definition at line 27 of file bit_vector_ranking.hpp.

Constructor & Destructor Documentation

template<typename bit_base>
jubatus::core::storage::bit_vector_base< bit_base >::bit_vector_base ( )
inline

Definition at line 138 of file bit_vector.hpp.

Referenced by jubatus::core::storage::bit_vector_base< uint64_t >::msgpack_unpack().

Here is the caller graph for this function:

template<typename bit_base>
jubatus::core::storage::bit_vector_base< bit_base >::bit_vector_base ( const bit_base *  bits,
size_t  bit_num 
)
inline
template<typename bit_base>
jubatus::core::storage::bit_vector_base< bit_base >::bit_vector_base ( int  bit_num)
inlineexplicit
template<typename bit_base>
jubatus::core::storage::bit_vector_base< bit_base >::bit_vector_base ( const bit_vector_base< bit_base > &  orig)
inline

Definition at line 156 of file bit_vector.hpp.

157  : bits_(NULL),
158  bit_num_(orig.bit_num_),
159  own_(false) {
160  if (orig.bits_ == NULL) {
161  return;
162  }
163  alloc_memory();
164  memcpy(bits_, orig.bits_, used_bytes());
165  }
template<typename bit_base>
jubatus::core::storage::bit_vector_base< bit_base >::~bit_vector_base ( )
inline

Definition at line 167 of file bit_vector.hpp.

167  {
168  if (bits_ && own_) {
169  release();
170  return;
171  }
172  }

Member Function Documentation

template<typename bit_base>
void jubatus::core::storage::bit_vector_base< bit_base >::alloc_memory ( )
inline
template<typename bit_base>
size_t jubatus::core::storage::bit_vector_base< bit_base >::bit_count ( ) const
inline

Definition at line 296 of file bit_vector.hpp.

Referenced by jubatus::core::storage::bit_vector_base< uint64_t >::calc_hamming_distance(), and jubatus::core::storage::bit_vector_base< uint64_t >::is_empty().

296  {
297  if (bits_ == NULL) {
298  return 0;
299  }
300  size_t result = 0;
301  for (int64_t i = (used_bytes() / sizeof(bit_base)) - 1; i >= 0; --i) {
302  result += detail::bitcount(bits_[i]);
303  }
304  return result;
305  }
int bitcount(unsigned bits)
Definition: bit_vector.hpp:102

Here is the caller graph for this function:

template<typename bit_base>
size_t jubatus::core::storage::bit_vector_base< bit_base >::bit_num ( ) const
inline
template<typename bit_base>
uint64_t jubatus::core::storage::bit_vector_base< bit_base >::calc_hamming_distance ( const bit_vector_base< bit_base > &  bv) const
inline

Definition at line 276 of file bit_vector.hpp.

Referenced by jubatus::core::storage::bit_vector_base< uint64_t >::calc_hamming_similarity().

276  {
277  if (bit_num() != bv.bit_num()) {
278  throw bit_vector_unmatch_exception(
279  "calc_hamming_similarity(): bit_vector length unmatch! " +
280  jubatus::util::lang::lexical_cast<std::string>(bit_num()) + " with " +
281  jubatus::util::lang::lexical_cast<std::string>(bv.bit_num()));
282  }
283  if (is_empty() && bv.is_empty()) {
284  return 0;
285  } else if (is_empty()) {
286  return bv.bit_count();
287  } else if (bv.is_empty()) {
288  return bit_count();
289  }
290  size_t match_num = 0;
291  for (size_t i = 0; i < used_bytes() / sizeof(bit_base); ++i) {
292  match_num += detail::bitcount(bits_[i] ^ bv.bits_[i]);
293  }
294  return match_num;
295  }
int bitcount(unsigned bits)
Definition: bit_vector.hpp:102

Here is the caller graph for this function:

template<typename bit_base>
uint64_t jubatus::core::storage::bit_vector_base< bit_base >::calc_hamming_similarity ( const bit_vector_base< bit_base > &  bv) const
inline

Definition at line 273 of file bit_vector.hpp.

Referenced by jubatus::core::storage::similar_row_one().

273  {
274  return bit_num() - calc_hamming_distance(bv);
275  }
uint64_t calc_hamming_distance(const bit_vector_base &bv) const
Definition: bit_vector.hpp:276

Here is the caller graph for this function:

template<typename bit_base>
void jubatus::core::storage::bit_vector_base< bit_base >::clear ( )
inline

Definition at line 268 of file bit_vector.hpp.

268  {
269  for (size_t i = 0; i < bit_num_; ++i) {
270  clear_bit(i);
271  }
272  }
template<typename bit_base>
void jubatus::core::storage::bit_vector_base< bit_base >::clear_bit ( size_t  pos)
inline

Definition at line 225 of file bit_vector.hpp.

Referenced by jubatus::core::storage::bit_vector_base< uint64_t >::clear().

225  {
226  if (bits_ == NULL) {
227  return;
228  }
229  bits_[pos / BASE_BITS] &= ~(1LLU << (pos % BASE_BITS));
230  }

Here is the caller graph for this function:

template<typename bit_base>
void jubatus::core::storage::bit_vector_base< bit_base >::debug_print ( std::ostream &  os) const
inline

Definition at line 323 of file bit_vector.hpp.

323  {
324  if (bits_ == NULL) {
325  os << "(unallocated)";
326  return;
327  }
328  for (uint64_t i = 0; i < used_bytes() * 8; ++i) {
329  if ((bits_[i / (sizeof(bit_base) * 8)] >> (i % (sizeof(bit_base) * 8))) &
330  1LLU) {
331  os << "1";
332  } else {
333  os << "0";
334  }
335  }
336  }
template<typename bit_base>
void jubatus::core::storage::bit_vector_base< bit_base >::duplicate ( const bit_vector_base< bit_base > &  orig)
inlineprivate

Definition at line 415 of file bit_vector.hpp.

415  {
416  if (bit_num_ != orig.bit_num_) {
417  throw bit_vector_unmatch_exception(
418  "failed copy bit vector from " +
419  jubatus::util::lang::lexical_cast<std::string>(orig.bit_num_) +
420  " to " +
421  jubatus::util::lang::lexical_cast<std::string>(bit_num_));
422  }
423  if (!own_) {
424  alloc_memory();
425  }
426  memcpy(bits_, orig.bits_, used_bytes());
427  }
template<typename bit_base>
bool jubatus::core::storage::bit_vector_base< bit_base >::get_bit ( size_t  pos) const
inline

Definition at line 258 of file bit_vector.hpp.

258  {
259  if (bits_ == NULL) {
260  return false;
261  }
262  return bits_[pos / BASE_BITS] & (1LLU << (pos % BASE_BITS));
263  }
template<typename bit_base>
bool jubatus::core::storage::bit_vector_base< bit_base >::is_empty ( ) const
inline

Definition at line 264 of file bit_vector.hpp.

Referenced by jubatus::core::storage::bit_vector_base< uint64_t >::calc_hamming_distance(), and jubatus::core::storage::bit_vector_base< uint64_t >::operator==().

264  {
265  return bit_count() == 0;
266  }

Here is the caller graph for this function:

template<typename bit_base>
static size_t jubatus::core::storage::bit_vector_base< bit_base >::memory_size ( size_t  bit_width)
inlinestatic

Definition at line 319 of file bit_vector.hpp.

Referenced by jubatus::core::storage::bit_vector_base< uint64_t >::msgpack_unpack(), and jubatus::core::storage::bit_vector_base< uint64_t >::used_bytes().

319  {
320  return ((((bit_width + 7) / 8) + BLOCKSIZE - 1) / BLOCKSIZE) * BLOCKSIZE;
321  }

Here is the caller graph for this function:

template<typename bit_base>
template<typename Buffer >
void jubatus::core::storage::bit_vector_base< bit_base >::msgpack_pack ( msgpack::packer< Buffer > &  packer) const
inline

Definition at line 355 of file bit_vector.hpp.

355  {
356  packer.pack_array(2);
357  packer.pack(static_cast<uint64_t>(bit_num_));
358  packer.pack_raw(used_bytes());
359  if (bits_) {
360  const size_t n = used_bytes() / BLOCKSIZE;
361  for (size_t i = 0; i < n; ++i) {
362  char buf[BLOCKSIZE];
364  packer.pack_raw_body(buf, BLOCKSIZE);
365  }
366  } else {
367  const char c = 0;
368  for (size_t i = 0; i < used_bytes(); ++i) {
369  packer.pack_raw_body(&c, 1);
370  }
371  }
372  }
void write_big_endian(uint32_t x, char *buf)
Definition: big_endian.hpp:34
template<typename bit_base>
void jubatus::core::storage::bit_vector_base< bit_base >::msgpack_unpack ( msgpack::object  o)
inline

Definition at line 373 of file bit_vector.hpp.

373  {
374  if (o.type != msgpack::type::ARRAY || o.via.array.size != 2) {
375  throw msgpack::type_error(); // like MSGPACK_DEFINE
376  }
377  const msgpack::object* objs = o.via.array.ptr;
378  const uint64_t bit_num = objs[0].as<uint64_t>();
379  const msgpack::type::raw_ref data = objs[1].as<msgpack::type::raw_ref>();
380  if (data.size != memory_size(bit_num)) {
381  throw bit_vector_unmatch_exception(
382  "msgpack_unpack(): invalid length of packed data: "
383  "expected: " +
384  jubatus::util::lang::lexical_cast<std::string>(bit_num_) +
385  ", got: " +
386  jubatus::util::lang::lexical_cast<std::string>(data.size));
387  }
388  std::vector<bit_base> buf;
389  for (size_t i = 0; i < data.size; i += BLOCKSIZE) {
390  buf.push_back(common::read_big_endian<bit_base>(&data.ptr[i]));
391  }
392  bit_vector_base(&buf[0], bit_num).swap(*this);
393  JUBATUS_ASSERT(own_ || bits_ == NULL);
394  }
static size_t memory_size(size_t bit_width)
Definition: bit_vector.hpp:319
#define JUBATUS_ASSERT(expr)
Definition: assert.hpp:55
vector< msgpack::object > objs
template<typename bit_base>
bool jubatus::core::storage::bit_vector_base< bit_base >::operator!= ( const bit_vector_base< bit_base > &  rhs) const
inline

Definition at line 195 of file bit_vector.hpp.

195  {
196  return !this->operator==(rhs);
197  }
bool operator==(const bit_vector_base &rhs) const
Definition: bit_vector.hpp:180
template<typename bit_base>
bit_vector_base& jubatus::core::storage::bit_vector_base< bit_base >::operator= ( const bit_vector_base< bit_base > &  orig)
inline

Definition at line 200 of file bit_vector.hpp.

200  {
201  if (&orig != this) {
202  bit_num_ = orig.bit_num_;
203  if (bits_ == NULL) {
204  alloc_memory();
205  }
206  if (orig.bits_ == NULL) {
207  memset(bits_, 0, used_bytes());
208  } else {
209  memcpy(bits_, orig.bits_, used_bytes());
210  }
211  }
212  return *this;
213  }
template<typename bit_base>
bool jubatus::core::storage::bit_vector_base< bit_base >::operator== ( const bit_vector_base< bit_base > &  rhs) const
inline

Definition at line 180 of file bit_vector.hpp.

Referenced by jubatus::core::storage::bit_vector_base< uint64_t >::operator!=().

180  {
181  if (bit_num_ != rhs.bit_num_) {
182  return false;
183  }
184  if (is_empty() && rhs.is_empty()) {
185  return true;
186  }
187  if (is_empty() && !rhs.is_empty()) {
188  return false;
189  }
190  if (!is_empty() && rhs.is_empty()) {
191  return false;
192  }
193  return memcmp(bits_, rhs.bits_, used_bytes()) == 0;
194  }

Here is the caller graph for this function:

template<typename bit_base>
template<typename packable >
void jubatus::core::storage::bit_vector_base< bit_base >::pack ( packable &  buffer) const
inline

Definition at line 351 of file bit_vector.hpp.

351  {
352  msgpack::pack(buffer, *this);
353  }
template<typename bit_base>
bit_base* jubatus::core::storage::bit_vector_base< bit_base >::raw_data_unsafe ( )
inline
template<typename bit_base>
const bit_base* jubatus::core::storage::bit_vector_base< bit_base >::raw_data_unsafe ( ) const
inline

Definition at line 310 of file bit_vector.hpp.

310  {
311  return bits_;
312  }
template<typename bit_base>
bool jubatus::core::storage::bit_vector_base< bit_base >::release ( )
inlineprivate

Definition at line 404 of file bit_vector.hpp.

Referenced by jubatus::core::storage::bit_vector_base< uint64_t >::resize_and_clear(), and jubatus::core::storage::bit_vector_base< uint64_t >::~bit_vector_base().

404  {
405  if (bits_ && own_) {
406  delete[] bits_;
407  own_ = false;
408  bits_ = NULL;
409  return true;
410  }
411  return false;
412  }

Here is the caller graph for this function:

template<typename bit_base>
void jubatus::core::storage::bit_vector_base< bit_base >::resize_and_clear ( uint64_t  bit_num)
inline

Definition at line 174 of file bit_vector.hpp.

Referenced by jubatus::core::recommender::set_bit_vector().

Here is the caller graph for this function:

template<typename bit_base>
void jubatus::core::storage::bit_vector_base< bit_base >::reverse_bit ( size_t  pos)
inline

Definition at line 244 of file bit_vector.hpp.

244  {
245  if (bits_ == NULL) {
246  alloc_memory();
247  }
248  if (bit_num_ < pos) {
249  throw bit_vector_unmatch_exception(
250  "reverse_bit(): invalid posison " +
251  jubatus::util::lang::lexical_cast<std::string>(pos) +
252  " for length: " +
253  jubatus::util::lang::lexical_cast<std::string>(bit_num_));
254  }
255  bits_[pos / BASE_BITS] ^= (1LLU << (pos % BASE_BITS));
256  }
template<typename bit_base>
void jubatus::core::storage::bit_vector_base< bit_base >::set_bit ( size_t  pos)
inline

Definition at line 231 of file bit_vector.hpp.

Referenced by jubatus::core::nearest_neighbor::binarize().

231  {
232  if (bits_ == NULL) {
233  alloc_memory();
234  }
235  if (static_cast<size_t>(bit_num_) < pos) {
236  throw bit_vector_unmatch_exception(
237  "set_bit(): invalid posison " +
238  jubatus::util::lang::lexical_cast<std::string>(pos) +
239  " for length: " +
240  jubatus::util::lang::lexical_cast<std::string>(bit_num_));
241  }
242  bits_[pos / BASE_BITS] |= (1LLU << (pos % BASE_BITS));
243  }

Here is the caller graph for this function:

template<typename bit_base>
void jubatus::core::storage::bit_vector_base< bit_base >::status ( std::ostream &  os) const
inline

Definition at line 341 of file bit_vector.hpp.

341  {
342  os << "status():";
343  if (bits_ == NULL) {
344  os << "(unallocated)" << " owns_" << own_;
345  return;
346  }
347  os << (own_ ? "[own]" : "[other]") << std::endl;
348  }
template<typename bit_base>
void jubatus::core::storage::bit_vector_base< bit_base >::swap ( bit_vector_base< bit_base > &  x)
inline

Definition at line 215 of file bit_vector.hpp.

Referenced by jubatus::core::storage::bit_vector_base< uint64_t >::swap().

215  {
216  using std::swap;
217  swap(bit_num_, x.bit_num_);
218  swap(bits_, x.bits_);
219  swap(own_, x.own_);
220  }
void swap(weighted_point &p1, weighted_point &p2)
Definition: types.hpp:47

Here is the caller graph for this function:

template<typename bit_base>
size_t jubatus::core::storage::bit_vector_base< bit_base >::used_bytes ( ) const
inline

Friends And Related Function Documentation

template<typename bit_base>
std::ostream& operator<< ( std::ostream &  os,
const bit_vector_base< bit_base > &  bv 
)
friend

Definition at line 337 of file bit_vector.hpp.

337  {
338  bv.debug_print(os);
339  return os;
340  }
template<typename bit_base>
void swap ( bit_vector_base< bit_base > &  l,
bit_vector_base< bit_base > &  r 
)
friend

Definition at line 221 of file bit_vector.hpp.

221  {
222  l.swap(r);
223  }

Member Data Documentation

template<typename bit_base>
const size_t jubatus::core::storage::bit_vector_base< bit_base >::BASE_BITS = sizeof(bit_base) * 8
static
template<typename bit_base>
size_t jubatus::core::storage::bit_vector_base< bit_base >::bit_num_
private
template<typename bit_base>
bit_base* jubatus::core::storage::bit_vector_base< bit_base >::bits_
private
template<typename bit_base>
const size_t jubatus::core::storage::bit_vector_base< bit_base >::BLOCKSIZE = sizeof(bit_base)
static
template<typename bit_base>
bool jubatus::core::storage::bit_vector_base< bit_base >::own_
private

The documentation for this struct was generated from the following files: