autoppl  v0.8
A C++ template library for probabilistic programming
range.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <cassert>
3 #include <functional>
4 
5 namespace ppl {
6 namespace util {
7 
11 template <class Iter>
12 struct range
13 {
14  using iter_t = Iter;
15 
17  : begin_{begin}
18  , end_{end}
19  , size_{static_cast<size_t>(std::distance(begin, end))}
20  {}
21 
22  auto& operator()(size_t i) {
23  assert(i < size_);
24  return *std::next(begin_, i);
25  }
26 
27  const auto& operator()(size_t i) const {
28  assert(i < size_);
29  return *std::next(begin_, i);
30  }
31 
32  iter_t begin() { return begin_; }
33  const iter_t begin() const { return begin_; }
34 
35  iter_t end() { return end_; }
36  const iter_t end() const { return end_; }
37 
38  size_t size() const { return size_; }
39 
41  {
42  begin_ = begin;
43  end_ = end;
44  size_ = static_cast<size_t>(std::distance(begin, end));
45  }
46 
47 private:
48  iter_t begin_;
49  iter_t end_;
50  size_t size_;
51 };
52 
53 } // namespace util
54 } // namespace ppl
ppl::util::range::begin
const iter_t begin() const
Definition: range.hpp:33
ppl::util::range::operator()
const auto & operator()(size_t i) const
Definition: range.hpp:27
ppl::util::range::range
range(iter_t begin, iter_t end)
Definition: range.hpp:16
ppl::util::range::size
size_t size() const
Definition: range.hpp:38
ppl::util::range::end
const iter_t end() const
Definition: range.hpp:36
ppl::util::range::begin
iter_t begin()
Definition: range.hpp:32
ppl::util::range
Definition: range.hpp:13
ppl::util::range::operator()
auto & operator()(size_t i)
Definition: range.hpp:22
ppl
Definition: bounded.hpp:11
ppl::util::range::bind
void bind(iter_t begin, iter_t end)
Definition: range.hpp:40
ppl::util::range::iter_t
Iter iter_t
Definition: range.hpp:14
ppl::util::range::end
iter_t end()
Definition: range.hpp:35