jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
sparse_matrix_storage.cpp
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 
18 
19 #include <algorithm>
20 #include <cmath>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 #include "jubatus/util/data/unordered_set.h"
25 
26 using std::istream;
27 using std::ostream;
28 using std::make_pair;
29 using std::pair;
30 using std::string;
31 using std::vector;
32 
33 namespace jubatus {
34 namespace core {
35 namespace storage {
36 
38 }
39 
41 }
42 
44  const sparse_matrix_storage& sms) {
45  tbl_ = sms.tbl_;
46  column2id_ = sms.column2id_;
47  return *this;
48 }
49 
51  const string& row,
52  const string& column,
53  float val) {
54  tbl_[row][column2id_.get_id(column)] = val;
55 }
56 
58  const string& row,
59  const vector<pair<string, float> >& columns) {
60  row_t& row_v = tbl_[row];
61  for (size_t i = 0; i < columns.size(); ++i) {
62  float& v = row_v[column2id_.get_id(columns[i].first)];
63  // norm_ptr_->notify(row, v, columns[i].second);
64  v = columns[i].second;
65  }
66 }
67 
69  const string& row,
70  const string& column) const {
71  tbl_t::const_iterator it = tbl_.find(row);
72  if (it == tbl_.end()) {
73  return 0.f;
74  }
75 
76  uint64_t id = column2id_.get_id_const(column);
78  return 0.f;
79  }
80 
81  row_t::const_iterator cit = it->second.find(id);
82  if (cit == it->second.end()) {
83  return 0.f;
84  }
85  return cit->second;
86 }
87 
89  const string& row,
90  vector<pair<string, float> >& columns) const {
91  columns.clear();
92  tbl_t::const_iterator it = tbl_.find(row);
93  if (it == tbl_.end()) {
94  return;
95  }
96  const row_t& row_v = it->second;
97  for (row_t::const_iterator row_it = row_v.begin(); row_it != row_v.end();
98  ++row_it) {
99  columns.push_back(
100  make_pair(column2id_.get_key(row_it->first), row_it->second));
101  }
102 }
103 
104 float sparse_matrix_storage::calc_l2norm(const string& row) const {
105  tbl_t::const_iterator it = tbl_.find(row);
106  if (it == tbl_.end()) {
107  return 0.f;
108  }
109  float sq_norm = 0.f;
110  const row_t& row_v = it->second;
111  for (row_t::const_iterator row_it = row_v.begin(); row_it != row_v.end();
112  ++row_it) {
113  sq_norm += row_it->second * row_it->second;
114  }
115  return std::sqrt(sq_norm);
116 }
117 
118 void sparse_matrix_storage::remove(const string& row, const string& column) {
119  tbl_t::iterator it = tbl_.find(row);
120  if (it == tbl_.end()) {
121  return;
122  }
123 
124  uint64_t id = column2id_.get_id_const(column);
125  if (id == common::key_manager::NOTFOUND) {
126  return;
127  }
128 
129  row_t::iterator cit = it->second.find(id);
130  if (cit == it->second.end()) {
131  return;
132  }
133  // norm_ptr_->notify(row, cit->second, 0.f);
134  it->second.erase(cit);
135 }
136 
137 void sparse_matrix_storage::remove_row(const string& row) {
138  tbl_t::iterator it = tbl_.find(row);
139  if (it == tbl_.end()) {
140  return;
141  }
142 
143  // for (row_t::const_iterator cit = it->second.begin();
144  // cit != it->second.end(); ++cit){
145  // norm_ptr_->notify(row, cit->second, 0.f);
146  // }
147 
148  tbl_.erase(it);
149 }
150 
151 void sparse_matrix_storage::get_all_row_ids(vector<string>& ids) const {
152  ids.clear();
153  for (tbl_t::const_iterator it = tbl_.begin(); it != tbl_.end(); ++it) {
154  ids.push_back(it->first);
155  }
156 }
157 
159  tbl_t().swap(tbl_);
161  // norm_ptr_->clear();
162 }
163 
165  const {
166  packer.pack(*this);
167 }
168 
169 void sparse_matrix_storage::unpack(msgpack::object o) {
170  o.convert(this);
171 }
172 
173 } // namespace storage
174 } // namespace core
175 } // namespace jubatus
void set(const std::string &row, const std::string &column, float val)
uint64_t get_id_const(const std::string &key) const
Definition: key_manager.cpp:67
void get_row(const std::string &row, std::vector< std::pair< std::string, float > > &columns) const
unordered_map< string, uint64_t >::const_iterator cit
Definition: key_manager.cpp:33
float calc_l2norm(const std::string &row) const
float get(const std::string &row, const std::string &column) const
const_iterator begin() const
void pack(framework::packer &packer) const
data_type::const_iterator const_iterator
const std::string & get_key(const uint64_t id) const
Definition: key_manager.cpp:78
msgpack::packer< jubatus_packer > packer
Definition: bandit_base.hpp:31
std::vector< T > v(size)
sparse_matrix_storage & operator=(const sparse_matrix_storage &)
uint64_t get_id(const std::string &key)
Definition: key_manager.cpp:48
jubatus::util::data::unordered_map< std::string, row_t > tbl_t
void get_all_row_ids(std::vector< std::string > &ids) const
void remove(const std::string &row, const std::string &column)
void set_row(const std::string &row, const std::vector< std::pair< std::string, float > > &columns)