jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
vector_util.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 "vector_util.hpp"
18 #include <algorithm>
19 #include <string>
20 
21 namespace jubatus {
22 namespace core {
23 namespace common {
24 
25 using std::sort;
26 using std::string;
27 
28 void sort_and_merge(sfv_t& sfv) {
29  if (sfv.size() <= 1) {
30  return;
31  }
32  sort(sfv.begin(), sfv.end());
33 
34  typedef sfv_t::iterator iterator;
35  iterator cur = sfv.begin();
36  iterator end = sfv.end();
37  for (iterator iter = cur+1; iter != end; ++iter) {
38  if (iter->first == cur->first) {
39  cur->second += iter->second;
40  } else {
41  ++cur;
42  *cur = *iter;
43  }
44  }
45  sfv.erase(cur+1, end);
46 }
47 
48 } // namespace common
49 } // namespace core
50 } // namespace jubatus
void sort_and_merge(sfv_t &sfv)
Definition: vector_util.cpp:28
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29