@@ -31,102 +31,6 @@ class IndexSelection {
3131
3232 template <typename T>
3333 mdio::Future<void > add_selection (const ValueDescriptor<T>& descriptor) {
34- // using Interval = typename Variable<T>::Interval;
35-
36- // MDIO_ASSIGN_OR_RETURN(auto var, dataset_.variables.get<T>(std::string(descriptor.label.label())));
37- // auto fut = var.Read();
38- // MDIO_ASSIGN_OR_RETURN(auto intervals, var.get_intervals());
39- // if (!fut.status().ok()) return fut.status();
40-
41- // auto data = fut.value();
42- // const T* data_ptr = data.get_data_accessor().data();
43- // Index offset = data.get_flattened_offset();
44- // Index n_samples = data.num_samples();
45-
46- // auto current_pos = intervals;
47- // bool isInRun = false;
48- // std::vector<std::vector<Interval>> local_runs;
49-
50- // for (mdio::Index idx = offset; idx < offset + n_samples; ++idx) {
51- // if (data_ptr[idx] == descriptor.value) {
52- // if (!isInRun) {
53- // isInRun = true;
54- // std::vector<Interval> run = current_pos;
55- // for (auto& pos : run) {
56- // pos.exclusive_max = pos.inclusive_min + 1;
57- // }
58- // local_runs.push_back(std::move(run));
59- // } else {
60- // auto& run = local_runs.back();
61- // for (auto i=0; i<current_pos.size(); ++i) {
62- // run[i].exclusive_max = current_pos[i].inclusive_min + 1;
63- // }
64- // }
65- // } else {
66- // isInRun = false;
67- // }
68- // _current_position_increment<T>(current_pos, intervals);
69- // }
70-
71- // if (local_runs.empty()) {
72- // std::stringstream ss;
73- // ss << "No matches for coordinate '" << descriptor.label.label() << "'";
74- // return absl::NotFoundError(ss.str());
75- // }
76-
77- // auto new_runs = _from_intervals<T>(local_runs);
78-
79- // // First time calling add_selection_2
80- // if (kept_runs_.empty()) {
81- // kept_runs_ = std::move(new_runs);
82- // } else {
83- // // now intersect each kept_run with each new local run
84- // std::vector<std::vector<mdio::RangeDescriptor<mdio::Index>>> new_kept;
85- // new_kept.reserve(kept_runs_.size() * local_runs.size());
86-
87- // for (const auto& kept : kept_runs_) {
88- // for (const auto& run : new_runs) {
89- // // start from the old run
90- // auto intersection = kept;
91- // bool empty = false;
92-
93- // // for each descriptor in the new run...
94- // for (const auto& d_new : run) {
95- // // try to find the same label in the kept run
96- // auto it = std::find_if(
97- // intersection.begin(), intersection.end(),
98- // [&](auto const& d_old) {
99- // return d_old.label.label() == d_new.label.label();
100- // });
101-
102- // if (it != intersection.end()) {
103- // // intersect intervals
104- // auto& d_old = *it;
105- // auto new_min = std::max(d_old.start, d_new.start);
106- // auto new_max = std::min(d_old.stop, d_new.stop);
107- // if (new_min >= new_max) {
108- // empty = true;
109- // break;
110- // }
111- // d_old.start = new_min;
112- // d_old.stop = new_max;
113- // } else {
114- // // brand-new dimension: append it
115- // intersection.push_back(d_new);
116- // }
117- // }
118-
119- // if (!empty) {
120- // new_kept.push_back(std::move(intersection));
121- // }
122- // }
123- // }
124-
125- // kept_runs_ = std::move(new_kept);
126- // }
127-
128- // return absl::OkStatus();
129-
13034 if (kept_runs_.empty ()) {
13135 return _init_runs (descriptor);
13236 } else {
@@ -240,25 +144,42 @@ class IndexSelection {
240144 bool isInRun = false ;
241145 std::vector<std::vector<Interval>> local_runs;
242146
147+ std::size_t run_idx = offset;
148+
243149 for (mdio::Index idx = offset; idx < offset + n_samples; ++idx) {
244- if (data_ptr[idx] == descriptor.value ) {
245- if (!isInRun) {
246- isInRun = true ;
247- std::vector<Interval> run = current_pos;
248- for (auto & pos : run) {
249- pos.exclusive_max = pos.inclusive_min + 1 ;
250- }
251- local_runs.push_back (std::move (run));
252- } else {
253- auto & run = local_runs.back ();
254- for (auto i=0 ; i<current_pos.size (); ++i) {
255- run[i].exclusive_max = current_pos[i].inclusive_min + 1 ;
256- }
257- }
258- } else {
150+ bool is_match = data_ptr[idx] == descriptor.value ;
151+
152+ if (is_match && !isInRun) {
153+ // The start of a new run
154+ isInRun = true ;
155+ for (auto i=run_idx; i<idx; ++i) {
156+ _current_position_increment<T>(current_pos, intervals);
157+ }
158+ // _current_position_stride<T>(current_pos, intervals, idx - run_idx);
159+ run_idx = idx;
160+ std::vector<Interval> run = current_pos;
161+ local_runs.push_back (std::move (run));
162+ } else if (is_match && isInRun) {
163+ // Somewhere in the middle of a run
164+ // do nothing TODO: Remove me
165+ } else if (!is_match && isInRun) {
166+ // The end of a run
259167 isInRun = false ;
168+ for (auto i=run_idx; i<idx; ++i) {
169+ _current_position_increment<T>(current_pos, intervals);
170+ }
171+ // _current_position_stride<T>(current_pos, intervals, idx - run_idx);
172+ run_idx = idx;
173+ auto & last_run = local_runs.back ();
174+ for (auto i=0 ; i<current_pos.size (); ++i) {
175+ last_run[i].exclusive_max = current_pos[i].inclusive_min + 1 ;
176+ }
177+ } else if (!is_match && !isInRun) {
178+ // No run at all
179+ // do nothing TODO: Remove me
180+ } else {
181+ // base case TODO: Remove me
260182 }
261- _current_position_increment<T>(current_pos, intervals);
262183 }
263184
264185 if (local_runs.empty ()) {
@@ -305,6 +226,21 @@ class IndexSelection {
305226 }
306227 }
307228
229+ template <typename T>
230+ void _current_position_stride (
231+ std::vector<typename Variable<T>::Interval>& position,
232+ const std::vector<typename Variable<T>::Interval>& interval,
233+ const std::size_t num_elements) {
234+ auto dims = position.size ();
235+ if (position[dims-1 ].exclusive_max < position[dims-1 ].inclusive_min + num_elements) {
236+ position[dims-1 ].inclusive_min = position[dims-1 ].inclusive_min + num_elements;
237+ return ;
238+ }
239+ for (auto i=0 ; i<num_elements; ++i) {
240+ _current_position_increment<T>(position, interval);
241+ }
242+ }
243+
308244 template <typename T>
309245 Result<std::tuple<VariableData<T>, const T*, Index, Index>> _resolve_future (Future<VariableData<T>>& fut) {
310246 if (!fut.status ().ok ()) return fut.status ();
0 commit comments