🛰️航天仿真算法库 SpaceAST 0.3.1
载入中...
搜索中...
未找到
TimePointRange.hpp
浏览该文件的文档.
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include "TimePoint.hpp"
25#include "AstUtil/Math.hpp"
26#include <iterator> // for std::input_iterator_tag
27#include <cstddef> // for size_t / ptrdiff_t
28#include <cmath>
29
30AST_NAMESPACE_BEGIN
31
32
43{
44public:
50 TimePointRange(const TimePoint& start, const TimePoint& stop, double step, size_t n)
51 : start_(start)
52 , stop_(stop)
53 , step_(step)
54 , n_(n)
55 {}
56
61 TimePointRange(const TimePoint& start, const TimePoint& stop, double step)
62 : start_(start)
63 , stop_(stop)
64 , step_(step)
65 , n_(aDiscretizedCount(stop - start, step))
66 {}
67
69 {
70 public:
71 using iterator_category = std::input_iterator_tag;
72 using value_type = TimePoint;
73 using difference_type = ptrdiff_t;
74 using pointer = const TimePoint*;
75 using reference = TimePoint; // input iterator:按值返回
76
77 iterator() = default;
78 iterator(const TimePointRange* range, size_t idx)
79 : range_(range), idx_(idx) {}
80
83 {
84 return (idx_ == range_->n_ - 1)
85 ? range_->stop_
86 : range_->start_ + range_->step_ * idx_;
87 }
88
89 iterator& operator++() { ++idx_; return *this; }
90 iterator operator++(int) { auto tmp = *this; ++*this; return tmp; }
91
92 bool operator==(const iterator& other) const { return idx_ == other.idx_; }
93 bool operator!=(const iterator& other) const { return !(*this == other); }
94
95 private:
96 const TimePointRange* range_ = nullptr;
97 size_t idx_ = 0;
98 };
99
100 iterator begin() const { return iterator(this, 0); }
101 iterator end() const { return iterator(this, n_); }
102 size_t size() const { return n_; }
103
107 TimePoint operator[](size_t i) const
108 {
109 return (i == n_ - 1) ? stop_ : start_ + step_ * i;
110 }
111
112 const TimePoint& start() const {return start_;}
113 const TimePoint& stop() const{return stop_;}
114 double step() const {return step_;}
115
116private:
117 TimePoint start_;
118 TimePoint stop_;
119 double step_;
120 size_t n_;
121};
122
123
127AST_NAMESPACE_END
绝对时间点
定义 TimePointRange.hpp:69
reference operator*() const
解引用:除末点按 step 步进,末点返回 stop_
定义 TimePointRange.hpp:82
按步长采样的时间点范围(惰性可迭代)
定义 TimePointRange.hpp:43
TimePointRange(const TimePoint &start, const TimePoint &stop, double step)
构造时间点采样范围
定义 TimePointRange.hpp:61
TimePointRange(const TimePoint &start, const TimePoint &stop, double step, size_t n)
构造时间点采样范围
定义 TimePointRange.hpp:50
TimePoint operator[](size_t i) const
下标访问:返回第 i 个采样时间点(末点返回 stop_)
定义 TimePointRange.hpp:107
绝对时间点
定义 TimePoint.hpp:108
A_ALWAYS_INLINE size_t aDiscretizedCount(double span, double step)
计算闭区间 [0, span] 按 step 步长采样(含两端点)的采样点数
定义 Math.hpp:107