Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
filter_iterator.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
45# ifndef FILTER_ITERATOR_H
46# define FILTER_ITERATOR_H
47
48# include <stdexcept>
49# include <ah-errors.H>
50# include <ah-errors.H>
51
52namespace Aleph
53{
54
122template <class Container, class It, class Show_Item>
123class Filter_Iterator : public It
124{
126
127 const Container *container_ptr = nullptr;
128
130 void *cookie = nullptr;
131
142 {
143 try
144 { // position at the first element that passes show_item filter
145 while (true)
146 {
147 if (not this->It::has_curr() or show_item(this->It::get_curr()))
148 return;
149 this->It::next();
150 }
151 }
152 catch (const std::overflow_error &) { /* continue in overflow; do not propagate */ }
153 }
154
164 void forward()
165 {
166 try
167 { // advance to the next item that passes show_item filter
168 this->It::next();
169 for (; true; this->It::next())
170 if (not this->It::has_curr() or show_item(this->It::get_curr()))
171 return;
172 }
173 catch (const std::overflow_error &) { /* continue in overflow; do not propagate */ }
174 }
175
186 {
187 try
188 {
189 for (this->It::reset_last(); true; this->It::prev())
190 if (not this->It::has_curr() or show_item(this->It::get_curr()))
191 return;
192 }
193 catch (const std::underflow_error &) { /* continue in underflow; do not propagate */ }
194 }
195
205 void backward()
206 {
207 try
208 {
209 this->It::prev();
210 for (; true; this->It::prev())
211 if (not this->It::has_curr() or show_item(this->It::get_curr()))
212 return;
213 }
214 catch (const std::underflow_error &) { /* continue in underflow; do not propagate */ }
215 }
216
217public:
219 using Item_Type = typename It::Item_Type;
220
222 using Iterator_Type = It;
223
226
229
236 const Container & get_container() const
237 {
238 ah_domain_error_if(container_ptr == nullptr) << "Filter_Iterator: no container set";
239 return *container_ptr;
240 }
241
246 bool has_container() const noexcept { return container_ptr != nullptr; }
247
254 It & get_iterator() noexcept { return *this; }
255
260 const It & get_iterator() const noexcept { return *this; }
261
271
277
287
295 void set_cookie(void *__cookie) noexcept { cookie = __cookie; }
296
301 void * get_cookie() const noexcept { return cookie; }
302
315 : show_item(si)
316 {
317 // empty
318 }
319
330 : It(c), show_item(si), container_ptr(&c)
331 {
333 }
334
347 : Filter_Iterator(c, si)
348 {
350 }
351
357 void next() { forward(); }
358
367 {
368 try
369 {
370 forward();
371 }
372 catch (...) {}
373 }
374
380 void prev() { backward(); }
381
390 {
391 try
392 {
393 backward();
394 }
395 catch (...) {}
396 }
397
404 {
405 this->It::reset_first();
407 }
408
415
426 size_t count()
427 {
428 size_t n = 0;
429 for (reset_first(); this->has_curr(); next())
430 ++n;
431 return n;
432 }
433
443 bool empty()
444 {
445 reset_first();
446 return not this->has_curr();
447 }
448
463 template <typename Op>
464 void for_each(Op op)
465 {
466 for (reset_first(); this->has_curr(); next())
467 op(this->get_curr());
468 }
469
481 template <typename Pred>
483 {
484 for (reset_first(); this->has_curr(); next())
485 if (pred(this->get_curr()))
486 return true;
487 return false;
488 }
489};
490
498template <typename T>
500{
502 bool operator()(const T &) const noexcept { return true; }
503};
504
512template <typename T>
514{
516 bool operator()(const T &) const noexcept { return false; }
517};
518
519} // end namespace Aleph
520
521# endif // FILTER_ITERATOR_H
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:522
Generic filter iterator wrapper.
void set_cookie(void *__cookie) noexcept
Sets the cookie pointer.
void next()
Advances the iterator to the next filtered element.
void prev_ne() noexcept
Moves the iterator backward (noexcept version).
void next_ne() noexcept
Advances the iterator to the next filtered element (noexcept version).
const Container & get_container() const
Returns a const reference to the container being iterated over.
Filter_Iterator(const Container &c, Show_Item si=Show_Item())
Constructs a filter iterator over a container.
It & get_iterator() noexcept
Returns a reference to the underlying base iterator.
void reset_last()
Resets the iterator to the last filtered element.
Filter_Iterator(const Container &c, void *__cookie, Show_Item si=Show_Item())
Constructs a filter iterator with a cookie.
const It & get_iterator() const noexcept
Returns a const reference to the underlying base iterator.
It Iterator_Type
The type of the base iterator.
Show_Item Filter_Type
The type of the filter functor.
void prev()
Moves the iterator backward to the previous filtered element.
typename It::Item_Type Item_Type
The type of element returned by get_curr()
Filter_Iterator(Show_Item si=Show_Item()) noexcept
Default constructor.
const Container * container_ptr
void * get_cookie() const noexcept
Gets the cookie pointer.
bool empty()
Check if there are no elements that pass the filter.
size_t count()
Count the number of elements that pass the filter.
void backward()
Move backward to the previous element that passes the filter.
void * cookie
User-defined pointer for extensibility (e.g., passing context data to filter)
void reset_first()
Resets the iterator to the first filtered element.
bool has_container() const noexcept
Check if a container has been set.
void forward()
Move forward to the next element that passes the filter.
void set_filter(Show_Item si)
Sets a new filter functor.
void goto_first_valid_item()
Advance to the first element that passes the filter.
const Show_Item & get_filter() const noexcept
Returns a const reference to the filter functor.
void for_each(Op op)
Apply a function to all filtered elements.
bool find_if(Pred pred)
Find the first element that satisfies an additional predicate.
Show_Item & get_filter() noexcept
Returns a reference to the filter functor.
void goto_last_valid_item()
Move to the last element that passes the filter.
Freq_Node * pred
Predecessor node in level-order traversal.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:107
auto get_curr() const
Return the current tuple (bounds-checked).
Definition ah-zip.H:149
bool has_curr() const noexcept
Return true if all underlying iterators are positioned on a valid item.
Definition ah-zip.H:130
DynList< T > maps(const C &c, Op op)
Classic map operation.
Default filter functor that shows all elements.
bool operator()(const T &) const noexcept
Always returns true (shows all elements).
Filter functor that hides all elements.
bool operator()(const T &) const noexcept
Always returns false (hides all elements).