jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
input_window.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_BURST_INPUT_WINDOW_HPP_
18 #define JUBATUS_CORE_BURST_INPUT_WINDOW_HPP_
19 
20 #include <stdint.h>
21 #include <algorithm>
22 #include <cmath>
23 #include <vector>
24 #include <msgpack.hpp>
25 #include "../common/assert.hpp"
26 #include "../common/exception.hpp"
27 
28 #include "window_fwd.hpp"
29 
30 namespace jubatus {
31 namespace core {
32 namespace burst {
33 
34 template<class Batch>
35 class basic_window {
36  public:
37  typedef Batch batch_type;
38 
39  explicit basic_window(double batch_interval = 1)
40  : batches_(), start_pos_(0), batch_interval_(batch_interval) {
41  if (!(batch_interval > 0)) {
43  "window: batch_interval should be > 0"));
44  }
45  }
46 
47  basic_window(double start_pos, double batch_interval, int32_t batch_size)
48  : batches_(), start_pos_(start_pos), batch_interval_(batch_interval) {
49  if (!(batch_interval > 0)) {
51  "window: batch_interval should be > 0"));
52  }
53  if (!(batch_size >= 0)) {
55  "window: batch_size should be >= 0"));
56  }
57  batches_.resize(batch_size);
58  }
59 
60  // get index for pos; return -1 if out of range
61  int get_index(double pos) const {
62  int n = get_batch_size();
63  int i = get_index_(pos);
64 
65  if (i < 0 || n <= i) {
66  JUBATUS_ASSERT(!contains(pos));
67  return -1;
68  }
69 
71  return i;
72  }
73 
74  double get_start_pos() const {
75  return start_pos_;
76  }
77  double get_end_pos() const {
78  return start_pos_ + get_all_interval();
79  }
80  bool contains(double pos) const {
81  return get_start_pos() <= pos && pos < get_end_pos();
82  }
83  double get_all_interval() const {
85  }
86 
87  int32_t get_batch_size() const {
88  return batches_.size();
89  }
90  double get_batch_interval() const {
91  return batch_interval_;
92  }
93 
94  const std::vector<batch_type>& get_batches() const {
95  return batches_;
96  }
97 
98  batch_type& get_batch_by_index(size_t i) {
99  return batches_[i];
100  }
101  const batch_type& get_batch_by_index(size_t i) const {
102  return batches_[i];
103  }
104 
105  void swap(basic_window& x) {
106  using std::swap;
107  swap(batches_, x.batches_);
110  }
111  friend void swap(basic_window& x, basic_window& y) {
112  x.swap(y);
113  }
114 
115  protected:
116  std::vector<batch_type> batches_;
117  double start_pos_;
119 
120  // get index for pos; no out of range, return as if extended
121  int get_index_(double pos) const {
122  return static_cast<int>(
123  std::floor((pos - start_pos_) / get_batch_interval()));
124  }
125 };
126 
127 class input_window : private basic_window<batch_input> {
129 
130  public:
131  explicit input_window(double start_pos = 0,
132  double batch_interval = 1,
133  int32_t batch_size = 0)
134  : base_(start_pos, batch_interval, batch_size) {
135  }
136 
137  bool add_document(uint32_t d, uint32_t r, double pos) {
138  int i = base_::get_index(pos);
139 
140  if (i < 0) {
141  return false;
142  }
143 
144  batches_[i].d += d;
145  batches_[i].r += r;
146  return true;
147  }
148 
149  using base_::get_start_pos;
150  using base_::get_end_pos;
151  using base_::contains;
153 
154  using base_::get_batch_size;
156  using base_::get_batches;
158 
159  void swap(input_window& x) {
160  base_::swap(static_cast<base_&>(x));
161  }
162  friend void swap(input_window& x, input_window& y) {
163  x.swap(y);
164  }
165 
167 };
168 
169 } // namespace burst
170 } // namespace core
171 } // namespace jubatus
172 
173 #endif // JUBATUS_CORE_BURST_INPUT_WINDOW_HPP_
friend void swap(input_window &x, input_window &y)
input_window(double start_pos=0, double batch_interval=1, int32_t batch_size=0)
basic_window< batch_input > base_
batch_type & get_batch_by_index(size_t i)
std::vector< batch_type > batches_
bool contains(double pos) const
#define JUBATUS_ASSERT(expr)
Definition: assert.hpp:55
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79
friend void swap(basic_window &x, basic_window &y)
int get_index(double pos) const
void swap(weighted_point &p1, weighted_point &p2)
Definition: types.hpp:47
basic_window(double batch_interval=1)
const batch_type & get_batch_by_index(size_t i) const
bool add_document(uint32_t d, uint32_t r, double pos)
const std::vector< batch_type > & get_batches() const
basic_window(double start_pos, double batch_interval, int32_t batch_size)
MSGPACK_DEFINE(batches_, start_pos_, batch_interval_)
int get_index_(double pos) const