jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
window_intersection.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_WINDOW_INTERSECTION_HPP_
18 #define JUBATUS_CORE_BURST_WINDOW_INTERSECTION_HPP_
19 
20 #include <stdint.h>
21 #include <algorithm>
22 #include <utility>
23 #include <vector>
24 #include <limits>
25 #include <msgpack.hpp>
26 #include "jubatus/util/lang/shared_ptr.h"
27 
28 #include "input_window.hpp"
29 #include "../framework/mixable_helper.hpp"
30 
31 namespace jubatus {
32 namespace core {
33 namespace burst {
34 
36  double pos0, double pos1, double batch_interval) {
37  return std::abs(pos1 - pos0) / batch_interval < 0.01;
38 }
39 
41  public:
42  explicit intersection_helper(double start_pos = 0,
43  double batch_interval = 1,
44  int batch_size = 0)
45  : start_pos_(start_pos),
46  batch_interval_(batch_interval),
47  batch_size_(batch_size) {
48  }
49 
50  template<class Window>
51  explicit intersection_helper(const Window& w)
52  : start_pos_(w.get_start_pos()),
53  batch_interval_(w.get_batch_interval()),
54  batch_size_(w.get_batch_size()) {
55  }
56 
57  template<class Window>
58  std::pair<int, int> get_intersection(const Window& w) const {
59  int begin = get_index_for_boundary(w.get_start_pos());
60  int end = get_index_for_boundary(w.get_end_pos());
61 
62  if (begin == no_index() ||
63  end == no_index() ||
64  !has_batch_interval_equals_to(w.get_batch_interval())) {
65  // nonsense data is given (should throw an exception?)
66  return std::make_pair(no_index(), no_index());
67  }
68 
69  return std::make_pair(adjust_index(begin),
70  adjust_index(end));
71  }
72 
73  int get_index_for_boundary(double boundary_pos) const {
74  int candidate = get_index_(boundary_pos + batch_interval_/2);
75  double candidate_pos = start_pos_ + candidate * batch_interval_;
76  if (!position_near(boundary_pos, candidate_pos)) {
77  return no_index();
78  }
79  return candidate;
80  }
81 
82  // index value when index cannot be determined
83  static int no_index() { // constexpr is needed
84  return std::numeric_limits<int>::min();
85  }
86 
87  bool position_near(double pos0, double pos1) const {
88  return window_position_near(pos0, pos1, batch_interval_);
89  }
90 
91  bool has_batch_interval_equals_to(double interval1) const {
92  // comparing end_pos
93  size_t n = (std::max)(batch_size_, 1);
94  return position_near(n * interval1, n * batch_interval_);
95  }
96 
97  int adjust_index(int index) const {
98  if (index < 0) {
99  return 0;
100  } else if (index > batch_size_) {
101  return batch_size_;
102  } else {
103  return index;
104  }
105  }
106 
107  int get_index_(double pos) const {
108  return static_cast<int>(std::floor((pos - start_pos_) / batch_interval_));
109  }
110 
111  private:
112  double start_pos_;
115 };
116 
117 template<class W1, class W2>
118 std::pair<int, int> get_intersection(const W1& w1, const W2& w2) {
119  return intersection_helper(w1).get_intersection(w2);
120 }
121 
122 } // namespace burst
123 } // namespace core
124 } // namespace jubatus
125 
126 #endif // JUBATUS_CORE_BURST_WINDOW_INTERSECTION_HPP_
bool position_near(double pos0, double pos1) const
int get_index_for_boundary(double boundary_pos) const
intersection_helper(double start_pos=0, double batch_interval=1, int batch_size=0)
std::pair< int, int > get_intersection(const W1 &w1, const W2 &w2)
bool has_batch_interval_equals_to(double interval1) const
bool window_position_near(double pos0, double pos1, double batch_interval)
std::pair< int, int > get_intersection(const Window &w) const