1 /*
2 * Copyright (C) 2018 The Android Open Source Project
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
17 #include "src/trace_processor/row_iterators.h"
18
19 #include <algorithm>
20
21 #include "src/trace_processor/sqlite_utils.h"
22
23 namespace perfetto {
24 namespace trace_processor {
25
26 namespace {
27
28 template <typename Iterator>
FindNextOffset(Iterator begin,Iterator end,uint32_t offset)29 uint32_t FindNextOffset(Iterator begin, Iterator end, uint32_t offset) {
30 auto prev_it = begin + static_cast<ptrdiff_t>(offset);
31 auto current_it = std::find(prev_it, end, true);
32 return static_cast<uint32_t>(std::distance(begin, current_it));
33 }
34
FindNextOffset(const std::vector<bool> & filter,uint32_t offset,bool desc)35 uint32_t FindNextOffset(const std::vector<bool>& filter,
36 uint32_t offset,
37 bool desc) {
38 if (desc)
39 return FindNextOffset(filter.rbegin(), filter.rend(), offset);
40 return FindNextOffset(filter.begin(), filter.end(), offset);
41 }
42
43 } // namespace
44
45 RowIterator::~RowIterator() = default;
46
RangeRowIterator(uint32_t start_row,uint32_t end_row,bool desc)47 RangeRowIterator::RangeRowIterator(uint32_t start_row,
48 uint32_t end_row,
49 bool desc)
50 : start_row_(start_row), end_row_(end_row), desc_(desc) {}
51
RangeRowIterator(uint32_t start_row,bool desc,std::vector<bool> row_filter)52 RangeRowIterator::RangeRowIterator(uint32_t start_row,
53 bool desc,
54 std::vector<bool> row_filter)
55 : start_row_(start_row),
56 end_row_(start_row_ + static_cast<uint32_t>(row_filter.size())),
57 desc_(desc),
58 row_filter_(std::move(row_filter)) {
59 if (start_row_ < end_row_)
60 offset_ = FindNextOffset(row_filter_, offset_, desc_);
61 }
62
NextRow()63 void RangeRowIterator::NextRow() {
64 PERFETTO_DCHECK(!IsEnd());
65 offset_++;
66
67 if (!row_filter_.empty())
68 offset_ = FindNextOffset(row_filter_, offset_, desc_);
69 }
70
IsEnd()71 bool RangeRowIterator::IsEnd() {
72 return offset_ >= end_row_ - start_row_;
73 }
74
Row()75 uint32_t RangeRowIterator::Row() {
76 return desc_ ? end_row_ - offset_ - 1 : start_row_ + offset_;
77 }
78
RowCount() const79 uint32_t RangeRowIterator::RowCount() const {
80 if (row_filter_.empty()) {
81 return end_row_ - start_row_;
82 }
83 auto count = std::count(row_filter_.begin(), row_filter_.end(), true);
84 return static_cast<uint32_t>(count);
85 }
86
VectorRowIterator(std::vector<uint32_t> row_indices)87 VectorRowIterator::VectorRowIterator(std::vector<uint32_t> row_indices)
88 : row_indices_(std::move(row_indices)) {}
89 VectorRowIterator::~VectorRowIterator() = default;
90
NextRow()91 void VectorRowIterator::NextRow() {
92 offset_++;
93 }
94
IsEnd()95 bool VectorRowIterator::IsEnd() {
96 return offset_ >= row_indices_.size();
97 }
98
Row()99 uint32_t VectorRowIterator::Row() {
100 return row_indices_[offset_];
101 }
102
103 } // namespace trace_processor
104 } // namespace perfetto
105