jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
key_manager.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_COMMON_KEY_MANAGER_HPP_
18 #define JUBATUS_CORE_COMMON_KEY_MANAGER_HPP_
19 
20 #include <stdint.h>
21 
22 #include <algorithm>
23 #include <string>
24 #include <vector>
25 
26 #include <msgpack.hpp>
27 #include "jubatus/util/data/unordered_map.h"
28 #include "unordered_map.hpp"
29 
30 namespace jubatus {
31 namespace core {
32 namespace common {
33 
34 class key_manager {
35  public:
36  enum {
37  NOTFOUND = 0xFFFFFFFFFFFFFFFFLLU
38  };
39 
40  key_manager();
41  // following member funcions are implicitly defined:
42  // key_manager(const key_manager& k) = default;
43  // key_manager& operator=(const key_manager& k) = default;
44  // ~key_manager() = default;
45  void swap(key_manager& km) {
46  key2id_.swap(km.key2id_);
47  id2key_.swap(km.id2key_);
48  }
49 
50  size_t size() const {
51  return key2id_.size();
52  }
53 
54  uint64_t get_id(const std::string& key);
55  uint64_t get_id_const(const std::string& key) const;
56  const std::string& get_key(const uint64_t id) const;
57  std::vector<std::string> get_all_id2key() const;
58  void clear();
59  bool set_key(const std::string& key);
60 
61  void init_by_id2key(const std::vector<std::string>& id2key);
62  void delete_key(const std::string& name);
63 
64  uint64_t get_max_id() const;
65 
66  friend std::ostream& operator<<(std::ostream& os, const key_manager& km) {
67  typedef jubatus::util::data::unordered_map<std::string, uint64_t> key2id_t;
68  os << "[";
69  for (key2id_t::const_iterator it = km.key2id_.begin();
70  it != km.key2id_.end();
71  ++it) {
72  os << it->first << ":" << it->second << ", ";
73  }
74  os << "]";
75  return os;
76  }
78 
79  private:
80  uint64_t append_key(const std::string& key);
81 
82  uint64_t next_id_;
83  util::data::unordered_map<std::string, uint64_t> key2id_;
84  util::data::unordered_map<uint64_t, std::string> id2key_;
85 };
86 
87 inline void swap(key_manager& l, key_manager& r) { // NOLINT
88  l.swap(r);
89 }
90 
91 } // namespace common
92 } // namespace core
93 } // namespace jubatus
94 
95 #endif // JUBATUS_CORE_COMMON_KEY_MANAGER_HPP_
uint64_t get_id_const(const std::string &key) const
Definition: key_manager.cpp:67
void swap(byte_buffer &one, byte_buffer &another)
Definition: byte_buffer.hpp:95
MSGPACK_DEFINE(key2id_, id2key_, next_id_)
void init_by_id2key(const std::vector< std::string > &id2key)
bool set_key(const std::string &key)
Definition: key_manager.cpp:57
util::data::unordered_map< uint64_t, std::string > id2key_
Definition: key_manager.hpp:84
const std::string & get_key(const uint64_t id) const
Definition: key_manager.cpp:78
util::data::unordered_map< std::string, uint64_t > key2id_
Definition: key_manager.hpp:83
friend std::ostream & operator<<(std::ostream &os, const key_manager &km)
Definition: key_manager.hpp:66
uint64_t get_id(const std::string &key)
Definition: key_manager.cpp:48
uint64_t append_key(const std::string &key)
Definition: key_manager.cpp:39
void delete_key(const std::string &name)
std::vector< std::string > get_all_id2key() const
Definition: key_manager.cpp:87