jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
discrete_distribution.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 
18 
19 #include <vector>
20 
21 using std::vector;
22 
23 namespace jubatus {
24 namespace core {
25 namespace clustering {
26 
28  vector<double>::iterator begin,
29  vector<double>::iterator end)
30  : r_(),
31  whist_() {
32  sum_ = 0;
33  for (vector<double>::iterator it = begin; it != end; ++it) {
34  sum_ += *it;
35  whist_.push_back(sum_);
36  }
37 }
38 
40  double d = r_.next_double() * sum_;
41  return convert(d);
42 }
43 
45  size_t low = 0;
46  size_t high = whist_.size() - 1;
47  size_t mid = 0;
48  while (low < high) {
49  mid = (low + high) / 2;
50  if (whist_[mid] < d) {
51  if (low == mid) {
52  break;
53  }
54  low = mid;
55  } else {
56  if (high == mid) {
57  break;
58  }
59  high = mid;
60  }
61  }
62  return high;
63 }
64 
65 } // namespace clustering
66 } // namespace core
67 } // namespace jubatus
discrete_distribution(std::vector< double >::iterator begin, std::vector< double >::iterator end)