jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
assoc_vector.hpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2014 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_COMMON_ASSOC_VECTOR_HPP_
18 #define JUBATUS_CORE_COMMON_ASSOC_VECTOR_HPP_
19 
20 #include <algorithm>
21 #include <utility>
22 #include <vector>
23 #include <msgpack.hpp>
24 
25 namespace jubatus {
26 namespace core {
27 namespace common {
28 
29 template <typename K, typename V>
30 class assoc_vector {
31  public:
32  typedef std::vector<std::pair<K, V> > data_type;
33  typedef typename data_type::const_iterator const_iterator;
34  typedef typename data_type::iterator iterator;
35 
36  const_iterator begin() const {
37  return data_.begin();
38  }
39 
40  iterator begin() {
41  return data_.begin();
42  }
43 
44  const_iterator end() const {
45  return data_.end();
46  }
47 
48  iterator end() {
49  return data_.end();
50  }
51 
52  size_t size() const {
53  return data_.size();
54  }
55 
56  bool empty() const {
57  return data_.empty();
58  }
59 
60  const_iterator find(const K& key) const {
61  const_iterator it = std::lower_bound(begin(),
62  end(),
63  key,
65  if (it != end() && it->first == key) {
66  return it;
67  } else {
68  return end();
69  }
70  }
71 
72  iterator find(const K& key) {
73  const_iterator it = const_cast<const assoc_vector&>(*this).find(key);
74  return begin() + (it - begin());
75  }
76 
77  size_t count(const K& key) const {
78  if (find(key) == end()) {
79  return 0;
80  } else {
81  return 1;
82  }
83  }
84 
85  iterator erase(const K& key) {
86  iterator it = find(key);
87  if (it != data_.end()) {
88  return erase(it);
89  } else {
90  return end();
91  }
92  }
93 
94  iterator erase(iterator it) {
95  return data_.erase(it);
96  }
97 
98  V& operator[](const K& key) {
99  iterator it = std::lower_bound(begin(), end(), key, less_pair_and_key());
100  if (it != data_.end() && it->first == key) {
101  return it->second;
102  } else {
103  it = data_.insert(it, std::make_pair(key, V()));
104  return it->second;
105  }
106  }
107 
108  template <typename Packer>
109  void msgpack_pack(Packer& pk) const {
110  pk.pack_map(data_.size());
111  for (std::size_t i = 0; i < data_.size(); ++i) {
112  pk.pack(data_[i].first);
113  pk.pack(data_[i].second);
114  }
115  }
116 
117  void msgpack_unpack(msgpack::object o) {
118  if (o.type != msgpack::type::MAP) {
119  throw msgpack::type_error();
120  }
121  std::vector<std::pair<K, V> > data(o.via.map.size);
122  for (std::size_t i = 0; i < data.size(); ++i) {
123  o.via.map.ptr[i].key.convert(&data[i].first);
124  o.via.map.ptr[i].val.convert(&data[i].second);
125  }
126  data.swap(data_);
127  }
128 
129  private:
131  template <class Pair, class Key>
132  bool operator()(Pair const& p, Key const& k) const {
133  return p.first < k;
134  }
135  };
136 
137  std::vector<std::pair<K, V> > data_;
138 };
139 
140 } // namespace common
141 } // namespace core
142 } // namespace jubatus
143 
144 #endif // JUBATUS_CORE_COMMON_ASSOC_VECTOR_HPP_
const_iterator find(const K &key) const
void msgpack_pack(Packer &pk) const
std::vector< std::pair< K, V > > data_
void msgpack_unpack(msgpack::object o)
const_iterator begin() const
data_type::const_iterator const_iterator
std::vector< std::pair< K, V > > data_type
size_t count(const K &key) const
bool operator()(Pair const &p, Key const &k) const