jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
random_unlearner.cpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2013 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 "random_unlearner.hpp"
18 
19 #include <string>
20 #include <limits>
21 #include "../common/exception.hpp"
22 
23 namespace jubatus {
24 namespace core {
25 namespace unlearner {
26 
28  : max_size_(conf.max_size) {
29  if (conf.max_size <= 0) {
30  throw JUBATUS_EXCEPTION(
32  "max_size must be a positive integer"));
33  }
34  if (conf.seed) {
35  if (*conf.seed < 0 || std::numeric_limits<uint32_t>::max() < *conf.seed) {
36  throw JUBATUS_EXCEPTION(
38  "unlearner seed must be within unsigned 32 bit integer"));
39  }
40  mtr_ = jubatus::util::math::random::mtrand(*conf.seed);
41  }
42  id_map_.reserve(max_size_);
43  ids_.reserve(max_size_);
44 }
45 
46 bool random_unlearner::can_touch(const std::string& id) {
47  return true;
48 }
49 
50 bool random_unlearner::touch(const std::string& id) {
51  if (id_map_.count(id) > 0) {
52  return true;
53  }
54 
55  size_t new_id_pos = -1;
56  if (id_map_.size() < max_size_) {
57  // Just add new ID to the ID set.
58  ids_.push_back(id);
59  new_id_pos = ids_.size() - 1;
60  } else {
61  // Need to unlearn the old entry and replace it with new one.
62  new_id_pos = mtr_(id_map_.size());
63  const std::string old_id = ids_[new_id_pos];
64  unlearn(old_id);
65  id_map_.erase(old_id);
66  ids_[new_id_pos] = id;
67  }
68  id_map_.insert(std::make_pair(id, new_id_pos));
69 
70  return true;
71 }
72 
73 bool random_unlearner::remove(const std::string& id) {
74  if (id_map_.count(id) == 0) {
75  return false;
76  }
77 
78  const size_t id_pos = id_map_[id];
79  id_map_.erase(id);
80 
81  // Overwrite the ID with the last element to avoid calling erase to vector.
82  const std::string back_id = ids_.back();
83  ids_.pop_back();
84  if (id != back_id) {
85  ids_[id_pos] = back_id;
86  id_map_[back_id] = id_pos;
87  }
88 
89  return true;
90 }
91 
92 bool random_unlearner::exists_in_memory(const std::string& id) const {
93  return id_map_.count(id) > 0;
94 }
95 
96 } // namespace unlearner
97 } // namespace core
98 } // namespace jubatus
jubatus::util::math::random::mtrand mtr_
void unlearn(const std::string &id) const
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79
jubatus::util::data::optional< std::string > unlearner
bool exists_in_memory(const std::string &id) const
jubatus::util::data::unordered_map< std::string, size_t > id_map_
jubatus::util::data::optional< int64_t > seed