mf
Media Framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ndarray_timed_view.h
Go to the documentation of this file.
1 #ifndef MF_NDARRAY_TIMED_VIEW_H_
2 #define MF_NDARRAY_TIMED_VIEW_H_
3 
4 #include <stdexcept>
5 #include "../common.h"
6 #include "ndarray_view.h"
7 
8 namespace mf {
9 
11 
12 template<std::size_t Dim, typename T>
13 class ndarray_timed_view : public ndarray_view<Dim, T> {
14  using base = ndarray_view<Dim, T>;
15 
16 private:
17  time_unit start_time_;
18 
19 public:
21  ndarray_timed_view() : base(), start_time_(-1) { }
22 
24 
26  base(vw), start_time_(start_time)
27  {
28  MF_EXPECTS(! vw.is_null());
29  }
30 
32 
33  time_unit start_time() const { return start_time_; }
34  time_unit end_time() const { return start_time_ + base::shape().front(); }
35  time_unit duration() const { return base::shape().front(); }
36 
37  std::ptrdiff_t time_index(time_unit t) const { return t - start_time_; }
38  time_unit time_at(std::ptrdiff_t i) const { return start_time_ + base::fix_coordinate_(i, 0); }
39 
40  decltype(auto) at_time(time_unit t) const {
41  if(span().includes(t)) return base::operator[](time_index(t));
42  else throw std::out_of_range("time out of bounds");
43  }
44  decltype(auto) at_time(time_unit t) {
45  if(span().includes(t)) return base::operator[](time_index(t));
46  else throw std::out_of_range("time out of bounds");
47  }
48 
49  time_span span() const { return time_span(start_time(), start_time() + duration()); }
50 
51  void reset(const ndarray_timed_view& vw) {
52  start_time_ = vw.start_time_;
53  base::reset(vw);
54  }
55 };
56 // TODO section() etc.
57 
58 }
59 
60 #endif
Mapping between coordinates, indices, and addresses of multi-dimensional data.
Definition: ndarray_view.h:16
decltype(auto) at_time(time_unit t) const
Definition: ndarray_timed_view.h:40
std::ptrdiff_t time_unit
Discrete time unit type.
Definition: common.h:52
Ndarray view with absolute time indices associated to first dimension.
Definition: ndarray_timed_view.h:13
ndarray_timed_view()
Create null timed view.
Definition: ndarray_timed_view.h:21
const shape_type & shape() const noexcept
Definition: ndarray_view.h:200
static ndarray_timed_view null()
Definition: ndarray_timed_view.h:31
time_unit duration() const
Definition: ndarray_timed_view.h:35
decltype(auto) operator[](std::ptrdiff_t c) const
Subscript operator, creates slice on first dimension.
Definition: ndarray_view.h:163
ndarray_timed_view(const base &vw, time_unit start_time)
Create timed view where time index start_time is associated with frame vw[0].
Definition: ndarray_timed_view.h:25
std::ptrdiff_t time_index(time_unit t) const
Definition: ndarray_timed_view.h:37
void reset(const ndarray_timed_view &vw)
Definition: ndarray_timed_view.h:51
time_unit time_at(std::ptrdiff_t i) const
Definition: ndarray_timed_view.h:38
bool is_null() const noexcept
Definition: ndarray_view.h:123
time_unit end_time() const
Definition: ndarray_timed_view.h:34
time_span span() const
Definition: ndarray_timed_view.h:49
#define MF_EXPECTS(condition)
Definition: common.h:27
One-dimensional time span.
Definition: common.h:61
const T & front() const noexcept
Definition: ndcoord.h:117
time_unit start_time() const
Definition: ndarray_timed_view.h:33
void reset() noexcept
Definition: ndarray_view.h:127
std::ptrdiff_t fix_coordinate_(std::ptrdiff_t c, std::ptrdiff_t dim) const
Definition: ndarray_view.tcc:92