Apollo 10.0
自动驾驶开放平台
interval_pool.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include <algorithm>
20#include <fstream>
21#include <iomanip>
22
23#include "cyber/common/log.h"
24
25namespace apollo {
26namespace data {
27
28IntervalPool::IntervalPool() {}
29
30void IntervalPool::AddInterval(const Interval& interval) {
31 if (pool_.empty() || interval.begin_time > pool_iter_->end_time) {
32 pool_.push_back(interval);
33 pool_iter_ = std::prev(pool_.end());
34 return;
35 }
36 pool_iter_->begin_time =
37 std::min(interval.begin_time, pool_iter_->begin_time);
38 pool_iter_->end_time = std::max(interval.end_time, pool_iter_->end_time);
39}
40
41void IntervalPool::AddInterval(const uint64_t begin_time,
42 const uint64_t end_time) {
43 struct Interval interval;
44 interval.begin_time = begin_time;
45 interval.end_time = end_time;
46 AddInterval(interval);
47}
48
50 // Sort the intervals by begin_time ascending
51 std::sort(pool_.begin(), pool_.end(),
52 [](const Interval& x, const Interval& y) {
53 return x.begin_time < y.begin_time;
54 });
55 pool_iter_ = pool_.begin();
56 accu_end_values_.clear();
57}
58
59bool IntervalPool::MessageFallIntoRange(const uint64_t msg_time) {
60 // For each message comes for checking, the logic is:
61 // 1. Add end_time of any intervals whose begin_time is smaller
62 // than message time to the helper set
63 // 2. Now if the helper set is not empty, means some range is still
64 // in progress, returns true
65 // 3. After this, remove end_time equals to message time from the set,
66 // which means these ranges are done being used
67 // This way range groups iterate along with messages, time complexity O(N)
68 while (pool_iter_ != pool_.end() && msg_time >= pool_iter_->begin_time) {
69 accu_end_values_.insert(pool_iter_->end_time);
70 ++pool_iter_;
71 }
72 bool found = !accu_end_values_.empty();
73 accu_end_values_.erase(msg_time);
74 return found;
75}
76
78 pool_.clear();
79 pool_iter_ = pool_.begin();
80 accu_end_values_.clear();
81}
82
84 auto idx = 0;
85 for (const auto& interval : pool_) {
86 AINFO << "Interval " << ++idx << ": " << interval.begin_time << " - "
87 << interval.end_time;
88 }
89}
90
91void IntervalPool::LogIntervalEvent(const std::string& name,
92 const std::string& description,
93 const uint64_t msg_time,
94 const uint64_t backward_time,
95 const uint64_t forward_time) const {
96 std::ofstream logfile(interval_event_log_file_path_,
97 std::ios::out | std::ios::app);
98 if (!logfile) {
99 AERROR << "Failed to write " << interval_event_log_file_path_;
100 return;
101 }
102 logfile << std::fixed << std::setprecision(9);
103 logfile << "name=" << name << ", description=\"" << description << "\""
104 << ", msg_time=" << msg_time << ", interval_range=["
105 << msg_time - backward_time << ":" << msg_time + forward_time << "]"
106 << std::endl;
107}
108
110 if (pool_.empty()) {
111 struct Interval interval;
112 interval.begin_time = 0;
113 interval.end_time = 0;
114 return interval;
115 }
116 return *pool_iter_;
117}
118
119} // namespace data
120} // namespace apollo
void AddInterval(const Interval &interval)
bool MessageFallIntoRange(const uint64_t msg_time)
Interval GetNextInterval() const
void LogIntervalEvent(const std::string &name, const std::string &description, const uint64_t msg_time, const uint64_t backward_time, const uint64_t forward_time) const
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37