jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
counter.hpp
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 
17 #ifndef JUBATUS_CORE_FV_CONVERTER_COUNTER_HPP_
18 #define JUBATUS_CORE_FV_CONVERTER_COUNTER_HPP_
19 
20 #include <ostream>
21 #include <sstream>
22 
23 #include "jubatus/util/data/unordered_map.h"
24 #include "../common/unordered_map.hpp"
25 
26 namespace jubatus {
27 namespace core {
28 namespace fv_converter {
29 
30 template<class T>
31 class counter {
32  public:
33  typedef jubatus::util::data::unordered_map<T, double> map_t;
34  typedef typename jubatus::util::data::unordered_map<T, double>
36  typedef typename jubatus::util::data::unordered_map<T, double>
38 
39  bool contains(const T& key) const {
40  return data_.count(key) != 0;
41  }
42 
43  double operator[](const T& key) const {
44  const_iterator it = data_.find(key);
45  if (it == data_.end()) {
46  return 0;
47  } else {
48  return it->second;
49  }
50  }
51 
52  double& operator[](const T& key) {
53  if (data_.count(key) == 0) {
54  data_[key] = 0;
55  }
56  return data_[key];
57  }
58 
59  const_iterator begin() const {
60  return data_.begin();
61  }
62 
63  iterator begin() {
64  return data_.begin();
65  }
66 
67  const_iterator end() const {
68  return data_.end();
69  }
70 
71  iterator end() {
72  return data_.end();
73  }
74 
75  void clear() {
76  jubatus::util::data::unordered_map<T, double>().swap(data_);
77  }
78 
79  void add(const counter<T>& counts) {
80  for (const_iterator it = counts.begin(); it != counts.end(); ++it) {
81  (*this)[it->first] += it->second;
82  }
83  }
84 
86 
87  friend std::ostream& operator<<(std::ostream& os, const counter<T>& c) {
88  os << "{";
89  for (typename
90  jubatus::util::data::unordered_map<T, double>::const_iterator it
91  = c.data_.begin();
92  it != c.data_.end();
93  ++it) {
94  os << it->first << ":" << it->second << ", ";
95  }
96  os << "}";
97  return os;
98  }
99 
100  private:
101  jubatus::util::data::unordered_map<T, double> data_;
102 };
103 
104 } // namespace fv_converter
105 } // namespace core
106 } // namespace jubatus
107 
108 #endif // JUBATUS_CORE_FV_CONVERTER_COUNTER_HPP_
jubatus::util::data::unordered_map< T, double > data_
Definition: counter.hpp:101
jubatus::util::data::unordered_map< std::string, double >::const_iterator const_iterator
Definition: counter.hpp:35
const_iterator begin() const
Definition: counter.hpp:59
void swap(weighted_point &p1, weighted_point &p2)
Definition: types.hpp:47
double operator[](const T &key) const
Definition: counter.hpp:43
void add(const counter< T > &counts)
Definition: counter.hpp:79
double & operator[](const T &key)
Definition: counter.hpp:52
const_iterator end() const
Definition: counter.hpp:67
jubatus::util::data::unordered_map< T, double > map_t
Definition: counter.hpp:33
bool contains(const T &key) const
Definition: counter.hpp:39
jubatus::util::data::unordered_map< std::string, double >::iterator iterator
Definition: counter.hpp:37