jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
lsh_vector.cpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2012 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 #include "lsh_vector.hpp"
18 #include <algorithm>
19 #include <stdexcept>
20 #include <vector>
21 
22 using std::ostream;
23 using std::vector;
24 using std::out_of_range;
25 
26 namespace jubatus {
27 namespace core {
28 namespace storage {
29 
31 }
32 
34  : values_(lv.values_) {
35 }
36 
38  : values_(len) {
39 }
40 
41 lsh_vector::lsh_vector(const vector<int>& v)
42  : values_(v) {
43 }
44 
46 }
47 
48 bool lsh_vector::operator==(const lsh_vector& lv) const {
49  return values_ == lv.values_;
50 }
51 
52 bool lsh_vector::operator!=(const lsh_vector& lv) const {
53  return !operator==(lv);
54 }
55 
56 int lsh_vector::get(size_t pos) const {
57  return values_[pos];
58 }
59 
60 void lsh_vector::set(size_t pos, int value) {
61  values_[pos] = value;
62 }
63 
64 void lsh_vector::push_back(int value) {
65  values_.push_back(value);
66 }
67 
69  values_.reserve(sz);
70  values_.clear();
71  values_.resize(sz);
72 }
73 
74 size_t lsh_vector::size() const {
75  return values_.size();
76 }
77 
78 lsh_vector lsh_vector::slice(size_t from, size_t len) const {
79  if (from + len > values_.size()) {
80  throw out_of_range("slice range is out of size");
81  }
82 
83  lsh_vector lv;
84  lv.values_.assign(values_.begin() + from, values_.begin() + from + len);
85  return lv;
86 }
87 
88 lsh_vector lsh_vector::cut(size_t from, size_t len) const {
89  if (from + len > values_.size()) {
90  throw out_of_range("cut range is out of size");
91  }
92 
93  lsh_vector lv(values_.size() - len);
94  copy(values_.begin(), values_.begin() + from, lv.values_.begin());
95  copy(values_.begin() + from + len, values_.end(), lv.values_.begin() + from);
96 
97  return lv;
98 }
99 
100 void lsh_vector::debug_print(ostream& os) const {
101  for (size_t i = 0; i < values_.size(); ++i) {
102  if (i > 0) {
103  os << ' ';
104  }
105  os << values_[i];
106  }
107 }
108 
110  values_.swap(lv.values_);
111 }
112 
113 } // namespace storage
114 } // namespace core
115 } // namespace jubatus
lsh_vector slice(size_t from, size_t len) const
Definition: lsh_vector.cpp:78
bool operator==(const lsh_vector &lv) const
Definition: lsh_vector.cpp:48
void debug_print(std::ostream &os) const
Definition: lsh_vector.cpp:100
lsh_vector cut(size_t from, size_t len) const
Definition: lsh_vector.cpp:88
int get(size_t pos) const
Definition: lsh_vector.cpp:56
void set(size_t pos, int value)
Definition: lsh_vector.cpp:60
std::vector< T > v(size)
bool operator!=(const lsh_vector &lv) const
Definition: lsh_vector.cpp:52