]> sigrok.org Git - pulseview.git/blob - pv/data/decode/rowdata.cpp
a06af6169a9803997dd2e2b4d130f3c85e471b3c
[pulseview.git] / pv / data / decode / rowdata.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2014 Joel Holdsworth <joel@airwebreathe.org.uk>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <boost/bind.hpp>
22
23 #include <assert.h>
24
25 #include "rowdata.h"
26
27 using std::vector;
28
29 namespace pv {
30 namespace data {
31 namespace decode {
32
33 RowData::RowData()
34 {
35 }
36
37 uint64_t RowData::get_max_sample() const
38 {
39         if (_annotations.empty())
40                 return 0;
41         return _annotations.back().end_sample();
42 }
43
44 void RowData::get_annotation_subset(
45         std::vector<pv::data::decode::Annotation> &dest,
46         uint64_t start_sample, uint64_t end_sample) const
47 {
48         const vector<size_t>::const_iterator start_iter =
49                 lower_bound(_ann_end_index.begin(),
50                         _ann_end_index.end(), start_sample,
51                         bind(&RowData::index_entry_end_sample_lt,
52                                 this, _1, _2));
53
54         const vector<size_t>::const_iterator end_iter =
55                 upper_bound(_ann_start_index.begin(),
56                         _ann_start_index.end(), end_sample,
57                         bind(&RowData::index_entry_start_sample_gt,
58                                 this, _1, _2));
59
60         for (vector<size_t>::const_iterator i = start_iter;
61                 i != _ann_end_index.end() && *i != *end_iter; i++)
62                 dest.push_back(_annotations[*i]);
63 }
64
65 void RowData::push_annotation(const Annotation &a)
66 {
67         const size_t offset = _annotations.size();
68         _annotations.push_back(a);
69
70         // Insert the annotation into the start index
71         vector<size_t>::iterator i = _ann_start_index.end();
72         if (!_ann_start_index.empty() &&
73                 _annotations[_ann_start_index.back()].start_sample() >
74                         a.start_sample())
75                 i = upper_bound(_ann_start_index.begin(),
76                         _ann_start_index.end(), a.start_sample(),
77                         bind(&RowData::index_entry_start_sample_gt,
78                                 this, _1, _2));
79
80         _ann_start_index.insert(i, offset);
81
82         // Insert the annotation into the end index
83         vector<size_t>::iterator j = _ann_end_index.end();
84         if (!_ann_end_index.empty() &&
85                 _annotations[_ann_end_index.back()].end_sample() <
86                         a.end_sample())
87                 j = upper_bound(_ann_end_index.begin(),
88                         _ann_end_index.end(), a.end_sample(),
89                         bind(&RowData::index_entry_end_sample_gt,
90                                 this, _1, _2));
91
92         _ann_end_index.insert(j, offset);
93 }
94
95 bool RowData::index_entry_start_sample_gt(
96         const uint64_t sample, const size_t index) const
97 {
98         assert(index < _annotations.size());
99         return _annotations[index].start_sample() > sample;
100 }
101
102 bool RowData::index_entry_end_sample_lt(
103         const size_t index, const uint64_t sample) const
104 {
105         assert(index < _annotations.size());
106         return _annotations[index].end_sample() < sample;
107 }
108
109 bool RowData::index_entry_end_sample_gt(
110         const uint64_t sample, const size_t index) const
111 {
112         assert(index < _annotations.size());
113         return _annotations[index].end_sample() > sample;
114 }
115
116 } // decode
117 } // data
118 } // pv